chiark / gitweb /
Various manual fixes.
[mLib] / darray.h
index f2ebe1f7f01ff6258b0737cc5a79db95128a816b..a768c1dd7694e400d8f0654b986f088138583ad2 100644 (file)
--- a/darray.h
+++ b/darray.h
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: darray.h,v 1.3 1999/11/05 14:32:43 mdw Exp $
+ * $Id: darray.h,v 1.7 2004/04/08 01:36:11 mdw Exp $
  *
  * Dynamically growing dense arrays
  *
  * MA 02111-1307, USA.
  */
 
-/*----- Revision history --------------------------------------------------* 
- *
- * $Log: darray.h,v $
- * Revision 1.3  1999/11/05 14:32:43  mdw
- * Minor change in argument naming.
- *
- * Revision 1.2  1999/10/29 22:59:22  mdw
- * New array adjustment macros for unsigned arguments.
- *
- * Revision 1.1  1999/10/22 22:37:26  mdw
- * New dynamic array implementation replaces `dynarray.h'.
- *
- */
-
-#ifndef DARRAY_H
-#define DARRAY_H
+#ifndef MLIB_DARRAY_H
+#define MLIB_DARRAY_H
 
 #ifdef __cplusplus
   extern "C" {
 #include <stdlib.h>
 #include <string.h>
 
-#ifndef ALLOC_H
+#ifndef MLIB_ALLOC_H
 #  include "alloc.h"
 #endif
 
-#ifndef EXC_H
+#ifndef MLIB_EXC_H
 #  include "exc.h"
 #endif
 
@@ -101,6 +87,7 @@ typedef struct da_base {
   size_t len;                          /* Length of useful portion */
   size_t off;                          /* Offset of @v@ into space */
   unsigned push, unshift;              /* Pushes/unshifts since growth */
+  arena *a;                            /* Pointer to allocation arena */
 } da_base;
 
 /* --- @DA_DECL@ --- *
@@ -116,7 +103,7 @@ typedef struct da_base {
 
 /*----- Initialization, creation and destruction --------------------------*/
 
-#define DA_INIT { { 0, 0, 0, 0, 0 }, 0 } /* Standard initializer */
+#define DA_INIT { { 0, 0, 0, 0, 0, &arena_stdlib }, 0 }
 
 /* --- @DA_CREATE@ --- *
  *
@@ -125,11 +112,12 @@ typedef struct da_base {
  * Use:                Initializes an array block.
  */
 
-#define DA_CREATE(a) do {                                              \
-  (a)->b.sz = (a)->b.len = 0;                                          \
-  (a)->b.off = 0;                                                      \
-  (a)->b.push = (a)->b.unshift = 0;                                    \
-  (a)->v = 0;                                                          \
+#define DA_CREATE(aa) do {                                             \
+  (aa)->b.sz = (aa)->b.len = 0;                                                \
+  (aa)->b.off = 0;                                                     \
+  (aa)->b.push = (aa)->b.unshift = 0;                                  \
+  (aa)->b.a = &arena_stdlib;                                           \
+  (aa)->v = 0;                                                         \
 } while (0)
 
 /* --- @DA_DESTROY@ --- *
@@ -139,10 +127,10 @@ typedef struct da_base {
  * Use:                Destroys an array.  The array is left valid but empty.
  */
 
-#define DA_DESTROY(a) do {                                             \
-  if ((a)->v)                                                          \
-    free((a)->v - (a)->b.off);                                         \
-  DA_CREATE(a);                                                                \
+#define DA_DESTROY(aa) do {                                            \
+  if ((aa)->v)                                                         \
+    x_free((aa)->b.a, (aa)->v - (aa)->b.off);                          \
+  DA_CREATE(aa);                                                               \
 } while (0)
 
 /*----- Storage reservation -----------------------------------------------*/
@@ -402,6 +390,26 @@ typedef struct da_base {
 
 /*----- Stack-like operations ---------------------------------------------*/
 
+/* --- @DA_FIRST@ --- *
+ *
+ * Arguments:  @a@ = pointer to an array block (multiply evaluated)
+ *
+ * Use:                Evaluates to the initial element in array @a@.  It is unsafe
+ *             to do this if the array is empty.  The array is not changed. 
+ */
+
+#define DA_FIRST(a) (DA(a)[0])
+
+/* --- @DA_LAST@ --- *
+ *
+ * Arguments:  @a@ = pointer to an array block (multiply evaluated)
+ *
+ * Use:                Evaluates to the final element in array @a@.  It is unsafe
+ *             to do this if the array is empty.  The array is not changed.
+ */
+
+#define DA_LAST(a) (DA(a)[(a)->b.len - 1])
+
 /* --- @DA_PUSH@ --- *
  *
  * Arguments:  @a@ = pointer to an array block (multiply evaluated)
@@ -454,7 +462,7 @@ typedef struct da_base {
  */
 
 #define DA_SHIFT(a)                                                    \
-  ((a)->b.len ? ((void)0) : THROW(DAEXC_UFLOW),                        \
+  ((a)->b.len ? ((void)0) : THROW(DAEXC_UFLOW),                                \
    (a)->b.len--,                                                       \
    (a)->b.sz--,                                                                \
    (a)->b.off++,                                                       \