Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 59ea9ef

Browse files
committed
Use palloc in TopMemoryContext instead of malloc
As discussed, even if the PL needs a permanent memory location, it should use palloc, not malloc. It also makes error handling easier. Jan Urbański
1 parent 88047e5 commit 59ea9ef

File tree

1 file changed

+5
-10
lines changed

1 file changed

+5
-10
lines changed

src/pl/plpython/plpython.c

+5-10
Original file line numberDiff line numberDiff line change
@@ -1605,7 +1605,7 @@ PLy_procedure_compile(PLyProcedure *proc, const char *src)
16051605
*/
16061606
msrc = PLy_procedure_munge_source(proc->pyname, src);
16071607
crv = PyRun_String(msrc, Py_file_input, proc->globals, NULL);
1608-
free(msrc);
1608+
pfree(msrc);
16091609

16101610
if (crv != NULL)
16111611
{
@@ -1642,7 +1642,7 @@ PLy_procedure_munge_source(const char *name, const char *src)
16421642
*/
16431643
mlen = (strlen(src) * 2) + strlen(name) + 16;
16441644

1645-
mrc = PLy_malloc(mlen);
1645+
mrc = palloc(mlen);
16461646
plen = snprintf(mrc, mlen, "def %s():\n\t", name);
16471647
Assert(plen >= 0 && plen < mlen);
16481648

@@ -3664,13 +3664,8 @@ PLy_traceback(int *xlevel)
36643664
static void *
36653665
PLy_malloc(size_t bytes)
36663666
{
3667-
void *ptr = malloc(bytes);
3668-
3669-
if (ptr == NULL)
3670-
ereport(FATAL,
3671-
(errcode(ERRCODE_OUT_OF_MEMORY),
3672-
errmsg("out of memory")));
3673-
return ptr;
3667+
/* We need our allocations to be long-lived, so use TopMemoryContext */
3668+
return MemoryContextAlloc(TopMemoryContext, bytes);
36743669
}
36753670

36763671
static void *
@@ -3699,7 +3694,7 @@ PLy_strdup(const char *str)
36993694
static void
37003695
PLy_free(void *ptr)
37013696
{
3702-
free(ptr);
3697+
pfree(ptr);
37033698
}
37043699

37053700
/*

0 commit comments

Comments
 (0)