return idx;
}
+#ifdef CLOBBER_FREED_MEMORY
+
+/* Wipe freed memory for debugging purposes */
+static void
+wipe_mem(void *ptr, size_t size)
+{
+ memset(ptr, 0x7F, size);
+}
+#endif
+
+#ifdef MEMORY_CONTEXT_CHECKING
+static void
+set_sentinel(void *base, Size offset)
+{
+ char *ptr = (char *) base + offset;
+
+ *ptr = 0x7E;
+}
+
+static bool
+sentinel_ok(const void *base, Size offset)
+{
+ const char *ptr = (const char *) base + offset;
+ bool ret;
+
+ ret = *ptr == 0x7E;
+
+ return ret;
+}
+#endif
+
#ifdef RANDOMIZE_ALLOCATED_MEMORY
/*
char *datastart = ((char *) block) + ALLOC_BLOCKHDRSZ;
#ifdef CLOBBER_FREED_MEMORY
- /* Wipe freed memory for debugging purposes */
- memset(datastart, 0x7F, block->freeptr - datastart);
+ wipe_mem(datastart, block->freeptr - datastart);
#endif
block->freeptr = datastart;
block->next = NULL;
{
/* Normal case, release the block */
#ifdef CLOBBER_FREED_MEMORY
- /* Wipe freed memory for debugging purposes */
- memset(block, 0x7F, block->freeptr - ((char *) block));
+ wipe_mem(block, block->freeptr - ((char *) block));
#endif
free(block);
}
AllocBlock next = block->next;
#ifdef CLOBBER_FREED_MEMORY
- /* Wipe freed memory for debugging purposes */
- memset(block, 0x7F, block->freeptr - ((char *) block));
+ wipe_mem(block, block->freeptr - ((char *) block));
#endif
free(block);
block = next;
chunk->requested_size = size;
/* set mark to catch clobber of "unused" space */
if (size < chunk_size)
- ((char *) AllocChunkGetPointer(chunk))[size] = 0x7E;
+ set_sentinel(AllocChunkGetPointer(chunk), size);
#endif
#ifdef RANDOMIZE_ALLOCATED_MEMORY
/* fill the allocated space with junk */
chunk->requested_size = size;
/* set mark to catch clobber of "unused" space */
if (size < chunk->size)
- ((char *) AllocChunkGetPointer(chunk))[size] = 0x7E;
+ set_sentinel(AllocChunkGetPointer(chunk), size);
#endif
#ifdef RANDOMIZE_ALLOCATED_MEMORY
/* fill the allocated space with junk */
chunk->requested_size = size;
/* set mark to catch clobber of "unused" space */
if (size < chunk->size)
- ((char *) AllocChunkGetPointer(chunk))[size] = 0x7E;
+ set_sentinel(AllocChunkGetPointer(chunk), size);
#endif
#ifdef RANDOMIZE_ALLOCATED_MEMORY
/* fill the allocated space with junk */
#ifdef MEMORY_CONTEXT_CHECKING
/* Test for someone scribbling on unused space in chunk */
if (chunk->requested_size < chunk->size)
- if (((char *) pointer)[chunk->requested_size] != 0x7E)
+ if (!sentinel_ok(pointer, chunk->requested_size))
elog(WARNING, "detected write past chunk end in %s %p",
set->header.name, chunk);
#endif
else
prevblock->next = block->next;
#ifdef CLOBBER_FREED_MEMORY
- /* Wipe freed memory for debugging purposes */
- memset(block, 0x7F, block->freeptr - ((char *) block));
+ wipe_mem(block, block->freeptr - ((char *) block));
#endif
free(block);
}
chunk->aset = (void *) set->freelist[fidx];
#ifdef CLOBBER_FREED_MEMORY
- /* Wipe freed memory for debugging purposes */
- memset(pointer, 0x7F, chunk->size);
+ wipe_mem(pointer, chunk->size);
#endif
#ifdef MEMORY_CONTEXT_CHECKING
#ifdef MEMORY_CONTEXT_CHECKING
/* Test for someone scribbling on unused space in chunk */
if (chunk->requested_size < oldsize)
- if (((char *) pointer)[chunk->requested_size] != 0x7E)
+ if (!sentinel_ok(pointer, chunk->requested_size))
elog(WARNING, "detected write past chunk end in %s %p",
set->header.name, chunk);
#endif
chunk->requested_size = size;
/* set mark to catch clobber of "unused" space */
if (size < oldsize)
- ((char *) pointer)[size] = 0x7E;
+ set_sentinel(pointer, size);
#endif
return pointer;
}
chunk->requested_size = size;
/* set mark to catch clobber of "unused" space */
if (size < chunk->size)
- ((char *) AllocChunkGetPointer(chunk))[size] = 0x7E;
+ set_sentinel(AllocChunkGetPointer(chunk), size);
#endif
return AllocChunkGetPointer(chunk);
AllocChunk chunk = (AllocChunk) bpoz;
Size chsize,
dsize;
- char *chdata_end;
chsize = chunk->size; /* aligned chunk size */
dsize = chunk->requested_size; /* real data */
- chdata_end = ((char *) chunk) + (ALLOC_CHUNKHDRSZ + dsize);
/*
* Check chunk size
/*
* Check for overwrite of "unallocated" space in chunk
*/
- if (dsize > 0 && dsize < chsize && *chdata_end != 0x7E)
+ if (dsize > 0 && dsize < chsize &&
+ !sentinel_ok(chunk, ALLOC_CHUNKHDRSZ + dsize))
elog(WARNING, "problem in alloc set %s: detected write past chunk end in block %p, chunk %p",
name, block, chunk);
void *
MemoryContextAlloc(MemoryContext context, Size size)
{
+ void *ret;
+
AssertArg(MemoryContextIsValid(context));
if (!AllocSizeIsValid(size))
context->isReset = false;
- return (*context->methods->alloc) (context, size);
+ ret = (*context->methods->alloc) (context, size);
+
+ return ret;
}
/*
palloc(Size size)
{
/* duplicates MemoryContextAlloc to avoid increased overhead */
+ void *ret;
+
AssertArg(MemoryContextIsValid(CurrentMemoryContext));
if (!AllocSizeIsValid(size))
CurrentMemoryContext->isReset = false;
- return (*CurrentMemoryContext->methods->alloc) (CurrentMemoryContext, size);
+ ret = (*CurrentMemoryContext->methods->alloc) (CurrentMemoryContext, size);
+
+ return ret;
}
void *
void
pfree(void *pointer)
{
- StandardChunkHeader *header;
+ MemoryContext context;
/*
* Try to detect bogus pointers handed to us, poorly though we can.
/*
* OK, it's probably safe to look at the chunk header.
*/
- header = (StandardChunkHeader *)
- ((char *) pointer - STANDARDCHUNKHEADERSIZE);
+ context = ((StandardChunkHeader *)
+ ((char *) pointer - STANDARDCHUNKHEADERSIZE))->context;
- AssertArg(MemoryContextIsValid(header->context));
+ AssertArg(MemoryContextIsValid(context));
- (*header->context->methods->free_p) (header->context, pointer);
+ (*context->methods->free_p) (context, pointer);
}
/*
void *
repalloc(void *pointer, Size size)
{
- StandardChunkHeader *header;
+ MemoryContext context;
+ void *ret;
+
+ if (!AllocSizeIsValid(size))
+ elog(ERROR, "invalid memory alloc request size %lu",
+ (unsigned long) size);
/*
* Try to detect bogus pointers handed to us, poorly though we can.
/*
* OK, it's probably safe to look at the chunk header.
*/
- header = (StandardChunkHeader *)
- ((char *) pointer - STANDARDCHUNKHEADERSIZE);
+ context = ((StandardChunkHeader *)
+ ((char *) pointer - STANDARDCHUNKHEADERSIZE))->context;
- AssertArg(MemoryContextIsValid(header->context));
-
- if (!AllocSizeIsValid(size))
- elog(ERROR, "invalid memory alloc request size %lu",
- (unsigned long) size);
+ AssertArg(MemoryContextIsValid(context));
/* isReset must be false already */
- Assert(!header->context->isReset);
+ Assert(!context->isReset);
+
+ ret = (*context->methods->realloc) (context, pointer, size);
- return (*header->context->methods->realloc) (header->context,
- pointer, size);
+ return ret;
}
/*