[pypy-commit] stmgc default: Don't use an unbounded variable-sized array in the stack. Instead,

arigo noreply at buildbot.pypy.org
Sun Apr 6 11:49:01 CEST 2014


Author: Armin Rigo <arigo at tunes.org>
Branch: 
Changeset: r1138:5dbd50990e2c
Date: 2014-04-06 11:48 +0200
http://bitbucket.org/pypy/stmgc/changeset/5dbd50990e2c/

Log:	Don't use an unbounded variable-sized array in the stack. Instead,
	use a fixed-sized one and allocate in the heap if it's not enough.

diff --git a/c7/stm/largemalloc.c b/c7/stm/largemalloc.c
--- a/c7/stm/largemalloc.c
+++ b/c7/stm/largemalloc.c
@@ -134,6 +134,8 @@
         return +1;
 }
 
+#define MAX_STACK_COUNT  64
+
 static void really_sort_bin(size_t index)
 {
     dlist_t *unsorted = largebins[index].prev;
@@ -148,12 +150,20 @@
     scan->next = end;
 
     mchunk_t *chunk1;
-    mchunk_t *chunks[count];    /* dynamically-sized */
+    mchunk_t *chunk_array[MAX_STACK_COUNT];
+    mchunk_t **chunks = chunk_array;
+
     if (count == 1) {
         chunk1 = data2chunk(unsorted);   /* common case */
         count = 0;
     }
     else {
+        if (count > MAX_STACK_COUNT) {
+            chunks = malloc(count * sizeof(mchunk_t *));
+            if (chunks == NULL) {
+                stm_fatalerror("out of memory");   // XXX
+            }
+        }
         size_t i;
         for (i = 0; i < count; i++) {
             chunks[i] = data2chunk(unsorted);
@@ -200,6 +210,9 @@
         chunk1 = chunks[--count];
         search_size = chunk1->size;
     }
+
+    if (chunks != chunk_array)
+        free(chunks);
 }
 
 static void sort_bin(size_t index)


More information about the pypy-commit mailing list