GNUnet debian-0.24.3-28-g4f2a77692
 
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 = strlen (str) + 1;
210 char *res;
211
212 GNUNET_assert_at (str != NULL,
213 filename,
214 linenumber);
215 res = GNUNET_xmalloc_ (slen,
216 filename,
217 linenumber);
219 str,
220 slen);
221 return res;
222}
223
224
225#if ! HAVE_STRNLEN
226static size_t
227strnlen (const char *s,
228 size_t n)
229{
230 const char *e;
231
232 e = memchr (s,
233 '\0',
234 n);
235 if (NULL == e)
236 return n;
237 return e - s;
238}
239
240
241#endif
242
243
244char *
245GNUNET_xstrndup_ (const char *str,
246 size_t len,
247 const char *filename,
248 int linenumber)
249{
250 char *res;
251
252 if (0 == len)
253 return GNUNET_strdup ("");
254 GNUNET_assert_at (NULL != str,
255 filename,
256 linenumber);
257 len = strnlen (str, len);
258 res = GNUNET_xmalloc_ (len + 1,
259 filename,
260 linenumber);
261 GNUNET_memcpy (res, str, len);
262 /* res[len] = '\0'; 'malloc' zeros out anyway */
263 return res;
264}
265
266
267void
268GNUNET_xgrow_ (void **old,
269 size_t elementSize,
270 unsigned int *oldCount,
271 unsigned int newCount,
272 const char *filename,
273 int linenumber)
274{
275 void *tmp;
276 size_t size;
277
278 GNUNET_assert_at (elementSize > 0, filename, linenumber);
279 GNUNET_assert_at (INT_MAX / elementSize > newCount, filename, linenumber);
280 size = newCount * elementSize;
281 if (0 == size)
282 {
283 tmp = NULL;
284 }
285 else
286 {
287 tmp = GNUNET_xmalloc_ (size,
288 filename,
289 linenumber);
290 if (NULL != *old)
291 {
292 GNUNET_memcpy (tmp,
293 *old,
294 elementSize * GNUNET_MIN (*oldCount,
295 newCount));
296 }
297 }
298
299 if (NULL != *old)
300 {
301 GNUNET_xfree_ (*old,
302 filename,
303 linenumber);
304 }
305 *old = tmp;
306 *oldCount = newCount;
307}
308
309
310int
311GNUNET_asprintf (char **buf,
312 const char *format,
313 ...)
314{
315 int ret;
316 va_list args;
317
318 va_start (args,
319 format);
320 ret = vsnprintf (NULL,
321 0,
322 format,
323 args);
324 va_end (args);
325 GNUNET_assert (ret >= 0);
326 *buf = GNUNET_malloc (ret + 1);
327 va_start (args, format);
328 ret = vsnprintf (*buf,
329 ret + 1,
330 format,
331 args);
332 va_end (args);
333 return ret;
334}
335
336
337int
338GNUNET_snprintf (char *buf,
339 size_t size,
340 const char *format,
341 ...)
342{
343 int ret;
344 va_list args;
345
346 va_start (args,
347 format);
348 ret = vsnprintf (buf,
349 size,
350 format,
351 args);
352 va_end (args);
353 GNUNET_assert ((ret >= 0) && (((size_t) ret) < size));
354 return ret;
355}
356
357
360{
362 uint16_t msize;
363
364 msize = ntohs (msg->size);
365 GNUNET_assert (msize >= sizeof(struct GNUNET_MessageHeader));
366 ret = GNUNET_malloc (msize);
367 GNUNET_memcpy (ret, msg, msize);
368 return ret;
369}
370
371
372bool
373GNUNET_is_zero_ (const void *a,
374 size_t n)
375{
376 const char *b = a;
377
378 for (size_t i = 0; i < n; i++)
379 if (b[i])
380 return false;
381 return true;
382}
383
384
385/* 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.

◆ 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 228 of file common_allocation.c.

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}

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 312 of file common_allocation.c.

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}

References GNUNET_assert, GNUNET_malloc, and ret.

◆ GNUNET_snprintf()

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

Definition at line 339 of file common_allocation.c.

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}

References GNUNET_assert, ret, and size.