summaryrefslogtreecommitdiffstats
path: root/wsutil/wmem
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-09-19 04:14:33 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-09-19 04:14:33 +0000
commit9f153fbfec0fb9c9ce38e749a7c6f4a5e115d4e9 (patch)
tree2784370cda9bbf2da9114d70f05399c0b229d28c /wsutil/wmem
parentAdding debian version 4.2.6-1. (diff)
downloadwireshark-9f153fbfec0fb9c9ce38e749a7c6f4a5e115d4e9.tar.xz
wireshark-9f153fbfec0fb9c9ce38e749a7c6f4a5e115d4e9.zip
Merging upstream version 4.4.0.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'wsutil/wmem')
-rw-r--r--wsutil/wmem/wmem_allocator_block.c4
-rw-r--r--wsutil/wmem/wmem_core.c2
-rw-r--r--wsutil/wmem/wmem_core.h8
-rw-r--r--wsutil/wmem/wmem_interval_tree.c1
-rw-r--r--wsutil/wmem/wmem_list.c50
-rw-r--r--wsutil/wmem/wmem_list.h7
-rw-r--r--wsutil/wmem/wmem_map.c6
-rw-r--r--wsutil/wmem/wmem_map.h6
-rw-r--r--wsutil/wmem/wmem_miscutl.c4
-rw-r--r--wsutil/wmem/wmem_miscutl.h6
-rw-r--r--wsutil/wmem/wmem_strutl.c15
-rw-r--r--wsutil/wmem/wmem_strutl.h4
-rw-r--r--wsutil/wmem/wmem_test.c6
-rw-r--r--wsutil/wmem/wmem_tree.c1
14 files changed, 91 insertions, 29 deletions
diff --git a/wsutil/wmem/wmem_allocator_block.c b/wsutil/wmem/wmem_allocator_block.c
index b8a0f598..4723f0f3 100644
--- a/wsutil/wmem/wmem_allocator_block.c
+++ b/wsutil/wmem/wmem_allocator_block.c
@@ -18,7 +18,7 @@
#include "wmem_allocator.h"
#include "wmem_allocator_block.h"
-/* This has turned into a very interesting excercise in algorithms and data
+/* This has turned into a very interesting exercise in algorithms and data
* structures.
*
* HISTORY
@@ -479,7 +479,7 @@ wmem_block_pop_master(wmem_block_allocator_t *allocator)
/* CHUNK HELPERS */
/* Takes a free chunk and checks the chunks to its immediate right and left in
- * the block. If they are also free, the contigous free chunks are merged into
+ * the block. If they are also free, the contiguous free chunks are merged into
* a single free chunk. The resulting chunk ends up in either the master list or
* the recycler, depending on where the merged chunks were originally.
*/
diff --git a/wsutil/wmem/wmem_core.c b/wsutil/wmem/wmem_core.c
index a166bf84..6a589db1 100644
--- a/wsutil/wmem/wmem_core.c
+++ b/wsutil/wmem/wmem_core.c
@@ -25,7 +25,7 @@
/* Set according to the WIRESHARK_DEBUG_WMEM_OVERRIDE environment variable in
* wmem_init. Should not be set again. */
-static bool do_override = false;
+static bool do_override;
static wmem_allocator_type_t override_type;
void *
diff --git a/wsutil/wmem/wmem_core.h b/wsutil/wmem/wmem_core.h
index 2f423133..25784c19 100644
--- a/wsutil/wmem/wmem_core.h
+++ b/wsutil/wmem/wmem_core.h
@@ -73,7 +73,8 @@ typedef enum _wmem_allocator_type_t {
WS_DLL_PUBLIC
void *
wmem_alloc(wmem_allocator_t *allocator, const size_t size)
-G_GNUC_MALLOC;
+G_GNUC_MALLOC
+G_GNUC_ALLOC_SIZE(2);
/** Allocate memory sufficient to hold one object of the given type.
*
@@ -112,7 +113,8 @@ G_GNUC_MALLOC;
WS_DLL_PUBLIC
void *
wmem_alloc0(wmem_allocator_t *allocator, const size_t size)
-G_GNUC_MALLOC;
+G_GNUC_MALLOC
+G_GNUC_ALLOC_SIZE(2);
/** Allocate memory sufficient to hold one object of the given type.
* Initializes the allocated memory with zeroes.
@@ -161,7 +163,7 @@ wmem_free(wmem_allocator_t *allocator, void *ptr);
WS_DLL_PUBLIC
void *
wmem_realloc(wmem_allocator_t *allocator, void *ptr, const size_t size)
-G_GNUC_MALLOC;
+G_GNUC_ALLOC_SIZE(3);
/** Frees all the memory allocated in a pool. Depending on the allocator
* implementation used this can be significantly cheaper than calling
diff --git a/wsutil/wmem/wmem_interval_tree.c b/wsutil/wmem/wmem_interval_tree.c
index 8b28549a..d8ed8e48 100644
--- a/wsutil/wmem/wmem_interval_tree.c
+++ b/wsutil/wmem/wmem_interval_tree.c
@@ -25,7 +25,6 @@
#include "wmem_interval_tree.h"
#include "wmem_user_cb.h"
-
static void
print_range(const void *value)
{
diff --git a/wsutil/wmem/wmem_list.c b/wsutil/wmem/wmem_list.c
index c03ffe31..b5b4c52f 100644
--- a/wsutil/wmem/wmem_list.c
+++ b/wsutil/wmem/wmem_list.c
@@ -218,6 +218,56 @@ wmem_list_insert_sorted(wmem_list_t *list, void* data, GCompareFunc func)
new_frame->next->prev = new_frame;
}
+void
+wmem_list_append_sorted(wmem_list_t *list, void* data, GCompareFunc func)
+{
+ wmem_list_frame_t *new_frame;
+ wmem_list_frame_t *cur;
+ wmem_list_frame_t *next;
+
+ new_frame = wmem_new(list->allocator, wmem_list_frame_t);
+ new_frame->data = data;
+ new_frame->next = NULL;
+ new_frame->prev = NULL;
+
+ list->count++;
+
+ if (!list->head) {
+ list->head = new_frame;
+ list->tail = new_frame;
+ return;
+ }
+
+ cur = list->tail;
+
+ /* best case scenario: append */
+ if (func(cur->data, data) <= 0) {
+ cur->next = new_frame;
+ new_frame->prev = cur;
+ list->tail = new_frame;
+ return;
+ }
+
+ do {
+ next = cur;
+ cur = cur->prev;
+ } while (cur && func(cur->data, data) >= 0);
+
+ /* worst case scenario: prepend */
+ if (!cur) {
+ next->prev = new_frame;
+ new_frame->next = next;
+ list->head = new_frame;
+ return;
+ }
+
+ /* ordinary case: insert */
+ new_frame->next = next;
+ new_frame->prev = cur;
+ new_frame->prev->next = new_frame;
+ new_frame->next->prev = new_frame;
+}
+
wmem_list_t *
wmem_list_new(wmem_allocator_t *allocator)
{
diff --git a/wsutil/wmem/wmem_list.h b/wsutil/wmem/wmem_list.h
index fc0e5229..6b73e992 100644
--- a/wsutil/wmem/wmem_list.h
+++ b/wsutil/wmem/wmem_list.h
@@ -91,6 +91,13 @@ WS_DLL_PUBLIC
void
wmem_list_insert_sorted(wmem_list_t *list, void* data, GCompareFunc func);
+/*
+ * Appender Insertion (start search from the tail)
+ */
+WS_DLL_PUBLIC
+void
+wmem_list_append_sorted(wmem_list_t *list, void* data, GCompareFunc func);
+
WS_DLL_PUBLIC
wmem_list_t *
diff --git a/wsutil/wmem/wmem_map.c b/wsutil/wmem/wmem_map.c
index 047b8457..b3c581ae 100644
--- a/wsutil/wmem/wmem_map.c
+++ b/wsutil/wmem/wmem_map.c
@@ -484,19 +484,19 @@ wmem_strong_hash(const uint8_t *buf, const size_t len)
}
unsigned
-wmem_str_hash(gconstpointer key)
+wmem_str_hash(const void *key)
{
return wmem_strong_hash((const uint8_t *)key, strlen((const char *)key));
}
unsigned
-wmem_int64_hash(gconstpointer key)
+wmem_int64_hash(const void *key)
{
return wmem_strong_hash((const uint8_t *)key, sizeof(uint64_t));
}
unsigned
-wmem_double_hash(gconstpointer key)
+wmem_double_hash(const void *key)
{
return wmem_strong_hash((const uint8_t *)key, sizeof(double));
}
diff --git a/wsutil/wmem/wmem_map.h b/wsutil/wmem/wmem_map.h
index cb296fb4..6105aee9 100644
--- a/wsutil/wmem/wmem_map.h
+++ b/wsutil/wmem/wmem_map.h
@@ -207,21 +207,21 @@ wmem_strong_hash(const uint8_t *buf, const size_t len);
*/
WS_DLL_PUBLIC
unsigned
-wmem_str_hash(gconstpointer key);
+wmem_str_hash(const void *key);
/** An implementation of GHashFunc using wmem_strong_hash. Prefer this over
* g_int64_hash when the data comes from an untrusted source.
*/
WS_DLL_PUBLIC
unsigned
-wmem_int64_hash(gconstpointer key);
+wmem_int64_hash(const void *key);
/** An implementation of GHashFunc using wmem_strong_hash. Prefer this over
* g_double_hash when the data comes from an untrusted source.
*/
WS_DLL_PUBLIC
unsigned
-wmem_double_hash(gconstpointer key);
+wmem_double_hash(const void *key);
/** @}
* @} */
diff --git a/wsutil/wmem/wmem_miscutl.c b/wsutil/wmem/wmem_miscutl.c
index 14426447..6e3e2a3a 100644
--- a/wsutil/wmem/wmem_miscutl.c
+++ b/wsutil/wmem/wmem_miscutl.c
@@ -30,13 +30,13 @@ wmem_memdup(wmem_allocator_t *allocator, const void *source, const size_t size)
}
int
-wmem_compare_int(gconstpointer a, gconstpointer b)
+wmem_compare_int(const void *a, const void *b)
{
return GPOINTER_TO_INT(a) - GPOINTER_TO_INT(b);
}
int
-wmem_compare_uint(gconstpointer a, gconstpointer b)
+wmem_compare_uint(const void *a, const void *b)
{
return GPOINTER_TO_UINT(a) > GPOINTER_TO_UINT(b) ? 1 : (GPOINTER_TO_UINT(a) < GPOINTER_TO_UINT(b) ? -1 : 0);
}
diff --git a/wsutil/wmem/wmem_miscutl.h b/wsutil/wmem/wmem_miscutl.h
index 714ee5cc..1d988878 100644
--- a/wsutil/wmem/wmem_miscutl.h
+++ b/wsutil/wmem/wmem_miscutl.h
@@ -38,17 +38,17 @@ extern "C" {
WS_DLL_PUBLIC
void *
wmem_memdup(wmem_allocator_t *allocator, const void *source, const size_t size)
-G_GNUC_MALLOC;
+G_GNUC_ALLOC_SIZE(3);
/** Generic GCompareFunc implementations to compare signed/unsigned integer
*/
WS_DLL_PUBLIC
int
-wmem_compare_int(gconstpointer a, gconstpointer b);
+wmem_compare_int(const void *a, const void *b);
WS_DLL_PUBLIC
int
-wmem_compare_uint(gconstpointer a, gconstpointer b);
+wmem_compare_uint(const void *a, const void *b);
/** @}
* @} */
diff --git a/wsutil/wmem/wmem_strutl.c b/wsutil/wmem/wmem_strutl.c
index 99f1f8c6..2ff3d5a7 100644
--- a/wsutil/wmem/wmem_strutl.c
+++ b/wsutil/wmem/wmem_strutl.c
@@ -111,7 +111,8 @@ wmem_strdup_vprintf(wmem_allocator_t *allocator, const char *fmt, va_list ap)
/* Return the first occurrence of needle in haystack.
* If not found, return NULL.
- * If either haystack or needle has 0 length, return NULL.*/
+ * If either haystack has 0 length, return NULL.
+ * If needle has 0 length, return pointer to haystack. */
const uint8_t *
ws_memmem(const void *_haystack, size_t haystack_len,
const void *_needle, size_t needle_len)
@@ -126,7 +127,11 @@ ws_memmem(const void *_haystack, size_t haystack_len,
const uint8_t *const last_possible = haystack + haystack_len - needle_len;
if (needle_len == 0) {
- return NULL;
+ return haystack;
+ }
+
+ if (needle_len == 1) {
+ return memchr(haystack, needle[0], haystack_len);
}
if (needle_len > haystack_len) {
@@ -134,9 +139,9 @@ ws_memmem(const void *_haystack, size_t haystack_len,
}
for (begin = haystack ; begin <= last_possible; ++begin) {
- if (begin[0] == needle[0] &&
- !memcmp(&begin[1], needle + 1,
- needle_len - 1)) {
+ begin = memchr(begin, needle[0], last_possible - begin + 1);
+ if (begin == NULL) break;
+ if (!memcmp(&begin[1], needle + 1, needle_len - 1)) {
return begin;
}
}
diff --git a/wsutil/wmem/wmem_strutl.h b/wsutil/wmem/wmem_strutl.h
index ee3aed65..104a1eba 100644
--- a/wsutil/wmem/wmem_strutl.h
+++ b/wsutil/wmem/wmem_strutl.h
@@ -65,8 +65,8 @@ G_GNUC_MALLOC;
* @param needle The string to look for
* @param needle_len The length of the search string
* @return A pointer to the first occurrence of "needle" in
- * "haystack". If "needle" isn't found or is NULL, or if
- * "needle_len" is 0, NULL is returned.
+ * "haystack". If "needle" isn't found or is NULL, NULL is returned.
+ * If "needle_len" is 0, a pointer to "haystack" is returned.
*/
WS_DLL_PUBLIC
const uint8_t *ws_memmem(const void *haystack, size_t haystack_len,
diff --git a/wsutil/wmem/wmem_test.c b/wsutil/wmem/wmem_test.c
index 0dc908b7..65be3502 100644
--- a/wsutil/wmem/wmem_test.c
+++ b/wsutil/wmem/wmem_test.c
@@ -91,7 +91,7 @@ wmem_test_rand_string(wmem_allocator_t *allocator, int minlen, int maxlen)
}
static int
-wmem_test_compare_guint32(const void *a, const void *b)
+wmem_test_compare_uint32(const void *a, const void *b)
{
uint32_t l, r;
@@ -692,7 +692,7 @@ wmem_test_array(void)
}
}
- wmem_array_sort(array, wmem_test_compare_guint32);
+ wmem_array_sort(array, wmem_test_compare_uint32);
for (i=0, k=0; i<8; i++) {
for (j=0; j<=i; j++, k++) {
val = *(uint32_t*)wmem_array_index(array, k);
@@ -736,7 +736,7 @@ check_val_list(void * val, void * val_to_check)
}
static int
-str_compare(gconstpointer a, gconstpointer b)
+str_compare(const void *a, const void *b)
{
return strcmp((const char*)a, (const char*)b);
}
diff --git a/wsutil/wmem/wmem_tree.c b/wsutil/wmem/wmem_tree.c
index 05fa638e..d982b3b0 100644
--- a/wsutil/wmem/wmem_tree.c
+++ b/wsutil/wmem/wmem_tree.c
@@ -23,7 +23,6 @@
#include "wmem_tree-int.h"
#include "wmem_user_cb.h"
-
static wmem_tree_node_t *
node_uncle(wmem_tree_node_t *node)
{