Skip to content

Commit ac12ab0

Browse files
committed
Avoid trying to release a List's initial allocation via repalloc().
Commit 1cff1b9 included some code that supposed it could repalloc() a memory chunk to a smaller size without risk of the chunk moving. That was not a great idea, because it depended on undocumented behavior of AllocSetRealloc, which commit c477f3e changed thereby breaking it. (Not to mention that this code ought to work with other memory context types, which might not work the same...) So get rid of the repalloc calls, and instead just wipe the now-unused ListCell array and/or tell Valgrind it's NOACCESS, as if we'd freed it. In cases where the initial list allocation had been quite large, this could represent an annoying waste of space. In principle we could ameliorate that by allocating the initial cell array separately when it exceeds some threshold. But that would complicate new_list() which is hot code, and the returns would materialize only in narrow cases. On balance I don't think it'd be worth it. Discussion: https://postgr.es/m/17059.1570208426@sss.pgh.pa.us
1 parent 36425ec commit ac12ab0

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

src/backend/nodes/list.c

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "postgres.h"
1919

2020
#include "nodes/pg_list.h"
21+
#include "utils/memdebug.h"
2122
#include "utils/memutils.h"
2223

2324

@@ -172,8 +173,6 @@ enlarge_list(List *list, int min_size)
172173

173174
if (list->elements == list->initial_elements)
174175
{
175-
List *newlist PG_USED_FOR_ASSERTS_ONLY;
176-
177176
/*
178177
* Replace original in-line allocation with a separate palloc block.
179178
* Ensure it is in the same memory context as the List header. (The
@@ -188,16 +187,18 @@ enlarge_list(List *list, int min_size)
188187
list->length * sizeof(ListCell));
189188

190189
/*
191-
* Currently, asking aset.c to reduce the allocated size of the List
192-
* header is pointless in terms of reclaiming space, unless the list
193-
* is very long. However, it seems worth doing anyway to cause the
194-
* no-longer-needed initial_elements[] space to be cleared in
195-
* debugging builds.
190+
* We must not move the list header, so it's unsafe to try to reclaim
191+
* the initial_elements[] space via repalloc. In debugging builds,
192+
* however, we can clear that space and/or mark it inaccessible.
193+
* (wipe_mem includes VALGRIND_MAKE_MEM_NOACCESS.)
196194
*/
197-
newlist = (List *) repalloc(list, offsetof(List, initial_elements));
198-
199-
/* That better not have failed, nor moved the list header */
200-
Assert(newlist == list);
195+
#ifdef CLOBBER_FREED_MEMORY
196+
wipe_mem(list->initial_elements,
197+
list->max_length * sizeof(ListCell));
198+
#else
199+
VALGRIND_MAKE_MEM_NOACCESS(list->initial_elements,
200+
list->max_length * sizeof(ListCell));
201+
#endif
201202
}
202203
else
203204
{
@@ -736,13 +737,16 @@ list_delete_nth_cell(List *list, int n)
736737
else
737738
{
738739
/*
739-
* As in enlarge_list(), tell palloc code we're not using the
740-
* initial_elements space anymore.
740+
* As in enlarge_list(), clear the initial_elements[] space and/or
741+
* mark it inaccessible.
741742
*/
742-
List *newlist PG_USED_FOR_ASSERTS_ONLY;
743-
744-
newlist = (List *) repalloc(list, offsetof(List, initial_elements));
745-
Assert(newlist == list);
743+
#ifdef CLOBBER_FREED_MEMORY
744+
wipe_mem(list->initial_elements,
745+
list->max_length * sizeof(ListCell));
746+
#else
747+
VALGRIND_MAKE_MEM_NOACCESS(list->initial_elements,
748+
list->max_length * sizeof(ListCell));
749+
#endif
746750
}
747751
list->elements = newelems;
748752
list->max_length = newmaxlen;

0 commit comments

Comments
 (0)