]> git.ipfire.org Git - thirdparty/git.git/blobdiff - object.c
object_array: add a "clear" function
[thirdparty/git.git] / object.c
index a16b9f9e936d060ba190b6c7e82ff5f25795f490..6aeb1bbbe35b79f834c5506f523beeb4e92d8790 100644 (file)
--- a/object.c
+++ b/object.c
@@ -33,13 +33,20 @@ const char *typename(unsigned int type)
        return object_type_strings[type];
 }
 
-int type_from_string(const char *str)
+int type_from_string_gently(const char *str, ssize_t len, int gentle)
 {
        int i;
 
+       if (len < 0)
+               len = strlen(str);
+
        for (i = 1; i < ARRAY_SIZE(object_type_strings); i++)
-               if (!strcmp(str, object_type_strings[i]))
+               if (!strncmp(str, object_type_strings[i], len))
                        return i;
+
+       if (gentle)
+               return -1;
+
        die("invalid object type \"%s\"", str);
 }
 
@@ -312,7 +319,7 @@ static void add_object_array_with_mode_context(struct object *obj, const char *n
 
        if (nr >= alloc) {
                alloc = (alloc + 32) * 2;
-               objects = xrealloc(objects, alloc * sizeof(*objects));
+               REALLOC_ARRAY(objects, alloc);
                array->alloc = alloc;
                array->objects = objects;
        }
@@ -348,6 +355,16 @@ void add_object_array_with_context(struct object *obj, const char *name, struct
                add_object_array_with_mode_context(obj, name, array, S_IFINVALID, context);
 }
 
+/*
+ * Free all memory associated with an entry; the result is
+ * in an unspecified state and should not be examined.
+ */
+static void object_array_release_entry(struct object_array_entry *ent)
+{
+       if (ent->name != object_array_slopbuf)
+               free(ent->name);
+}
+
 void object_array_filter(struct object_array *array,
                         object_array_each_func_t want, void *cb_data)
 {
@@ -360,13 +377,22 @@ void object_array_filter(struct object_array *array,
                                objects[dst] = objects[src];
                        dst++;
                } else {
-                       if (objects[src].name != object_array_slopbuf)
-                               free(objects[src].name);
+                       object_array_release_entry(&objects[src]);
                }
        }
        array->nr = dst;
 }
 
+void object_array_clear(struct object_array *array)
+{
+       int i;
+       for (i = 0; i < array->nr; i++)
+               object_array_release_entry(&array->objects[i]);
+       free(array->objects);
+       array->objects = NULL;
+       array->nr = array->alloc = 0;
+}
+
 /*
  * Return true iff array already contains an entry with name.
  */
@@ -393,8 +419,7 @@ void object_array_remove_duplicates(struct object_array *array)
                                objects[array->nr] = objects[src];
                        array->nr++;
                } else {
-                       if (objects[src].name != object_array_slopbuf)
-                               free(objects[src].name);
+                       object_array_release_entry(&objects[src]);
                }
        }
 }