1
0
mirror of https://github.com/golang/go synced 2024-10-03 22:31:21 -06:00
go/usr/rsc/mem/triv.c
Russ Cox c1868bc89d malloc fixes.
can run peano 10 in 100 MB (instead of 1+ GB) of memory
when linking against this.
can run peano 11 in 1 GB of memory now.

R=r
DELTA=100  (44 added, 44 deleted, 12 changed)
OCL=20504
CL=20553
2008-12-04 21:04:26 -08:00

75 lines
1.6 KiB
C

// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Trivial base allocator.
#include "malloc.h"
// TODO: The call to sys·mmap should be a call to an assembly
// function sys·mmapnew that takes only a size parameter.
enum
{
PROT_NONE = 0x00,
PROT_READ = 0x01,
PROT_WRITE = 0x02,
PROT_EXEC = 0x04,
MAP_FILE = 0x0000,
MAP_SHARED = 0x0001,
MAP_PRIVATE = 0x0002,
MAP_FIXED = 0x0010,
MAP_ANON = 0x1000,
};
// Allocate and return zeroed memory.
// Simple allocator for small things like Span structures,
// and also used to grab large amounts of memory for
// the real allocator to hand out.
enum
{
Round = 15,
};
void*
trivalloc(int32 size)
{
static byte *p;
static int32 n;
byte *v;
uint64 oldfoot;
if(allocator·frozen)
throw("allocator frozen");
//prints("Newmem: ");
//sys·printint(size);
//prints("\n");
oldfoot = allocator·footprint;
if(size < 4096) { // TODO: Tune constant.
size = (size + Round) & ~Round;
if(size > n) {
n = 1<<20; // TODO: Tune constant.
p = sys·mmap(nil, n, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, 0, 0);
allocator·footprint += n;
}
v = p;
p += size;
goto out;
}
if(size & PageMask)
size += (1<<PageShift) - (size & PageMask);
v = sys·mmap(nil, size, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, 0, 0);
allocator·footprint += size;
out:
if((oldfoot>>24) != (allocator·footprint>>24))
printf("memory footprint = %D MB for %D MB\n", allocator·footprint>>20, allocator·allocated>>20);
if(allocator·footprint >= 2LL<<30) {
prints("out of memory\n");
sys·exit(1);
}
return v;
}