mem.c 987 Bytes
Newer Older
Russ Cox's avatar
Russ Cox committed
1 2 3 4 5 6
#include "runtime.h"
#include "defs.h"
#include "os.h"
#include "malloc.h"

void*
7
runtime·SysAlloc(uintptr n)
Russ Cox's avatar
Russ Cox committed
8
{
9 10
	void *v;

Russ Cox's avatar
Russ Cox committed
11
	mstats.sys += n;
12
	v = runtime·mmap(nil, n, PROT_READ|PROT_WRITE|PROT_EXEC, MAP_ANON|MAP_PRIVATE, -1, 0);
13
	if(v < (void*)4096) {
14 15
		runtime·printf("mmap: errno=%p\n", v);
		runtime·throw("mmap");
16 17
	}
	return v;
Russ Cox's avatar
Russ Cox committed
18 19 20
}

void
21
runtime·SysUnused(void *v, uintptr n)
Russ Cox's avatar
Russ Cox committed
22 23 24 25 26 27 28
{
	USED(v);
	USED(n);
	// TODO(rsc): call madvise MADV_DONTNEED
}

void
29
runtime·SysFree(void *v, uintptr n)
Russ Cox's avatar
Russ Cox committed
30
{
Russ Cox's avatar
Russ Cox committed
31
	mstats.sys -= n;
32
	runtime·munmap(v, n);
Russ Cox's avatar
Russ Cox committed
33 34
}

35 36

void
37
runtime·SysMemInit(void)
38 39 40 41 42 43 44
{
	// Code generators assume that references to addresses
	// on the first page will fault.  Map the page explicitly with
	// no permissions, to head off possible bugs like the system
	// allocating that page as the virtual address space fills.
	// Ignore any error, since other systems might be smart
	// enough to never allow anything there.
45
//	mmap(nil, 4096, PROT_NONE, MAP_FIXED|MAP_ANON|MAP_PRIVATE, -1, 0);
46
}