-
Notifications
You must be signed in to change notification settings - Fork 25
/
malloc.go
47 lines (40 loc) · 1.32 KB
/
malloc.go
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
package eal
/*
#include "../../csrc/core/common.h"
*/
import "C"
import (
"unsafe"
"go.uber.org/zap"
"golang.org/x/exp/constraints"
)
// Zmalloc allocates zero'ed memory on specified NumaSocket.
// Panics if out of memory.
func Zmalloc[T any, S constraints.Integer](dbgtype string, size S, socket NumaSocket) *T {
return ZmallocAligned[T](dbgtype, size, 0, socket)
}
// ZmallocAligned allocates zero'ed memory on specified NumaSocket.
// Panics if out of memory.
// align specifies alignment requirement in cachelines (must be power of 2), or 0 to indicate no requirement.
func ZmallocAligned[T any, S constraints.Integer](dbgtype string, size S, align int, socket NumaSocket) *T {
return (*T)(zmallocImpl(dbgtype, uintptr(size), align, socket))
}
func zmallocImpl(dbgtype string, size uintptr, align int, socket NumaSocket) unsafe.Pointer {
var typ [32]byte
copy(typ[:len(typ)-1], []byte(dbgtype))
typC := (*C.char)(unsafe.Pointer(unsafe.SliceData(typ[:])))
ptr := C.rte_zmalloc_socket(typC, C.size_t(size), C.uint(align*C.RTE_CACHE_LINE_SIZE), C.int(socket.ID()))
if ptr == nil {
logger.Panic(
"zmalloc failed",
zap.String("type", dbgtype),
zap.Uintptr("size", size),
socket.ZapField("socket"),
)
}
return ptr
}
// Free deallocates memory from Zmalloc.
func Free[T any](ptr *T) {
C.rte_free(unsafe.Pointer(ptr))
}