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

Memory allocation and deallocation declarations. More...

#include "rnr/rnrconfig.h"
#include <sys/types.h>
#include <stdlib.h>
#include <stdarg.h>

Go to the source code of this file.

Macros

#define NEW(T)   (T *)new(sizeof(T))
 Allocate new type. More...
 
#define NEWSTR(len)   (char *)new( sizeof(char)*((size_t)((len)+1)) )
 Allocate new string buffer of length len+1. More...
 

Functions

INLINE_IN_H void delete (void *p)
 delete() "operator" More...
 
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...
 
char * strcpy_s (char *dest, size_t n, const char *src)
 Copy source string to destinations string. At most n bytes will be copied. The destination string is guaranteed to be null terminated. More...
 
int sprintf_s (char *str, size_t n, const char *format,...)
 Format print to string up to n-1 characters. String is guaranteed to be null terminated. More...
 

Detailed Description

Memory allocation and deallocation declarations.

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

Package
RoadNarrows Robotics Common Library 1
Library
librnr
File
rnr/new.h
Author
Robin Knight (robin.nosp@m..kni.nosp@m.ght@r.nosp@m.oadn.nosp@m.arrow.nosp@m.s.co.nosp@m.m)
License
MIT
EULA
See the README and EULA files for any copyright and licensing information.

Definition in file new.h.

Macro Definition Documentation

#define NEW (   T)    (T *)new(sizeof(T))

Allocate new type.

Parameters
TData type.
Returns
Pointer T* to allocated memory
Exceptions
Bad AllocationProgram is terminated with exit code EC_ERROR.

Definition at line 49 of file new.h.

Referenced by AddNewBody(), ConfigDbIterNew(), ConfigDbNew(), ConfigSectionIterNew(), ConfigSectionNew(), DListVoidAppend(), DListVoidInsert(), DListVoidNew(), DListVoidPrepend(), hash_table_create(), hnode_create(), NewZooAnimal(), OptsNew(), SearchPathIterNew(), SimpleBufNew(), SimpleBufNewWithBuf(), SocketNew(), SockSetNew(), and UriParseNew().

#define NEWSTR (   len)    (char *)new( sizeof(char)*((size_t)((len)+1)) )

Allocate new string buffer of length len+1.

Parameters
lenLength of string buffer less null-termination character.
Returns
Returns char * to allocated, zero'ed out memory.
Exceptions
Bad AllocationProgram is terminated with exit code EC_ERROR.

Definition at line 64 of file new.h.

Referenced by new_strdup(), new_strndup(), NewJoinedPath(), NewNormPath(), NewPrettyBuf(), NewSearchPathCanonicalized(), NewSearchPathExpanded(), SearchPathIterFirst(), SearchPathIterNext(), SocketAddrNameInet(), SocketAddrNameUnix(), SocketThisHostName(), and UriStrNew().

Function Documentation

INLINE_IN_H void delete ( void *  p)

delete() "operator"

Frees previously allocated memory. NULL pointer produce no action.

Parameters
pPointer to memory.

Definition at line 77 of file new.h.

References C_DECLS_BEGIN, C_DECLS_END, new_memdup(), new_overs(), new_strdup(), new_strndup(), NULL, sprintf_s(), and strcpy_s().

78 {
79  if( p != NULL )
80  {
81  free(p);
82  }
83 }
#define NULL
null pointer
Definition: rnrconfig.h:199
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
int sprintf_s ( char *  str,
size_t  n,
const char *  format,
  ... 
)

Format print to string up to n-1 characters. String is guaranteed to be null terminated.

Parameters
strDestination string buffer.
nSize of string buffer.
formatStandard printf formatting string.
...Variable argument list.
Returns
Number of characters printed.

Definition at line 83 of file win.c.

Referenced by delete().

84 {
85  va_list ap;
86  int nPrinted;
87 
88  va_start(ap, format);
89  nPrinted = vsnprintf(str, (size_t)n, format, ap);
90  va_end(ap);
91  str[n-1] = 0;
92  return nPrinted;
93 }
char* strcpy_s ( char *  dest,
size_t  n,
const char *  src 
)

Copy source string to destinations string. At most n bytes will be copied. The destination string is guaranteed to be null terminated.

Parameters
destPointer to destination string.
nMaximum number of bytes to copy.
srcPointer to source string.
Returns
Return pointer to destination string.

Definition at line 65 of file win.c.

Referenced by delete().

66 {
67  strncpy(dest, src, (size_t)n);
68  dest[n-1] = 0;
69  return dest;
70 }