diff options
author | Evan Huus <eapache@gmail.com> | 2013-01-23 01:11:36 +0000 |
---|---|---|
committer | Evan Huus <eapache@gmail.com> | 2013-01-23 01:11:36 +0000 |
commit | 681a3ad5a2d80a1b76e336256fb90f9caf2b89df (patch) | |
tree | e0c520578e02285bfd71100c4df8f4dbc0625ad9 /epan/wmem/wmem_core.c | |
parent | c25c5915c5c94b5c70e51e4ca43f1aa5286c6349 (diff) | |
download | wireshark-681a3ad5a2d80a1b76e336256fb90f9caf2b89df.tar.gz |
Handle NULL-pointers and 0-lengths in a much simpler allocator-agnostic way,
and document the fact that allocator authors don't have to care.
svn path=/trunk/; revision=47220
Diffstat (limited to 'epan/wmem/wmem_core.c')
-rw-r--r-- | epan/wmem/wmem_core.c | 33 |
1 files changed, 27 insertions, 6 deletions
diff --git a/epan/wmem/wmem_core.c b/epan/wmem/wmem_core.c index 0cb8c64aed..e15f1ff913 100644 --- a/epan/wmem/wmem_core.c +++ b/epan/wmem/wmem_core.c @@ -37,6 +37,10 @@ void * wmem_alloc(wmem_allocator_t *allocator, const size_t size) { + if (size == 0) { + return NULL; + } + return allocator->alloc(allocator->private_data, size); } @@ -44,24 +48,41 @@ void * wmem_alloc0(wmem_allocator_t *allocator, const size_t size) { void *buf; + + if (size == 0) { + return NULL; + } buf = wmem_alloc(allocator, size); return memset(buf, 0, size); } -void * -wmem_realloc(wmem_allocator_t *allocator, void *ptr, const size_t size) -{ - return allocator->realloc(allocator->private_data, ptr, size); -} - void wmem_free(wmem_allocator_t *allocator, void *ptr) { + if (ptr == NULL) { + return; + } + allocator->free(allocator->private_data, ptr); } +void * +wmem_realloc(wmem_allocator_t *allocator, void *ptr, const size_t size) +{ + if (ptr == NULL) { + return wmem_alloc(allocator, size); + } + + if (size == 0) { + wmem_free(allocator, ptr); + return NULL; + } + + return allocator->realloc(allocator->private_data, ptr, size); +} + void wmem_free_all(wmem_allocator_t *allocator) { |