librnr  1.14.5
RoadNarrows Robotics Common Library 1
new.c
Go to the documentation of this file.
1 ////////////////////////////////////////////////////////////////////////////////
2 //
3 // Package: RoadNarrows Robotics Common Library 1
4 //
5 // Library: librnr
6 //
7 // File: new.c
8 //
9 /*! \file
10  *
11  * $LastChangedDate: 2010-03-24 10:19:36 -0600 (Wed, 24 Mar 2010) $
12  * $Rev: 307 $
13  *
14  * \brief Memory allocation and deallocation definitions.
15  *
16  * General purpose dynamic memory allocate and deallocate definitions.
17  * None of the calls will fail, unless fatally for the entire
18  * program. Modeled on C++ new() and delete() operators.
19  *
20  * The current implementation uses the malloc() and free() standard library
21  * calls.
22  *
23  * \author Robin Knight (robin.knight@roadnarrows.com)
24  *
25  * \pkgcopyright{2005-2018,RoadNarrows LLC.,http://www.roadnarrows.com}
26  */
27 // Permission is hereby granted, without written agreement and without
28 // license or royalty fees, to use, copy, modify, and distribute this
29 // software and its documentation for any purpose, provided that
30 // (1) The above copyright notice and the following two paragraphs
31 // appear in all copies of the source code and (2) redistributions
32 // including binaries reproduces these notices in the supporting
33 // documentation. Substantial modifications to this software may be
34 // copyrighted by their authors and need not follow the licensing terms
35 // described here, provided that the new terms are clearly indicated in
36 // all files where they apply.
37 //
38 // IN NO EVENT SHALL THE AUTHOR, ROADNARROWS LLC, OR ANY MEMBERS/EMPLOYEES
39 // OF ROADNARROW LLC OR DISTRIBUTORS OF THIS SOFTWARE BE LIABLE TO ANY
40 // PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
41 // DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION,
42 // EVEN IF THE AUTHORS OR ANY OF THE ABOVE PARTIES HAVE BEEN ADVISED OF
43 // THE POSSIBILITY OF SUCH DAMAGE.
44 //
45 // THE AUTHOR AND ROADNARROWS LLC SPECIFICALLY DISCLAIM ANY WARRANTIES,
46 // INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
47 // FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN
48 // "AS IS" BASIS, AND THE AUTHORS AND DISTRIBUTORS HAVE NO OBLIGATION TO
49 // PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
50 //
51 ////////////////////////////////////////////////////////////////////////////////
52 
53 #include <stdlib.h>
54 #include <stdio.h>
55 #include <string.h>
56 
57 #include "rnr/rnrconfig.h"
58 #include "rnr/new.h"
59 
60 /*!
61  * \brief new() "operator"
62  *
63  * Dynamic memory allocator. All allocated data are zero'ed (i.e. set to 0).
64  *
65  * \param size Number of bytes.
66  *
67  * \return
68  * Return void * to newly allocated memory. Memory is zero'ed.
69  *
70  * \exception "Bad Allocation"
71  * Program is terminated with exit code \ref EC_ERROR.
72  */
73 void *new(size_t size)
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 }
90 
91 /*!
92  * \brief Allocate and duplicate.
93  *
94  * A new block a memory of sizeNew bytes is allocated. sizeDup bytes from
95  * the source data are copied to the new block. All allocated, but not
96  * duplicated data are zero'ed (i.e. set to 0).
97  *
98  * \note The actual size of source block is assumed to be >= sizeDup.
99  *
100  * \param pSrc Source memory block to duplicate.
101  * \param sizeDup Number of bytes to duplicate, with sizeDup <= sizeNew.
102  * \param sizeNew Number of bytes for new block.
103  *
104  * \return
105  * Return void * to newly allocated and duplicated memory.
106  *
107  * \exception "Bad Allocation"
108  * Program is terminated with exit code \ref EC_ERROR.
109  * \exception "Bad Sizes"
110  * Program is terminated with exit code \ref EC_ERROR.
111  */
112 void *new_overs(void *pSrc, size_t sizeDup, size_t sizeNew)
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 }
132 
133 /*!
134  * \brief Duplicate data.
135  *
136  * \param size Number of bytes.
137  * \param data Data to duplicate
138  *
139  * \return Return pointer to duplicated memory.
140  *
141  * \exception "Bad Allocation"
142  * Program is terminated with exit code \ref EC_ERROR.
143  *
144  * \note If size is zero or data is NULL, one byte is allocated.
145  */
146 void *new_memdup(size_t size, void *data)
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 }
162 
163 /*!
164  * \brief Duplicate a string.
165  *
166  * \param s Null terminated string to duplicate.
167  *
168  * \return Return pointer to duplicated string.
169  *
170  * \exception "Bad Allocation"
171  * Program is terminated with exit code \ref EC_ERROR.
172  *
173  * \note Guaranteed to return a null terminated string.
174  * If string is NULL, one byte is allocated (== 0).
175  */
176 char *new_strdup(const char *s)
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 }
192 
193 /*!
194  * \brief Duplicate not more than n characters of string.
195  *
196  * Duplicate the first n characters of string or until a null character
197  * is encounter, whichever comes first. Guaranteed to return a null
198  * terminated string.
199  *
200  * \param s String to duplicate.
201  * \param n Maximum number of characters to duplicate.
202  *
203  * \exception "Bad Allocation"
204  * Program is terminated with exit code \ref EC_ERROR.
205  *
206  * \return Return pointer to duplicated string.
207  */
208 char *new_strndup(const char *s, size_t n)
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
Memory allocation and deallocation declarations.
char * new_strndup(const char *s, size_t n)
Duplicate not more than n characters of string.
Definition: new.c:208
void * new_overs(void *pSrc, size_t sizeDup, size_t sizeNew)
Allocate and duplicate.
Definition: new.c:112
RoadNarrows Robotics common configuration file.
char * new_strdup(const char *s)
Duplicate a string.
Definition: new.c:176
#define EC_ERROR
general error exit code
Definition: rnrconfig.h:306
void * new_memdup(size_t size, void *data)
Duplicate data.
Definition: new.c:146