Added a --enable-debug option to configure and
[pspp-builds.git] / src / pool.c
index bfb93b4bf17d7a5bc147ecbe30a385a8bc58a8d9..0f5663d1f064c5b6813d86dd3644c2bdf687c51a 100644 (file)
@@ -102,22 +102,18 @@ union align
 #define ALIGN_SIZE sizeof (union align)
 #endif
 
-/* DISCRETE_BLOCKS may be declared as nonzero to prevent suballocation
-   of blocks.  This is useful under memory debuggers like Checker
-   because it allows the source location of bugs to be more accurately
-   pinpointed.
+/* DISCRETE_BLOCKS may be declared as nonzero to prevent
+   suballocation of blocks.  This is useful under memory
+   debuggers like Checker or valgrind because it allows the
+   source location of bugs to be more accurately pinpointed.
 
    On the other hand, if we're testing the library, then we want to
    test the library's real functionality, not its crippled, slow,
    simplified functionality. */
-#if __CHECKER__  && !SELF_TEST
-#define DISCRETE_BLOCKS 1
-#endif
+/*#define DISCRETE_BLOCKS 1*/
 
 /* Enable debug code if appropriate. */
-#undef DEBUGGING
 #if SELF_TEST
-#define DEBUGGING 1
 #endif
 
 /* Size of each block allocated in the pool, in bytes.
@@ -219,7 +215,7 @@ pool_alloc (struct pool *pool, size_t amt)
 {
   assert (pool != NULL);
   
-#if !DISCRETE_BLOCKS /* Help identify source of bugs for Checker users. */
+#if !DISCRETE_BLOCKS
   if (amt <= MAX_SUBALLOC)
     {
       struct pool_block *b = pool->blocks;
@@ -246,34 +242,48 @@ pool_alloc (struct pool *pool, size_t amt)
     return pool_malloc (pool, amt);
 }
 
-/* Duplicates STRING within POOL and returns a pointer to the
-   duplicate. */
+/* Duplicates STRING, which has LENGTH characters, within POOL,
+   and returns a pointer to the duplicate.  LENGTH should not
+   include the null terminator, which is always added to the
+   duplicate.  For use only with strings, because the returned
+   pointere may not be aligned properly for other types. */
 char *
-pool_strdup (struct pool *pool, const char *string)
+pool_strndup (struct pool *pool, const char *string, size_t length)
 {
-  size_t amt;
-  void *p;
+  size_t size;
+  char *copy;
 
   assert (pool && string);
-  amt = strlen (string) + 1;
+  size = length + 1;
 
   /* Note that strings need not be aligned on any boundary. */
   {
 #if !DISCRETE_BLOCKS
     struct pool_block *const b = pool->blocks;
 
-    if (b->ofs + amt <= BLOCK_SIZE)
+    if (b->ofs + size <= BLOCK_SIZE)
       {
-       p = ((char *) b) + b->ofs;
-       b->ofs += amt;
+       copy = ((char *) b) + b->ofs;
+       b->ofs += size;
       }
     else
 #endif
-      p = pool_alloc (pool, amt);
+      copy = pool_alloc (pool, size);
   }
 
-  memcpy (p, string, amt);
-  return p;
+  memcpy (copy, string, length);
+  copy[length] = '\0';
+  return copy;
+}
+
+/* Duplicates null-terminated STRING, within POOL, and returns a
+   pointer to the duplicate.  For use only with strings, because
+   the returned pointere may not be aligned properly for other
+   types. */
+char *
+pool_strdup (struct pool *pool, const char *string) 
+{
+  return pool_strndup (pool, string, strlen (string));
 }
 \f
 /* Standard allocation routines. */