/* * MemoryContextAllocZeroAligned * MemoryContextAllocZero where length is suitable for MemSetLoop * * This might seem overly specialized, but it's not because newNode() * is so often called with compile-time-constant sizes. */ void * MemoryContextAllocZeroAlignedImpl(MemoryContext context, Size size, const char* sfile, const char *sfunc, int sline) { void *ret; #ifdef PGTRACE_ENABLED StandardChunkHeader *header; #endif AssertArg(MemoryContextIsValid(context)); #ifdef CDB_PALLOC_CALLER_ID context->callerFile = sfile; context->callerLine = sline; #endif if (!AllocSizeIsValid(size)) MemoryContextError(ERRCODE_INTERNAL_ERROR, context, CDB_MCXT_WHERE(context), "invalid memory alloc request size %lu", (unsigned long)size); ret = (*context->methods.alloc) (context, size); MemSetLoop(ret, 0, size); #ifdef PGTRACE_ENABLED header = (StandardChunkHeader *) ((char *) ret - STANDARDCHUNKHEADERSIZE); PG_TRACE5(memctxt__alloc, size, header->size, 0, 0, (long) context->name); #endif return ret; }
/* * AsetDirectAlloc * Returns pointer to allocated memory of given size; memory is added * to the set. */ static void * AsetDirectAlloc(MemoryContext context, Size size) { AsetDirectContext *set = (AsetDirectContext *)context; CdbPtrBuf_Ptr *pp; Assert(set && IsA(set, AsetDirectContext)); if (size < MAXALIGN(1)) size = MAXALIGN(1); /* Obtain a slot in 'areas' collection to point to the new allocation. */ pp = CdbPtrBuf_Append(&set->areas, NULL); /* Allocate the memory. */ *pp = malloc(size); if (!*pp) MemoryContextError(ERRCODE_OUT_OF_MEMORY, &set->header, CDB_MCXT_WHERE(&set->header), "Out of memory. Failed on request of size %lu bytes.", (unsigned long)size); /* Update statistics. */ set->size_total += size; set->narea_total++; MemoryContextNoteAlloc(&set->header, size); AllocAllocInfo(set, chunk); return *pp; } /* AsetDirectAlloc */
/* * repalloc * Adjust the size of a previously allocated chunk. */ void * MemoryContextReallocImpl(void *pointer, Size size, const char *sfile, const char *sfunc, int sline) { StandardChunkHeader *header; void *ret; #ifdef PGTRACE_ENABLED long old_reqsize; long old_size; #endif /* * Try to detect bogus pointers handed to us, poorly though we can. * Presumably, a pointer that isn't MAXALIGNED isn't pointing at an * allocated chunk. */ Assert(pointer != NULL); Assert(pointer == (void *) MAXALIGN(pointer)); /* * OK, it's probably safe to look at the chunk header. */ header = (StandardChunkHeader *) ((char *) pointer - STANDARDCHUNKHEADERSIZE); AssertArg(MemoryContextIsValid(header->sharedHeader->context)); #ifdef PGTRACE_ENABLED #ifdef MEMORY_CONTEXT_CHECKING old_reqsize = header->requested_size; #else old_reqsize = 0; #endif old_size = header->size; #endif #ifdef CDB_PALLOC_CALLER_ID header->sharedHeader->context->callerFile = sfile; header->sharedHeader->context->callerLine = sline; #endif if (!AllocSizeIsValid(size)) MemoryContextError(ERRCODE_INTERNAL_ERROR, header->sharedHeader->context, CDB_MCXT_WHERE(header->sharedHeader->context), "invalid memory alloc request size %lu", (unsigned long)size); ret = (*header->sharedHeader->context->methods.realloc) (header->sharedHeader->context, pointer, size); #ifdef PGTRACE_ENABLED header = (StandardChunkHeader *) ((char *) ret - STANDARDCHUNKHEADERSIZE); PG_TRACE5(memctxt__realloc, size, header->size, old_reqsize, old_size, (long) header->sharedHeader->context->name); #endif return ret; }