Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Kill the xbt_memdup inline function since it's not exactly what we need, and
[simgrid.git] / src / xbt / dict_elm.c
index 5436706..6f9cc70 100644 (file)
@@ -7,6 +7,8 @@
 /* This program is free software; you can redistribute it and/or modify it
  * under the terms of the license (GNU LGPL) which comes with this package. */
 
+#include "xbt/ex.h"
+#include "portable.h" /* PRINTF_STR */
 #include "dict_private.h"  /* prototypes of this module */
 
 XBT_LOG_EXTERNAL_CATEGORY(dict);
@@ -16,7 +18,6 @@ XBT_LOG_NEW_SUBCATEGORY(dict_add,dict,"Dictionaries internals: elements addition
 XBT_LOG_NEW_SUBCATEGORY(dict_search,dict,"Dictionaries internals: searching");
 XBT_LOG_NEW_SUBCATEGORY(dict_remove,dict,"Dictionaries internals: elements removal");
 XBT_LOG_NEW_SUBCATEGORY(dict_collapse,dict,"Dictionaries internals: post-removal cleanup");
-XBT_LOG_NEW_SUBCATEGORY(dict_multi,dict,"Dictionaries internals: dictionaries of dictionaries");
 
 /*####[ Private prototypes ]#################################################*/
 
@@ -49,15 +50,14 @@ static void _xbt_dictelm_set_rec(s_xbt_dictelm_t *head,
                                  int             offset,
                                  void           *data,
                                  void_f_pvoid_t *free_f);
-static xbt_error_t _xbt_dictelm_get_rec(s_xbt_dictelm_t *head,
-                                              const char     *key,
-                                              int             key_len,
-                                              int             offset,
-                                              /* OUT */void **data);
-static xbt_error_t _xbt_dictelm_remove_rec(s_xbt_dictelm_t *head,
-                                            const char     *key,
-                                            int             key_len,
-                                            int             offset);
+static void * _xbt_dictelm_get_rec(s_xbt_dictelm_t *head,
+                                   const char     *key,
+                                  int             key_len,
+                                  int             offset);
+static void _xbt_dictelm_remove_rec(s_xbt_dictelm_t *head,
+                                   const char     *key,
+                                   int             key_len,
+                                   int             offset);
 
 static _XBT_INLINE
 void
@@ -67,18 +67,6 @@ _collapse_if_need(s_xbt_dictelm_t *p_head,
 
 /* ---- */
 
-static _XBT_INLINE
-void *
-xbt_memdup(const void * const ptr,
-           const size_t       length) {
-  void * new_ptr = NULL;
-
-  new_ptr = xbt_malloc(length);
-  memcpy(new_ptr, ptr, length);
-   
-  return new_ptr;
-}
-
 /*
  * _xbt_nibble_to_char:
  *
@@ -344,18 +332,18 @@ _xbt_dictelm_child_search(s_xbt_dictelm_t *p_elm,
   CDEBUG6(dict_search, "search child [%.*s] under [%.*s]=%p (len=%lu)",
          key_len, key,
           p_elm ? (p_elm->key_len?p_elm->key_len:6) : 6, 
-         p_elm ? (p_elm->key?p_elm->key:"(NULL)") : "(head)",
+         p_elm ? PRINTF_STR(p_elm->key) : "(head)",
          p_elm,
          (p_elm&&p_elm->sub) ? xbt_dynar_length(p_elm->sub) : 0);
   
 
   len = xbt_dynar_length(p_elm->sub);
 
-  if(1) { /* FIXME: Arnaud, did you leave dead code here? */
+  {
     int p_min = 0;
     int p_max = len-1;
     int cmp = 0;
-
+    
     p = p_min;
     if(len==0) {
       p=0;
@@ -392,18 +380,6 @@ _xbt_dictelm_child_search(s_xbt_dictelm_t *p_elm,
        }
       }
     }
-  } else {
-    for (p = 0; p < len; p++) {
-      int          cmp     = 0;
-      
-      _dict_child_cmp(p_elm, p, key, key_len, &o, &m, &cmp);
-      
-      if (m)
-       break;
-      
-      o = *p_offset;
-      m = 0;
-    }
   }
 
   *p_offset = o;
@@ -411,7 +387,7 @@ _xbt_dictelm_child_search(s_xbt_dictelm_t *p_elm,
   *p_match  = m;
   CDEBUG6(dict_search, "search [%.*s] in [%.*s]=%p => %s",
          key_len, key,
-          p_elm?(p_elm->key_len?p_elm->key_len:6):6, p_elm?(p_elm->key?p_elm->key:"(null)"):"(head)",
+          p_elm?(p_elm->key_len?p_elm->key_len:6):6, p_elm?PRINTF_STR(p_elm->key):"(head)",
          p_elm,
          ( m == 0 ? "no child have a common prefix" :
            ( m == 1 ? "selected child have exactly this key" :
@@ -559,7 +535,11 @@ _xbt_dictelm_set_rec(s_xbt_dictelm_t     *p_head,
       _xbt_dictelm_alloc(key, key_len, offset, FALSE, data, free_f, &p_new);
       p_child=xbt_dynar_get_as(p_head->sub, pos, s_xbt_dictelm_t*);
 
-      anc_key = xbt_memdup(key, anc_key_len);
+      /* memdup the old key, taking care of the terminating NULL byte */
+      anc_key = xbt_malloc(anc_key_len+1);
+      memcpy(anc_key, key, anc_key_len);
+      anc_key[anc_key_len] = '\0';
+
 
       _xbt_dictelm_alloc(anc_key, anc_key_len, old_offset, TRUE, NULL, NULL, &p_anc);
 
@@ -582,7 +562,7 @@ _xbt_dictelm_set_rec(s_xbt_dictelm_t     *p_head,
     }
 
   default:
-    DIE_IMPOSSIBLE;
+    THROW_IMPOSSIBLE;
   }
 }
 
@@ -605,7 +585,10 @@ xbt_dictelm_set_ext(s_xbt_dictelm_t **pp_head,
   s_xbt_dictelm_t  *p_head  = *pp_head;
   char         *key     =  NULL;
 
-  key = xbt_memdup(_key, key_len+1);
+  /* Make sure the copied key is NULL-terminated */
+  key = xbt_malloc(key_len+1);
+  memcpy(key, _key, key_len);
+  key[key_len] = '\0';
 
   /* there is no head, create it */
   if (!p_head) {
@@ -654,17 +637,15 @@ xbt_dictelm_set(s_xbt_dictelm_t **pp_head,
  * @key: the key to find data
  * @offset: offset on the key
  * @data: the data that we are looking for
- * @Returns: xbt_error
  *
- * Search the given @key. mismatch_error when not found.
+ * Search the given @key. Throws not_found_error when not found.
  */
 static 
-xbt_error_t
+void *
 _xbt_dictelm_get_rec(s_xbt_dictelm_t *p_head,
                      const char     *key,
                      int             key_len,
-                     int             offset,
-                     void **data) {
+                     int             offset) {
 
   CDEBUG3(dict_search, "Search %.*s in %p", key_len, key, (void*)p_head); 
 
@@ -672,33 +653,26 @@ _xbt_dictelm_get_rec(s_xbt_dictelm_t *p_head,
 
   /* we did enough recursion, we're done */
   if (offset >= key_len) {
-    *data = p_head->content;
-
-    return no_error;
+    return p_head->content;
   }
 
   {
     int match = 0;
     int pos   = 0;
 
-    *data = NULL; /* Let's be clean */
-
     /*** Search where is the good child, and how good it is ***/
     _xbt_dictelm_child_search(p_head, key, key_len, &pos, &offset, &match);
 
     switch (match) {
 
     case 0: /* no child have a common prefix */
-      return mismatch_error;
+      THROW1(not_found_error,0,"key '%s' not found",key);
 
     case 1: /* A child have exactly this key => Got it */
       {
         s_xbt_dictelm_t *p_child = NULL;
-
         p_child = xbt_dynar_get_as(p_head->sub, pos, s_xbt_dictelm_t*);
-        *data = p_child->content;
-
-        return no_error;
+        return p_child->content;
       }
 
     case 2: /* A child constitutes a prefix of the key => recurse */
@@ -707,17 +681,17 @@ _xbt_dictelm_get_rec(s_xbt_dictelm_t *p_head,
 
         p_child = xbt_dynar_get_as(p_head->sub, pos, s_xbt_dictelm_t*);
 
-        return _xbt_dictelm_get_rec(p_child, key, key_len, offset, data);
+        return _xbt_dictelm_get_rec(p_child, key, key_len, offset);
       }
 
     case 3: /* The key is a prefix of the child => not found */
-      return mismatch_error;
+      THROW1(not_found_error,0,"key %s not found",key);
 
     case 4: /* A child share a common prefix with this key => not found */
-      return mismatch_error;
+      THROW1(not_found_error,0,"key %s not found",key);
 
     default:
-      RAISE_IMPOSSIBLE;
+      THROW_IMPOSSIBLE;
     }
   }
 }
@@ -728,21 +702,18 @@ _xbt_dictelm_get_rec(s_xbt_dictelm_t *p_head,
  * @head: the head of the dict
  * @key: the key to find data
  * @data: the data that we are looking for
- * @Returns: xbt_error
  *
- * Search the given @key. mismatch_error when not found.
+ * Search the given @key. Throws not_found_error when not found.
  */
-xbt_error_t
+void *
 xbt_dictelm_get_ext(s_xbt_dictelm_t *p_head,
-                         const char     *key,
-                         int             key_len,
-                         /* OUT */void **data) {
+                   const char      *key,
+                   int              key_len) {
   /* there is no head, go to hell */
-  if (!p_head) {
-    return mismatch_error;
-  }
+  if (!p_head)
+    THROW2(not_found_error,0,"Key '%*s' not found in dict",key_len,key);
 
-  return _xbt_dictelm_get_rec(p_head, key, key_len, 0, data);
+  return _xbt_dictelm_get_rec(p_head, key, key_len, 0);
 }
 
 /**
@@ -751,16 +722,14 @@ xbt_dictelm_get_ext(s_xbt_dictelm_t *p_head,
  * @head: the head of the dict
  * @key: the key to find data
  * @data: the data that we are looking for
- * @Returns: xbt_error
  *
- * Search the given @key. mismatch_error when not found.
+ * Search the given @key. Throws not_found_error when not found.
  */
-xbt_error_t
-xbt_dictelm_get(s_xbt_dictelm_t    *p_head,
-                   const char     *key,
-                   /* OUT */void **data) {
+void *
+xbt_dictelm_get(s_xbt_dictelm_t *p_head,
+               const char      *key) {
 
-  return xbt_dictelm_get_ext(p_head, key, strlen(key), data);
+  return xbt_dictelm_get_ext(p_head, key, strlen(key));
 }
 
 /*----[ _xbt_dict_collapse ]------------------------------------------------*/
@@ -823,16 +792,14 @@ _collapse_if_need(xbt_dictelm_t head,
  * @head: the head of the dict
  * @key: the key of the data to be removed
  * @offset: offset on the key
- * @Returns: xbt_error_t
  *
- * Remove the entry associated with the given @key
+ * Remove the entry associated with the given @key. Throws not_found_error.
  */
-xbt_error_t
+void
 _xbt_dictelm_remove_rec(xbt_dictelm_t head,
                         const char  *key,
                         int          key_len,
                         int          offset) {
-  xbt_error_t errcode = no_error;
 
   /* there is no key to search, we did enough recursion => kill current */
   if (offset >= key_len) {
@@ -852,7 +819,7 @@ _xbt_dictelm_remove_rec(xbt_dictelm_t head,
       head->key_len = 0; /* killme. Cleanup done one step higher in recursion */
     }
 
-    return errcode;
+    return;
 
   } else {
     int match      =      0;
@@ -873,10 +840,10 @@ _xbt_dictelm_remove_rec(xbt_dictelm_t head,
         p_child = xbt_dynar_get_as(head->sub, pos, s_xbt_dictelm_t*);
         /*DEBUG5("Recurse on child %d of %p to remove %.*s (prefix=%d)",
           pos, (void*)p_child, key+offset, key_len-offset,offset);*/
-        TRY(_xbt_dictelm_remove_rec(p_child, key, key_len, offset));
+        _xbt_dictelm_remove_rec(p_child, key, key_len, offset);
 
         _collapse_if_need(head, pos, old_offset);
-       return no_error;
+       return;
       }
 
 
@@ -884,11 +851,10 @@ _xbt_dictelm_remove_rec(xbt_dictelm_t head,
     case 3: /* The key is a prefix of the child => not found */
     case 4: /* A child share a common prefix with this key => not found */
 
-      return mismatch_error;
-
+      THROW2(not_found_error,0,"Unable to remove key '%*s': not found",key_len,key);
 
     default:
-      RAISE_IMPOSSIBLE;
+      THROW_IMPOSSIBLE;
 
     }
   }
@@ -899,20 +865,18 @@ _xbt_dictelm_remove_rec(xbt_dictelm_t head,
  *
  * @head: the head of the dict
  * @key: the key of the data to be removed
- * @Returns: xbt_error_t
  *
- * Remove the entry associated with the given @key
+ * Remove the entry associated with the given @key. Throws not_found_err
  */
-xbt_error_t
+void
 xbt_dictelm_remove_ext(xbt_dictelm_t head,
                        const char  *key,
                        int          key_len) {
   /* there is no head, go to hell */
-  if (!head) {
-    RAISE0(mismatch_error, "there is no head, go to hell");
-  }
+  if (!head) 
+    THROW1(arg_error,0,"Asked to remove key %s from NULL dict",key);
   
-  return _xbt_dictelm_remove_rec(head, key, key_len, 0);
+  _xbt_dictelm_remove_rec(head, key, key_len, 0);
 }
 
 /**
@@ -920,14 +884,13 @@ xbt_dictelm_remove_ext(xbt_dictelm_t head,
  *
  * @head: the head of the dict
  * @key: the key of the data to be removed
- * @Returns: xbt_error_t
  *
- * Remove the entry associated with the given @key
+ * Throws not_found when applicable
  */
-xbt_error_t
+void
 xbt_dictelm_remove(xbt_dictelm_t head,
                    const char     *key) {
-  return _xbt_dictelm_remove_rec(head, key, strlen(key),0);
+  _xbt_dictelm_remove_rec(head, key, strlen(key),0);
 }
 
 /*----[ _xbt_dict_dump_rec ]------------------------------------------------*/
@@ -1005,7 +968,6 @@ _xbt_dictelm_dump_rec(xbt_dictelm_t  head,
  *
  * @head: the head of the dict
  * @output: a function to dump each data in the tree
- * @Returns: xbt_error_t
  *
  * Ouputs the content of the structure. (for debuging purpose). @ouput is a
  * function to output the data. If NULL, data won't be displayed.