OSDN Git Service

Update Go compiler, library, and testsuite on gcc 4.7 branch.
[pf3gnuchains/gcc-fork.git] / libgo / runtime / malloc.goc
index 9ad9eda..3fde250 100644 (file)
@@ -19,6 +19,7 @@ package runtime
 #include "go-type.h"
 
 MHeap runtime_mheap;
+
 extern MStats mstats;  // defined in extern.go
 
 extern volatile int32 runtime_MemProfileRate
@@ -276,15 +277,27 @@ runtime_mallocinit(void)
        uintptr arena_size, bitmap_size;
        extern byte end[];
        byte *want;
+       uintptr limit;
 
        runtime_sizeof_C_MStats = sizeof(MStats);
 
+       p = nil;
+       arena_size = 0;
+       bitmap_size = 0;
+       
+       // for 64-bit build
+       USED(p);
+       USED(arena_size);
+       USED(bitmap_size);
+
        runtime_InitSizes();
 
+       limit = runtime_memlimit();
+
        // Set up the allocation arena, a contiguous area of memory where
        // allocated data will be found.  The arena begins with a bitmap large
        // enough to hold 4 bits per allocated word.
-       if(sizeof(void*) == 8) {
+       if(sizeof(void*) == 8 && (limit == 0 || limit > (1<<30))) {
                // On a 64-bit machine, allocate from a single contiguous reservation.
                // 16 GB should be big enough for now.
                //
@@ -307,12 +320,13 @@ runtime_mallocinit(void)
                // Actually we reserve 17 GB (because the bitmap ends up being 1 GB)
                // but it hardly matters: fc is not valid UTF-8 either, and we have to
                // allocate 15 GB before we get that far.
+               //
+               // If this fails we fall back to the 32 bit memory mechanism
                arena_size = (uintptr)(16LL<<30);
                bitmap_size = arena_size / (sizeof(void*)*8/4);
                p = runtime_SysReserve((void*)(0x00f8ULL<<32), bitmap_size + arena_size);
-               if(p == nil)
-                       runtime_throw("runtime: cannot reserve arena virtual address space");
-       } else {
+       }
+       if (p == nil) {
                // On a 32-bit machine, we can't typically get away
                // with a giant virtual address space reservation.
                // Instead we map the memory information bitmap
@@ -332,6 +346,10 @@ runtime_mallocinit(void)
                // of address space, which is probably too much in a 32-bit world.
                bitmap_size = MaxArena32 / (sizeof(void*)*8/4);
                arena_size = 512<<20;
+               if(limit > 0 && arena_size+bitmap_size > limit) {
+                       bitmap_size = (limit / 9) & ~((1<<PageShift) - 1);
+                       arena_size = bitmap_size * 8;
+               }
                
                // SysReserve treats the address we ask for, end, as a hint,
                // not as an absolute requirement.  If we ask for the end
@@ -348,6 +366,8 @@ runtime_mallocinit(void)
                p = runtime_SysReserve(want, bitmap_size + arena_size);
                if(p == nil)
                        runtime_throw("runtime: cannot reserve arena virtual address space");
+               if((uintptr)p & (((uintptr)1<<PageShift)-1))
+                       runtime_printf("runtime: SysReserve returned unaligned address %p; asked for %p", p, (void*)(bitmap_size+arena_size));
        }
        if((uintptr)p & (((uintptr)1<<PageShift)-1))
                runtime_throw("runtime: SysReserve returned unaligned address");
@@ -379,8 +399,8 @@ runtime_MHeap_SysAlloc(MHeap *h, uintptr n)
                return p;
        }
        
-       // On 64-bit, our reservation is all we have.
-       if(sizeof(void*) == 8)
+       // If using 64-bit, our reservation is all we have.
+       if(sizeof(void*) == 8 && (uintptr)h->bitmap >= 0xffffffffU)
                return nil;
 
        // On 32-bit, once the reservation is gone we can
@@ -419,18 +439,6 @@ func new(typ *Type) (ret *uint8) {
        ret = runtime_mallocgc(typ->__size, flag, 1, 1);
 }
 
-func Alloc(n uintptr) (p *byte) {
-       p = runtime_malloc(n);
-}
-
-func Free(p *byte) {
-       runtime_free(p);
-}
-
-func Lookup(p *byte) (base *byte, size uintptr) {
-       runtime_mlookup(p, &base, &size, nil);
-}
-
 func GC() {
        runtime_gc(1);
 }