librnr  1.14.5
RoadNarrows Robotics Common Library 1
new.c File Reference

Memory allocation and deallocation definitions. More...

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "rnr/rnrconfig.h"
#include "rnr/new.h"

Go to the source code of this file.

Functions

void * new (size_t size)
 new() "operator" More...
 
void * new_overs (void *pSrc, size_t sizeDup, size_t sizeNew)
 Allocate and duplicate. More...
 
void * new_memdup (size_t size, void *data)
 Duplicate data. More...
 
char * new_strdup (const char *s)
 Duplicate a string. More...
 
char * new_strndup (const char *s, size_t n)
 Duplicate not more than n characters of string. More...
 

Detailed Description

Memory allocation and deallocation definitions.

LastChangedDate
2010-03-24 10:19:36 -0600 (Wed, 24 Mar 2010)
Rev
307

General purpose dynamic memory allocate and deallocate definitions. None of the calls will fail, unless fatally for the entire program. Modeled on C++ new() and delete() operators.

The current implementation uses the malloc() and free() standard library calls.

Author
Robin Knight (robin.nosp@m..kni.nosp@m.ght@r.nosp@m.oadn.nosp@m.arrow.nosp@m.s.co.nosp@m.m)

Definition in file new.c.

Function Documentation

void* new ( size_t  size)

new() "operator"

Dynamic memory allocator. All allocated data are zero'ed (i.e. set to 0).

Parameters
sizeNumber of bytes.
Returns
Return void * to newly allocated memory. Memory is zero'ed.
Exceptions
Bad AllocationProgram is terminated with exit code EC_ERROR.

Definition at line 73 of file new.c.

References EC_ERROR, and NULL.

74 {
75  void *p;
76 
77  if( size == 0 )
78  {
79  size = 1;
80  }
81 
82  if( (p = malloc(size)) == NULL )
83  {
84  fprintf(stderr, "new(): malloc() failed\n");
85  exit(EC_ERROR);
86  }
87  memset(p, 0, size);
88  return p;
89 }
#define NULL
null pointer
Definition: rnrconfig.h:199
#define EC_ERROR
general error exit code
Definition: rnrconfig.h:306
void* new_memdup ( size_t  size,
void *  data 
)

Duplicate data.

Parameters
sizeNumber of bytes.
dataData to duplicate
Returns
Return pointer to duplicated memory.
Exceptions
Bad AllocationProgram is terminated with exit code EC_ERROR.
Note
If size is zero or data is NULL, one byte is allocated.

Definition at line 146 of file new.c.

References NULL.

Referenced by delete().

147 {
148  void *p;
149 
150  if( (size == 0) || (data == NULL) )
151  {
152  p = (void *)new((size_t)1);
153  memset(p, 0, (size_t)1);
154  }
155  else
156  {
157  p = new(size);
158  memcpy(p, data, size);
159  }
160  return p;
161 }
#define NULL
null pointer
Definition: rnrconfig.h:199
void* new_overs ( void *  pSrc,
size_t  sizeDup,
size_t  sizeNew 
)

Allocate and duplicate.

A new block a memory of sizeNew bytes is allocated. sizeDup bytes from the source data are copied to the new block. All allocated, but not duplicated data are zero'ed (i.e. set to 0).

Note
The actual size of source block is assumed to be >= sizeDup.
Parameters
pSrcSource memory block to duplicate.
sizeDupNumber of bytes to duplicate, with sizeDup <= sizeNew.
sizeNewNumber of bytes for new block.
Returns
Return void * to newly allocated and duplicated memory.
Exceptions
Bad AllocationProgram is terminated with exit code EC_ERROR.
Bad SizesProgram is terminated with exit code EC_ERROR.

Definition at line 112 of file new.c.

References EC_ERROR.

Referenced by delete(), grow_table(), and shrink_table().

113 {
114  void *pNew;
115 
116  if( sizeDup > sizeNew )
117  {
118  fprintf(stderr, "new_overs(): duplicate size %lu > allocated size %lu\n",
119  (unsigned long)sizeDup, (unsigned long)sizeNew);
120  exit(EC_ERROR);
121  }
122 
123  pNew = new(sizeNew);
124 
125  if( sizeDup > 0 )
126  {
127  memcpy(pNew, pSrc, sizeDup);
128  }
129 
130  return pNew;
131 }
#define EC_ERROR
general error exit code
Definition: rnrconfig.h:306
char* new_strdup ( const char *  s)

Duplicate a string.

Parameters
sNull terminated string to duplicate.
Returns
Return pointer to duplicated string.
Exceptions
Bad AllocationProgram is terminated with exit code EC_ERROR.
Note
Guaranteed to return a null terminated string. If string is NULL, one byte is allocated (== 0).

Definition at line 176 of file new.c.

References NEWSTR, and NULL.

Referenced by ConfigDbNew(), ConfigDbSetName(), ConfigSectionAddPair(), ConfigSectionNew(), delete(), force_grow(), LOG_ATTACH_LOGFP(), LOG_SET_LOGFILE(), LOGGER(), LOGGER_CALL(), main(), NewExpandTilde(), NewNormPath(), NewZooAnimal(), OptsCvtArgStr(), SocketAddrName(), SocketAttrSetLocalName(), SocketAttrSetRemoteName(), SocketThisHostName(), test_hash(), UriParseNew(), UriSetHostName(), UriSetPath(), and UriSetScheme().

177 {
178  char *t;
179 
180  if( (s != NULL) && (*s != 0) )
181  {
182  t = NEWSTR(strlen(s));
183  strcpy(t, s);
184  }
185  else
186  {
187  t = NEWSTR(1);
188  t[0] = 0;
189  }
190  return t;
191 }
#define NEWSTR(len)
Allocate new string buffer of length len+1.
Definition: new.h:64
#define NULL
null pointer
Definition: rnrconfig.h:199
char* new_strndup ( const char *  s,
size_t  n 
)

Duplicate not more than n characters of string.

Duplicate the first n characters of string or until a null character is encounter, whichever comes first. Guaranteed to return a null terminated string.

Parameters
sString to duplicate.
nMaximum number of characters to duplicate.
Exceptions
Bad AllocationProgram is terminated with exit code EC_ERROR.
Returns
Return pointer to duplicated string.

Definition at line 208 of file new.c.

References NEWSTR, and NULL.

Referenced by delete(), NewSearchPathExpanded(), UriParseHostNew(), and UriParseNew().

209 {
210  char *t;
211 
212  if( (s != NULL) && (*s != 0) && (n > 0) )
213  {
214  t = NEWSTR(n+1);
215  strncpy(t, s, n);
216  t[n] = 0;
217  }
218  else
219  {
220  t = NEWSTR(1);
221  t[0] = 0;
222  }
223  return t;
224 }
#define NEWSTR(len)
Allocate new string buffer of length len+1.
Definition: new.h:64
#define NULL
null pointer
Definition: rnrconfig.h:199