summaryrefslogtreecommitdiff
path: root/src/gallium
diff options
context:
space:
mode:
authorLuca Barbieri <luca@luca-barbieri.com>2010-02-25 13:11:12 +0100
committerLuca Barbieri <luca@luca-barbieri.com>2010-04-12 23:50:37 +0200
commit6cd82eb399c8a6c248c2d9b135bafefbac8fbe0f (patch)
tree094849c924fd4695d894e6de63e64a06bdb7b479 /src/gallium
parent70fe9fd0075faf1fe731401fba9f38e17d7d062b (diff)
gallium/auxiliary: add dynamically sized buffer/array/vector
Diffstat (limited to 'src/gallium')
-rw-r--r--src/gallium/auxiliary/util/u_buffer.h57
1 files changed, 57 insertions, 0 deletions
diff --git a/src/gallium/auxiliary/util/u_buffer.h b/src/gallium/auxiliary/util/u_buffer.h
new file mode 100644
index 0000000000..5eb53bcf0a
--- /dev/null
+++ b/src/gallium/auxiliary/util/u_buffer.h
@@ -0,0 +1,57 @@
+#ifndef U_BUFFER_H
+#define U_BUFFER_H
+
+struct util_buffer
+{
+ void* data;
+ unsigned size;
+ unsigned capacity;
+};
+
+static inline void
+util_buffer_init(struct util_buffer* buf)
+{
+ memset(buf, 0, sizeof(*buf));
+}
+
+static inline void
+util_buffer_fini(struct util_buffer* buf)
+{
+ if(buf->data) {
+ free(buf->data);
+ util_buffer_init(buf);
+ }
+}
+
+static inline void*
+util_buffer_grow(struct util_buffer* buf, int size)
+{
+ unsigned newsize = buf->size + size;
+ char* p;
+ if(newsize > buf->capacity) {
+ buf->capacity <<= 1;
+ if(newsize > buf->capacity)
+ buf->capacity = newsize;
+ buf->data = realloc(buf->data, buf->capacity);
+ }
+
+ p = (char*)buf->data + buf->size;
+ buf->size = newsize;
+ return p;
+}
+
+static inline void
+util_buffer_trim(struct util_buffer* buf)
+{
+ buf->data = realloc(buf->data, buf->size);
+ buf->capacity = buf->size;
+}
+
+#define util_buffer_append(buf, type, v) do {type __v = (v); memcpy(util_buffer_grow((buf), sizeof(type)), &__v, sizeof(type));} while(0)
+#define util_buffer_top_ptr(buf, type) (type*)((buf)->data + (buf)->size - sizeof(type))
+#define util_buffer_top(buf, type) *util_buffer_top_ptr(buf, type)
+#define util_buffer_pop_ptr(buf, type) (type*)((buf)->data + ((buf)->size -= sizeof(type)))
+#define util_buffer_pop(buf, type) *util_buffer_pop_ptr(buf, type)
+#define util_buffer_contains(buf, type) ((buf)->size >= sizeof(type))
+
+#endif /* U_BUFFER_H */