]> khamidou — GitHub mirrors - te.git/commitdiff
adding mm routines
authorkhamidou <karim@lambda-expr.org>
Mon, 30 Mar 2009 10:27:58 +0000 (12:27 +0200)
committerkhamidou <karim@lambda-expr.org>
Mon, 30 Mar 2009 10:27:58 +0000 (12:27 +0200)
buffer.c [new file with mode: 0644]
buffer.h
errors.c [new file with mode: 0644]
errors.h [new file with mode: 0644]
zalloc.h [new file with mode: 0644]

diff --git a/buffer.c b/buffer.c
new file mode 100644 (file)
index 0000000..326a170
--- /dev/null
+++ b/buffer.c
@@ -0,0 +1,52 @@
+#include <stdlib.h>
+
+#include "zalloc.h"
+#include "error.h"
+#include "buffer.h"
+
+struct te_char *alloc_and_insert_char(struct te_buffer *buf)
+{
+       if (buf == NULL)
+               fail("The buffer parameter to alloc_and_insert_char is NULL");
+
+       struct te_char *c = zalloc(sizeof(struct te_buffer));
+
+       if (c == NULL)
+               fail("Unable to allocate memory for te_char struct");
+
+       TAILQ_INSERT_TAIL(&buf->chars_head, c, chars);
+
+       return c;
+}
+
+struct te_buffer *alloc_and_insert_buffer(void)
+{
+       struct te_buffer *b = zalloc(sizeof(struct te_buffer));
+       
+       if (b == NULL)
+               fail("Unable to allocate memory for te_buffer struct");
+
+       TAILQ_INSERT_TAIL(&buffers_head, b, buffers);
+
+       return b;
+}
+
+void free_char(struct te_buffer *b, struct te_char *c)
+{
+       if (b == NULL || c == NULL)
+               fail("One of the parameters to free_char() is NULL");
+
+       TAILQ_REMOVE(&b->chars, c, chars);
+
+       free(c);
+}
+
+void free_buffer(struct te_buffer *b)
+{
+       if (b == NULL)
+               fail("The parameter to free_buffer() is NULL");
+
+       TAILQ_REMOVE(&buffers_head, b, buffers);
+
+       free(b);
+}
index 8ff36531d9340f9c1d55d8519ed85112b0764d08..e8589a8453f70c8524ad5b802a32798253bc0dc0 100644 (file)
--- a/buffer.h
+++ b/buffer.h
@@ -17,4 +17,11 @@ struct te_buffer {
 
 TAILQ_HEAD(te_buffers_head, te_buffer) buffers_head;
 
+struct te_char                 *alloc_and_insert_char(struct te_buffer *buf);
+struct te_buffer       *alloc_and_insert_buffer(void);
+void                   free_char(struct te_buffer *b, struct te_char *c);
+void                   free_buffer(struct te_buffer *b);
+
+void init_buffers();
+
 #endif
diff --git a/errors.c b/errors.c
new file mode 100644 (file)
index 0000000..3ef3996
--- /dev/null
+++ b/errors.c
@@ -0,0 +1,13 @@
+#include "errors.h"
+
+
+void fail(char *fmt, ...)
+{
+       va_list ap;
+       va_start(ap, fmt);
+       vfprintf(stderr, fmt, ap);
+       va_end(ap);
+
+       exit(EXIT_FAILURE);
+}
+
diff --git a/errors.h b/errors.h
new file mode 100644 (file)
index 0000000..baf0f4b
--- /dev/null
+++ b/errors.h
@@ -0,0 +1,10 @@
+#ifndef __ERRORS_H__
+#define __ERRORS_H__
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <stdarg.h>
+
+void fail(char *fmt, ...);
+
+#endif
diff --git a/zalloc.h b/zalloc.h
new file mode 100644 (file)
index 0000000..9b577f8
--- /dev/null
+++ b/zalloc.h
@@ -0,0 +1,8 @@
+#ifndef __ZALLOC_H__
+#define __ZALLOC_H__
+
+#include <stdlib.h>
+
+#define zalloc(size) calloc(1, size)
+
+#endif