File tree Expand file tree Collapse file tree 4 files changed +85
-12
lines changed Expand file tree Collapse file tree 4 files changed +85
-12
lines changed Original file line number Diff line number Diff line change @@ -864,6 +864,43 @@ palloc0(Size size)
864864 return ret ;
865865}
866866
867+ void *
868+ palloc_extended (Size size , int flags )
869+ {
870+ /* duplicates MemoryContextAllocExtended to avoid increased overhead */
871+ void * ret ;
872+
873+ AssertArg (MemoryContextIsValid (CurrentMemoryContext ));
874+ AssertNotInCriticalSection (CurrentMemoryContext );
875+
876+ if (((flags & MCXT_ALLOC_HUGE ) != 0 && !AllocHugeSizeIsValid (size )) ||
877+ ((flags & MCXT_ALLOC_HUGE ) == 0 && !AllocSizeIsValid (size )))
878+ elog (ERROR , "invalid memory alloc request size %zu" , size );
879+
880+ CurrentMemoryContext -> isReset = false;
881+
882+ ret = (* CurrentMemoryContext -> methods -> alloc ) (CurrentMemoryContext , size );
883+ if (ret == NULL )
884+ {
885+ if ((flags & MCXT_ALLOC_NO_OOM ) == 0 )
886+ {
887+ MemoryContextStats (TopMemoryContext );
888+ ereport (ERROR ,
889+ (errcode (ERRCODE_OUT_OF_MEMORY ),
890+ errmsg ("out of memory" ),
891+ errdetail ("Failed on request of size %zu." , size )));
892+ }
893+ return NULL ;
894+ }
895+
896+ VALGRIND_MEMPOOL_ALLOC (CurrentMemoryContext , ret , size );
897+
898+ if ((flags & MCXT_ALLOC_ZERO ) != 0 )
899+ MemSetAligned (ret , 0 , size );
900+
901+ return ret ;
902+ }
903+
867904/*
868905 * pfree
869906 * Release an allocated chunk.
Original file line number Diff line number Diff line change 1919
2020#include "postgres_fe.h"
2121
22- void *
23- pg_malloc (size_t size )
22+ static inline void *
23+ pg_malloc_internal (size_t size , int flags )
2424{
2525 void * tmp ;
2626
2727 /* Avoid unportable behavior of malloc(0) */
2828 if (size == 0 )
2929 size = 1 ;
3030 tmp = malloc (size );
31- if (! tmp )
31+ if (tmp == NULL )
3232 {
33- fprintf (stderr , _ ("out of memory\n" ));
34- exit (EXIT_FAILURE );
33+ if ((flags & MCXT_ALLOC_NO_OOM ) == 0 )
34+ {
35+ fprintf (stderr , _ ("out of memory\n" ));
36+ exit (EXIT_FAILURE );
37+ }
38+ return NULL ;
3539 }
40+
41+ if ((flags & MCXT_ALLOC_ZERO ) != 0 )
42+ MemSet (tmp , 0 , size );
3643 return tmp ;
3744}
3845
46+ void *
47+ pg_malloc (size_t size )
48+ {
49+ return pg_malloc_internal (size , 0 );
50+ }
51+
3952void *
4053pg_malloc0 (size_t size )
4154{
42- void * tmp ;
55+ return pg_malloc_internal (size , MCXT_ALLOC_ZERO );
56+ }
4357
44- tmp = pg_malloc (size );
45- MemSet (tmp , 0 , size );
46- return tmp ;
58+ void *
59+ pg_malloc_extended (size_t size , int flags )
60+ {
61+ return pg_malloc_internal (size , flags );
4762}
4863
4964void *
@@ -100,13 +115,19 @@ pg_free(void *ptr)
100115void *
101116palloc (Size size )
102117{
103- return pg_malloc (size );
118+ return pg_malloc_internal (size , 0 );
104119}
105120
106121void *
107122palloc0 (Size size )
108123{
109- return pg_malloc0 (size );
124+ return pg_malloc_internal (size , MCXT_ALLOC_ZERO );
125+ }
126+
127+ void *
128+ palloc_extended (Size size , int flags )
129+ {
130+ return pg_malloc_internal (size , flags );
110131}
111132
112133void
Original file line number Diff line number Diff line change 99#ifndef FE_MEMUTILS_H
1010#define FE_MEMUTILS_H
1111
12- /* "Safe" memory allocation functions --- these exit(1) on failure */
12+ /*
13+ * Flags for pg_malloc_extended and palloc_extended, deliberately named
14+ * the same as the backend flags.
15+ */
16+ #define MCXT_ALLOC_HUGE 0x01 /* allow huge allocation (> 1 GB)
17+ * not actually used for frontends */
18+ #define MCXT_ALLOC_NO_OOM 0x02 /* no failure if out-of-memory */
19+ #define MCXT_ALLOC_ZERO 0x04 /* zero allocated memory */
20+
21+ /*
22+ * "Safe" memory allocation functions --- these exit(1) on failure
23+ * (except pg_malloc_extended with MCXT_ALLOC_NO_OOM)
24+ */
1325extern char * pg_strdup (const char * in );
1426extern void * pg_malloc (size_t size );
1527extern void * pg_malloc0 (size_t size );
28+ extern void * pg_malloc_extended (size_t size , int flags );
1629extern void * pg_realloc (void * pointer , size_t size );
1730extern void pg_free (void * pointer );
1831
1932/* Equivalent functions, deliberately named the same as backend functions */
2033extern char * pstrdup (const char * in );
2134extern void * palloc (Size size );
2235extern void * palloc0 (Size size );
36+ extern void * palloc_extended (Size size , int flags );
2337extern void * repalloc (void * pointer , Size size );
2438extern void pfree (void * pointer );
2539
Original file line number Diff line number Diff line change @@ -76,6 +76,7 @@ extern void *MemoryContextAllocExtended(MemoryContext context,
7676
7777extern void * palloc (Size size );
7878extern void * palloc0 (Size size );
79+ extern void * palloc_extended (Size size , int flags );
7980extern void * repalloc (void * pointer , Size size );
8081extern void pfree (void * pointer );
8182
You can’t perform that action at this time.
0 commit comments