Add back the fix from revision 54693.
[metze/wireshark/wip.git] / epan / emem.c
index 9f9ef055b6a34803ad566bee78f1ba0fefd305b5..c4868bad20d1f594abc84227dc9d5510b46099bf 100644 (file)
@@ -43,6 +43,7 @@
 
 #include "app_mem_usage.h"
 #include "proto.h"
+#include "exceptions.h"
 #include "emem.h"
 #include "wmem/wmem.h"
 
@@ -605,7 +606,7 @@ print_alloc_stats(void)
 static gboolean
 emem_verify_pointer_list(const emem_chunk_t *chunk_list, const void *ptr)
 {
-       const gchar *cptr = (gchar *)ptr;
+       const gchar *cptr = (const gchar *)ptr;
        const emem_chunk_t *chunk;
 
        for (chunk = chunk_list; chunk; chunk = chunk->next) {
@@ -1271,50 +1272,6 @@ se_free_all(void)
        emem_free_all(&se_packet_mem);
 }
 
-ep_stack_t
-ep_stack_new(void) {
-       ep_stack_t s = ep_new(struct _ep_stack_frame_t*);
-       *s = ep_new0(struct _ep_stack_frame_t);
-       return s;
-}
-
-/*  for ep_stack_t we'll keep the popped frames so we reuse them instead
-of allocating new ones.
-*/
-
-void *
-ep_stack_push(ep_stack_t stack, void* data)
-{
-       struct _ep_stack_frame_t* frame;
-       struct _ep_stack_frame_t* head = (*stack);
-
-       if (head->above) {
-               frame = head->above;
-       } else {
-               frame = ep_new(struct _ep_stack_frame_t);
-               head->above = frame;
-               frame->below = head;
-               frame->above = NULL;
-       }
-
-       frame->payload = data;
-       (*stack) = frame;
-
-       return data;
-}
-
-void *
-ep_stack_pop(ep_stack_t stack)
-{
-
-       if ((*stack)->below) {
-               (*stack) = (*stack)->below;
-               return (*stack)->above->payload;
-       } else {
-               return NULL;
-       }
-}
-
 emem_tree_t *
 se_tree_create(int type, const char *name)
 {
@@ -1354,99 +1311,6 @@ emem_tree_lookup32(emem_tree_t *se_tree, guint32 key)
        return NULL;
 }
 
-void *
-emem_tree_lookup32_le(emem_tree_t *se_tree, guint32 key)
-{
-       emem_tree_node_t *node;
-
-       node=se_tree->tree;
-
-       if(!node){
-               return NULL;
-       }
-
-
-       while(node){
-               if(key==node->key32){
-                       return node->data;
-               }
-               if(key<node->key32){
-                       if(node->left){
-                               node=node->left;
-                               continue;
-                       } else {
-                               break;
-                       }
-               }
-               if(key>node->key32){
-                       if(node->right){
-                               node=node->right;
-                               continue;
-                       } else {
-                               break;
-                       }
-               }
-       }
-
-
-       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){
-               if(key>node->key32){
-                       return node->data;
-               } else {
-                       return NULL;
-               }
-       }
-
-       if(node->parent->left==node){
-               /* left child */
-
-               if(key>node->key32){
-                       /* if this is a left child and its key is smaller than
-                        * the search key, then this is the node we want.
-                        */
-                       return node->data;
-               } else {
-                       /* if this is a left child and its key is bigger than
-                        * the search key, we have to check if any
-                        * of our ancestors are smaller than the search key.
-                        */
-                       while(node){
-                               if(key>node->key32){
-                                       return node->data;
-                               }
-                               node=node->parent;
-                       }
-                       return NULL;
-               }
-       } else {
-               /* right child */
-
-               if(node->key32<key){
-                       /* if this is the right child and its key is smaller
-                        * than the search key then this is the one we want.
-                        */
-                       return node->data;
-               } else {
-                       /* if this is the right child and its key is larger
-                        * than the search key then our parent is the one we
-                        * want.
-                        */
-                       return node->parent->data;
-               }
-       }
-
-}
-
-
 static inline emem_tree_node_t *
 emem_tree_parent(emem_tree_node_t *node)
 {
@@ -1696,337 +1560,6 @@ emem_tree_insert32(emem_tree_t *se_tree, guint32 key, void *data)
        }
 }
 
-static void *
-lookup_or_insert32(emem_tree_t *se_tree, guint32 key, void*(*func)(void*),void* ud, int is_subtree)
-{
-       emem_tree_node_t *node;
-
-       node=se_tree->tree;
-
-       /* is this the first node ?*/
-       if(!node){
-               node=(emem_tree_node_t *)se_tree->malloc(sizeof(emem_tree_node_t));
-               switch(se_tree->type){
-                       case EMEM_TREE_TYPE_RED_BLACK:
-                               node->u.rb_color=EMEM_TREE_RB_COLOR_BLACK;
-                               break;
-               }
-               node->parent=NULL;
-               node->left=NULL;
-               node->right=NULL;
-               node->key32=key;
-               node->data= func(ud);
-               node->u.is_subtree = is_subtree;
-               se_tree->tree=node;
-               return node->data;
-       }
-
-       /* it was not the new root so walk the tree until we find where to
-               * insert this new leaf.
-               */
-       while(1){
-               /* this node already exists, so just return the data pointer*/
-               if(key==node->key32){
-                       return node->data;
-               }
-               if(key<node->key32) {
-                       if(!node->left){
-                               /* new node to the left */
-                               emem_tree_node_t *new_node;
-                               new_node=(emem_tree_node_t *)se_tree->malloc(sizeof(emem_tree_node_t));
-                               node->left=new_node;
-                               new_node->parent=node;
-                               new_node->left=NULL;
-                               new_node->right=NULL;
-                               new_node->key32=key;
-                               new_node->data= func(ud);
-                               new_node->u.is_subtree = is_subtree;
-                               node=new_node;
-                               break;
-                       }
-                       node=node->left;
-                       continue;
-               }
-               if(key>node->key32) {
-                       if(!node->right){
-                               /* new node to the right */
-                               emem_tree_node_t *new_node;
-                               new_node=(emem_tree_node_t *)se_tree->malloc(sizeof(emem_tree_node_t));
-                               node->right=new_node;
-                               new_node->parent=node;
-                               new_node->left=NULL;
-                               new_node->right=NULL;
-                               new_node->key32=key;
-                               new_node->data= func(ud);
-                               new_node->u.is_subtree = is_subtree;
-                               node=new_node;
-                               break;
-                       }
-                       node=node->right;
-                       continue;
-               }
-       }
-
-       /* node will now point to the newly created node */
-       switch(se_tree->type){
-               case EMEM_TREE_TYPE_RED_BLACK:
-                       node->u.rb_color=EMEM_TREE_RB_COLOR_RED;
-                       rb_insert_case1(se_tree, node);
-                       break;
-       }
-
-       return node->data;
-}
-
-/* create another (sub)tree using the same memory allocation scope
- * as the parent tree.
- */
-static emem_tree_t *
-emem_tree_create_subtree(emem_tree_t *parent_tree, const char *name)
-{
-       emem_tree_t *tree_list;
-
-       tree_list=(emem_tree_t *)parent_tree->malloc(sizeof(emem_tree_t));
-       tree_list->next=NULL;
-       tree_list->type=parent_tree->type;
-       tree_list->tree=NULL;
-       tree_list->name=name;
-       tree_list->malloc=parent_tree->malloc;
-
-       return tree_list;
-}
-
-static void *
-create_sub_tree(void* d)
-{
-       emem_tree_t *se_tree = (emem_tree_t *)d;
-       return emem_tree_create_subtree(se_tree, "subtree");
-}
-
-/* insert a new node in the tree. if this node matches an already existing node
- * then just replace the data for that node */
-
-void
-emem_tree_insert32_array(emem_tree_t *se_tree, emem_tree_key_t *key, void *data)
-{
-       emem_tree_t *insert_tree = NULL;
-       emem_tree_key_t *cur_key;
-       guint32 i, insert_key32 = 0;
-
-       if(!se_tree || !key) return;
-
-       for (cur_key = key; cur_key->length > 0; cur_key++) {
-               if(cur_key->length > 100) {
-                       DISSECTOR_ASSERT_NOT_REACHED();
-               }
-
-               for (i = 0; i < cur_key->length; i++) {
-                       /* Insert using the previous key32 */
-                       if (!insert_tree) {
-                               insert_tree = se_tree;
-                       } else {
-                               insert_tree = (emem_tree_t *)lookup_or_insert32(insert_tree, insert_key32, create_sub_tree, se_tree, EMEM_TREE_NODE_IS_SUBTREE);
-                       }
-                       insert_key32 = cur_key->key[i];
-               }
-       }
-
-       if(!insert_tree) {
-               /* We didn't get a valid key. Should we return NULL instead? */
-               DISSECTOR_ASSERT_NOT_REACHED();
-       }
-
-       emem_tree_insert32(insert_tree, insert_key32, data);
-
-}
-
-void *
-emem_tree_lookup32_array(emem_tree_t *se_tree, emem_tree_key_t *key)
-{
-       emem_tree_t *lookup_tree = NULL;
-       emem_tree_key_t *cur_key;
-       guint32 i, lookup_key32 = 0;
-
-       if(!se_tree || !key) return NULL; /* prevent searching on NULL pointer */
-
-       for (cur_key = key; cur_key->length > 0; cur_key++) {
-               if(cur_key->length > 100) {
-                       DISSECTOR_ASSERT_NOT_REACHED();
-               }
-
-               for (i = 0; i < cur_key->length; i++) {
-                       /* Lookup using the previous key32 */
-                       if (!lookup_tree) {
-                               lookup_tree = se_tree;
-                       } else {
-                               lookup_tree = (emem_tree_t *)emem_tree_lookup32(lookup_tree, lookup_key32);
-                               if (!lookup_tree) {
-                                       return NULL;
-                               }
-                       }
-                       lookup_key32 = cur_key->key[i];
-               }
-       }
-
-       if(!lookup_tree) {
-               /* We didn't get a valid key. Should we return NULL instead? */
-               DISSECTOR_ASSERT_NOT_REACHED();
-       }
-
-       return emem_tree_lookup32(lookup_tree, lookup_key32);
-}
-
-void *
-emem_tree_lookup32_array_le(emem_tree_t *se_tree, emem_tree_key_t *key)
-{
-       emem_tree_t *lookup_tree = NULL;
-       emem_tree_key_t *cur_key;
-       guint32 i, lookup_key32 = 0;
-
-       if(!se_tree || !key) return NULL; /* prevent searching on NULL pointer */
-
-       for (cur_key = key; cur_key->length > 0; cur_key++) {
-               if(cur_key->length > 100) {
-                       DISSECTOR_ASSERT_NOT_REACHED();
-               }
-
-               for (i = 0; i < cur_key->length; i++) {
-                       /* Lookup using the previous key32 */
-                       if (!lookup_tree) {
-                               lookup_tree = se_tree;
-                       } else {
-                               lookup_tree = (emem_tree_t *)emem_tree_lookup32_le(lookup_tree, lookup_key32);
-                               if (!lookup_tree) {
-                                       return NULL;
-                               }
-                       }
-                       lookup_key32 = cur_key->key[i];
-               }
-       }
-
-       if(!lookup_tree) {
-               /* We didn't get a valid key. Should we return NULL instead? */
-               DISSECTOR_ASSERT_NOT_REACHED();
-       }
-
-       return emem_tree_lookup32_le(lookup_tree, lookup_key32);
-
-}
-
-/* Strings are stored as an array of uint32 containing the string characters
-   with 4 characters in each uint32.
-   The first byte of the string is stored as the most significant byte.
-   If the string is not a multiple of 4 characters in length the last
-   uint32 containing the string bytes are padded with 0 bytes.
-   After the uint32's containing the string, there is one final terminator
-   uint32 with the value 0x00000001
-*/
-void
-emem_tree_insert_string(emem_tree_t* se_tree, const gchar* k, void* v, guint32 flags)
-{
-       emem_tree_key_t key[2];
-       guint32 *aligned=NULL;
-       guint32 len = (guint32) strlen(k);
-       guint32 divx = (len+3)/4+1;
-       guint32 i;
-       guint32 tmp;
-
-       aligned = (guint32 *)g_malloc(divx * sizeof (guint32));
-
-       /* pack the bytes one one by one into guint32s */
-       tmp = 0;
-       for (i = 0;i < len;i++) {
-               unsigned char ch;
-
-               ch = (unsigned char)k[i];
-               if (flags & EMEM_TREE_STRING_NOCASE) {
-                       if(isupper(ch)) {
-                               ch = tolower(ch);
-                       }
-               }
-               tmp <<= 8;
-               tmp |= ch;
-               if (i%4 == 3) {
-                       aligned[i/4] = tmp;
-                       tmp = 0;
-               }
-       }
-       /* add required padding to the last uint32 */
-       if (i%4 != 0) {
-               while (i%4 != 0) {
-                       i++;
-                       tmp <<= 8;
-               }
-               aligned[i/4-1] = tmp;
-       }
-
-       /* add the terminator */
-       aligned[divx-1] = 0x00000001;
-
-       key[0].length = divx;
-       key[0].key = aligned;
-       key[1].length = 0;
-       key[1].key = NULL;
-
-
-       emem_tree_insert32_array(se_tree, key, v);
-       g_free(aligned);
-}
-
-void *
-emem_tree_lookup_string(emem_tree_t* se_tree, const gchar* k, guint32 flags)
-{
-       emem_tree_key_t key[2];
-       guint32 *aligned=NULL;
-       guint32 len = (guint) strlen(k);
-       guint32 divx = (len+3)/4+1;
-       guint32 i;
-       guint32 tmp;
-       void *ret;
-
-       aligned = (guint32 *)g_malloc(divx * sizeof (guint32));
-
-       /* pack the bytes one one by one into guint32s */
-       tmp = 0;
-       for (i = 0;i < len;i++) {
-               unsigned char ch;
-
-               ch = (unsigned char)k[i];
-               if (flags & EMEM_TREE_STRING_NOCASE) {
-                       if(isupper(ch)) {
-                               ch = tolower(ch);
-                       }
-               }
-               tmp <<= 8;
-               tmp |= ch;
-               if (i%4 == 3) {
-                       aligned[i/4] = tmp;
-                       tmp = 0;
-               }
-       }
-       /* add required padding to the last uint32 */
-       if (i%4 != 0) {
-               while (i%4 != 0) {
-                       i++;
-                       tmp <<= 8;
-               }
-               aligned[i/4-1] = tmp;
-       }
-
-       /* add the terminator */
-       aligned[divx-1] = 0x00000001;
-
-       key[0].length = divx;
-       key[0].key = aligned;
-       key[1].length = 0;
-       key[1].key = NULL;
-
-
-       ret = emem_tree_lookup32_array(se_tree, key);
-       g_free(aligned);
-       return ret;
-}
-
 static gboolean
 emem_tree_foreach_nodes(emem_tree_node_t* node, tree_foreach_func callback, void *user_data)
 {