[pypy-commit] stmgc c5: Test and fix

arigo noreply at buildbot.pypy.org
Fri Dec 20 14:12:57 CET 2013


Author: Armin Rigo <arigo at tunes.org>
Branch: c5
Changeset: r574:d90013065da3
Date: 2013-12-20 14:12 +0100
http://bitbucket.org/pypy/stmgc/changeset/d90013065da3/

Log:	Test and fix

diff --git a/c5/demo2.c b/c5/demo2.c
--- a/c5/demo2.c
+++ b/c5/demo2.c
@@ -3,24 +3,12 @@
 #include <assert.h>
 
 
-#define END_MARKER           0xDEADBEEF
-
 char *stm_large_malloc(size_t request_size);
 void stm_large_free(char *data);
+void _stm_large_dump(char *data);
 
+#define dump _stm_large_dump
 
-static void dump(char *data)
-{
-    while (1) {
-        fprintf(stderr, "[ %p: %zu\n", data - 16, *(size_t*)(data - 16));
-        if (*(size_t*)(data - 8) == END_MARKER)
-            break;
-        fprintf(stderr, "  %p: %zu ]\n", data - 8, *(size_t*)(data - 8));
-        data += (*(size_t*)(data - 8)) & ~1;
-        data += 16;
-    }
-    fprintf(stderr, "  %p: end. ]\n\n", data - 8);
-}
 
 int main()
 {
diff --git a/c5/largemalloc.c b/c5/largemalloc.c
--- a/c5/largemalloc.c
+++ b/c5/largemalloc.c
@@ -231,6 +231,7 @@
     size_t remaining_size_plus_1 = mscan->size - request_size;
     if (remaining_size_plus_1 <= sizeof(struct malloc_chunk)) {
         next_chunk_s(mscan)->prev_size = BOTH_CHUNKS_USED;
+        request_size = mscan->size & ~FLAG_SORTED;
     }
     else {
         /* only part of the chunk is being used; reduce the size
@@ -243,8 +244,8 @@
         new->size = remaining_size;
         next_chunk_u(new)->prev_size = remaining_size;
         insert_unsorted(new);
-        mscan->size = request_size;
     }
+    mscan->size = request_size;
     mscan->prev_size = BOTH_CHUNKS_USED;
     return (char *)&mscan->d;
 }
@@ -259,6 +260,7 @@
         fprintf(stderr, "out of memory!\n");
         abort();
     }
+    fprintf(stderr, "allocate_more: %p\n", &big_chunk->d);
 
     big_chunk->prev_size = THIS_CHUNK_FREE;
     big_chunk->size = big_size - CHUNK_HEADER_SIZE * 2;
@@ -284,7 +286,7 @@
     mchunk_t *mscan = chunk_at_offset(chunk, msize);
 
     if (mscan->prev_size == BOTH_CHUNKS_USED) {
-        assert((mscan->size & (sizeof(char *) - 1)) == 0);
+        assert((mscan->size & ((sizeof(char *) - 1) & ~FLAG_SORTED)) == 0);
         mscan->prev_size = chunk->size;
     }
     else {
@@ -333,3 +335,31 @@
 
     insert_unsorted(chunk);
 }
+
+
+void _stm_large_dump(char *data)
+{
+    size_t prev_size_if_free = 0;
+    while (1) {
+        fprintf(stderr, "[ %p: %zu\n", data - 16, *(size_t*)(data - 16));
+        if (prev_size_if_free == 0) {
+            assert(*(size_t*)(data - 16) == THIS_CHUNK_FREE ||
+                   *(size_t*)(data - 16) == BOTH_CHUNKS_USED);
+            if (*(size_t*)(data - 16) == THIS_CHUNK_FREE)
+                prev_size_if_free = (*(size_t*)(data - 8)) & ~FLAG_SORTED;
+        }
+        else {
+            assert(*(size_t*)(data - 16) == prev_size_if_free);
+            prev_size_if_free = 0;
+        }
+        if (*(size_t*)(data - 8) == END_MARKER)
+            break;
+        fprintf(stderr, "  %p: %zu]%s\n", data - 8, *(size_t*)(data - 8),
+                prev_size_if_free ? " (free)" : "");
+        if (!prev_size_if_free)
+            assert(!((*(size_t*)(data - 8)) & FLAG_SORTED));
+        data += (*(size_t*)(data - 8)) & ~FLAG_SORTED;
+        data += 16;
+    }
+    fprintf(stderr, "  %p: end. ]\n\n", data - 8);
+}
diff --git a/c5/largemalloc.h b/c5/largemalloc.h
--- a/c5/largemalloc.h
+++ b/c5/largemalloc.h
@@ -2,3 +2,4 @@
 
 char *stm_large_malloc(size_t request_size);
 void stm_large_free(char *data);
+void _stm_large_dump(char *data);
diff --git a/c5/test/support.py b/c5/test/support.py
--- a/c5/test/support.py
+++ b/c5/test/support.py
@@ -44,9 +44,13 @@
 
 char *stm_large_malloc(size_t request_size);
 void stm_large_free(char *data);
+void _stm_large_dump(char *data);
+
+void *memset(void *s, int c, size_t n);
 """)
 
 lib = ffi.verify('''
+#include <string.h>
 #include "core.h"
 #include "largemalloc.h"
 ''', sources=source_files,
diff --git a/c5/test/test_largemalloc.py b/c5/test/test_largemalloc.py
--- a/c5/test/test_largemalloc.py
+++ b/c5/test/test_largemalloc.py
@@ -1,4 +1,5 @@
 from support import *
+import random
 
 
 class TestLargeMalloc(object):
@@ -29,3 +30,25 @@
         assert d7 == d6 + 616
         d8 = lib.stm_large_malloc(600)
         assert d8 == d4
+
+    def test_random(self):
+        r = random.Random(1005)
+        p = []
+        for i in range(100000):
+            if len(p) != 0 and (len(p) > 100 or r.randrange(0, 5) < 2):
+                index = r.randrange(0, len(p))
+                d, length, content1, content2 = p.pop(index)
+                print ' free %5d  (%s)' % (length, d)
+                assert d[0] == content1
+                assert d[length - 1] == content2
+                lib.stm_large_free(d)
+            else:
+                sz = r.randrange(8, 160) * 8
+                d = lib.stm_large_malloc(sz)
+                print 'alloc %5d  (%s)' % (sz, d)
+                lib.memset(d, 0xdd, sz)
+                content1 = chr(r.randrange(0, 256))
+                content2 = chr(r.randrange(0, 256))
+                d[0] = content1
+                d[sz - 1] = content2
+                p.append((d, sz, content1, content2))


More information about the pypy-commit mailing list