Major memory management overhaul. Added arena support. Use the secure
[u/mdw/catacomb] / mp-mem.c
index 833874d..7f9d630 100644 (file)
--- a/mp-mem.c
+++ b/mp-mem.c
@@ -1,6 +1,6 @@
 /* -*-c-*-
  *
- * $Id: mp-mem.c,v 1.2 1999/12/10 23:19:02 mdw Exp $
+ * $Id: mp-mem.c,v 1.3 2000/06/17 11:45:09 mdw Exp $
  *
  * Memory management for multiprecision numbers
  *
 /*----- Revision history --------------------------------------------------* 
  *
  * $Log: mp-mem.c,v $
+ * Revision 1.3  2000/06/17 11:45:09  mdw
+ * Major memory management overhaul.  Added arena support.  Use the secure
+ * arena for secret integers.  Replace and improve the MP management macros
+ * (e.g., replace MP_MODIFY by MP_DEST).
+ *
  * Revision 1.2  1999/12/10 23:19:02  mdw
  * Improve error-checking.
  *
 
 /*----- Header files ------------------------------------------------------*/
 
+#include <mLib/sub.h>
+
+
 #include "mp.h"
 
 /*----- Main code ---------------------------------------------------------*/
 
+/* --- @mp_new@ --- *
+ *
+ * Arguments:  @size_t sz@ = size of vector required
+ *             @unsigned f@ = flags to set
+ *
+ * Returns:    Pointer to a new MP structure.
+ *
+ * Use:                Allocates a new multiprecision integer.  The data space is
+ *             allocated from either the standard global or secret arena,
+ *             depending on the initial flags requested.
+ */
+
+mp *mp_new(size_t sz, unsigned f)
+{
+  mp *m = CREATE(mp);
+  m->a = (f & MP_BURN) ? MPARENA_SECURE : MPARENA_GLOBAL;
+  m->v = mpalloc(m->a, sz);
+  m->vl = m->v + sz;
+  m->sz = sz;
+  m->f = f & ~(MP_CONST | MP_DESTROYED);
+  m->ref = 1;
+  return (m);
+}
+
 /* --- @mp_create@ --- *
  *
  * Arguments:  @size_t sz@ = size of vector required
 mp *mp_create(size_t sz)
 {
   mp *m = CREATE(mp);
-  m->v = MP_ALLOC(sz);
+  m->v = mpalloc(MPARENA_GLOBAL, sz);
   m->vl = m->v + sz;
   m->sz = sz;
+  m->a = MPARENA_GLOBAL;
   m->f = MP_UNDEF;
   m->ref = 1;
   return (m);
 }
 
+/* --- @mp_createsecure@ --- *
+ *
+ * Arguments:  @size_t sz@ = size of vector required
+ *
+ * Returns:    Pointer to pristine new MP structure with enough memory
+ *             bolted onto it.
+ *
+ * Use:                Creates a new multiprecision integer with indeterminate
+ *             contents.  The integer has a single reference.  The integer's
+ *             data space is allocated from the secure arena.  Its burn flag
+ *             is set.
+ */
+
+mp *mp_createsecure(size_t sz)
+{
+  mp *m = CREATE(mp);  
+  m->v = mpalloc(MPARENA_SECURE, sz);
+  m->vl = m->v + sz;
+  m->sz = sz;
+  m->a = MPARENA_SECURE;
+  m->f = MP_UNDEF | MP_BURN;
+  m->ref = 1;
+  return (m);
+}
+
 /* --- @mp_build@ --- *
  *
  * Arguments:  @mp *m@ = pointer to an MP block to fill in
@@ -106,7 +164,7 @@ void mp_destroy(mp *m)
   assert(((void)"Attempted to destroy a constant", !(m->f & MP_CONST)));
   if (m->f & MP_BURN)
     memset(m->v, 0, MPWS(m->sz));
-  MP_FREE(m->v);
+  mpfree(m->a, m->v);
   m->f |= MP_DESTROYED;
   DESTROY(m);
 }
@@ -145,7 +203,19 @@ void mp_drop(mp *m) { MP_DROP(m); }
  * Use:                Splits off a modifiable version of the integer referred to.
  */
 
-mp *mp_split(mp *m) { MP_SPLIT(m); return (m); }
+mp *mp_split(mp *m)
+{
+/*   if ((m->f & MP_CONST) || m->ref > 1) { */
+/*     size_t len = MP_LEN(m); */
+/*     mp *mm = mp_new(len, m->f); */
+/*     if (!(m->f & MP_UNDEF)) */
+/*       memcpy(mm->v, m->v, MPWS(len)); */
+/*     m->ref--; */
+/*     m = mm; */
+/*   } */
+  MP_SPLIT(m);
+  return (m);
+}
 
 /* --- @mp_resize@ --- *
  *
@@ -154,38 +224,165 @@ mp *mp_split(mp *m) { MP_SPLIT(m); return (m); }
  *
  * Returns:    ---
  *
- * Use:                Resizes the vector containing the integer's digits.  The new
- *             size must be at least as large as the current integer's
- *             length.  This isn't really intended for client use.
+ * Use:                Changes an integer's size.  The length and value are not
+ *             changed.  It is an error to 
  */
 
-void mp_resize(mp *m, size_t sz) { MP_RESIZE(m, sz); }
+void mp_resize(mp *m, size_t sz)
+{
+/*   mparena *a = (m->f & MP_BURN) ? MPARENA_SECURE : MPARENA_GLOBAL; */
+/*   mpw *v; */
+/*   size_t len = MP_LEN(m); */
+/*   assert(((void)"can't make size less than length", sz >= len)); */
+/*   v = mpalloc(a, sz); */
+/*   if (!(m->f & MP_UNDEF)) */
+/*     memcpy(v, m->v, MPWS(len)); */
+/*   if (m->f & MP_BURN) */
+/*     memset(m->v, 0, MPWS(m->sz)); */
+/*   mpfree(m->a, m->v); */
+/*   m->a = a; */
+/*   m->v = v; */
+/*   m->vl = v + len; */
+}
 
 /* --- @mp_ensure@ --- *
  *
  * Arguments:  @mp *m@ = pointer to a multiprecision integer
- *             @size_t sz@ = required size
+ *             @size_t sz@ = required length
  *
  * Returns:    ---
  *
- * Use:                Ensures that the integer has enough space for @sz@ digits.
- *             The value is not changed.
+ * Use:                Changes an integer's length.  If there is not enough space
+ *             allocated for the new length then the size is increased.  It
  */
 
-void mp_ensure(mp *m, size_t sz) { MP_ENSURE(m, sz); }
+void mp_ensure(mp *m, size_t sz)
+{
+/*   size_t len = MP_LEN(m); */
+/*   if (sz >= len) { */
+/*     if (sz > m->sz) */
+/*       mp_resize(m, sz); */
+/*     if (!(m->f & MP_UNDEF) && sz > len) */
+/*       memset(m->vl, 0, MPWS(sz - len)); */
+/*     m->vl = m->v + sz; */
+/*   } */
+}
 
-/* --- @mp_modify@ --- *
+/* --- @mp_dest@ --- *
  *
- * Arguments:  @mp *m@ = pointer to a multiprecision integer
- *             @size_t sz@ = size required
+ * Arguments:  @mp *m@ = a suggested destination integer
+ *             @size_t sz@ = size required for result, in digits
+ *             @unsigned f@ = various flags
+ *
+ * Returns:    A pointer to an appropriate destination.
+ *
+ * Use:                Converts a suggested destination into a real destination with
+ *             the required properties.  If the real destination is @d@,
+ *             then the following properties will hold:
  *
- * Returns:    Pointer to the integer (possibly different).
+ *               * @d@ will have exactly one reference.
  *
- * Use:                Prepares an integer to be overwritten.  It's split off from
- *             other references to the same integer, and sufficient space is
- *             allocated.
+ *               * If @m@ is not @MP_NEW@, then the contents of @m@ will not
+ *                 change, unless @f@ has the @MP_UNDEF@ flag set.
+ *
+ *               * If @m@ is not @MP_NEW@, then he reference count of @m@ on
+ *                 entry is equal to the sum of the counts of @d@ and @m@ on
+ *                 exit.
+ *
+ *               * The size of @d@ will be at least @sz@.
+ *
+ *               * If @f@ has the @MP_BURN@ flag set, then @d@ will be
+ *                 allocated from @MPARENA_SECURE@.
+ *
+ *             Understanding this function is crucial to using Catacomb's
+ *             multiprecision integer library effectively.
  */
 
-mp *mp_modify(mp *m, size_t sz) { MP_MODIFY(m, sz); return (m); }
+mp *mp_dest(mp *m, size_t sz, unsigned f)
+{
+  /* --- If no destination, make one --- */
+
+  if (m == MP_NEWSEC)
+    m = mp_new(sz, f | MP_UNDEF | MP_BURN);
+  else if (m == MP_NEW)
+    m = mp_new(sz, f | MP_UNDEF);
+  else {
+    size_t len = MP_LEN(m);
+    unsigned undef = (m->f | f) & MP_UNDEF;
+
+    /* --- If the value must be preserved, the block can't shrink --- */
+
+    if (!undef && sz < len)
+      sz = len;
+
+    /* --- Otherwise check whether the destination is suitable --- */
+
+    if (m->ref > 1 || (m->f & MP_CONST) ||
+       m->sz > len || !((f & ~m->f) & MP_BURN)) {
+
+      /* --- No -- allocate a new buffer --- *
+       *
+       * The buffer must be secure if (a) the caller requested a secure
+       * buffer, or (b) the old buffer is secure and I'm not allowed to
+       * discard the old contents.
+       */
+      
+      mparena *a;
+      mpw *v;
+
+      if ((f & MP_BURN) || (!undef && (m->f & MP_BURN)))
+       a = MPARENA_SECURE;
+      else
+       a = MPARENA_GLOBAL;
+      v = mpalloc(a, sz);
+
+      /* --- Copy the data over --- */
+
+      if (!undef) {
+       memcpy(v, m->v, MPWS(len));
+       if (sz - len > 0)
+         memset(v + len, 0, MPWS(sz - len));
+      }
+
+      /* --- If @m@ has other references, make a new node --- *
+       *
+       * Otherwise dispose of the old buffer.
+       */
+
+      if (!(m->f & MP_CONST) && m->ref == 1) {
+       if (m->f & MP_BURN)
+         memset(m->v, 0, MPWS(m->sz));
+       mpfree(m->a, m->v);
+      } else {
+       mp *mm = CREATE(mp);
+       mm->ref = 1;
+       mm->f = m->f;
+       m->ref--;
+       m = mm;
+      }
+
+      /* --- Fix up the node --- */
+
+      m->v = v;
+      m->vl = v + sz;
+      m->sz = sz;
+      m->f = ((m->f & ~(MP_CONST | MP_BURN)) |
+             (f & (MP_BURN | MP_UNDEF)));
+      m->a = a;
+    }
+
+    /* --- If the number is growing in its buffer, fix it up --- */
+
+    else if (sz > len) {
+      if (!undef)
+       memset(m->vl, 0, MPWS(sz - len));
+      m->vl = m->v + sz;
+    }
+  }
+
+  /* --- Done --- */
+
+  return (m);
+}
 
 /*----- That's all, folks -------------------------------------------------*/