mem_openbsd.c 2.13 KB
Newer Older
1 2 3 4 5
// Copyright 2010 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.

#include "runtime.h"
Russ Cox's avatar
Russ Cox committed
6 7 8
#include "arch_GOARCH.h"
#include "defs_GOOS_GOARCH.h"
#include "os_GOOS.h"
9
#include "malloc.h"
10
#include "textflag.h"
11 12 13 14 15 16

enum
{
	ENOMEM = 12,
};

Russ Cox's avatar
Russ Cox committed
17
#pragma textflag NOSPLIT
18
void*
19
runtime·sysAlloc(uintptr n, uint64 *stat)
20 21 22
{
	void *v;

23
	v = runtime·mmap(nil, n, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0);
24 25
	if(v < (void*)4096)
		return nil;
26
	runtime·xadd64(stat, n);
27 28 29 30 31 32
	return v;
}

void
runtime·SysUnused(void *v, uintptr n)
{
33
	runtime·madvise(v, n, MADV_FREE);
34 35
}

36 37 38 39 40 41 42
void
runtime·SysUsed(void *v, uintptr n)
{
	USED(v);
	USED(n);
}

43
void
44
runtime·SysFree(void *v, uintptr n, uint64 *stat)
45
{
46
	runtime·xadd64(stat, -(uint64)n);
47 48 49
	runtime·munmap(v, n);
}

50 51 52
void
runtime·SysFault(void *v, uintptr n)
{
Dmitriy Vyukov's avatar
Dmitriy Vyukov committed
53
	runtime·mmap(v, n, PROT_NONE, MAP_ANON|MAP_PRIVATE|MAP_FIXED, -1, 0);
54 55
}

56
void*
57
runtime·SysReserve(void *v, uintptr n, bool *reserved)
58 59 60 61 62 63
{
	void *p;

	// On 64-bit, people with ulimit -v set complain if we reserve too
	// much address space.  Instead, assume that the reservation is okay
	// and check the assumption in SysMap.
64 65
	if(sizeof(void*) == 8 && n > 1LL<<32) {
		*reserved = false;
66
		return v;
67
	}
68 69

	p = runtime·mmap(v, n, PROT_NONE, MAP_ANON|MAP_PRIVATE, -1, 0);
70
	if(p < (void*)4096)
71
		return nil;
72
	*reserved = true;
73
	return p;
74 75 76
}

void
77
runtime·SysMap(void *v, uintptr n, bool reserved, uint64 *stat)
78 79 80
{
	void *p;
	
81
	runtime·xadd64(stat, n);
82 83

	// On 64-bit, we don't actually have v reserved, so tread carefully.
84
	if(!reserved) {
85
		p = runtime·mmap(v, n, PROT_READ|PROT_WRITE, MAP_ANON|MAP_PRIVATE, -1, 0);
86
		if(p == (void*)ENOMEM)
87 88 89 90 91 92 93 94
			runtime·throw("runtime: out of memory");
		if(p != v) {
			runtime·printf("runtime: address space conflict: map(%p) = %p\n", v, p);
			runtime·throw("runtime: address space conflict");
		}
		return;
	}

95
	p = runtime·mmap(v, n, PROT_READ|PROT_WRITE, MAP_ANON|MAP_FIXED|MAP_PRIVATE, -1, 0);
96
	if(p == (void*)ENOMEM)
97 98 99 100
		runtime·throw("runtime: out of memory");
	if(p != v)
		runtime·throw("runtime: cannot map pages in arena address space");
}