Skip to content

Commit b0d6903

Browse files
* mmap.c (backtrace_free_locked): Don't put more than 16 entries
on the free list. Fixes #5 Fixes rust-lang/rust#29293 Fixes rust-lang/rust#37477
1 parent 3739537 commit b0d6903

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

mmap.c

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,33 @@ struct backtrace_freelist_struct
6969
static void
7070
backtrace_free_locked (struct backtrace_state *state, void *addr, size_t size)
7171
{
72-
/* Just leak small blocks. We don't have to be perfect. */
72+
/* Just leak small blocks. We don't have to be perfect. Don't put
73+
more than 16 entries on the free list, to avoid wasting time
74+
searching when allocating a block. If we have more than 16
75+
entries, leak the smallest entry. */
76+
7377
if (size >= sizeof (struct backtrace_freelist_struct))
7478
{
79+
size_t c;
80+
struct backtrace_freelist_struct **ppsmall;
81+
struct backtrace_freelist_struct **pp;
7582
struct backtrace_freelist_struct *p;
7683

84+
c = 0;
85+
ppsmall = NULL;
86+
for (pp = &state->freelist; *pp != NULL; pp = &(*pp)->next)
87+
{
88+
if (ppsmall == NULL || (*pp)->size < (*ppsmall)->size)
89+
ppsmall = pp;
90+
++c;
91+
}
92+
if (c >= 16)
93+
{
94+
if (size <= (*ppsmall)->size)
95+
return;
96+
*ppsmall = (*ppsmall)->next;
97+
}
98+
7799
p = (struct backtrace_freelist_struct *) addr;
78100
p->next = state->freelist;
79101
p->size = size;

0 commit comments

Comments
 (0)