aboutsummaryrefslogtreecommitdiff
path: root/src/util/common_allocation.c
diff options
context:
space:
mode:
authorChristian Grothoff <christian@grothoff.org>2012-07-05 07:58:05 +0000
committerChristian Grothoff <christian@grothoff.org>2012-07-05 07:58:05 +0000
commit491bcd3a92a3585cbbe6a1aa59aae18bf141a7c5 (patch)
tree0f16ed97b2754b88054fbcbfc5e301a863160319 /src/util/common_allocation.c
parent60327189b5bcededb1c800415c1972d66d81366f (diff)
downloadgnunet-491bcd3a92a3585cbbe6a1aa59aae18bf141a7c5.tar.gz
gnunet-491bcd3a92a3585cbbe6a1aa59aae18bf141a7c5.zip
-LRN: Portable memory poisoning:
Uses GNU extension function as a non-W32 equivalent of msize() (there are subtle differences, but they are of no consequence). Also swaps the poison bits depending on endianness (i'm tired of seeing 0xdf0adba everywhere instead of 0xbaadf00d).
Diffstat (limited to 'src/util/common_allocation.c')
-rw-r--r--src/util/common_allocation.c24
1 files changed, 21 insertions, 3 deletions
diff --git a/src/util/common_allocation.c b/src/util/common_allocation.c
index c86185685..a385450b9 100644
--- a/src/util/common_allocation.c
+++ b/src/util/common_allocation.c
@@ -182,6 +182,24 @@ GNUNET_xrealloc_ (void *ptr, size_t n, const char *filename, int linenumber)
182} 182}
183 183
184 184
185# if __BYTE_ORDER == __LITTLE_ENDIAN
186#define BAADFOOD_STR "\x0D\xF0\xAD\xBA"
187#endif
188# if __BYTE_ORDER == __BIG_ENDIAN
189#define BAADFOOD_STR "\xBA\xAD\xF0\x0D"
190#endif
191
192#if WINDOWS
193#define MSIZE(p) _msize (p)
194#endif
195#if LINUX
196/* FIXME: manpage claims that this function is a GNU extension,
197 * but googling shows that it is available on many platforms via
198 * inclusion of various headers. For now let's make it Linux-only.
199 */
200#define MSIZE(p) malloc_usable_size (p)
201#endif
202
185/** 203/**
186 * Free memory. Merely a wrapper for the case that we 204 * Free memory. Merely a wrapper for the case that we
187 * want to keep track of allocations. 205 * want to keep track of allocations.
@@ -198,12 +216,12 @@ GNUNET_xfree_ (void *ptr, const char *filename, int linenumber)
198 ptr = &((size_t *) ptr)[-1]; 216 ptr = &((size_t *) ptr)[-1];
199 mem_used -= *((size_t *) ptr); 217 mem_used -= *((size_t *) ptr);
200#endif 218#endif
201#if WINDOWS 219#if defined(MSIZE)
202#if ENABLE_POISONING 220#if ENABLE_POISONING
203 { 221 {
204 size_t i; 222 size_t i;
205 char baadfood[4] = "\xBA\xAD\xF0\x0D"; 223 char baadfood[5] = BAADFOOD_STR;
206 size_t s = _msize (ptr); 224 size_t s = MSIZE (ptr);
207 for (i = 0; i < s; i++) 225 for (i = 0; i < s; i++)
208 ((char *) ptr)[i] = baadfood[i % 4]; 226 ((char *) ptr)[i] = baadfood[i % 4];
209 } 227 }