2012-12-18 23:36:45 +00:00
|
|
|
/* wmem_allocator_simple.c
|
|
|
|
* Wireshark Memory Manager Simple Allocator
|
2012-10-24 02:04:40 +00:00
|
|
|
* Copyright 2012, Evan Huus <eapache@gmail.com>
|
|
|
|
*
|
|
|
|
* Wireshark - Network traffic analyzer
|
|
|
|
* By Gerald Combs <gerald@wireshark.org>
|
|
|
|
* Copyright 1998 Gerald Combs
|
|
|
|
*
|
2018-02-08 16:31:35 +00:00
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later
|
2012-10-24 02:04:40 +00:00
|
|
|
*/
|
|
|
|
|
2013-08-14 00:31:14 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
2012-10-24 02:04:40 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include <glib.h>
|
|
|
|
|
2012-10-24 05:52:55 +00:00
|
|
|
#include "wmem_core.h"
|
2012-10-24 02:04:40 +00:00
|
|
|
#include "wmem_allocator.h"
|
2013-12-20 06:30:13 +00:00
|
|
|
#include "wmem_allocator_simple.h"
|
2012-10-24 02:04:40 +00:00
|
|
|
|
2021-03-20 13:13:27 +00:00
|
|
|
#include <wsutil/ws_assert.h>
|
|
|
|
|
2014-05-11 22:25:23 +00:00
|
|
|
#define DEFAULT_ALLOCS 8192
|
|
|
|
|
2012-12-18 23:36:45 +00:00
|
|
|
typedef struct _wmem_simple_allocator_t {
|
2014-05-11 22:25:23 +00:00
|
|
|
int size;
|
|
|
|
int count;
|
|
|
|
void **ptrs;
|
2012-12-18 23:36:45 +00:00
|
|
|
} wmem_simple_allocator_t;
|
2012-10-24 02:04:40 +00:00
|
|
|
|
|
|
|
static void *
|
2012-12-18 23:36:45 +00:00
|
|
|
wmem_simple_alloc(void *private_data, const size_t size)
|
2012-10-24 02:04:40 +00:00
|
|
|
{
|
2013-01-19 19:56:20 +00:00
|
|
|
wmem_simple_allocator_t *allocator;
|
|
|
|
|
|
|
|
allocator = (wmem_simple_allocator_t*) private_data;
|
2013-09-01 13:37:38 +00:00
|
|
|
|
2018-04-22 18:13:46 +00:00
|
|
|
if (G_UNLIKELY(allocator->count == allocator->size)) {
|
2014-05-11 22:25:23 +00:00
|
|
|
allocator->size *= 2;
|
|
|
|
allocator->ptrs = (void**)wmem_realloc(NULL, allocator->ptrs,
|
|
|
|
sizeof(void*) * allocator->size);
|
|
|
|
}
|
2012-10-24 02:04:40 +00:00
|
|
|
|
2014-05-11 22:25:23 +00:00
|
|
|
return allocator->ptrs[allocator->count++] = wmem_alloc(NULL, size);
|
2013-09-01 13:37:38 +00:00
|
|
|
}
|
|
|
|
|
2013-02-08 01:47:48 +00:00
|
|
|
static void
|
|
|
|
wmem_simple_free(void *private_data, void *ptr)
|
|
|
|
{
|
2014-05-11 22:25:23 +00:00
|
|
|
int i;
|
2013-02-08 01:47:48 +00:00
|
|
|
wmem_simple_allocator_t *allocator;
|
|
|
|
|
|
|
|
allocator = (wmem_simple_allocator_t*) private_data;
|
|
|
|
|
2014-05-11 22:25:23 +00:00
|
|
|
wmem_free(NULL, ptr);
|
|
|
|
allocator->count--;
|
|
|
|
|
|
|
|
for (i=allocator->count; i>=0; i--) {
|
|
|
|
if (ptr == allocator->ptrs[i]) {
|
|
|
|
if (i < allocator->count) {
|
|
|
|
allocator->ptrs[i] = allocator->ptrs[allocator->count];
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2021-03-20 13:13:27 +00:00
|
|
|
|
|
|
|
ws_assert_not_reached();
|
2013-02-08 01:47:48 +00:00
|
|
|
}
|
|
|
|
|
2013-01-19 19:56:20 +00:00
|
|
|
static void *
|
|
|
|
wmem_simple_realloc(void *private_data, void *ptr, const size_t size)
|
2014-05-11 22:25:23 +00:00
|
|
|
{
|
|
|
|
int i;
|
2013-01-19 19:56:20 +00:00
|
|
|
wmem_simple_allocator_t *allocator;
|
|
|
|
|
|
|
|
allocator = (wmem_simple_allocator_t*) private_data;
|
2013-09-01 13:37:38 +00:00
|
|
|
|
2014-05-11 22:25:23 +00:00
|
|
|
for (i=allocator->count-1; i>=0; i--) {
|
|
|
|
if (ptr == allocator->ptrs[i]) {
|
|
|
|
return allocator->ptrs[i] = wmem_realloc(NULL, allocator->ptrs[i], size);
|
|
|
|
}
|
2013-01-19 19:56:20 +00:00
|
|
|
}
|
|
|
|
|
2021-03-20 13:13:27 +00:00
|
|
|
ws_assert_not_reached();
|
2013-01-19 19:56:20 +00:00
|
|
|
}
|
|
|
|
|
2012-10-24 02:04:40 +00:00
|
|
|
static void
|
2012-12-18 23:36:45 +00:00
|
|
|
wmem_simple_free_all(void *private_data)
|
2012-10-24 02:04:40 +00:00
|
|
|
{
|
2013-01-19 19:56:20 +00:00
|
|
|
wmem_simple_allocator_t *allocator;
|
2014-05-11 22:25:23 +00:00
|
|
|
int i;
|
2013-09-01 13:37:38 +00:00
|
|
|
|
2013-01-19 19:56:20 +00:00
|
|
|
allocator = (wmem_simple_allocator_t*) private_data;
|
|
|
|
|
2014-05-11 22:25:23 +00:00
|
|
|
for (i = 0; i<allocator->count; i++) {
|
|
|
|
wmem_free(NULL, allocator->ptrs[i]);
|
|
|
|
}
|
|
|
|
allocator->count = 0;
|
2013-01-19 19:56:20 +00:00
|
|
|
}
|
2012-10-24 02:04:40 +00:00
|
|
|
|
2013-01-19 19:56:20 +00:00
|
|
|
static void
|
|
|
|
wmem_simple_gc(void *private_data _U_)
|
|
|
|
{
|
|
|
|
/* In this simple allocator, there is nothing to garbage-collect */
|
2012-10-24 02:04:40 +00:00
|
|
|
}
|
|
|
|
|
2012-10-27 02:29:46 +00:00
|
|
|
static void
|
2013-05-22 15:42:12 +00:00
|
|
|
wmem_simple_allocator_cleanup(void *private_data)
|
2012-10-27 02:29:46 +00:00
|
|
|
{
|
2013-05-22 15:42:12 +00:00
|
|
|
wmem_simple_allocator_t *allocator;
|
2013-09-01 13:37:38 +00:00
|
|
|
|
2013-05-22 15:42:12 +00:00
|
|
|
allocator = (wmem_simple_allocator_t*) private_data;
|
2013-01-19 19:56:20 +00:00
|
|
|
|
2014-05-11 22:25:23 +00:00
|
|
|
wmem_free(NULL, allocator->ptrs);
|
2013-09-01 13:37:38 +00:00
|
|
|
wmem_free(NULL, allocator);
|
2012-10-27 02:29:46 +00:00
|
|
|
}
|
|
|
|
|
2013-05-22 15:42:12 +00:00
|
|
|
void
|
|
|
|
wmem_simple_allocator_init(wmem_allocator_t *allocator)
|
2012-10-24 02:04:40 +00:00
|
|
|
{
|
2012-12-18 23:36:45 +00:00
|
|
|
wmem_simple_allocator_t *simple_allocator;
|
2012-10-24 02:04:40 +00:00
|
|
|
|
2013-09-01 13:37:38 +00:00
|
|
|
simple_allocator = wmem_new(NULL, wmem_simple_allocator_t);
|
2012-10-24 02:04:40 +00:00
|
|
|
|
2016-05-12 17:39:15 +00:00
|
|
|
allocator->walloc = &wmem_simple_alloc;
|
|
|
|
allocator->wrealloc = &wmem_simple_realloc;
|
|
|
|
allocator->wfree = &wmem_simple_free;
|
2013-01-19 19:56:20 +00:00
|
|
|
|
|
|
|
allocator->free_all = &wmem_simple_free_all;
|
|
|
|
allocator->gc = &wmem_simple_gc;
|
2013-05-22 15:42:12 +00:00
|
|
|
allocator->cleanup = &wmem_simple_allocator_cleanup;
|
|
|
|
|
|
|
|
allocator->private_data = (void*) simple_allocator;
|
2013-01-19 16:15:32 +00:00
|
|
|
|
2014-05-11 22:25:23 +00:00
|
|
|
simple_allocator->count = 0;
|
|
|
|
simple_allocator->size = DEFAULT_ALLOCS;
|
|
|
|
simple_allocator->ptrs = wmem_alloc_array(NULL, void*, DEFAULT_ALLOCS);
|
2012-10-24 02:04:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2019-07-26 18:43:17 +00:00
|
|
|
* Editor modelines - https://www.wireshark.org/tools/modelines.html
|
2012-10-24 02:04:40 +00:00
|
|
|
*
|
|
|
|
* 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:
|
|
|
|
*/
|