test_sballoc: make a big linked list, then free it one node at a time
authorRobert Haas <rhaas@postgresql.org>
Tue, 29 Apr 2014 11:36:24 +0000 (07:36 -0400)
committerRobert Haas <rhaas@postgresql.org>
Tue, 29 Apr 2014 11:36:24 +0000 (07:36 -0400)
contrib/test_sballoc/test_sballoc--1.0.sql
contrib/test_sballoc/test_sballoc.c

index 97e48d3e52859ea9d22d98e7a989bc554490873a..1cf8a5ae370bb2271a36fe30e3052af2cf77baef 100644 (file)
@@ -10,3 +10,11 @@ CREATE FUNCTION alloc(size pg_catalog.int8, count pg_catalog.int8)
 CREATE FUNCTION alloc_with_palloc(size pg_catalog.int8, count pg_catalog.int8)
     RETURNS pg_catalog.void
        AS 'MODULE_PATHNAME' LANGUAGE C STRICT;
+
+CREATE FUNCTION alloc_list(size pg_catalog.int8, count pg_catalog.int8)
+    RETURNS pg_catalog.void
+       AS 'MODULE_PATHNAME' LANGUAGE C STRICT;
+
+CREATE FUNCTION alloc_list_with_palloc(size pg_catalog.int8, count pg_catalog.int8)
+    RETURNS pg_catalog.void
+       AS 'MODULE_PATHNAME' LANGUAGE C STRICT;
index 607695d68170acd37df630a667f8069d4176d365..6a049d6fbeff9d49f5ad3b0ef242c11947749c22 100644 (file)
 #include "utils/sb_alloc.h"
 #include "utils/sb_region.h"
 
+typedef struct llnode
+{
+       struct llnode *next;
+} llnode;
+
 PG_MODULE_MAGIC;
 PG_FUNCTION_INFO_V1(alloc);
 PG_FUNCTION_INFO_V1(alloc_with_palloc);
-
-Datum          alloc(PG_FUNCTION_ARGS);
-Datum          alloc_with_palloc(PG_FUNCTION_ARGS);
+PG_FUNCTION_INFO_V1(alloc_list);
+PG_FUNCTION_INFO_V1(alloc_list_with_palloc);
 
 Datum
 alloc(PG_FUNCTION_ARGS)
@@ -70,3 +74,70 @@ alloc_with_palloc(PG_FUNCTION_ARGS)
 
        PG_RETURN_VOID();
 }
+
+Datum
+alloc_list(PG_FUNCTION_ARGS)
+{
+       int64 size = PG_GETARG_INT64(0);
+       int64 count = PG_GETARG_INT64(1);
+       int64 i;
+       llnode *h = NULL;
+       llnode *p;
+       sb_allocator *a;
+
+       if (size < sizeof(llnode))
+               elog(ERROR, "size too small");
+
+       a = sb_create_private_allocator();
+       for (i = 0; i < count; ++i)
+       {
+               p = sb_alloc(a, size, 0);
+               p->next = h;
+               h = p;
+       }
+       while (h != NULL)
+       {
+               p = h->next;
+               sb_free(h);
+               h = p;
+       }
+       sb_dump_regions();
+
+       PG_RETURN_VOID();
+}
+
+Datum
+alloc_list_with_palloc(PG_FUNCTION_ARGS)
+{
+       int64 size = PG_GETARG_INT64(0);
+       int64 count = PG_GETARG_INT64(1);
+       int64 i;
+       llnode *h = NULL;
+       llnode *p;
+       MemoryContext context;
+
+       if (size < sizeof(llnode))
+               elog(ERROR, "size too small");
+
+       context = AllocSetContextCreate(CurrentMemoryContext,
+                                                                   "alloc_list_with_palloc test",
+                                                                   ALLOCSET_DEFAULT_MINSIZE,
+                                                                   ALLOCSET_DEFAULT_INITSIZE,
+                                                                   ALLOCSET_DEFAULT_MAXSIZE);
+       for (i = 0; i < count; ++i)
+       {
+               p = MemoryContextAlloc(context, size);
+               p->next = h;
+               h = p;
+       }
+       while (h != NULL)
+       {
+               p = h->next;
+               pfree(h);
+               h = p;
+       }
+       MemoryContextStats(context);
+       MemoryContextDelete(context);
+
+       PG_RETURN_VOID();
+}