]> Cypherpunks repositories - gostls13.git/commitdiff
runtime: pass correct size to malloc
authorDmitriy Vyukov <dvyukov@google.com>
Mon, 21 Jul 2014 21:56:19 +0000 (01:56 +0400)
committerDmitriy Vyukov <dvyukov@google.com>
Mon, 21 Jul 2014 21:56:19 +0000 (01:56 +0400)
In both cases we lie to malloc about the actual size that we need.
In panic we ask for less memory than we are going to use.
In slice we ask for more memory than we are going to use
(potentially asking for a fractional number of elements).
This breaks the new GC.

LGTM=khr
R=golang-codereviews, dave, khr
CC=golang-codereviews, rsc
https://golang.org/cl/116940043

src/pkg/runtime/panic.c
src/pkg/runtime/slice.goc

index ce05725037ec3224a66bdf446ffd09afb6c0645b..47e956627872ab128e051b5969aac58a0753697f 100644 (file)
@@ -41,7 +41,7 @@ newdefer(int32 siz)
        }
        if(d == nil) {
                // deferpool is empty or just a big defer
-               total = TOTALSIZE(siz);
+               total = runtime·roundupsize(TOTALSIZE(siz));
                d = runtime·malloc(total);
        }
        d->siz = siz;
index 2a14dafab5b52531fe4caf0eccd324da54a39adf..5f12a09620ca386ae75fa64eab0e85d92712aec2 100644 (file)
@@ -117,14 +117,18 @@ growslice1(SliceType *t, Slice x, intgo newcap, Slice *ret)
 
        if(newcap1 > MaxMem/typ->size)
                runtime·panicstring("growslice: cap out of range");
+       // Try to use all memory that malloc will give us...
        capmem = runtime·roundupsize(newcap1*typ->size);
+       // ...but don't ask for fractional number of elements (that can confuse GC).
+       newcap1 = capmem/typ->size;
+       capmem = newcap1*typ->size;
        flag = 0;
        // Can't use FlagNoZero w/o FlagNoScan, because otherwise GC can scan unitialized memory.
        if(typ->kind&KindNoPointers)
                flag = FlagNoScan|FlagNoZero;
        ret->array = runtime·mallocgc(capmem, (uintptr)typ|TypeInfo_Array, flag);
        ret->len = x.len;
-       ret->cap = capmem/typ->size;
+       ret->cap = newcap1;
        lenmem = x.len*typ->size;
        runtime·memmove(ret->array, x.array, lenmem);
        if(typ->kind&KindNoPointers)