diff options
Diffstat (limited to 'src/util/common_allocation.c')
-rw-r--r-- | src/util/common_allocation.c | 71 |
1 files changed, 37 insertions, 34 deletions
diff --git a/src/util/common_allocation.c b/src/util/common_allocation.c index 40ffe8c61..6f01e8230 100644 --- a/src/util/common_allocation.c +++ b/src/util/common_allocation.c | |||
@@ -55,15 +55,16 @@ void * | |||
55 | GNUNET_xmalloc_ (size_t size, const char *filename, int linenumber) | 55 | GNUNET_xmalloc_ (size_t size, const char *filename, int linenumber) |
56 | { | 56 | { |
57 | void *ret; | 57 | void *ret; |
58 | |||
58 | /* As a security precaution, we generally do not allow very large | 59 | /* As a security precaution, we generally do not allow very large |
59 | allocations using the default 'GNUNET_malloc' macro */ | 60 | * allocations using the default 'GNUNET_malloc' macro */ |
60 | GNUNET_assert_at (size <= GNUNET_MAX_MALLOC_CHECKED, filename, linenumber); | 61 | GNUNET_assert_at (size <= GNUNET_MAX_MALLOC_CHECKED, filename, linenumber); |
61 | ret = GNUNET_xmalloc_unchecked_ (size, filename, linenumber); | 62 | ret = GNUNET_xmalloc_unchecked_ (size, filename, linenumber); |
62 | if (ret == NULL) | 63 | if (ret == NULL) |
63 | { | 64 | { |
64 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "malloc"); | 65 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "malloc"); |
65 | abort (); | 66 | abort (); |
66 | } | 67 | } |
67 | return ret; | 68 | return ret; |
68 | } | 69 | } |
69 | 70 | ||
@@ -79,11 +80,14 @@ GNUNET_xmalloc_ (size_t size, const char *filename, int linenumber) | |||
79 | * @param linenumber line where this call is being made (for debugging) | 80 | * @param linenumber line where this call is being made (for debugging) |
80 | * @return allocated memory, never NULL | 81 | * @return allocated memory, never NULL |
81 | */ | 82 | */ |
82 | void *GNUNET_xmemdup_ (const void *buf, size_t size, const char *filename, int linenumber) | 83 | void * |
84 | GNUNET_xmemdup_ (const void *buf, size_t size, const char *filename, | ||
85 | int linenumber) | ||
83 | { | 86 | { |
84 | void *ret; | 87 | void *ret; |
88 | |||
85 | /* As a security precaution, we generally do not allow very large | 89 | /* As a security precaution, we generally do not allow very large |
86 | allocations here */ | 90 | * allocations here */ |
87 | GNUNET_assert_at (size <= GNUNET_MAX_MALLOC_CHECKED, filename, linenumber); | 91 | GNUNET_assert_at (size <= GNUNET_MAX_MALLOC_CHECKED, filename, linenumber); |
88 | #ifdef W32_MEM_LIMIT | 92 | #ifdef W32_MEM_LIMIT |
89 | size += sizeof (size_t); | 93 | size += sizeof (size_t); |
@@ -93,10 +97,10 @@ void *GNUNET_xmemdup_ (const void *buf, size_t size, const char *filename, int l | |||
93 | GNUNET_assert_at (size < INT_MAX, filename, linenumber); | 97 | GNUNET_assert_at (size < INT_MAX, filename, linenumber); |
94 | ret = malloc (size); | 98 | ret = malloc (size); |
95 | if (ret == NULL) | 99 | if (ret == NULL) |
96 | { | 100 | { |
97 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "malloc"); | 101 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "malloc"); |
98 | abort (); | 102 | abort (); |
99 | } | 103 | } |
100 | #ifdef W32_MEM_LIMIT | 104 | #ifdef W32_MEM_LIMIT |
101 | *((size_t *) ret) = size; | 105 | *((size_t *) ret) = size; |
102 | ret = &((size_t *) ret)[1]; | 106 | ret = &((size_t *) ret)[1]; |
@@ -155,9 +159,7 @@ GNUNET_xmalloc_unchecked_ (size_t size, const char *filename, int linenumber) | |||
155 | * @return pointer to size bytes of memory | 159 | * @return pointer to size bytes of memory |
156 | */ | 160 | */ |
157 | void * | 161 | void * |
158 | GNUNET_xrealloc_ (void *ptr, | 162 | GNUNET_xrealloc_ (void *ptr, size_t n, const char *filename, int linenumber) |
159 | size_t n, | ||
160 | const char *filename, int linenumber) | ||
161 | { | 163 | { |
162 | #ifdef W32_MEM_LIMIT | 164 | #ifdef W32_MEM_LIMIT |
163 | n += sizeof (size_t); | 165 | n += sizeof (size_t); |
@@ -165,11 +167,11 @@ GNUNET_xrealloc_ (void *ptr, | |||
165 | mem_used = mem_used - *((size_t *) ptr) + n; | 167 | mem_used = mem_used - *((size_t *) ptr) + n; |
166 | #endif | 168 | #endif |
167 | ptr = realloc (ptr, n); | 169 | ptr = realloc (ptr, n); |
168 | if ( (NULL == ptr) && (n > 0) ) | 170 | if ((NULL == ptr) && (n > 0)) |
169 | { | 171 | { |
170 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "realloc"); | 172 | GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "realloc"); |
171 | abort (); | 173 | abort (); |
172 | } | 174 | } |
173 | #ifdef W32_MEM_LIMIT | 175 | #ifdef W32_MEM_LIMIT |
174 | ptr = &((size_t *) ptr)[1]; | 176 | ptr = &((size_t *) ptr)[1]; |
175 | #endif | 177 | #endif |
@@ -226,12 +228,13 @@ GNUNET_xstrdup_ (const char *str, const char *filename, int linenumber) | |||
226 | * @return strndup(str,len) | 228 | * @return strndup(str,len) |
227 | */ | 229 | */ |
228 | char * | 230 | char * |
229 | GNUNET_xstrndup_ (const char *str, size_t len, const char *filename, int linenumber) | 231 | GNUNET_xstrndup_ (const char *str, size_t len, const char *filename, |
232 | int linenumber) | ||
230 | { | 233 | { |
231 | char *res; | 234 | char *res; |
232 | 235 | ||
233 | GNUNET_assert_at (str != NULL, filename, linenumber); | 236 | GNUNET_assert_at (str != NULL, filename, linenumber); |
234 | len = GNUNET_MIN(len,strlen(str)); | 237 | len = GNUNET_MIN (len, strlen (str)); |
235 | res = GNUNET_xmalloc_ (len + 1, filename, linenumber); | 238 | res = GNUNET_xmalloc_ (len + 1, filename, linenumber); |
236 | memcpy (res, str, len); | 239 | memcpy (res, str, len); |
237 | res[len] = '\0'; | 240 | res[len] = '\0'; |
@@ -263,22 +266,22 @@ GNUNET_xgrow_ (void **old, | |||
263 | GNUNET_assert_at (INT_MAX / elementSize > newCount, filename, linenumber); | 266 | GNUNET_assert_at (INT_MAX / elementSize > newCount, filename, linenumber); |
264 | size = newCount * elementSize; | 267 | size = newCount * elementSize; |
265 | if (size == 0) | 268 | if (size == 0) |
266 | { | 269 | { |
267 | tmp = NULL; | 270 | tmp = NULL; |
268 | } | 271 | } |
269 | else | 272 | else |
270 | { | 273 | { |
271 | tmp = GNUNET_xmalloc_ (size, filename, linenumber); | 274 | tmp = GNUNET_xmalloc_ (size, filename, linenumber); |
272 | memset (tmp, 0, size); /* client code should not rely on this, though... */ | 275 | memset (tmp, 0, size); /* client code should not rely on this, though... */ |
273 | if (*oldCount > newCount) | 276 | if (*oldCount > newCount) |
274 | *oldCount = newCount; /* shrink is also allowed! */ | 277 | *oldCount = newCount; /* shrink is also allowed! */ |
275 | memcpy (tmp, *old, elementSize * (*oldCount)); | 278 | memcpy (tmp, *old, elementSize * (*oldCount)); |
276 | } | 279 | } |
277 | 280 | ||
278 | if (*old != NULL) | 281 | if (*old != NULL) |
279 | { | 282 | { |
280 | GNUNET_xfree_ (*old, filename, linenumber); | 283 | GNUNET_xfree_ (*old, filename, linenumber); |
281 | } | 284 | } |
282 | *old = tmp; | 285 | *old = tmp; |
283 | *oldCount = newCount; | 286 | *oldCount = newCount; |
284 | } | 287 | } |