2013-06-15 10:40:56 +00:00
|
|
|
/* wmem_tree.c
|
|
|
|
* Wireshark Memory Manager Red-Black Tree
|
|
|
|
* Based on the red-black tree implementation in epan/emem.*
|
|
|
|
* 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-06-15 10:40:56 +00:00
|
|
|
*/
|
|
|
|
|
2013-08-14 00:31:14 +00:00
|
|
|
#include "config.h"
|
|
|
|
|
2013-06-15 10:40:56 +00:00
|
|
|
#include <string.h>
|
2013-06-18 05:31:12 +00:00
|
|
|
#include <stdio.h>
|
2013-06-15 10:40:56 +00:00
|
|
|
#include <glib.h>
|
|
|
|
|
|
|
|
#include "wmem_core.h"
|
2015-06-25 22:24:03 +00:00
|
|
|
#include "wmem_strutl.h"
|
2013-06-15 10:40:56 +00:00
|
|
|
#include "wmem_tree.h"
|
2015-11-11 02:19:43 +00:00
|
|
|
#include "wmem_tree-int.h"
|
2013-06-15 10:40:56 +00:00
|
|
|
#include "wmem_user_cb.h"
|
2021-03-20 13:13:27 +00:00
|
|
|
|
|
|
|
#include <wsutil/ws_assert.h>
|
2013-06-18 19:32:40 +00:00
|
|
|
|
|
|
|
|
2015-06-25 22:24:03 +00:00
|
|
|
|
2013-06-15 10:40:56 +00:00
|
|
|
static wmem_tree_node_t *
|
|
|
|
node_uncle(wmem_tree_node_t *node)
|
|
|
|
{
|
|
|
|
wmem_tree_node_t *parent, *grandparent;
|
|
|
|
|
|
|
|
parent = node->parent;
|
|
|
|
if (parent == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
grandparent = parent->parent;
|
|
|
|
if (grandparent == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parent == grandparent->left) {
|
|
|
|
return grandparent->right;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return grandparent->left;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rb_insert_case1(wmem_tree_t *tree, wmem_tree_node_t *node);
|
|
|
|
static void rb_insert_case2(wmem_tree_t *tree, wmem_tree_node_t *node);
|
|
|
|
|
|
|
|
static void
|
|
|
|
rotate_left(wmem_tree_t *tree, wmem_tree_node_t *node)
|
|
|
|
{
|
|
|
|
if (node->parent) {
|
|
|
|
if (node->parent->left == node) {
|
|
|
|
node->parent->left = node->right;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
node->parent->right = node->right;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
tree->root = node->right;
|
|
|
|
}
|
|
|
|
|
|
|
|
node->right->parent = node->parent;
|
|
|
|
node->parent = node->right;
|
|
|
|
node->right = node->right->left;
|
|
|
|
if (node->right) {
|
|
|
|
node->right->parent = node;
|
|
|
|
}
|
|
|
|
node->parent->left = node;
|
2015-11-11 02:19:43 +00:00
|
|
|
|
|
|
|
if (tree->post_rotation_cb) {
|
|
|
|
tree->post_rotation_cb (node);
|
|
|
|
}
|
2013-06-15 10:40:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
rotate_right(wmem_tree_t *tree, wmem_tree_node_t *node)
|
|
|
|
{
|
|
|
|
if (node->parent) {
|
|
|
|
if (node->parent->left == node) {
|
|
|
|
node->parent->left = node->left;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
node->parent->right = node->left;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
tree->root = node->left;
|
|
|
|
}
|
|
|
|
|
|
|
|
node->left->parent = node->parent;
|
|
|
|
node->parent = node->left;
|
|
|
|
node->left = node->left->right;
|
|
|
|
if (node->left) {
|
|
|
|
node->left->parent = node;
|
|
|
|
}
|
|
|
|
node->parent->right = node;
|
2015-11-11 02:19:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
if (tree->post_rotation_cb) {
|
|
|
|
tree->post_rotation_cb (node);
|
|
|
|
}
|
2013-06-15 10:40:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
rb_insert_case5(wmem_tree_t *tree, wmem_tree_node_t *node)
|
|
|
|
{
|
|
|
|
wmem_tree_node_t *parent, *grandparent;
|
|
|
|
|
|
|
|
parent = node->parent;
|
|
|
|
grandparent = parent->parent;
|
|
|
|
|
2013-06-18 19:32:40 +00:00
|
|
|
parent->color = WMEM_NODE_COLOR_BLACK;
|
|
|
|
grandparent->color = WMEM_NODE_COLOR_RED;
|
2013-06-15 10:40:56 +00:00
|
|
|
|
|
|
|
if (node == parent->left && parent == grandparent->left) {
|
|
|
|
rotate_right(tree, grandparent);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rotate_left(tree, grandparent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
rb_insert_case4(wmem_tree_t *tree, wmem_tree_node_t *node)
|
|
|
|
{
|
|
|
|
wmem_tree_node_t *parent, *grandparent;
|
|
|
|
|
|
|
|
parent = node->parent;
|
|
|
|
grandparent = parent->parent;
|
|
|
|
if (!grandparent) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (node == parent->right && parent == grandparent->left) {
|
|
|
|
rotate_left(tree, parent);
|
|
|
|
node = node->left;
|
|
|
|
}
|
|
|
|
else if (node == parent->left && parent == grandparent->right) {
|
|
|
|
rotate_right(tree, parent);
|
|
|
|
node = node->right;
|
|
|
|
}
|
|
|
|
|
|
|
|
rb_insert_case5(tree, node);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
rb_insert_case3(wmem_tree_t *tree, wmem_tree_node_t *node)
|
|
|
|
{
|
|
|
|
wmem_tree_node_t *parent, *grandparent, *uncle;
|
|
|
|
|
|
|
|
uncle = node_uncle(node);
|
|
|
|
|
2013-06-18 19:32:40 +00:00
|
|
|
if (uncle && uncle->color == WMEM_NODE_COLOR_RED) {
|
2013-06-15 10:40:56 +00:00
|
|
|
parent = node->parent;
|
|
|
|
grandparent = parent->parent;
|
|
|
|
|
2013-06-18 19:32:40 +00:00
|
|
|
parent->color = WMEM_NODE_COLOR_BLACK;
|
|
|
|
uncle->color = WMEM_NODE_COLOR_BLACK;
|
|
|
|
grandparent->color = WMEM_NODE_COLOR_RED;
|
2013-06-15 10:40:56 +00:00
|
|
|
|
|
|
|
rb_insert_case1(tree, grandparent);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
rb_insert_case4(tree, node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
rb_insert_case2(wmem_tree_t *tree, wmem_tree_node_t *node)
|
|
|
|
{
|
|
|
|
/* parent is always non-NULL here */
|
2013-06-18 19:32:40 +00:00
|
|
|
if (node->parent->color == WMEM_NODE_COLOR_RED) {
|
2013-06-15 10:40:56 +00:00
|
|
|
rb_insert_case3(tree, node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
rb_insert_case1(wmem_tree_t *tree, wmem_tree_node_t *node)
|
|
|
|
{
|
|
|
|
wmem_tree_node_t *parent = node->parent;
|
|
|
|
|
|
|
|
if (parent == NULL) {
|
2013-06-18 19:32:40 +00:00
|
|
|
node->color = WMEM_NODE_COLOR_BLACK;
|
2013-06-15 10:40:56 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
rb_insert_case2(tree, node);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
wmem_tree_t *
|
|
|
|
wmem_tree_new(wmem_allocator_t *allocator)
|
|
|
|
{
|
|
|
|
wmem_tree_t *tree;
|
|
|
|
|
2017-03-04 23:33:33 +00:00
|
|
|
tree = wmem_new0(allocator, wmem_tree_t);
|
2020-06-07 21:41:39 +00:00
|
|
|
tree->metadata_allocator = allocator;
|
|
|
|
tree->data_allocator = allocator;
|
2017-03-04 23:33:33 +00:00
|
|
|
|
2013-06-15 10:40:56 +00:00
|
|
|
return tree;
|
|
|
|
}
|
|
|
|
|
2013-06-16 22:07:24 +00:00
|
|
|
static gboolean
|
2013-06-17 00:08:44 +00:00
|
|
|
wmem_tree_reset_cb(wmem_allocator_t *allocator _U_, wmem_cb_event_t event,
|
2013-06-15 10:40:56 +00:00
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
wmem_tree_t *tree = (wmem_tree_t *)user_data;
|
|
|
|
|
|
|
|
tree->root = NULL;
|
2013-06-16 22:07:24 +00:00
|
|
|
|
2013-06-17 00:08:44 +00:00
|
|
|
if (event == WMEM_CB_DESTROY_EVENT) {
|
2020-06-07 21:41:39 +00:00
|
|
|
wmem_unregister_callback(tree->metadata_allocator, tree->metadata_scope_cb_id);
|
|
|
|
wmem_free(tree->metadata_allocator, tree);
|
2013-06-17 00:08:44 +00:00
|
|
|
}
|
|
|
|
|
2013-06-16 22:07:24 +00:00
|
|
|
return TRUE;
|
2013-06-15 10:40:56 +00:00
|
|
|
}
|
|
|
|
|
2013-06-17 00:08:44 +00:00
|
|
|
static gboolean
|
|
|
|
wmem_tree_destroy_cb(wmem_allocator_t *allocator _U_, wmem_cb_event_t event _U_,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
wmem_tree_t *tree = (wmem_tree_t *)user_data;
|
|
|
|
|
2020-06-07 21:41:39 +00:00
|
|
|
wmem_unregister_callback(tree->data_allocator, tree->data_scope_cb_id);
|
2013-06-17 00:08:44 +00:00
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2013-06-15 10:40:56 +00:00
|
|
|
wmem_tree_t *
|
2020-06-07 21:41:39 +00:00
|
|
|
wmem_tree_new_autoreset(wmem_allocator_t *metadata_scope, wmem_allocator_t *data_scope)
|
2013-06-15 10:40:56 +00:00
|
|
|
{
|
|
|
|
wmem_tree_t *tree;
|
|
|
|
|
2020-06-07 21:41:39 +00:00
|
|
|
tree = wmem_new0(metadata_scope, wmem_tree_t);
|
|
|
|
tree->metadata_allocator = metadata_scope;
|
|
|
|
tree->data_allocator = data_scope;
|
2013-06-15 10:40:56 +00:00
|
|
|
|
2020-06-07 21:41:39 +00:00
|
|
|
tree->metadata_scope_cb_id = wmem_register_callback(metadata_scope, wmem_tree_destroy_cb,
|
2013-06-17 00:08:44 +00:00
|
|
|
tree);
|
2020-06-07 21:41:39 +00:00
|
|
|
tree->data_scope_cb_id = wmem_register_callback(data_scope, wmem_tree_reset_cb,
|
2013-06-17 00:08:44 +00:00
|
|
|
tree);
|
2013-06-15 10:40:56 +00:00
|
|
|
|
|
|
|
return tree;
|
|
|
|
}
|
|
|
|
|
2017-03-04 13:26:22 +00:00
|
|
|
static void
|
|
|
|
free_tree_node(wmem_allocator_t *allocator, wmem_tree_node_t* node, gboolean free_keys, gboolean free_values)
|
|
|
|
{
|
|
|
|
if (node == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (node->left) {
|
|
|
|
free_tree_node(allocator, node->left, free_keys, free_values);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (node->is_subtree) {
|
|
|
|
wmem_tree_destroy((wmem_tree_t *)node->data, free_keys, free_values);
|
|
|
|
node->data = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (node->right) {
|
|
|
|
free_tree_node(allocator, node->right, free_keys, free_values);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (free_keys) {
|
|
|
|
wmem_free(allocator, (void*)node->key);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (free_values) {
|
|
|
|
wmem_free(allocator, node->data);
|
|
|
|
}
|
|
|
|
wmem_free(allocator, node);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
wmem_tree_destroy(wmem_tree_t *tree, gboolean free_keys, gboolean free_values)
|
|
|
|
{
|
2020-06-07 21:41:39 +00:00
|
|
|
free_tree_node(tree->data_allocator, tree->root, free_keys, free_values);
|
|
|
|
if (tree->metadata_allocator) {
|
|
|
|
wmem_unregister_callback(tree->metadata_allocator, tree->metadata_scope_cb_id);
|
2018-10-11 10:14:00 +00:00
|
|
|
}
|
2020-06-07 21:41:39 +00:00
|
|
|
if (tree->data_allocator) {
|
|
|
|
wmem_unregister_callback(tree->data_allocator, tree->data_scope_cb_id);
|
2018-10-11 10:14:00 +00:00
|
|
|
}
|
2020-06-07 21:41:39 +00:00
|
|
|
wmem_free(tree->metadata_allocator, tree);
|
2017-03-04 13:26:22 +00:00
|
|
|
}
|
|
|
|
|
2013-07-31 01:48:47 +00:00
|
|
|
gboolean
|
|
|
|
wmem_tree_is_empty(wmem_tree_t *tree)
|
|
|
|
{
|
|
|
|
return tree->root == NULL;
|
|
|
|
}
|
|
|
|
|
2017-02-07 21:52:42 +00:00
|
|
|
static gboolean
|
|
|
|
count_nodes(const void *key _U_, void *value _U_, void *userdata)
|
|
|
|
{
|
|
|
|
guint* count = (guint*)userdata;
|
|
|
|
(*count)++;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
guint
|
|
|
|
wmem_tree_count(wmem_tree_t* tree)
|
|
|
|
{
|
|
|
|
guint count = 0;
|
|
|
|
|
|
|
|
/* Recursing through the tree counting each node is the simplest approach.
|
|
|
|
We don't keep track of the count within the tree because it can get
|
|
|
|
complicated with subtrees within the tree */
|
|
|
|
wmem_tree_foreach(tree, count_nodes, &count);
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2013-06-18 19:19:21 +00:00
|
|
|
static wmem_tree_node_t *
|
2015-06-25 22:24:03 +00:00
|
|
|
create_node(wmem_allocator_t *allocator, wmem_tree_node_t *parent, const void *key,
|
2013-06-18 19:32:40 +00:00
|
|
|
void *data, wmem_node_color_t color, gboolean is_subtree)
|
2013-06-18 19:19:21 +00:00
|
|
|
{
|
|
|
|
wmem_tree_node_t *node;
|
|
|
|
|
|
|
|
node = wmem_new(allocator, wmem_tree_node_t);
|
|
|
|
|
|
|
|
node->left = NULL;
|
|
|
|
node->right = NULL;
|
|
|
|
node->parent = parent;
|
|
|
|
|
2015-06-25 22:23:29 +00:00
|
|
|
node->key = key;
|
2015-06-25 18:09:28 +00:00
|
|
|
node->data = data;
|
2013-06-18 19:19:21 +00:00
|
|
|
|
2013-06-18 19:32:40 +00:00
|
|
|
node->color = color;
|
|
|
|
node->is_subtree = is_subtree;
|
2015-08-11 14:11:46 +00:00
|
|
|
node->is_removed = FALSE;
|
2013-06-18 19:19:21 +00:00
|
|
|
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2013-06-18 19:55:29 +00:00
|
|
|
#define CREATE_DATA(TRANSFORM, DATA) ((TRANSFORM) ? (TRANSFORM)(DATA) : (DATA))
|
2015-11-11 02:19:43 +00:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* return inserted node
|
|
|
|
*/
|
|
|
|
static wmem_tree_node_t *
|
|
|
|
lookup_or_insert32_node(wmem_tree_t *tree, guint32 key,
|
2013-06-18 19:55:29 +00:00
|
|
|
void*(*func)(void*), void* data, gboolean is_subtree, gboolean replace)
|
2013-06-15 10:40:56 +00:00
|
|
|
{
|
2013-06-18 19:43:18 +00:00
|
|
|
wmem_tree_node_t *node = tree->root;
|
|
|
|
wmem_tree_node_t *new_node = NULL;
|
2013-06-27 03:01:53 +00:00
|
|
|
|
2013-06-15 10:40:56 +00:00
|
|
|
/* is this the first node ?*/
|
2013-06-18 19:43:18 +00:00
|
|
|
if (!node) {
|
2020-06-07 21:41:39 +00:00
|
|
|
new_node = create_node(tree->data_allocator, NULL, GUINT_TO_POINTER(key),
|
2013-06-18 19:55:29 +00:00
|
|
|
CREATE_DATA(func, data), WMEM_NODE_COLOR_BLACK, is_subtree);
|
2013-06-18 19:43:18 +00:00
|
|
|
tree->root = new_node;
|
2015-11-11 02:19:43 +00:00
|
|
|
return new_node;
|
2013-06-15 10:40:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* it was not the new root so walk the tree until we find where to
|
|
|
|
* insert this new leaf.
|
|
|
|
*/
|
2013-06-18 19:43:18 +00:00
|
|
|
while (!new_node) {
|
2013-06-18 05:31:12 +00:00
|
|
|
/* this node already exists, so just return the data pointer*/
|
2015-06-25 18:09:28 +00:00
|
|
|
if (key == GPOINTER_TO_UINT(node->key)) {
|
2013-06-18 19:55:29 +00:00
|
|
|
if (replace) {
|
|
|
|
node->data = CREATE_DATA(func, data);
|
|
|
|
}
|
2015-11-11 02:19:43 +00:00
|
|
|
return node;
|
2013-06-15 10:40:56 +00:00
|
|
|
}
|
2015-06-25 18:09:28 +00:00
|
|
|
else if (key < GPOINTER_TO_UINT(node->key)) {
|
2013-06-18 19:43:18 +00:00
|
|
|
if (node->left) {
|
|
|
|
node = node->left;
|
|
|
|
}
|
|
|
|
else {
|
2013-06-18 05:31:12 +00:00
|
|
|
/* new node to the left */
|
2020-06-07 21:41:39 +00:00
|
|
|
new_node = create_node(tree->data_allocator, node, GUINT_TO_POINTER(key),
|
2013-06-18 19:55:29 +00:00
|
|
|
CREATE_DATA(func, data), WMEM_NODE_COLOR_RED,
|
|
|
|
is_subtree);
|
2013-06-18 19:43:18 +00:00
|
|
|
node->left = new_node;
|
2013-06-15 10:40:56 +00:00
|
|
|
}
|
|
|
|
}
|
2015-06-25 18:09:28 +00:00
|
|
|
else if (key > GPOINTER_TO_UINT(node->key)) {
|
2013-06-18 19:43:18 +00:00
|
|
|
if (node->right) {
|
|
|
|
node = node->right;
|
|
|
|
}
|
|
|
|
else {
|
2013-06-27 03:01:53 +00:00
|
|
|
/* new node to the right */
|
2020-06-07 21:41:39 +00:00
|
|
|
new_node = create_node(tree->data_allocator, node, GUINT_TO_POINTER(key),
|
2013-06-18 19:55:29 +00:00
|
|
|
CREATE_DATA(func, data), WMEM_NODE_COLOR_RED,
|
|
|
|
is_subtree);
|
2013-06-18 19:43:18 +00:00
|
|
|
node->right = new_node;
|
2013-06-15 10:40:56 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-18 05:31:12 +00:00
|
|
|
/* node will now point to the newly created node */
|
2013-06-18 19:43:18 +00:00
|
|
|
rb_insert_case1(tree, new_node);
|
2013-06-15 10:40:56 +00:00
|
|
|
|
2015-11-11 02:19:43 +00:00
|
|
|
return new_node;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void *
|
|
|
|
lookup_or_insert32(wmem_tree_t *tree, guint32 key,
|
|
|
|
void*(*func)(void*), void* data, gboolean is_subtree, gboolean replace)
|
|
|
|
{
|
|
|
|
wmem_tree_node_t *node = lookup_or_insert32_node(tree, key, func, data, is_subtree, replace);
|
|
|
|
return node->data;
|
2013-06-15 10:40:56 +00:00
|
|
|
}
|
|
|
|
|
2015-06-25 22:24:03 +00:00
|
|
|
static void *
|
|
|
|
wmem_tree_lookup(wmem_tree_t *tree, const void *key, compare_func cmp)
|
|
|
|
{
|
2015-06-26 18:40:35 +00:00
|
|
|
wmem_tree_node_t *node;
|
|
|
|
|
|
|
|
if (tree == NULL || key == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
node = tree->root;
|
2015-06-25 22:24:03 +00:00
|
|
|
|
|
|
|
while (node) {
|
|
|
|
int result = cmp(key, node->key);
|
|
|
|
if (result == 0) {
|
|
|
|
return node->data;
|
|
|
|
}
|
|
|
|
else if (result < 0) {
|
|
|
|
node = node->left;
|
|
|
|
}
|
|
|
|
else if (result > 0) {
|
|
|
|
node = node->right;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-11-11 02:19:43 +00:00
|
|
|
wmem_tree_node_t *
|
2015-06-25 22:24:03 +00:00
|
|
|
wmem_tree_insert(wmem_tree_t *tree, const void *key, void *data, compare_func cmp)
|
|
|
|
{
|
|
|
|
wmem_tree_node_t *node = tree->root;
|
|
|
|
wmem_tree_node_t *new_node = NULL;
|
|
|
|
|
|
|
|
/* is this the first node ?*/
|
|
|
|
if (!node) {
|
2020-06-07 21:41:39 +00:00
|
|
|
tree->root = create_node(tree->data_allocator, node, key,
|
2015-06-25 22:24:03 +00:00
|
|
|
data, WMEM_NODE_COLOR_BLACK, FALSE);
|
2015-11-11 02:19:43 +00:00
|
|
|
return tree->root;
|
2015-06-25 22:24:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* it was not the new root so walk the tree until we find where to
|
|
|
|
* insert this new leaf.
|
|
|
|
*/
|
|
|
|
while (!new_node) {
|
|
|
|
int result = cmp(key, node->key);
|
|
|
|
if (result == 0) {
|
|
|
|
node->data = data;
|
2015-08-11 19:52:32 +00:00
|
|
|
node->is_removed = data ? FALSE : TRUE;
|
2015-11-11 02:19:43 +00:00
|
|
|
return node;
|
2015-06-25 22:24:03 +00:00
|
|
|
}
|
|
|
|
else if (result < 0) {
|
|
|
|
if (node->left) {
|
|
|
|
node = node->left;
|
|
|
|
}
|
|
|
|
else {
|
2020-06-07 21:41:39 +00:00
|
|
|
new_node = create_node(tree->data_allocator, node, key,
|
2015-06-25 22:24:03 +00:00
|
|
|
data, WMEM_NODE_COLOR_RED, FALSE);
|
|
|
|
node->left = new_node;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (result > 0) {
|
|
|
|
if (node->right) {
|
|
|
|
node = node->right;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* new node to the right */
|
2020-06-07 21:41:39 +00:00
|
|
|
new_node = create_node(tree->data_allocator, node, key,
|
2015-06-25 22:24:03 +00:00
|
|
|
data, WMEM_NODE_COLOR_RED, FALSE);
|
|
|
|
node->right = new_node;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* node will now point to the newly created node */
|
|
|
|
rb_insert_case1(tree, new_node);
|
2015-11-11 02:19:43 +00:00
|
|
|
|
|
|
|
return new_node;
|
2015-06-25 22:24:03 +00:00
|
|
|
}
|
|
|
|
|
2013-06-15 10:40:56 +00:00
|
|
|
void
|
|
|
|
wmem_tree_insert32(wmem_tree_t *tree, guint32 key, void *data)
|
|
|
|
{
|
2013-06-18 19:55:29 +00:00
|
|
|
lookup_or_insert32(tree, key, NULL, data, FALSE, TRUE);
|
2013-06-15 10:40:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
|
|
|
wmem_tree_lookup32(wmem_tree_t *tree, guint32 key)
|
|
|
|
{
|
|
|
|
wmem_tree_node_t *node = tree->root;
|
|
|
|
|
|
|
|
while (node) {
|
2015-06-25 18:09:28 +00:00
|
|
|
if (key == GPOINTER_TO_UINT(node->key)) {
|
2013-06-15 10:40:56 +00:00
|
|
|
return node->data;
|
|
|
|
}
|
2015-06-25 18:09:28 +00:00
|
|
|
else if (key < GPOINTER_TO_UINT(node->key)) {
|
2013-06-15 10:40:56 +00:00
|
|
|
node = node->left;
|
|
|
|
}
|
2015-06-25 18:09:28 +00:00
|
|
|
else if (key > GPOINTER_TO_UINT(node->key)) {
|
2013-06-15 10:40:56 +00:00
|
|
|
node = node->right;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
|
|
|
wmem_tree_lookup32_le(wmem_tree_t *tree, guint32 key)
|
|
|
|
{
|
|
|
|
wmem_tree_node_t *node = tree->root;
|
|
|
|
|
|
|
|
while (node) {
|
2015-06-25 18:09:28 +00:00
|
|
|
if (key == GPOINTER_TO_UINT(node->key)) {
|
2013-06-15 10:40:56 +00:00
|
|
|
return node->data;
|
|
|
|
}
|
2015-06-25 18:09:28 +00:00
|
|
|
else if (key < GPOINTER_TO_UINT(node->key)) {
|
2013-06-15 10:40:56 +00:00
|
|
|
if (node->left == NULL) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
node = node->left;
|
|
|
|
}
|
2015-06-25 18:09:28 +00:00
|
|
|
else if (key > GPOINTER_TO_UINT(node->key)) {
|
2013-06-15 10:40:56 +00:00
|
|
|
if (node->right == NULL) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
node = node->right;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!node) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If we are still at the root of the tree this means that this node
|
|
|
|
* is either smaller than the search key and then we return this
|
|
|
|
* node or else there is no smaller key available and then
|
|
|
|
* we return NULL.
|
|
|
|
*/
|
|
|
|
if (node->parent == NULL) {
|
2015-06-25 18:09:28 +00:00
|
|
|
if (key > GPOINTER_TO_UINT(node->key)) {
|
2013-06-15 10:40:56 +00:00
|
|
|
return node->data;
|
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-25 18:09:28 +00:00
|
|
|
if (GPOINTER_TO_UINT(node->key) <= key) {
|
2013-06-15 10:40:56 +00:00
|
|
|
/* if our key is <= the search key, we have the right node */
|
|
|
|
return node->data;
|
|
|
|
}
|
|
|
|
else if (node == node->parent->left) {
|
|
|
|
/* our key is bigger than the search key and we're a left child,
|
|
|
|
* we have to check if any of our ancestors are smaller. */
|
|
|
|
while (node) {
|
2015-06-25 18:09:28 +00:00
|
|
|
if (key > GPOINTER_TO_UINT(node->key)) {
|
2013-06-15 10:40:56 +00:00
|
|
|
return node->data;
|
|
|
|
}
|
|
|
|
node=node->parent;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* our key is bigger than the search key and we're a right child,
|
|
|
|
* our parent is the one we want */
|
|
|
|
return node->parent->data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-09 00:28:25 +00:00
|
|
|
void *
|
|
|
|
wmem_tree_remove32(wmem_tree_t *tree, guint32 key)
|
|
|
|
{
|
|
|
|
void *ret = wmem_tree_lookup32(tree, key);
|
|
|
|
if (ret) {
|
|
|
|
/* Not really a remove, but set data to NULL to mark node with is_removed */
|
|
|
|
wmem_tree_insert32(tree, key, NULL);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-06-18 17:39:15 +00:00
|
|
|
void
|
|
|
|
wmem_tree_insert_string(wmem_tree_t* tree, const gchar* k, void* v, guint32 flags)
|
|
|
|
{
|
2015-06-25 22:24:03 +00:00
|
|
|
char *key;
|
|
|
|
compare_func cmp;
|
2013-06-18 17:39:15 +00:00
|
|
|
|
2020-06-07 21:41:39 +00:00
|
|
|
key = wmem_strdup(tree->data_allocator, k);
|
2013-06-15 10:40:56 +00:00
|
|
|
|
2015-06-25 22:24:03 +00:00
|
|
|
if (flags & WMEM_TREE_STRING_NOCASE) {
|
|
|
|
cmp = (compare_func)g_ascii_strcasecmp;
|
|
|
|
} else {
|
|
|
|
cmp = (compare_func)strcmp;
|
|
|
|
}
|
2013-06-15 10:40:56 +00:00
|
|
|
|
2015-06-25 22:24:03 +00:00
|
|
|
wmem_tree_insert(tree, key, v, cmp);
|
2013-06-15 10:40:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void *
|
2013-06-18 05:31:12 +00:00
|
|
|
wmem_tree_lookup_string(wmem_tree_t* tree, const gchar* k, guint32 flags)
|
2013-06-15 10:40:56 +00:00
|
|
|
{
|
2015-06-25 22:24:03 +00:00
|
|
|
compare_func cmp;
|
2013-06-15 10:40:56 +00:00
|
|
|
|
2015-06-25 22:24:03 +00:00
|
|
|
if (flags & WMEM_TREE_STRING_NOCASE) {
|
|
|
|
cmp = (compare_func)g_ascii_strcasecmp;
|
|
|
|
} else {
|
|
|
|
cmp = (compare_func)strcmp;
|
|
|
|
}
|
2013-06-15 10:40:56 +00:00
|
|
|
|
2015-06-25 22:24:03 +00:00
|
|
|
return wmem_tree_lookup(tree, k, cmp);
|
2013-06-15 10:40:56 +00:00
|
|
|
}
|
|
|
|
|
2015-08-11 12:08:08 +00:00
|
|
|
void *
|
|
|
|
wmem_tree_remove_string(wmem_tree_t* tree, const gchar* k, guint32 flags)
|
|
|
|
{
|
|
|
|
void *ret = wmem_tree_lookup_string(tree, k, flags);
|
|
|
|
if (ret) {
|
2015-08-11 14:11:46 +00:00
|
|
|
/* Not really a remove, but set data to NULL to mark node with is_removed */
|
2015-08-11 12:08:08 +00:00
|
|
|
wmem_tree_insert_string(tree, k, NULL, flags);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2013-06-15 10:40:56 +00:00
|
|
|
static void *
|
2013-06-18 05:31:12 +00:00
|
|
|
create_sub_tree(void* d)
|
2013-06-15 10:40:56 +00:00
|
|
|
{
|
2020-06-07 21:41:39 +00:00
|
|
|
return wmem_tree_new(((wmem_tree_t *)d)->data_allocator);
|
2013-06-15 10:40:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
wmem_tree_insert32_array(wmem_tree_t *tree, wmem_tree_key_t *key, void *data)
|
|
|
|
{
|
|
|
|
wmem_tree_t *insert_tree = NULL;
|
|
|
|
wmem_tree_key_t *cur_key;
|
|
|
|
guint32 i, insert_key32 = 0;
|
|
|
|
|
|
|
|
for (cur_key = key; cur_key->length > 0; cur_key++) {
|
|
|
|
for (i = 0; i < cur_key->length; i++) {
|
|
|
|
/* Insert using the previous key32 */
|
|
|
|
if (!insert_tree) {
|
|
|
|
insert_tree = tree;
|
|
|
|
} else {
|
2013-06-18 19:55:29 +00:00
|
|
|
insert_tree = (wmem_tree_t *)lookup_or_insert32(insert_tree,
|
|
|
|
insert_key32, create_sub_tree, tree, TRUE, FALSE);
|
2013-06-15 10:40:56 +00:00
|
|
|
}
|
|
|
|
insert_key32 = cur_key->key[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-20 13:13:27 +00:00
|
|
|
ws_assert(insert_tree);
|
2013-06-15 10:40:56 +00:00
|
|
|
|
|
|
|
wmem_tree_insert32(insert_tree, insert_key32, data);
|
|
|
|
}
|
|
|
|
|
2013-12-20 06:30:13 +00:00
|
|
|
static void *
|
2013-06-18 18:57:28 +00:00
|
|
|
wmem_tree_lookup32_array_helper(wmem_tree_t *tree, wmem_tree_key_t *key,
|
|
|
|
void*(*helper)(wmem_tree_t*, guint32))
|
2013-06-15 10:40:56 +00:00
|
|
|
{
|
|
|
|
wmem_tree_t *lookup_tree = NULL;
|
|
|
|
wmem_tree_key_t *cur_key;
|
|
|
|
guint32 i, lookup_key32 = 0;
|
|
|
|
|
2013-07-31 01:48:47 +00:00
|
|
|
if (!tree || !key) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2013-06-22 21:00:36 +00:00
|
|
|
|
2013-06-15 10:40:56 +00:00
|
|
|
for (cur_key = key; cur_key->length > 0; cur_key++) {
|
|
|
|
for (i = 0; i < cur_key->length; i++) {
|
|
|
|
/* Lookup using the previous key32 */
|
|
|
|
if (!lookup_tree) {
|
|
|
|
lookup_tree = tree;
|
2013-06-22 21:00:36 +00:00
|
|
|
}
|
|
|
|
else {
|
2013-06-18 18:57:28 +00:00
|
|
|
lookup_tree =
|
|
|
|
(wmem_tree_t *)(*helper)(lookup_tree, lookup_key32);
|
2013-06-15 10:40:56 +00:00
|
|
|
if (!lookup_tree) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
lookup_key32 = cur_key->key[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-22 21:00:36 +00:00
|
|
|
/* Assert if we didn't get any valid keys */
|
2021-03-20 13:13:27 +00:00
|
|
|
ws_assert(lookup_tree);
|
2013-06-15 10:40:56 +00:00
|
|
|
|
2013-06-18 18:57:28 +00:00
|
|
|
return (*helper)(lookup_tree, lookup_key32);
|
2013-06-17 22:51:50 +00:00
|
|
|
}
|
2013-06-15 10:40:56 +00:00
|
|
|
|
2013-06-17 22:51:50 +00:00
|
|
|
void *
|
2013-06-18 18:57:28 +00:00
|
|
|
wmem_tree_lookup32_array(wmem_tree_t *tree, wmem_tree_key_t *key)
|
2013-06-17 22:51:50 +00:00
|
|
|
{
|
2013-06-18 18:57:28 +00:00
|
|
|
return wmem_tree_lookup32_array_helper(tree, key, wmem_tree_lookup32);
|
2013-06-15 10:40:56 +00:00
|
|
|
}
|
|
|
|
|
2013-06-18 18:57:28 +00:00
|
|
|
void *
|
|
|
|
wmem_tree_lookup32_array_le(wmem_tree_t *tree, wmem_tree_key_t *key)
|
|
|
|
{
|
|
|
|
return wmem_tree_lookup32_array_helper(tree, key, wmem_tree_lookup32_le);
|
|
|
|
}
|
2013-06-18 05:31:12 +00:00
|
|
|
|
2013-06-15 10:40:56 +00:00
|
|
|
static gboolean
|
|
|
|
wmem_tree_foreach_nodes(wmem_tree_node_t* node, wmem_foreach_func callback,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
gboolean stop_traverse = FALSE;
|
|
|
|
|
|
|
|
if (!node) {
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (node->left) {
|
|
|
|
if (wmem_tree_foreach_nodes(node->left, callback, user_data)) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-11 12:08:08 +00:00
|
|
|
if (node->is_subtree) {
|
2013-06-15 10:40:56 +00:00
|
|
|
stop_traverse = wmem_tree_foreach((wmem_tree_t *)node->data,
|
|
|
|
callback, user_data);
|
2015-08-11 14:11:46 +00:00
|
|
|
} else if (!node->is_removed) {
|
2015-08-11 12:08:08 +00:00
|
|
|
/* No callback for "removed" nodes */
|
2015-11-10 11:30:25 +00:00
|
|
|
stop_traverse = callback(node->key, node->data, user_data);
|
2013-06-15 10:40:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (stop_traverse) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(node->right) {
|
|
|
|
if (wmem_tree_foreach_nodes(node->right, callback, user_data)) {
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
gboolean
|
|
|
|
wmem_tree_foreach(wmem_tree_t* tree, wmem_foreach_func callback,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
if(!tree->root)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
return wmem_tree_foreach_nodes(tree->root, callback, user_data);
|
|
|
|
}
|
|
|
|
|
2015-11-11 02:19:43 +00:00
|
|
|
static void wmem_print_subtree(wmem_tree_t *tree, guint32 level, wmem_printer_func key_printer, wmem_printer_func data_printer);
|
2013-06-18 05:31:12 +00:00
|
|
|
|
|
|
|
static void
|
2015-11-11 02:19:43 +00:00
|
|
|
wmem_print_indent(guint32 level) {
|
2013-06-18 05:31:12 +00:00
|
|
|
guint32 i;
|
2015-11-11 02:19:43 +00:00
|
|
|
for (i=0; i<level; i++) {
|
2021-02-27 03:38:15 +00:00
|
|
|
printf(" ");
|
2015-11-11 02:19:43 +00:00
|
|
|
}
|
|
|
|
}
|
2013-06-18 05:31:12 +00:00
|
|
|
|
2015-11-11 02:19:43 +00:00
|
|
|
static void
|
|
|
|
wmem_tree_print_nodes(const char *prefix, wmem_tree_node_t *node, guint32 level,
|
|
|
|
wmem_printer_func key_printer, wmem_printer_func data_printer)
|
|
|
|
{
|
2013-06-18 05:31:12 +00:00
|
|
|
if (!node)
|
|
|
|
return;
|
|
|
|
|
2015-11-11 02:19:43 +00:00
|
|
|
wmem_print_indent(level);
|
2013-06-18 05:31:12 +00:00
|
|
|
|
2021-02-27 03:38:15 +00:00
|
|
|
printf("%sNODE:%p parent:%p left:%p right:%p colour:%s key:%p %s:%p\n",
|
2013-07-15 21:22:34 +00:00
|
|
|
prefix,
|
|
|
|
(void *)node, (void *)node->parent,
|
|
|
|
(void *)node->left, (void *)node->right,
|
2015-11-11 02:19:43 +00:00
|
|
|
node->color?"Black":"Red", node->key,
|
2013-06-18 19:32:40 +00:00
|
|
|
node->is_subtree?"tree":"data", node->data);
|
2017-01-16 21:14:26 +00:00
|
|
|
if (key_printer) {
|
2015-11-11 02:19:43 +00:00
|
|
|
wmem_print_indent(level);
|
|
|
|
key_printer(node->key);
|
2021-02-27 03:38:15 +00:00
|
|
|
printf("\n");
|
2015-11-11 02:19:43 +00:00
|
|
|
}
|
2017-01-16 21:14:26 +00:00
|
|
|
if (data_printer && !node->is_subtree) {
|
2015-11-11 02:19:43 +00:00
|
|
|
wmem_print_indent(level);
|
|
|
|
data_printer(node->data);
|
2021-02-27 03:38:15 +00:00
|
|
|
printf("\n");
|
2015-11-11 02:19:43 +00:00
|
|
|
}
|
|
|
|
|
2013-06-18 05:31:12 +00:00
|
|
|
if (node->left)
|
2015-11-11 02:19:43 +00:00
|
|
|
wmem_tree_print_nodes("L-", node->left, level+1, key_printer, data_printer);
|
2013-06-18 05:31:12 +00:00
|
|
|
if (node->right)
|
2015-11-11 02:19:43 +00:00
|
|
|
wmem_tree_print_nodes("R-", node->right, level+1, key_printer, data_printer);
|
2013-06-18 05:31:12 +00:00
|
|
|
|
2013-06-18 19:32:40 +00:00
|
|
|
if (node->is_subtree)
|
2015-11-11 02:19:43 +00:00
|
|
|
wmem_print_subtree((wmem_tree_t *)node->data, level+1, key_printer, data_printer);
|
2013-06-18 05:31:12 +00:00
|
|
|
}
|
|
|
|
|
2015-11-11 02:19:43 +00:00
|
|
|
|
2013-06-18 05:31:12 +00:00
|
|
|
static void
|
2015-11-11 02:19:43 +00:00
|
|
|
wmem_print_subtree(wmem_tree_t *tree, guint32 level, wmem_printer_func key_printer, wmem_printer_func data_printer)
|
2013-06-18 05:31:12 +00:00
|
|
|
{
|
|
|
|
if (!tree)
|
|
|
|
return;
|
|
|
|
|
2015-11-11 02:19:43 +00:00
|
|
|
wmem_print_indent(level);
|
2013-06-18 05:31:12 +00:00
|
|
|
|
2021-02-27 03:38:15 +00:00
|
|
|
printf("WMEM tree:%p root:%p\n", (void *)tree, (void *)tree->root);
|
2013-06-18 05:31:12 +00:00
|
|
|
if (tree->root) {
|
2015-11-11 02:19:43 +00:00
|
|
|
wmem_tree_print_nodes("Root-", tree->root, level, key_printer, data_printer);
|
2013-06-18 05:31:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2015-11-11 02:19:43 +00:00
|
|
|
wmem_print_tree(wmem_tree_t *tree, wmem_printer_func key_printer, wmem_printer_func data_printer)
|
2013-06-18 05:31:12 +00:00
|
|
|
{
|
2015-11-11 02:19:43 +00:00
|
|
|
wmem_print_subtree(tree, 0, key_printer, data_printer);
|
2013-06-18 05:31:12 +00:00
|
|
|
}
|
2013-06-15 10:40:56 +00:00
|
|
|
/*
|
2019-07-26 18:43:17 +00:00
|
|
|
* Editor modelines - https://www.wireshark.org/tools/modelines.html
|
2013-06-15 10:40:56 +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:
|
|
|
|
*/
|