diff options
author | Evan Huus <eapache@gmail.com> | 2012-12-18 23:36:45 +0000 |
---|---|---|
committer | Evan Huus <eapache@gmail.com> | 2012-12-18 23:36:45 +0000 |
commit | 561525cbb538b030d28aa4951cf77c9f3f286954 (patch) | |
tree | db11c853df698f691bd2537fd68717ae8fa8f0ce /epan/wmem/wmem_allocator_simple.c | |
parent | 09221f45e4db2e2fb01fda57bc20e315a29c9838 (diff) |
Rename allocator_glib to allocator_simple, since the block allocator also
uses glib and 'simple' describes the intention far better.
svn path=/trunk/; revision=46603
Diffstat (limited to 'epan/wmem/wmem_allocator_simple.c')
-rw-r--r-- | epan/wmem/wmem_allocator_simple.c | 109 |
1 files changed, 109 insertions, 0 deletions
diff --git a/epan/wmem/wmem_allocator_simple.c b/epan/wmem/wmem_allocator_simple.c new file mode 100644 index 0000000000..c5d9f25d47 --- /dev/null +++ b/epan/wmem/wmem_allocator_simple.c @@ -0,0 +1,109 @@ +/* wmem_allocator_simple.c + * Wireshark Memory Manager Simple Allocator + * Copyright 2012, Evan Huus <eapache@gmail.com> + * + * $Id$ + * + * Wireshark - Network traffic analyzer + * By Gerald Combs <gerald@wireshark.org> + * Copyright 1998 Gerald Combs + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include <string.h> + +#include <glib.h> + +#include "wmem_core.h" +#include "wmem_allocator.h" + +/* In this trivial allocator, we just store a GSList of g_malloc()ed + * blocks in the private_data pointer. */ +typedef struct _wmem_simple_allocator_t { + GSList *block_list; +} wmem_simple_allocator_t; + +static void * +wmem_simple_alloc(void *private_data, const size_t size) +{ + void *buf; + wmem_simple_allocator_t *allocator = (wmem_simple_allocator_t*) private_data; + + buf = g_malloc(size); + + allocator->block_list = g_slist_prepend(allocator->block_list, buf); + + return buf; +} + +static void +wmem_simple_free_all(void *private_data) +{ + wmem_simple_allocator_t *allocator = (wmem_simple_allocator_t*) private_data; + GSList *tmp; + + /* We can't use g_slist_free_full() as it was only introduced in GLIB 2.28 + * while we support way back to 2.14. So loop through and manually free + * each block, then call g_slist_free(). */ + tmp = allocator->block_list; + while (tmp) { + g_free(tmp->data); + tmp = tmp->next; + } + g_slist_free(allocator->block_list); + + allocator->block_list = NULL; +} + +static void +wmem_simple_allocator_destroy(wmem_allocator_t *allocator) +{ + g_free(allocator->private_data); + g_free(allocator); +} + +wmem_allocator_t * +wmem_simple_allocator_new(void) +{ + wmem_allocator_t *allocator; + wmem_simple_allocator_t *simple_allocator; + + allocator = g_new(wmem_allocator_t, 1); + simple_allocator = g_new(wmem_simple_allocator_t, 1); + + allocator->alloc = &wmem_simple_alloc; + allocator->free_all = &wmem_simple_free_all; + allocator->destroy = &wmem_simple_allocator_destroy; + allocator->private_data = (void*) simple_allocator; + + simple_allocator->block_list = NULL; + + return allocator; +} + + +/* + * Editor modelines - http://www.wireshark.org/tools/modelines.html + * + * Local variables: + * c-basic-offset: 4 + * tab-width: 8 + * indent-tabs-mode: nil + * End: + * + * vi: set shiftwidth=4 tabstop=8 expandtab: + * :indentSize=4:tabSize=8:noTabs=true: + */ |