-- complain if script is sourced in psql, rather than via CREATE EXTENSION
\echo Use "CREATE EXTENSION test_balloc" to load this file. \quit
-CREATE FUNCTION alloc(size pg_catalog.int8, count pg_catalog.int8)
+CREATE FUNCTION alloc(size pg_catalog.int8, count pg_catalog.int8,
+ usenew pg_catalog.bool default true)
RETURNS pg_catalog.void
AS 'MODULE_PATHNAME' LANGUAGE C STRICT;
-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)
+CREATE FUNCTION alloc_list(size pg_catalog.int8, count pg_catalog.int8,
+ usenew pg_catalog.bool default true)
RETURNS pg_catalog.void
AS 'MODULE_PATHNAME' LANGUAGE C STRICT;
PG_MODULE_MAGIC;
PG_FUNCTION_INFO_V1(alloc);
-PG_FUNCTION_INFO_V1(alloc_with_palloc);
PG_FUNCTION_INFO_V1(alloc_list);
-PG_FUNCTION_INFO_V1(alloc_list_with_palloc);
Datum
alloc(PG_FUNCTION_ARGS)
{
int64 size = PG_GETARG_INT64(0);
int64 count = PG_GETARG_INT64(1);
- int64 i;
- int64 *p;
- BlockAllocatorContext *context;
-
- context = BlockAllocatorContextCreate();
- for (i = 0; i < count; ++i)
- {
- p = BlockAllocatorAlloc(context, size, 0);
- *p = i;
- }
- BlockAllocatorReset(context);
- DumpAllocatorRegions();
-
- PG_RETURN_VOID();
-}
-
-Datum
-alloc_with_palloc(PG_FUNCTION_ARGS)
-{
- int64 size = PG_GETARG_INT64(0);
- int64 count = PG_GETARG_INT64(1);
+ bool usenew = PG_GETARG_INT64(2);
int64 i;
int64 *p;
MemoryContext context;
- context = AllocSetContextCreate(CurrentMemoryContext,
- "alloc_with_palloc test",
- ALLOCSET_DEFAULT_MINSIZE,
- ALLOCSET_DEFAULT_INITSIZE,
- ALLOCSET_DEFAULT_MAXSIZE);
+ if (usenew)
+ context = BlockAllocatorContextCreate(CurrentMemoryContext, "test");
+ else
+ context = AllocSetContextCreate(CurrentMemoryContext,
+ "alloc test",
+ ALLOCSET_DEFAULT_MINSIZE,
+ ALLOCSET_DEFAULT_INITSIZE,
+ ALLOCSET_DEFAULT_MAXSIZE);
+
for (i = 0; i < count; ++i)
{
p = MemoryContextAlloc(context, size);
*p = i;
}
- MemoryContextStats(context);
- MemoryContextDelete(context);
-
- 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;
- BlockAllocatorContext *context;
-
- if (size < sizeof(llnode))
- elog(ERROR, "size too small");
-
- context = BlockAllocatorContextCreate();
- for (i = 0; i < count; ++i)
+ if (usenew)
{
- p = BlockAllocatorAlloc(context, size, 0);
- p->next = h;
- h = p;
+ BlockAllocatorReset(context);
+ DumpAllocatorRegions();
}
- while (h != NULL)
+ else
{
- p = h->next;
- BlockAllocatorFree(h);
- h = p;
+ MemoryContextStats(context);
+ MemoryContextDelete(context);
}
- DumpAllocatorRegions();
- BlockAllocatorReset(context);
PG_RETURN_VOID();
}
Datum
-alloc_list_with_palloc(PG_FUNCTION_ARGS)
+alloc_list(PG_FUNCTION_ARGS)
{
int64 size = PG_GETARG_INT64(0);
int64 count = PG_GETARG_INT64(1);
+ bool usenew = PG_GETARG_INT64(2);
int64 i;
llnode *h = NULL;
llnode *p;
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);
+ if (usenew)
+ context = BlockAllocatorContextCreate(CurrentMemoryContext, "test");
+ else
+ context = AllocSetContextCreate(CurrentMemoryContext,
+ "alloc test",
+ ALLOCSET_DEFAULT_MINSIZE,
+ ALLOCSET_DEFAULT_INITSIZE,
+ ALLOCSET_DEFAULT_MAXSIZE);
+
for (i = 0; i < count; ++i)
{
p = MemoryContextAlloc(context, size);
pfree(h);
h = p;
}
- MemoryContextStats(context);
- MemoryContextDelete(context);
+ if (usenew)
+ {
+ DumpAllocatorRegions();
+ BlockAllocatorReset(context);
+ }
+ else
+ {
+ MemoryContextStats(context);
+ MemoryContextDelete(context);
+ }
PG_RETURN_VOID();
}