mirror of
https://github.com/postgres/postgres.git
synced 2025-07-09 22:41:56 +03:00
Add palloc_extended for frontend and backend.
This commit also adds pg_malloc_extended for frontend. These interfaces can be used to control at a lower level memory allocation using an interface similar to MemoryContextAllocExtended. For example, the callers can specify MCXT_ALLOC_NO_OOM if they want to suppress the "out of memory" error while allocating the memory and handle a NULL return value. Michael Paquier, reviewed by me.
This commit is contained in:
@ -19,8 +19,8 @@
|
||||
|
||||
#include "postgres_fe.h"
|
||||
|
||||
void *
|
||||
pg_malloc(size_t size)
|
||||
static inline void *
|
||||
pg_malloc_internal(size_t size, int flags)
|
||||
{
|
||||
void *tmp;
|
||||
|
||||
@ -28,22 +28,37 @@ pg_malloc(size_t size)
|
||||
if (size == 0)
|
||||
size = 1;
|
||||
tmp = malloc(size);
|
||||
if (!tmp)
|
||||
if (tmp == NULL)
|
||||
{
|
||||
fprintf(stderr, _("out of memory\n"));
|
||||
exit(EXIT_FAILURE);
|
||||
if ((flags & MCXT_ALLOC_NO_OOM) == 0)
|
||||
{
|
||||
fprintf(stderr, _("out of memory\n"));
|
||||
exit(EXIT_FAILURE);
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if ((flags & MCXT_ALLOC_ZERO) != 0)
|
||||
MemSet(tmp, 0, size);
|
||||
return tmp;
|
||||
}
|
||||
|
||||
void *
|
||||
pg_malloc(size_t size)
|
||||
{
|
||||
return pg_malloc_internal(size, 0);
|
||||
}
|
||||
|
||||
void *
|
||||
pg_malloc0(size_t size)
|
||||
{
|
||||
void *tmp;
|
||||
return pg_malloc_internal(size, MCXT_ALLOC_ZERO);
|
||||
}
|
||||
|
||||
tmp = pg_malloc(size);
|
||||
MemSet(tmp, 0, size);
|
||||
return tmp;
|
||||
void *
|
||||
pg_malloc_extended(size_t size, int flags)
|
||||
{
|
||||
return pg_malloc_internal(size, flags);
|
||||
}
|
||||
|
||||
void *
|
||||
@ -100,13 +115,19 @@ pg_free(void *ptr)
|
||||
void *
|
||||
palloc(Size size)
|
||||
{
|
||||
return pg_malloc(size);
|
||||
return pg_malloc_internal(size, 0);
|
||||
}
|
||||
|
||||
void *
|
||||
palloc0(Size size)
|
||||
{
|
||||
return pg_malloc0(size);
|
||||
return pg_malloc_internal(size, MCXT_ALLOC_ZERO);
|
||||
}
|
||||
|
||||
void *
|
||||
palloc_extended(Size size, int flags)
|
||||
{
|
||||
return pg_malloc_internal(size, flags);
|
||||
}
|
||||
|
||||
void
|
||||
|
Reference in New Issue
Block a user