Update of /cvsroot/monetdb/pathfinder/compiler/mem
In directory sc8-pr-cvs16.sourceforge.net:/tmp/cvs-serv24398/mem

Modified Files:
        array.c bitset.c 
Log Message:
-- Extended the PFarray_t type to store a ``clear'' bit
   that ensures that memory is erased during allocation.

-- Split up PFarray (size_t itemsize) into four variants

   o PFarray_default (size_t itemsize),
   o PFcarray_default (size_t itemsize),
   o PFarray (size_t itemsize, unsigned int slots), and
   o PFcarray (size_t itemsize, unsigned int slots)

   where the '_default' variants are currently seeded with 20 slots,
   the 'c' variants provide a cleared chunk of memory, and the slots
   argument indicates how much memory is initially allocated (#slots).

-- Added memory debugging support for arrays:
   If the environment variable ``PF_DEBUG_MEMORY'' is set the memory
   reallocation for arrays reports the function that requires more
   memory (and thus more slots for the storage).


REMARK: Currently the inital array sizes (#slots) are good guesses
        about the upper limit of required slots. Anybody adding new
        PFarray calls may start with a very low value. This way the
        the debug printing may report (reallocation) problems in
        comparison to a very high initial value whose consequences
        might not be directly visible.



Index: array.c
===================================================================
RCS file: /cvsroot/monetdb/pathfinder/compiler/mem/array.c,v
retrieving revision 1.26
retrieving revision 1.27
diff -u -d -r1.26 -r1.27
--- array.c     24 Feb 2008 17:22:25 -0000      1.26
+++ array.c     17 Mar 2008 17:41:24 -0000      1.27
@@ -7,8 +7,8 @@
  * does not yield an error but lets the array grow as needed.
  *
  * Basic interface:
- * - #PFarray (s)       create new array with elements of s bytes length each,
- *                      set array ``append index'' to 0
+ *  - PFarray (s,sl)    create new array with elements of s bytes length each
+ *                      and initially sl slots, set array ``append index'' to 0
  * - #PFarray_at (a, i) address of ith element in array a (a grows as needed)
  * - #PFarray_last (a)  ``append index'': where to append to this array (r/w)
  *
@@ -70,10 +70,16 @@
 #include "oops.h"
 
 /** 
- * size (in bytes) allocated whenever a fresh array is allocated or
+ * size (in bytes) rounded up to whenever a fresh array is allocated or
  * an out-of-bounds index has been accessed 
  */
-#define ACHUNK 2048U
+#define ACHUNK 8U
+
+#ifndef NDEBUG
+/* switch to turn debug printing
+   of memory reallocations on/off */
+bool debug_memory; 
+#endif
 
 /**
  * Create a fresh dynamic array, prepared to hold elements of byte size
@@ -83,20 +89,24 @@
  * @return fresh dynamic array (or 0 in case of errors)
  */
 PFarray_t *
-PFarray (size_t s)
+PFarray_ (size_t s, unsigned int slots, bool clear)
 {
   PFarray_t *a;
   size_t nbytes;
 
   a = (PFarray_t *) PFmalloc (sizeof (PFarray_t));
 
-  /* round up to nearest multiple of ACHUNK larger than s */
-  nbytes = (s + ACHUNK) & (~ACHUNK + 1);
+  /* round up to nearest multiple of ACHUNK larger than s * slots */
+  nbytes = (s * slots + ACHUNK) & (~ACHUNK + 1);
 
   a->base = PFmalloc (nbytes);
 
+  if (clear)
+      memset (a->base, 0, nbytes);
+
   a->bound = nbytes / s;
   a->esize = s;
+  a->clear = clear;
 
   /* 0 indicates emptiness (see macro PFarray_empty ()) */
   a->appi  = 0;
@@ -113,22 +123,47 @@
  * @return array element address (or 0, in case of errors)
  */
 void * 
-PFarray_at (PFarray_t *a, unsigned int i) 
+#ifndef NDEBUG
+PFarray_at_ (PFarray_t *a, unsigned int i,
+             const char *file, const char *func, const int line) 
+#else
+PFarray_at (PFarray_t *a, unsigned int i)
+#endif
 { 
   size_t nbytes;
 
   assert (a);
 
   if (i >= a->bound) {
-    /* out-of-bounds index access, 
-     * grow array such that index position i becomes valid 
-     */
-    nbytes = ((i + 1) * a->esize + ACHUNK) & (~ACHUNK + 1);
+#ifndef NDEBUG
+      if (debug_memory &&
+          /* The following functions have been checked and the initial
+             sizes appear to be bigger than the average sizes. */
+          strcmp (func, "la_cse") != 0 &&
+          strcmp (func, "PFarray_nadd") != 0 &&
+          strcmp (func, "add_subdom") != 0)
+          PFlog ("array not big enough"
+                 " in %s, %s(), line %d"
+                 " (required slots: %i; got: %i)",
+                 file, func, line, i, a->bound-1);
+#endif
 
-    assert(a->base);
-    a->base = PFrealloc (a->base, a->bound*a->esize, nbytes);
+      if (i > 2 * a->bound)
+          /* out-of-bounds index access, 
+           * grow array such that index position i becomes valid 
+           */
+          nbytes = ((i + 1) * a->esize + ACHUNK) & (~ACHUNK + 1);
+      else
+          /* amortize reallocation costs for small chunks */
+          nbytes = 2 * a->bound * a->esize;
 
-    a->bound = nbytes / a->esize;
+      assert(a->base);
+      a->base = PFrealloc (a->base, a->bound*a->esize, nbytes);
+
+      if (a->clear)
+          memset (a->base, 0, nbytes);
+
+      a->bound = nbytes / a->esize;
   }
 
   /* return address of requested index position i */
@@ -287,7 +322,7 @@
     assert (input);
 
     unsigned size = PFarray_last (input);
-    PFarray_t *output = PFarray (input->esize);
+    PFarray_t *output = PFarray_ (input->esize, input->bound, input->clear);
 
     if (size) {
         PFarray_nadd (output, size);
@@ -313,4 +348,14 @@
         (a->appi)--;
 }
 
+#ifndef NDEBUG
+/**
+ * Look up the environment variable only once.
+ */
+void PFarray_init (void)
+{
+    debug_memory = getenv("PF_DEBUG_MEMORY") != NULL;
+}
+#endif
+
 /* vim:set shiftwidth=4 expandtab: */

Index: bitset.c
===================================================================
RCS file: /cvsroot/monetdb/pathfinder/compiler/mem/bitset.c,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -d -r1.6 -r1.7
--- bitset.c    11 Jan 2008 10:47:10 -0000      1.6
+++ bitset.c    17 Mar 2008 17:41:24 -0000      1.7
@@ -59,7 +59,7 @@
 
 PFbitset_t *PFbitset (void) 
 {
-    return PFarray (sizeof (bitset_unit)); 
+    return PFarray (sizeof (bitset_unit), 128 /* start with 1024 bits */);
 }
 
 void PFbitset_set (PFbitset_t *b, unsigned int pos, bool value)


-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Monetdb-pf-checkins mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/monetdb-pf-checkins

Reply via email to