chiark / gitweb /
Basic arena management.
authormdw <mdw>
Sat, 17 Jun 2000 10:37:53 +0000 (10:37 +0000)
committermdw <mdw>
Sat, 17 Jun 2000 10:37:53 +0000 (10:37 +0000)
arena.c [new file with mode: 0644]
arena.h [new file with mode: 0644]

diff --git a/arena.c b/arena.c
new file mode 100644 (file)
index 0000000..20d286c
--- /dev/null
+++ b/arena.c
@@ -0,0 +1,87 @@
+/* -*-c-*-
+ *
+ * $Id: arena.c,v 1.1 2000/06/17 10:37:53 mdw Exp $
+ *
+ * Abstraction for memory allocation arenas
+ *
+ * (c) 2000 Straylight/Edgeware
+ */
+
+/*----- Licensing notice --------------------------------------------------* 
+ *
+ * This file is part of the mLib utilities library.
+ *
+ * mLib is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Library General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ * 
+ * mLib is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * 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.
+ */
+
+/*----- Revision history --------------------------------------------------* 
+ *
+ * $Log: arena.c,v $
+ * Revision 1.1  2000/06/17 10:37:53  mdw
+ * Basic arena management.
+ *
+ */
+
+/*----- Header files ------------------------------------------------------*/
+
+#include <stdlib.h>
+
+#include "arena.h"
+
+/*----- The standard arena ------------------------------------------------*/
+
+static void *_alloc(arena *a, size_t sz) { return malloc(sz); }
+static void *_realloc(arena *a, void *p, size_t sz){ return realloc(p, sz); }
+static void _free(arena *a, void *p) { free (p); }
+
+static arena_ops stdlib_ops = { _alloc, _realloc, _free, 0 };
+arena arena_stdlib = { &stdlib_ops };
+
+/*----- Global variables --------------------------------------------------*/
+
+arena *arena_global = &arena_stdlib;
+
+/*----- Main code ---------------------------------------------------------*/
+
+/* --- @arena_fakerealloc@ --- *
+ *
+ * Arguments:  @arena *a@ = pointer to arena block
+ *             @void *p@ = pointer to memory block to resize
+ *             @size_t sz@ = size desired for the block
+ *
+ * Returns:    ---
+ *
+ * Use:                Standard fake @realloc@ function, for use if you don't
+ *             support @realloc@ properly.
+ */
+
+void *arena_fakerealloc(arena *a, void *p, size_t sz)
+{
+  void *q = A_ALLOC(a, sz);
+  if (!q)
+    return (0);
+  memcpy(q, p, sz);
+  A_FREE(a, p);
+  return (q);
+}
+
+/* --- Function equivalents of the macros --- */
+
+void *a_alloc(arena *a, size_t sz) { return (A_ALLOC(a, sz)); }
+void *a_realloc(arena *a, void *p, size_t sz) { return A_REALLOC(a, p, sz); }
+void a_free(arena *a, void *p) { A_FREE(a, p); }
+
+/*----- That's all, folks -------------------------------------------------*/
diff --git a/arena.h b/arena.h
new file mode 100644 (file)
index 0000000..6ceef09
--- /dev/null
+++ b/arena.h
@@ -0,0 +1,103 @@
+/* -*-c-*-
+ *
+ * $Id: arena.h,v 1.1 2000/06/17 10:37:53 mdw Exp $
+ *
+ * Abstraction for memory allocation arenas
+ *
+ * (c) 2000 Straylight/Edgeware
+ */
+
+/*----- Licensing notice --------------------------------------------------* 
+ *
+ * This file is part of the mLib utilities library.
+ *
+ * mLib is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU Library General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ * 
+ * mLib is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * 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.
+ */
+
+/*----- Revision history --------------------------------------------------* 
+ *
+ * $Log: arena.h,v $
+ * Revision 1.1  2000/06/17 10:37:53  mdw
+ * Basic arena management.
+ *
+ */
+
+#ifndef MLIB_ARENA_H
+#define MLIB_ARENA_H
+
+#ifdef __cplusplus
+  extern "C" {
+#endif
+
+/*----- Header files ------------------------------------------------------*/
+
+#include <stdlib.h>
+
+/*----- Data structures ---------------------------------------------------*/
+
+/* --- An arena structure --- */
+
+typedef struct arena {
+  struct arena_ops *ops;
+} arena;
+      
+typedef struct arena_ops {
+  void *(*alloc)(arena */*a*/, size_t /*sz*/);
+  void *(*realloc)(arena */*a*/, void */*p*/, size_t /*sz*/);
+  void (*free)(arena */*a*/, void */*p*/);
+  void (*purge)(arena */*a*/);
+} arena_ops;
+
+/*----- Global variables --------------------------------------------------*/
+
+extern arena *arena_global;            /* Standard global arena */
+extern arena arena_stdlib;             /* Arena based on @malloc@/@free@ */
+
+/*----- Functions provided ------------------------------------------------*/
+
+/* --- @arena_fakerealloc@ --- *
+ *
+ * Arguments:  @arena *a@ = pointer to arena block
+ *             @void *p@ = pointer to memory block to resize
+ *             @size_t sz@ = size desired for the block
+ *
+ * Returns:    ---
+ *
+ * Use:                Standard fake @realloc@ function, for use if you don't
+ *             support @realloc@ properly.
+ */
+
+extern void *arena_fakerealloc(arena */*a*/, void */*p*/, size_t /*sz*/);
+
+/* --- Useful macros --- */
+
+#define A_ALLOC(a, sz) (((a)->ops->alloc)((a), (sz)))
+#define A_REALLOC(a, p, sz) (((a)->ops->realloc)((a), (p), (sz)))
+#define A_FREE(a, p) (((a)->ops->free)((a), (p)))
+
+/* --- Simple function equivalents --- */
+
+extern void *a_alloc(arena */*a*/, size_t /*sz*/);
+extern void *a_realloc(arena */*a*/, void */*p*/, size_t /*sz*/);
+extern void a_free(arena */*a*/, void */*p*/);
+
+/*----- That's all, folks -------------------------------------------------*/
+
+#ifdef __cplusplus
+  }
+#endif
+
+#endif