Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit8c8a886

Browse files
committed
Add palloc_extended for frontend and backend.
This commit also adds pg_malloc_extended for frontend. These interfacescan be used to control at a lower level memory allocation using an interfacesimilar to MemoryContextAllocExtended. For example, the callers can specifyMCXT_ALLOC_NO_OOM if they want to suppress the "out of memory" error whileallocating the memory and handle a NULL return value.Michael Paquier, reviewed by me.
1 parentbc49d93 commit8c8a886

File tree

4 files changed

+85
-12
lines changed

4 files changed

+85
-12
lines changed

‎src/backend/utils/mmgr/mcxt.c

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -864,6 +864,43 @@ palloc0(Size size)
864864
returnret;
865865
}
866866

867+
void*
868+
palloc_extended(Sizesize,intflags)
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+
returnNULL;
894+
}
895+
896+
VALGRIND_MEMPOOL_ALLOC(CurrentMemoryContext,ret,size);
897+
898+
if ((flags&MCXT_ALLOC_ZERO)!=0)
899+
MemSetAligned(ret,0,size);
900+
901+
returnret;
902+
}
903+
867904
/*
868905
* pfree
869906
*Release an allocated chunk.

‎src/common/fe_memutils.c

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,31 +19,46 @@
1919

2020
#include"postgres_fe.h"
2121

22-
void*
23-
pg_malloc(size_tsize)
22+
staticinlinevoid*
23+
pg_malloc_internal(size_tsize,intflags)
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+
returnNULL;
3539
}
40+
41+
if ((flags&MCXT_ALLOC_ZERO)!=0)
42+
MemSet(tmp,0,size);
3643
returntmp;
3744
}
3845

46+
void*
47+
pg_malloc(size_tsize)
48+
{
49+
returnpg_malloc_internal(size,0);
50+
}
51+
3952
void*
4053
pg_malloc0(size_tsize)
4154
{
42-
void*tmp;
55+
returnpg_malloc_internal(size,MCXT_ALLOC_ZERO);
56+
}
4357

44-
tmp=pg_malloc(size);
45-
MemSet(tmp,0,size);
46-
returntmp;
58+
void*
59+
pg_malloc_extended(size_tsize,intflags)
60+
{
61+
returnpg_malloc_internal(size,flags);
4762
}
4863

4964
void*
@@ -100,13 +115,19 @@ pg_free(void *ptr)
100115
void*
101116
palloc(Sizesize)
102117
{
103-
returnpg_malloc(size);
118+
returnpg_malloc_internal(size,0);
104119
}
105120

106121
void*
107122
palloc0(Sizesize)
108123
{
109-
returnpg_malloc0(size);
124+
returnpg_malloc_internal(size,MCXT_ALLOC_ZERO);
125+
}
126+
127+
void*
128+
palloc_extended(Sizesize,intflags)
129+
{
130+
returnpg_malloc_internal(size,flags);
110131
}
111132

112133
void

‎src/include/common/fe_memutils.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,31 @@
99
#ifndefFE_MEMUTILS_H
1010
#defineFE_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+
#defineMCXT_ALLOC_HUGE0x01/* allow huge allocation (> 1 GB)
17+
* not actually used for frontends */
18+
#defineMCXT_ALLOC_NO_OOM0x02/* no failure if out-of-memory */
19+
#defineMCXT_ALLOC_ZERO0x04/* 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+
*/
1325
externchar*pg_strdup(constchar*in);
1426
externvoid*pg_malloc(size_tsize);
1527
externvoid*pg_malloc0(size_tsize);
28+
externvoid*pg_malloc_extended(size_tsize,intflags);
1629
externvoid*pg_realloc(void*pointer,size_tsize);
1730
externvoidpg_free(void*pointer);
1831

1932
/* Equivalent functions, deliberately named the same as backend functions */
2033
externchar*pstrdup(constchar*in);
2134
externvoid*palloc(Sizesize);
2235
externvoid*palloc0(Sizesize);
36+
externvoid*palloc_extended(Sizesize,intflags);
2337
externvoid*repalloc(void*pointer,Sizesize);
2438
externvoidpfree(void*pointer);
2539

‎src/include/utils/palloc.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ extern void *MemoryContextAllocExtended(MemoryContext context,
7676

7777
externvoid*palloc(Sizesize);
7878
externvoid*palloc0(Sizesize);
79+
externvoid*palloc_extended(Sizesize,intflags);
7980
externvoid*repalloc(void*pointer,Sizesize);
8081
externvoidpfree(void*pointer);
8182

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp