summaryrefslogtreecommitdiff
path: root/lib/buffer.h
diff options
context:
space:
mode:
authorOndrej Zajicek <santiago@crfreenet.org>2013-11-22 02:43:41 +0100
committerOndrej Zajicek <santiago@crfreenet.org>2013-11-22 02:48:44 +0100
commit0aeac9cb7f9887374ce0258c8653f9518529bf08 (patch)
tree2d16e0904be1658c08a4c779cf4132bc8bf20efa /lib/buffer.h
parent8931425d02dd8656b48142f608d3119ab6f4a96f (diff)
parent7c9930f9c8feb3b08f7a9e94a08807ccbbc096f5 (diff)
Merge commit 'origin/bfd'
Diffstat (limited to 'lib/buffer.h')
-rw-r--r--lib/buffer.h35
1 files changed, 35 insertions, 0 deletions
diff --git a/lib/buffer.h b/lib/buffer.h
new file mode 100644
index 00000000..cf073e88
--- /dev/null
+++ b/lib/buffer.h
@@ -0,0 +1,35 @@
+
+#define BUFFER(type) struct { type *data; uint used, size; }
+
+#define BUFFER_SIZE(v) ((v).size * sizeof(* (v).data))
+
+#define BUFFER_INIT(v,pool,isize) \
+ ({ \
+ (v).used = 0; \
+ (v).size = (isize); \
+ (v).data = mb_alloc(pool, BUFFER_SIZE(v)); \
+ })
+
+#define BUFFER_SET(v,nsize) \
+ ({ \
+ (v).used = (nsize); \
+ if ((v).used > (v).size) \
+ buffer_realloc((void **) &((v).data), &((v).size), (v).used, sizeof(* (v).data)); \
+ })
+
+#define BUFFER_INC(v,step) \
+ ({ \
+ uint _o = (v).used; \
+ BUFFER_SET(v, (v).used + (step)); \
+ (v).data + _o; \
+ })
+
+#define BUFFER_DEC(v,step) ({ (v).used -= (step); })
+
+#define BUFFER_PUSH(v) (*BUFFER_INC(v,1))
+
+#define BUFFER_POP(v) BUFFER_DEC(v,1)
+
+#define BUFFER_FLUSH(v) ({ (v).used = 0; })
+
+