gallium/auxiliary: fix util_dynarray_trim to 0 to free the data
authorLuca Barbieri <luca@luca-barbieri.com>
Tue, 13 Apr 2010 06:18:59 +0000 (08:18 +0200)
committerLuca Barbieri <luca@luca-barbieri.com>
Tue, 13 Apr 2010 06:18:59 +0000 (08:18 +0200)
This works better and preserves the
data != 0 if and only if capacity != 0 invariant.

src/gallium/auxiliary/util/u_dynarray.h

index 94a882b59ee6d1ed884e7f5c5dc22e22647d15d9..5d124832be34fc18279f2647e9ef2393503fef36 100644 (file)
@@ -81,8 +81,14 @@ util_dynarray_grow(struct util_dynarray *buf, int size)
 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->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)