diff options
author | Christian Grothoff <christian@grothoff.org> | 2010-11-24 14:23:32 +0000 |
---|---|---|
committer | Christian Grothoff <christian@grothoff.org> | 2010-11-24 14:23:32 +0000 |
commit | c6fe079ca7000f7cd1fe6978d891f4d75b9f44ba (patch) | |
tree | b4b96d109a9916392d1c718d7ebaa5c69a84253d /src | |
parent | 50c90b76002c2e37e36a98ca0b2f01b1d0e8f84d (diff) | |
download | gnunet-c6fe079ca7000f7cd1fe6978d891f4d75b9f44ba.tar.gz gnunet-c6fe079ca7000f7cd1fe6978d891f4d75b9f44ba.zip |
memdup
Diffstat (limited to 'src')
-rw-r--r-- | src/include/gnunet_common.h | 24 | ||||
-rw-r--r-- | src/util/common_allocation.c | 40 |
2 files changed, 64 insertions, 0 deletions
diff --git a/src/include/gnunet_common.h b/src/include/gnunet_common.h index badb52880..dddee068a 100644 --- a/src/include/gnunet_common.h +++ b/src/include/gnunet_common.h | |||
@@ -354,6 +354,15 @@ unsigned long long GNUNET_htonll (unsigned long long n); | |||
354 | #define GNUNET_malloc(size) GNUNET_xmalloc_(size, __FILE__, __LINE__) | 354 | #define GNUNET_malloc(size) GNUNET_xmalloc_(size, __FILE__, __LINE__) |
355 | 355 | ||
356 | /** | 356 | /** |
357 | * Allocate and initialize a block of memory. | ||
358 | * | ||
359 | * @param buf data to initalize the block with | ||
360 | * @param size the number of bytes in buf (and size of the allocation) | ||
361 | * @return pointer to size bytes of memory, never NULL (!) | ||
362 | */ | ||
363 | #define GNUNET_memdup(buf,size) GNUNET_xmemdup_(buf, size, __FILE__, __LINE__) | ||
364 | |||
365 | /** | ||
357 | * Wrapper around malloc. Allocates size bytes of memory. | 366 | * Wrapper around malloc. Allocates size bytes of memory. |
358 | * The memory will be zero'ed out. | 367 | * The memory will be zero'ed out. |
359 | * | 368 | * |
@@ -478,6 +487,21 @@ int GNUNET_asprintf (char **buf, const char *format, ...); | |||
478 | void *GNUNET_xmalloc_ (size_t size, const char *filename, int linenumber); | 487 | void *GNUNET_xmalloc_ (size_t size, const char *filename, int linenumber); |
479 | 488 | ||
480 | 489 | ||
490 | |||
491 | /** | ||
492 | * Allocate and initialize memory. Checks the return value, aborts if no more | ||
493 | * memory is available. Don't use GNUNET_xmemdup_ directly. Use the | ||
494 | * GNUNET_memdup macro. | ||
495 | * | ||
496 | * @param buf buffer to initialize from (must contain size bytes) | ||
497 | * @param size number of bytes to allocate | ||
498 | * @param filename where is this call being made (for debugging) | ||
499 | * @param linenumber line where this call is being made (for debugging) | ||
500 | * @return allocated memory, never NULL | ||
501 | */ | ||
502 | void *GNUNET_xmemdup_ (const void *buf, size_t size, const char *filename, int linenumber); | ||
503 | |||
504 | |||
481 | /** | 505 | /** |
482 | * Allocate memory. This function does not check if the allocation | 506 | * Allocate memory. This function does not check if the allocation |
483 | * request is within reasonable bounds, allowing allocations larger | 507 | * request is within reasonable bounds, allowing allocations larger |
diff --git a/src/util/common_allocation.c b/src/util/common_allocation.c index 92cbc9747..59fa2dc05 100644 --- a/src/util/common_allocation.c +++ b/src/util/common_allocation.c | |||
@@ -69,6 +69,46 @@ GNUNET_xmalloc_ (size_t size, const char *filename, int linenumber) | |||
69 | 69 | ||
70 | 70 | ||
71 | /** | 71 | /** |
72 | * Allocate and initialize memory. Checks the return value, aborts if no more | ||
73 | * memory is available. Don't use GNUNET_xmemdup_ directly. Use the | ||
74 | * GNUNET_memdup macro. | ||
75 | * | ||
76 | * @param buf buffer to initialize from (must contain size bytes) | ||
77 | * @param size number of bytes to allocate | ||
78 | * @param filename where is this call being made (for debugging) | ||
79 | * @param linenumber line where this call is being made (for debugging) | ||
80 | * @return allocated memory, never NULL | ||
81 | */ | ||
82 | void *GNUNET_xmemdup_ (const void *buf, size_t size, const char *filename, int linenumber) | ||
83 | { | ||
84 | void *ret; | ||
85 | /* As a security precaution, we generally do not allow very large | ||
86 | allocations here */ | ||
87 | GNUNET_assert_at (size <= GNUNET_MAX_MALLOC_CHECKED, filename, linenumber); | ||
88 | #ifdef W32_MEM_LIMIT | ||
89 | size += sizeof (size_t); | ||
90 | if (mem_used + size > W32_MEM_LIMIT) | ||
91 | return NULL; | ||
92 | #endif | ||
93 | GNUNET_assert_at (size < INT_MAX, filename, linenumber); | ||
94 | ret = malloc (size); | ||
95 | if (ret == NULL) | ||
96 | { | ||
97 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "malloc"); | ||
98 | abort (); | ||
99 | } | ||
100 | #ifdef W32_MEM_LIMIT | ||
101 | *((size_t *) ret) = size; | ||
102 | ret = &((size_t *) ret)[1]; | ||
103 | mem_used += size; | ||
104 | #endif | ||
105 | memcpy (ret, buf, size); | ||
106 | return ret; | ||
107 | } | ||
108 | |||
109 | |||
110 | |||
111 | /** | ||
72 | * Wrapper around malloc. Allocates size bytes of memory. | 112 | * Wrapper around malloc. Allocates size bytes of memory. |
73 | * The memory will be zero'ed out. | 113 | * The memory will be zero'ed out. |
74 | * | 114 | * |