2013-07-06 04:02:08 +00:00
|
|
|
/* wmem_array.c
|
|
|
|
* Wireshark Memory Manager Array
|
|
|
|
* Copyright 2013, 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
|
2013-07-06 04:02:08 +00:00
|
|
|
*/
|
|
|
|
|
2013-08-14 00:31:14 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
2013-07-06 04:02:08 +00:00
|
|
|
#include <string.h>
|
2013-07-06 17:47:32 +00:00
|
|
|
#include <stdlib.h>
|
2013-07-06 04:02:08 +00:00
|
|
|
#include <glib.h>
|
|
|
|
|
|
|
|
#include "wmem_core.h"
|
|
|
|
#include "wmem_array.h"
|
|
|
|
|
2021-03-20 13:13:27 +00:00
|
|
|
#include <wsutil/ws_assert.h>
|
|
|
|
|
2013-07-06 04:02:08 +00:00
|
|
|
/* Holds a wmem-allocated array.
|
|
|
|
* elem_len is the size of each element
|
|
|
|
* elem_count is the number of used elements
|
|
|
|
* alloc_count is the length (in elems) of the raw buffer pointed to by buf,
|
|
|
|
* regardless of how many elems are used (the contents)
|
|
|
|
*/
|
|
|
|
struct _wmem_array_t {
|
|
|
|
wmem_allocator_t *allocator;
|
|
|
|
|
|
|
|
guint8 *buf;
|
|
|
|
|
|
|
|
gsize elem_size;
|
|
|
|
|
|
|
|
guint elem_count;
|
|
|
|
guint alloc_count;
|
2016-02-18 10:32:07 +00:00
|
|
|
|
|
|
|
gboolean null_terminated;
|
2013-07-06 04:02:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
wmem_array_t *
|
|
|
|
wmem_array_sized_new(wmem_allocator_t *allocator, gsize elem_size,
|
|
|
|
guint alloc_count)
|
|
|
|
{
|
|
|
|
wmem_array_t *array;
|
|
|
|
|
|
|
|
array = wmem_new(allocator, wmem_array_t);
|
|
|
|
|
|
|
|
array->allocator = allocator;
|
|
|
|
array->elem_size = elem_size;
|
|
|
|
array->elem_count = 0;
|
|
|
|
array->alloc_count = alloc_count ? alloc_count : 1;
|
2016-02-18 10:32:07 +00:00
|
|
|
array->null_terminated = FALSE;
|
2013-07-06 04:02:08 +00:00
|
|
|
|
|
|
|
array->buf = (guint8 *)wmem_alloc(array->allocator,
|
|
|
|
array->elem_size * array->alloc_count);
|
|
|
|
|
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
|
|
|
wmem_array_t *
|
|
|
|
wmem_array_new(wmem_allocator_t *allocator, const gsize elem_size)
|
|
|
|
{
|
|
|
|
wmem_array_t *array;
|
|
|
|
|
|
|
|
array = wmem_array_sized_new(allocator, elem_size, 1);
|
|
|
|
|
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
2020-07-30 13:40:14 +00:00
|
|
|
void
|
2013-07-06 04:02:08 +00:00
|
|
|
wmem_array_grow(wmem_array_t *array, const guint to_add)
|
|
|
|
{
|
|
|
|
guint new_alloc_count, new_count;
|
|
|
|
|
|
|
|
new_alloc_count = array->alloc_count;
|
|
|
|
new_count = array->elem_count + to_add;
|
|
|
|
|
|
|
|
while (new_alloc_count < new_count) {
|
|
|
|
new_alloc_count *= 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (new_alloc_count == array->alloc_count) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
array->buf = (guint8 *)wmem_realloc(array->allocator, array->buf,
|
|
|
|
new_alloc_count * array->elem_size);
|
|
|
|
|
|
|
|
array->alloc_count = new_alloc_count;
|
|
|
|
}
|
|
|
|
|
2016-02-18 10:32:07 +00:00
|
|
|
static void
|
|
|
|
wmem_array_write_null_terminator(wmem_array_t *array)
|
|
|
|
{
|
|
|
|
if (array->null_terminated) {
|
|
|
|
wmem_array_grow(array, 1);
|
|
|
|
memset(&array->buf[array->elem_count * array->elem_size], 0x0, array->elem_size);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
wmem_array_set_null_terminator(wmem_array_t *array)
|
|
|
|
{
|
|
|
|
array->null_terminated = TRUE;
|
|
|
|
wmem_array_write_null_terminator(array);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
wmem_array_bzero(wmem_array_t *array)
|
|
|
|
{
|
|
|
|
memset(array->buf, 0x0, array->elem_size * array->elem_count);
|
|
|
|
}
|
|
|
|
|
2013-07-06 04:02:08 +00:00
|
|
|
void
|
|
|
|
wmem_array_append(wmem_array_t *array, const void *in, guint count)
|
|
|
|
{
|
|
|
|
wmem_array_grow(array, count);
|
|
|
|
|
|
|
|
memcpy(&array->buf[array->elem_count * array->elem_size], in,
|
|
|
|
count * array->elem_size);
|
|
|
|
|
|
|
|
array->elem_count += count;
|
2016-02-18 10:32:07 +00:00
|
|
|
|
|
|
|
wmem_array_write_null_terminator(array);
|
2013-07-06 04:02:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
2013-07-06 08:33:07 +00:00
|
|
|
wmem_array_index(wmem_array_t *array, guint array_index)
|
2013-07-06 04:02:08 +00:00
|
|
|
{
|
2021-05-24 00:50:58 +00:00
|
|
|
ws_abort_if_fail(array_index < array->elem_count);
|
2013-07-06 08:33:07 +00:00
|
|
|
return &array->buf[array_index * array->elem_size];
|
2013-07-06 04:02:08 +00:00
|
|
|
}
|
|
|
|
|
2019-07-05 14:08:18 +00:00
|
|
|
int
|
|
|
|
wmem_array_try_index(wmem_array_t *array, guint array_index, void *val)
|
|
|
|
{
|
|
|
|
if (array_index >= array->elem_count)
|
|
|
|
return -1;
|
|
|
|
memcpy(val, &array->buf[array_index * array->elem_size], array->elem_size);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-07-06 17:47:32 +00:00
|
|
|
void
|
|
|
|
wmem_array_sort(wmem_array_t *array, int (*compar)(const void*,const void*))
|
|
|
|
{
|
|
|
|
qsort(array->buf, array->elem_count, array->elem_size, compar);
|
|
|
|
}
|
|
|
|
|
2013-07-06 04:02:08 +00:00
|
|
|
void *
|
|
|
|
wmem_array_get_raw(wmem_array_t *array)
|
|
|
|
{
|
|
|
|
return array->buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
guint
|
|
|
|
wmem_array_get_count(wmem_array_t *array)
|
|
|
|
{
|
|
|
|
return array->elem_count;
|
|
|
|
}
|
|
|
|
|
2016-01-02 07:38:43 +00:00
|
|
|
void
|
|
|
|
wmem_destroy_array(wmem_array_t *array)
|
|
|
|
{
|
|
|
|
wmem_free(array->allocator, array->buf);
|
|
|
|
wmem_free(array->allocator, array);
|
|
|
|
}
|
|
|
|
|
2013-07-06 04:02:08 +00:00
|
|
|
/*
|
2019-07-26 18:43:17 +00:00
|
|
|
* Editor modelines - https://www.wireshark.org/tools/modelines.html
|
2013-07-06 04:02:08 +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:
|
|
|
|
*/
|