chiark / gitweb /
Version bump.
[mLib] / alloc.c
diff --git a/alloc.c b/alloc.c
index e178853fd7e736cf7ff1ccdad18354e5bf7e85a7..9bc536e78a0f1fa11d2a8e96dda7577552023768 100644 (file)
--- a/alloc.c
+++ b/alloc.c
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: alloc.c,v 1.2 1999/05/05 18:50:31 mdw Exp $
+ * $Id: alloc.c,v 1.5 2000/07/16 12:29:16 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.c,v $
+ * Revision 1.5  2000/07/16 12:29:16  mdw
+ * Change to arena `realloc' interface, to fix a design bug.
+ *
+ * Revision 1.4  2000/06/17 10:35:51  mdw
+ * Major overhaul for arena support.
+ *
+ * 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.
  *
 /* --- Local headers --- */
 
 #include "alloc.h"
+#include "arena.h"
 #include "exc.h"
-#include "track.h"
 
-/*----- Functions provided ------------------------------------------------*/
+/*----- Functions and macros ----------------------------------------------*/
 
-/* --- @xmalloc@ --- *
+/* --- @x_alloc@ --- *
  *
- * Arguments:  @size_t sz@ = size of block to allocate
+ * Arguments:  @arena *a@ = pointer to underlying arena
+ *             @size_t sz@ = size of block to allocate
  *
  * Returns:    Pointer to allocated block.
  *
  *             exception @EXC_NOMEM@ is thrown.
  */
 
-void *xmalloc(size_t sz)
+void *x_alloc(arena *a, size_t sz)
 {
-  void *p = malloc(sz);
+  void *p = A_ALLOC(a, sz);
   if (!p)
     THROW(EXC_NOMEM);
   return (p);
 }
 
-/* --- @xstrdup@ --- *
+/* --- @x_strdup@ --- *
  *
- * Arguments:  @const char *s@ = pointer to a string
+ * Arguments:  @arena *a@ = pointer to underlying arena
+ *             @const char *s@ = pointer to a string
  *
  * Returns:    Pointer to a copy of the string.
  *
@@ -82,18 +94,20 @@ void *xmalloc(size_t sz)
  *             thrown.
  */
 
-char *xstrdup(const char *s)
+char *x_strdup(arena *a, const char *s)
 {
   size_t sz = strlen(s) + 1;
-  char *p = xmalloc(sz);
+  char *p = x_alloc(a, sz);
   memcpy(p, s, sz);
   return (p);
 }
 
-/* --- @xrealloc@ --- *
+/* --- @x_realloc@ --- *
  *
- * Arguments:  @void *p@ = pointer to a block of memory
+ * Arguments:  @arena *a@ = pointer to underlying arena
+ *             @void *p@ = pointer to a block of memory
  *             @size_t sz@ = new size desired for the block
+ *             @size_t osz@ = size of the old block
  *
  * Returns:    Pointer to the resized memory block (which is almost
  *             certainly not in the same place any more).
@@ -102,12 +116,78 @@ char *xstrdup(const char *s)
  *             exception @EXC_NOMEM@ is thrown.
  */
 
-void *xrealloc(void *p, size_t sz)
+void *x_realloc(arena *a, void *p, size_t sz, size_t osz)
 {
-  p = realloc(p, sz);
+  p = A_REALLOC(a, p, sz, osz);
   if (!p)
     THROW(EXC_NOMEM);
   return (p);
 }
 
+/* --- @x_free@ --- *
+ *
+ * Arguments:  @arena *a@ = pointer to underlying arena
+ *             @void *p@ = pointer to a block of memory.
+ *
+ * Returns:    ---
+ *
+ * Use:                Frees a block of memory.
+ */
+
+void (x_free)(arena *a, void *p) { x_free(a, p); }
+
+/*----- Old functions for the standard arena ------------------------------*/
+
+/* --- @xmalloc@ --- *
+ *
+ * Arguments:  @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.
+ */
+
+void *(xmalloc)(size_t sz) { return xmalloc(sz); }
+
+/* --- @xstrdup@ --- *
+ *
+ * Arguments:  @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.
+ */
+
+char *(xstrdup)(const char *s) { return xstrdup(s); }
+
+/* --- @xrealloc@ --- *
+ *
+ * Arguments:  @void *p@ = pointer to a block of memory
+ *             @size_t sz@ = new size desired for the block
+ *             @size_t osz@ = size of the old 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.
+ */
+
+void *(xrealloc)(void *p, size_t sz, size_t osz)
+{ return xrealloc(p, sz, osz); }
+
+/* --- @xfree@ --- *
+ *
+ * Arguments:  @void *p@ = pointer to a block of memory.
+ *
+ * Returns:    ---
+ *
+ * Use:                Frees a block of memory.
+ */
+
+void (xfree)(void *p) { xfree(p); }
+
 /*----- That's all, folks -------------------------------------------------*/