about summary refs log tree commit diff
path: root/vendor/modernc.org/libc/mem_brk.go
blob: 2b4f155b16e953bbe9aa6f4953d9dee04b6804f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright 2021 The Libc Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

//go:build libc.membrk && !libc.memgrind && !(linux && (amd64 || arm64 || loong64))

// This is a debug-only version of the memory handling functions. When a
// program is built with -tags=libc.membrk a simple but safe version of malloc
// and friends is used that works like sbrk(2). Additionally free becomes a
// nop.

package libc // import "modernc.org/libc"

import (
	"unsafe"

	"modernc.org/libc/errno"
	"modernc.org/libc/sys/types"
)

const (
	heapAlign = 16
	memgrind  = false
)

var (
	heap     = make([]byte, heapSize)
	heapP    = uintptr(unsafe.Pointer(&heap[heapAlign]))
	heapLast = uintptr(unsafe.Pointer(&heap[heapSize-1]))
)

// void *malloc(size_t size);
func Xmalloc(t *TLS, n types.Size_t) uintptr {
	if __ccgo_strace {
		trc("t=%v n=%v, (%v:)", t, n, origin(2))
	}
	if n == 0 {
		return 0
	}

	allocMu.Lock()

	defer allocMu.Unlock()

	n2 := uintptr(n) + uintptrSize // reserve space for recording block size
	p := roundup(heapP, 16)
	if p+uintptr(n2) >= heapLast {
		t.setErrno(errno.ENOMEM)
		return 0
	}

	heapP = p + uintptr(n2)
	*(*uintptr)(unsafe.Pointer(p - uintptrSize)) = uintptr(n)
	return p
}

// void *calloc(size_t nmemb, size_t size);
func Xcalloc(t *TLS, n, size types.Size_t) uintptr {
	if __ccgo_strace {
		trc("t=%v size=%v, (%v:)", t, size, origin(2))
	}
	return Xmalloc(t, n*size)
}

// void *realloc(void *ptr, size_t size);
func Xrealloc(t *TLS, ptr uintptr, size types.Size_t) uintptr {
	if __ccgo_strace {
		trc("t=%v ptr=%v size=%v, (%v:)", t, ptr, size, origin(2))
	}
	switch {
	case ptr != 0 && size != 0:
		p := Xmalloc(t, size)
		sz0 := UsableSize(ptr)
		if p != 0 {
			copy((*RawMem)(unsafe.Pointer(p))[:size:size], (*RawMem)(unsafe.Pointer(ptr))[:sz0:sz0])
		}
		return p
	case ptr == 0 && size != 0:
		return Xmalloc(t, size)
	}
	return 0
}

// void free(void *ptr);

func Xfree(t *TLS, p uintptr) {
	if __ccgo_strace {
		trc("t=%v p=%v, (%v:)", t, p, origin(2))
	}
}

func UsableSize(p uintptr) types.Size_t {
	return types.Size_t(*(*uintptr)(unsafe.Pointer(p - uintptrSize)))
}

// MemAuditStart locks the memory allocator, initializes and enables memory
// auditing. Finaly it unlocks the memory allocator.
//
// Some memory handling errors, like double free or freeing of unallocated
// memory, will panic when memory auditing is enabled.
//
// This memory auditing functionality has to be enabled using the libc.memgrind
// build tag.
//
// It is intended only for debug/test builds. It slows down memory allocation
// routines and it has additional memory costs.
func MemAuditStart() {}

// MemAuditReport locks the memory allocator, reports memory leaks, if any.
// Finally it disables memory auditing and unlocks the memory allocator.
//
// This memory auditing functionality has to be enabled using the libc.memgrind
// build tag.
//
// It is intended only for debug/test builds. It slows down memory allocation
// routines and it has additional memory costs.
func MemAuditReport() error { return nil }