-
Notifications
You must be signed in to change notification settings - Fork 77
Expand file tree
/
Copy pathnative.go
More file actions
144 lines (119 loc) · 3.26 KB
/
Copy pathnative.go
File metadata and controls
144 lines (119 loc) · 3.26 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
//go:build cgo
// +build cgo
package encoding
import (
"bytes"
"sync/atomic"
"unsafe"
"github.com/maxpert/marmot/pkg/mimalloc"
"github.com/vmihailenco/msgpack/v5"
)
// NativeBytes represents memory allocated outside the Go heap.
// It must be explicitly disposed when no longer needed.
type NativeBytes interface {
Bytes() []byte
Dispose()
Len() int
}
// NativeArena represents a memory arena for batch allocations.
// All allocations are freed when the arena is destroyed.
type NativeArena interface {
Alloc(size int) []byte
Destroy()
}
// nativeBytes implements NativeBytes using mimalloc.
type nativeBytes struct {
ptr unsafe.Pointer
len int
disposed atomic.Bool
}
// nativeArena implements NativeArena using mimalloc arenas.
type nativeArena struct {
arena unsafe.Pointer
destroyed atomic.Bool
}
// MarshalNative marshals a value to msgpack format using native memory allocation.
// The returned NativeBytes must be explicitly disposed when no longer needed.
func MarshalNative(v interface{}) (NativeBytes, error) {
var buf bytes.Buffer
enc := msgpack.NewEncoder(&buf)
if err := enc.Encode(v); err != nil {
return nil, err
}
size := buf.Len()
ptr := mimalloc.Malloc(uintptr(size))
if ptr == nil {
return nil, &MarshalError{msg: "mimalloc allocation failed"}
}
dst := unsafe.Slice((*byte)(ptr), size)
copy(dst, buf.Bytes())
return &nativeBytes{ptr: ptr, len: size}, nil
}
// MarshalToArena marshals a value to msgpack format using arena memory allocation.
// The returned slice is valid until the arena is destroyed.
func MarshalToArena(arena NativeArena, v interface{}) ([]byte, error) {
var buf bytes.Buffer
enc := msgpack.NewEncoder(&buf)
if err := enc.Encode(v); err != nil {
return nil, err
}
size := buf.Len()
slice := arena.Alloc(size)
if slice == nil {
return nil, &MarshalError{msg: "arena allocation failed"}
}
copy(slice, buf.Bytes())
return slice, nil
}
// NewArena creates a new memory arena for batch allocations.
func NewArena() NativeArena {
return &nativeArena{
arena: mimalloc.ArenaCreate(),
}
}
// Bytes returns the underlying byte slice.
// Panics if the memory has been disposed.
func (nb *nativeBytes) Bytes() []byte {
if nb.disposed.Load() {
panic("use after dispose: NativeBytes has been disposed")
}
return unsafe.Slice((*byte)(nb.ptr), nb.len)
}
// Dispose frees the native memory.
// Safe to call multiple times (no-op after first call).
func (nb *nativeBytes) Dispose() {
if nb.disposed.CompareAndSwap(false, true) {
mimalloc.Free(nb.ptr)
nb.ptr = nil
}
}
// Len returns the length of the byte slice.
func (nb *nativeBytes) Len() int {
return nb.len
}
// Alloc allocates a byte slice from the arena.
func (na *nativeArena) Alloc(size int) []byte {
if na.destroyed.Load() {
return nil
}
ptr := mimalloc.ArenaMalloc(na.arena, uintptr(size))
if ptr == nil {
return nil
}
return unsafe.Slice((*byte)(ptr), size)
}
// Destroy frees all memory allocated from the arena.
// Safe to call multiple times (no-op after first call).
func (na *nativeArena) Destroy() {
if na.destroyed.CompareAndSwap(false, true) {
mimalloc.ArenaDestroy(na.arena)
na.arena = nil
}
}
// MarshalError represents a marshaling error.
type MarshalError struct {
msg string
}
func (e *MarshalError) Error() string {
return e.msg
}