New multiprecision integer arithmetic suite.
[u/mdw/catacomb] / mparena.h
diff --git a/mparena.h b/mparena.h
new file mode 100644 (file)
index 0000000..61c735e
--- /dev/null
+++ b/mparena.h
@@ -0,0 +1,167 @@
+/* -*-c-*-
+ *
+ * $Id: mparena.h,v 1.1 1999/11/17 18:02:16 mdw Exp $
+ *
+ * Allocation and freeing of MP buffers
+ *
+ * (c) 1999 Straylight/Edgeware
+ */
+
+/*----- Licensing notice --------------------------------------------------* 
+ *
+ * This file is part of Catacomb.
+ *
+ * Catacomb 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.
+ * 
+ * Catacomb 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 Catacomb; if not, write to the Free
+ * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
+ * MA 02111-1307, USA.
+ */
+
+/*----- Revision history --------------------------------------------------* 
+ *
+ * $Log: mparena.h,v $
+ * Revision 1.1  1999/11/17 18:02:16  mdw
+ * New multiprecision integer arithmetic suite.
+ *
+ */
+
+#ifndef MPARENA_H
+#define MPARENA_H
+
+#ifdef __cplusplus
+  extern "C" {
+#endif
+
+/*----- Header files ------------------------------------------------------*/
+
+#ifndef MPW_H
+#  include "mpw.h"
+#endif
+
+/*----- Data structures ---------------------------------------------------*/
+
+/* --- @mparena_node@ --- *
+ *
+ * For internal use by the MP arena manager.  The free blocks are held in a
+ * binary tree by size, held in the first digit of each vector.
+ */
+
+typedef struct mparena_node {
+  struct mparena_node *left, *right;
+  mpw *v;
+} mparena_node;
+
+/* --- @mparena@ --- *
+ *
+ * The actual arena.
+ */
+
+typedef struct mparena {
+  mparena_node *root;
+  struct mparena_ops *ops;
+} mparena;
+
+/* --- @mparena_ops@ --- *
+ *
+ * Operations required for an arena memory manager.  The default manager just
+ * calls @xmalloc@ and @free@, although it's possible to envisage a more
+ * paranoid implementation which allocates locked memory pages.  Switch them
+ * over with @mparena_setops@.  It's usual to only do this when you've
+ * attached your extra state to the end of the @mparena@ structure.
+ */
+
+typedef struct mparena_ops {
+  void *(*alloc)(mparena */*a*/, size_t /*sz*/);
+  void (*free)(mparena */*a*/, void */*p*/);
+} mparena_ops;
+
+/*----- Magical constants -------------------------------------------------*/
+
+#define MPARENA_GLOBAL ((mparena *)0)
+
+extern mparena_ops mparena_defaultops;
+
+/*----- Functions provided ------------------------------------------------*/
+
+/* --- @mparena_create@ --- *
+ *
+ * Arguments:  @mparena *a@ = pointer to arena block
+ *
+ * Returns:    ---
+ *
+ * Use:                Initializes an MP arena so that blocks can be allocated from
+ *             it.
+ */
+
+extern void mparena_create(mparena */*a*/);
+
+#define MPARENA_INIT { 0, &mparena_defaultops }
+
+/* --- @mparena_setops@ --- *
+ *
+ * Arguments:  @mparena *a@ = pointer to arena block
+ *             @mparena_ops *ops@ = pointer to operations block or null
+ *
+ * Returns:    The previous operations block.
+ *
+ * Use:                Sets or queries the operations attached to an arena.
+ */
+
+extern mparena_ops *mparena_setops(mparena */*a*/, mparena_ops */*ops*/);
+
+/* --- @mparena_destroy@ --- *
+ *
+ * Arguments:  @mparena *a@ = pointer to arena block
+ *
+ * Returns:    ---
+ *
+ * Use:                Frees an MP arena, and all the vectors held within it.  The
+ *             blocks which are currently allocated can be freed into some
+ *             other arena.
+ */
+
+extern void mparena_destroy(mparena */*a*/);
+
+/* --- @mpalloc@ --- *
+ *
+ * Arguments:  @mparena *a@ = pointer to arena block
+ *             @size_t sz@ = number of digits required
+ *
+ * Returns:    Pointer to a suitably sized block.
+ *
+ * Use:                Allocates a lump of data suitable for use as an array of MP
+ *             digits.
+ */
+
+extern mpw *mpalloc(mparena */*a*/, size_t /*sz*/);
+
+/* --- @mpfree@ --- *
+ *
+ * Arguments:  @mparena *a@ = pointer to arena block
+ *             @mpw *v@ = pointer to allocated vector
+ *
+ * Returns:    ---
+ *
+ * Use:                Returns an MP vector to an arena.  It doesn't have to be
+ *             returned to the arena from which it was allocated.
+ */
+
+extern void mpfree(mparena */*a*/, mpw */*v*/);
+
+/*----- That's all, folks -------------------------------------------------*/
+
+#ifdef __cplusplus
+  }
+#endif
+
+#endif