Merge commit 'origin/master' into gallium-msaa
[mesa.git] / src / gallium / auxiliary / util / u_dynarray.h
index 94a882b59ee6d1ed884e7f5c5dc22e22647d15d9..9d1c1713a7c47646362a003617b9af9e0bb54013 100644 (file)
@@ -59,10 +59,10 @@ util_dynarray_fini(struct util_dynarray *buf)
    }
 }
 
+/* use util_dynarray_trim to reduce the allocated storage */
 static INLINE void *
-util_dynarray_grow(struct util_dynarray *buf, int size)
+util_dynarray_resize(struct util_dynarray *buf, unsigned newsize)
 {
-   unsigned newsize = buf->size + size;
    char *p;
    if(newsize > buf->capacity)
    {
@@ -78,11 +78,26 @@ util_dynarray_grow(struct util_dynarray *buf, int size)
    return p;
 }
 
+static INLINE void *
+util_dynarray_grow(struct util_dynarray *buf, int diff)
+{
+   return util_dynarray_resize(buf, buf->size + diff);
+}
+
 static INLINE void
 util_dynarray_trim(struct util_dynarray *buf)
 {
-   buf->data = REALLOC(buf->data, buf->capacity, buf->size);
-   buf->capacity = buf->size;
+   if (buf->size != buf->capacity) {
+      if (buf->size) {
+         buf->data = REALLOC(buf->data, buf->capacity, buf->size);
+         buf->capacity = buf->size;
+      }
+      else {
+         FREE(buf->data);
+         buf->data = 0;
+         buf->capacity = 0;
+      }
+   }
 }
 
 #define util_dynarray_append(buf, type, v) do {type __v = (v); memcpy(util_dynarray_grow((buf), sizeof(type)), &__v, sizeof(type));} while(0)