chiark / gitweb /
Support for resource pools, based on the Apache model.
[mLib] / alloc.h
diff --git a/alloc.h b/alloc.h
index 5b8832bea5b10e2e88b554f37be3735450f3ce81..edf555af7a3265afe630caf4629c21102eaedbfe 100644 (file)
--- a/alloc.h
+++ b/alloc.h
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: alloc.h,v 1.2 1999/05/05 18:50:31 mdw Exp $
+ * $Id: alloc.h,v 1.5 2000/06/17 10:35:51 mdw Exp $
  *
  * Memory allocation functions
  *
  * GNU Library General Public License for more details.
  * 
  * You should have received a copy of the GNU Library General Public
- * License along with mLib; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ * License along with mLib; if not, write to the Free
+ * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
+ * MA 02111-1307, USA.
  */
 
 /*----- Revision history --------------------------------------------------*
  *
  * $Log: alloc.h,v $
+ * Revision 1.5  2000/06/17 10:35:51  mdw
+ * Major overhaul for arena support.
+ *
+ * Revision 1.4  1999/12/10 23:42:04  mdw
+ * Change header file guard names.
+ *
+ * Revision 1.3  1999/05/06 19:51:35  mdw
+ * Reformatted the LGPL notice a little bit.
+ *
  * Revision 1.2  1999/05/05 18:50:31  mdw
  * Change licensing conditions to LGPL.
  *
  *
  */
 
-#ifndef ALLOC_H
-#define ALLOC_H
+#ifndef MLIB_ALLOC_H
+#define MLIB_ALLOC_H
 
 #ifdef __cplusplus
   extern "C" {
 #endif
 
+/*----- Required header files ---------------------------------------------*/
+
 #include <stddef.h>
 
-/*----- Functions provided ------------------------------------------------*/
+#ifndef MLIB_ARENA_H
+#  include "arena.h"
+#endif
+
+/*----- Functions and macros ----------------------------------------------*/
+
+/* --- @x_alloc@ --- *
+ *
+ * Arguments:  @arena *a@ = pointer to underlying arena
+ *             @size_t sz@ = size of block to allocate
+ *
+ * Returns:    Pointer to allocated block.
+ *
+ * Use:                Allocates memory.  If there's not enough memory, the
+ *             exception @EXC_NOMEM@ is thrown.
+ */
+
+extern void *x_alloc(arena */*a*/, size_t /*sz*/);
+
+/* --- @x_strdup@ --- *
+ *
+ * Arguments:  @arena *a@ = pointer to underlying arena
+ *             @const char *s@ = pointer to a string
+ *
+ * Returns:    Pointer to a copy of the string.
+ *
+ * Use:                Copies a string (like @strdup@ would, if it existed).  If
+ *             there's not enough memory, the exception @EXC_NOMEM@ is
+ *             thrown.
+ */
+
+extern char *x_strdup(arena */*a*/, const char */*s*/);
+
+/* --- @x_realloc@ --- *
+ *
+ * Arguments:  @arena *a@ = pointer to underlying arena
+ *             @void *p@ = pointer to a block of memory
+ *             @size_t sz@ = new size desired for the block
+ *
+ * Returns:    Pointer to the resized memory block (which is almost
+ *             certainly not in the same place any more).
+ *
+ * Use:                Resizes a memory block.  If there's not enough memory, the
+ *             exception @EXC_NOMEM@ is thrown.
+ */
+
+extern void *x_realloc(arena */*a*/, void */*p*/, size_t /*sz*/);
+
+/* --- @x_free@ --- *
+ *
+ * Arguments:  @arena *a@ = pointer to underlying arena
+ *             @void *p@ = pointer to a block of memory.
+ *
+ * Returns:    ---
+ *
+ * Use:                Frees a block of memory.
+ */
+
+extern void x_free(arena */*a*/, void */*p*/);
+#define x_free(a, p) A_FREE(a, p)
+
+/*----- Old functions for the standard arena ------------------------------*/
 
 /* --- @xmalloc@ --- *
  *
  */
 
 extern void *xmalloc(size_t /*sz*/);
+#define xmalloc(sz) x_alloc(arena_global, (sz))
 
 /* --- @xstrdup@ --- *
  *
@@ -72,6 +146,7 @@ extern void *xmalloc(size_t /*sz*/);
  */
 
 extern char *xstrdup(const char */*s*/);
+#define xstrdup(p) x_strdup(arena_global, (p))
 
 /* --- @xrealloc@ --- *
  *
@@ -86,6 +161,19 @@ extern char *xstrdup(const char */*s*/);
  */
 
 extern void *xrealloc(void */*p*/, size_t /*sz*/);
+#define xrealloc(p, sz) x_realloc(arena_global, (p), (sz))
+
+/* --- @xfree@ --- *
+ *
+ * Arguments:  @void *p@ = pointer to a block of memory.
+ *
+ * Returns:    ---
+ *
+ * Use:                Frees a block of memory.
+ */
+
+extern void xfree(void */*p*/);
+#define xfree(p) x_free(arena_global, (p))
 
 /*----- That's all, folks -------------------------------------------------*/