GNUnet 0.26.2-106-g126384b46
 
Loading...
Searching...
No Matches
common_allocation.c File Reference

wrapper around malloc/free More...

#include "platform.h"
#include "gnunet_util_lib.h"
Include dependency graph for common_allocation.c:

Go to the source code of this file.

Macros

#define LOG(kind, ...)    GNUNET_log_from (kind, "util-common-allocation", __VA_ARGS__)
 
#define LOG_STRERROR(kind, syscall)    GNUNET_log_from_strerror (kind, "util-common-allocation", syscall)
 
#define INT_MAX   0x7FFFFFFF
 
#define BAADFOOD_STR   "\x0D\xF0\xAD\xBA"
 
#define BAADFOOD_STR   "\xBA\xAD\xF0\x0D"
 

Functions

void * GNUNET_xmalloc_ (size_t size, const char *filename, int linenumber)
 Allocate memory.
 
void * GNUNET_xmemdup_ (const void *buf, size_t size, const char *filename, int linenumber)
 Allocate and initialize memory.
 
void * GNUNET_xmalloc_unchecked_ (size_t size, const char *filename, int linenumber)
 Allocate memory.
 
void * GNUNET_xrealloc_ (void *ptr, size_t n, const char *filename, int linenumber)
 Reallocate memory.
 
void GNUNET_xfree_ (void *ptr, const char *filename, int linenumber)
 Free memory.
 
char * GNUNET_xstrdup_ (const char *str, const char *filename, int linenumber)
 Dup a string.
 
static size_t strnlen (const char *s, size_t n)
 
char * GNUNET_xstrndup_ (const char *str, size_t len, const char *filename, int linenumber)
 Dup partially a string.
 
void GNUNET_xgrow_ (void **old, size_t elementSize, unsigned int *oldCount, unsigned int newCount, const char *filename, int linenumber)
 Grow an array, the new elements are zeroed out.
 
int GNUNET_asprintf (char **buf, const char *format,...)
 
int GNUNET_snprintf (char *buf, size_t size, const char *format,...)
 
struct GNUNET_MessageHeaderGNUNET_copy_message (const struct GNUNET_MessageHeader *msg)
 Create a copy of the given message.
 
bool GNUNET_is_zero_ (const void *a, size_t n)
 Check that memory in a is all zeros.
 

Detailed Description

wrapper around malloc/free

Author
Christian Grothoff

Definition in file common_allocation.c.

Macro Definition Documentation

◆ LOG

#define LOG (   kind,
  ... 
)     GNUNET_log_from (kind, "util-common-allocation", __VA_ARGS__)

Definition at line 36 of file common_allocation.c.

50{
51 void *ret;
52
53 /* As a security precaution, we generally do not allow very large
54 * allocations using the default 'GNUNET_malloc()' macro */
57 linenumber);
60 linenumber);
61 if (NULL == ret)
62 {
64 "malloc");
65 GNUNET_assert (0);
66 }
67 return ret;
68}
69
70
71void *
72GNUNET_xmemdup_ (const void *buf,
73 size_t size,
74 const char *filename,
75 int linenumber)
76{
77 void *ret;
78
79 /* As a security precaution, we generally do not allow very large
80 * allocations here */
83 linenumber);
86 linenumber);
87 ret = malloc (size);
88 if (NULL == ret)
89 {
91 "malloc");
92 GNUNET_assert (0);
93 }
95 buf,
96 size);
97 return ret;
98}
99
100
101void *
103 const char *filename,
104 int linenumber)
105{
106 void *result;
107
108 (void) filename;
109 (void) linenumber;
110 result = malloc (size);
111 if (NULL == result)
112 return NULL;
113 memset (result,
114 0,
115 size);
116 return result;
117}
118
119
120void *
121GNUNET_xrealloc_ (void *ptr,
122 size_t n,
123 const char *filename,
124 int linenumber)
125{
126 (void) filename;
127 (void) linenumber;
128
129#if defined(M_SIZE)
130#if ENABLE_POISONING
131 {
132 uint64_t *base = ptr;
133 size_t s = M_SIZE (ptr);
134
135 if (s > n)
136 {
137 const uint64_t baadfood = GNUNET_ntohll (0xBAADF00DBAADF00DLL);
138 char *cbase = ptr;
139
140 GNUNET_memcpy (&cbase[n],
141 &baadfood,
142 GNUNET_MIN (8 - (n % 8),
143 s - n));
144 for (size_t i = 1 + (n + 7) / 8; i < s / 8; i++)
145 base[i] = baadfood;
146 GNUNET_memcpy (&base[s / 8],
147 &baadfood,
148 s % 8);
149 }
150 }
151#endif
152#endif
153 ptr = realloc (ptr, n);
154 if ((NULL == ptr) && (n > 0))
155 {
157 "realloc");
158 GNUNET_assert (0);
159 }
160 return ptr;
161}
162
163
164#if __BYTE_ORDER == __LITTLE_ENDIAN
165#define BAADFOOD_STR "\x0D\xF0\xAD\xBA"
166#endif
167#if __BYTE_ORDER == __BIG_ENDIAN
168#define BAADFOOD_STR "\xBA\xAD\xF0\x0D"
169#endif
170
171#if HAVE_MALLOC_NP_H
172#include <malloc_np.h>
173#endif
174#if HAVE_MALLOC_USABLE_SIZE
175#define M_SIZE(p) malloc_usable_size (p)
176#elif HAVE_MALLOC_SIZE
177#define M_SIZE(p) malloc_size (p)
178#endif
179
180void
181GNUNET_xfree_ (void *ptr,
182 const char *filename,
183 int linenumber)
184{
185 if (NULL == ptr)
186 return;
187#if defined(M_SIZE)
188#if ENABLE_POISONING
189 {
190 const uint64_t baadfood = GNUNET_ntohll (0xBAADF00DBAADF00DLL);
191 uint64_t *base = ptr;
192 size_t s = M_SIZE (ptr);
193
194 for (size_t i = 0; i < s / 8; i++)
195 base[i] = baadfood;
196 GNUNET_memcpy (&base[s / 8], &baadfood, s % 8);
197 }
198#endif
199#endif
200 free (ptr);
201}
202
203
204char *
205GNUNET_xstrdup_ (const char *str,
206 const char *filename,
207 int linenumber)
208{
209 size_t slen;
210 char *res;
211
212 GNUNET_assert_at (str != NULL,
213 filename,
214 linenumber);
215 slen = strlen (str) + 1;
216 res = GNUNET_xmalloc_ (slen,
217 filename,
218 linenumber);
220 str,
221 slen);
222 return res;
223}
224
225
226#if ! HAVE_STRNLEN
227static size_t
228strnlen (const char *s,
229 size_t n)
230{
231 const char *e;
232
233 e = memchr (s,
234 '\0',
235 n);
236 if (NULL == e)
237 return n;
238 return e - s;
239}
240
241
242#endif
243
244
245char *
246GNUNET_xstrndup_ (const char *str,
247 size_t len,
248 const char *filename,
249 int linenumber)
250{
251 char *res;
252
253 if (0 == len)
254 return GNUNET_strdup ("");
255 GNUNET_assert_at (NULL != str,
256 filename,
257 linenumber);
258 len = strnlen (str, len);
259 res = GNUNET_xmalloc_ (len + 1,
260 filename,
261 linenumber);
262 GNUNET_memcpy (res, str, len);
263 /* res[len] = '\0'; 'malloc' zeros out anyway */
264 return res;
265}
266
267
268void
269GNUNET_xgrow_ (void **old,
270 size_t elementSize,
271 unsigned int *oldCount,
272 unsigned int newCount,
273 const char *filename,
274 int linenumber)
275{
276 void *tmp;
277 size_t size;
278
279 GNUNET_assert_at (elementSize > 0, filename, linenumber);
280 GNUNET_assert_at (INT_MAX / elementSize > newCount, filename, linenumber);
281 size = newCount * elementSize;
282 if (0 == size)
283 {
284 tmp = NULL;
285 }
286 else
287 {
288 tmp = GNUNET_xmalloc_ (size,
289 filename,
290 linenumber);
291 if (NULL != *old)
292 {
293 GNUNET_memcpy (tmp,
294 *old,
295 elementSize * GNUNET_MIN (*oldCount,
296 newCount));
297 }
298 }
299
300 if (NULL != *old)
301 {
302 GNUNET_xfree_ (*old,
303 filename,
304 linenumber);
305 }
306 *old = tmp;
307 *oldCount = newCount;
308}
309
310
311int
312GNUNET_asprintf (char **buf,
313 const char *format,
314 ...)
315{
316 int ret;
317 va_list args;
318
319 va_start (args,
320 format);
321 ret = vsnprintf (NULL,
322 0,
323 format,
324 args);
325 va_end (args);
326 GNUNET_assert (ret >= 0);
327 *buf = GNUNET_malloc (ret + 1);
328 va_start (args, format);
329 ret = vsnprintf (*buf,
330 ret + 1,
331 format,
332 args);
333 va_end (args);
334 return ret;
335}
336
337
338int
339GNUNET_snprintf (char *buf,
340 size_t size,
341 const char *format,
342 ...)
343{
344 int ret;
345 va_list args;
346
347 va_start (args,
348 format);
349 ret = vsnprintf (buf,
350 size,
351 format,
352 args);
353 va_end (args);
354 GNUNET_assert ((ret >= 0) && (((size_t) ret) < size));
355 return ret;
356}
357
358
361{
363 uint16_t msize;
364
365 msize = ntohs (msg->size);
366 GNUNET_assert (msize >= sizeof(struct GNUNET_MessageHeader));
367 ret = GNUNET_malloc (msize);
368 GNUNET_memcpy (ret, msg, msize);
369 return ret;
370}
371
372
373bool
374GNUNET_is_zero_ (const void *a,
375 size_t n)
376{
377 const char *b = a;
378
379 for (size_t i = 0; i < n; i++)
380 if (b[i])
381 return false;
382 return true;
383}
384
385
386/* end of common_allocation.c */
struct GNUNET_MessageHeader * msg
Definition 005.c:2
static size_t strnlen(const char *s, size_t n)
#define INT_MAX
int GNUNET_snprintf(char *buf, size_t size, const char *format,...)
int GNUNET_asprintf(char **buf, const char *format,...)
#define LOG_STRERROR(kind, syscall)
static int ret
Final status code.
Definition gnunet-arm.c:93
static char * filename
static char * res
Currently read line or NULL on EOF.
static int result
Global testing status.
char * GNUNET_xstrndup_(const char *str, size_t len, const char *filename, int linenumber)
Dup partially a string.
void GNUNET_xgrow_(void **old, size_t elementSize, unsigned int *oldCount, unsigned int newCount, const char *filename, int linenumber)
Grow an array, the new elements are zeroed out.
char * GNUNET_xstrdup_(const char *str, const char *filename, int linenumber)
Dup a string.
uint64_t GNUNET_ntohll(uint64_t n)
Convert unsigned 64-bit integer to host byte order.
void * GNUNET_xmalloc_unchecked_(size_t size, const char *filename, int linenumber)
Allocate memory.
void GNUNET_xfree_(void *ptr, const char *filename, int linenumber)
Free memory.
void * GNUNET_xrealloc_(void *ptr, size_t n, const char *filename, int linenumber)
Reallocate memory.
void * GNUNET_xmalloc_(size_t size, const char *filename, int linenumber)
Allocate memory.
bool GNUNET_is_zero_(const void *a, size_t n)
Check that memory in a is all zeros.
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
void * GNUNET_xmemdup_(const void *buf, size_t size, const char *filename, int linenumber)
Allocate and initialize memory.
#define GNUNET_MIN(a, b)
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_assert_at(cond, f, l)
Use this for fatal errors that cannot be handled.
@ GNUNET_ERROR_TYPE_ERROR
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define GNUNET_MAX_MALLOC_CHECKED
Maximum allocation with GNUNET_malloc macro.
struct GNUNET_MessageHeader * GNUNET_copy_message(const struct GNUNET_MessageHeader *msg)
Create a copy of the given message.
#define GNUNET_malloc(size)
Wrapper around malloc.
static unsigned int size
Size of the "table".
Definition peer.c:68
Header for all communications.
const char * str
Definition time.c:1252

◆ LOG_STRERROR

#define LOG_STRERROR (   kind,
  syscall 
)     GNUNET_log_from_strerror (kind, "util-common-allocation", syscall)

Definition at line 39 of file common_allocation.c.

◆ INT_MAX

#define INT_MAX   0x7FFFFFFF

Definition at line 43 of file common_allocation.c.

◆ BAADFOOD_STR [1/2]

#define BAADFOOD_STR   "\x0D\xF0\xAD\xBA"

Definition at line 166 of file common_allocation.c.

◆ BAADFOOD_STR [2/2]

#define BAADFOOD_STR   "\xBA\xAD\xF0\x0D"

Definition at line 166 of file common_allocation.c.

Function Documentation

◆ strnlen()

static size_t strnlen ( const char *  s,
size_t  n 
)
static

Definition at line 229 of file common_allocation.c.

231{
232 const char *e;
233
234 e = memchr (s,
235 '\0',
236 n);
237 if (NULL == e)
238 return n;
239 return e - s;
240}

Referenced by GNUNET_strlcpy(), GNUNET_xstrndup_(), handle_gns_resolution_result(), and send_init().

Here is the caller graph for this function:

◆ GNUNET_asprintf()

int GNUNET_asprintf ( char **  buf,
const char *  format,
  ... 
)

Definition at line 313 of file common_allocation.c.

316{
317 int ret;
318 va_list args;
319
320 va_start (args,
321 format);
322 ret = vsnprintf (NULL,
323 0,
324 format,
325 args);
326 va_end (args);
327 GNUNET_assert (ret >= 0);
328 *buf = GNUNET_malloc (ret + 1);
329 va_start (args, format);
330 ret = vsnprintf (*buf,
331 ret + 1,
332 format,
333 args);
334 va_end (args);
335 return ret;
336}

References GNUNET_assert, GNUNET_malloc, and ret.

◆ GNUNET_snprintf()

int GNUNET_snprintf ( char *  buf,
size_t  size,
const char *  format,
  ... 
)

Definition at line 340 of file common_allocation.c.

344{
345 int ret;
346 va_list args;
347
348 va_start (args,
349 format);
350 ret = vsnprintf (buf,
351 size,
352 format,
353 args);
354 va_end (args);
355 GNUNET_assert ((ret >= 0) && (((size_t) ret) < size));
356 return ret;
357}

References GNUNET_assert, ret, and size.