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/util/common_allocation.c | |
parent | 50c90b76002c2e37e36a98ca0b2f01b1d0e8f84d (diff) | |
download | gnunet-c6fe079ca7000f7cd1fe6978d891f4d75b9f44ba.tar.gz gnunet-c6fe079ca7000f7cd1fe6978d891f4d75b9f44ba.zip |
memdup
Diffstat (limited to 'src/util/common_allocation.c')
-rw-r--r-- | src/util/common_allocation.c | 40 |
1 files changed, 40 insertions, 0 deletions
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 | * |