-
Notifications
You must be signed in to change notification settings - Fork 1
/
fallback.go
39 lines (32 loc) · 984 Bytes
/
fallback.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
package cgoalloc
import (
"unsafe"
)
// FallbackAllocator is an Allocator implementation which accepts a FixedBlockAllocator and sends all Malloc calls which
// can fit in the FBA's block size to that FixedBlockAllocator. All other calls are sent to a fallback allocator.
type FallbackAllocator struct {
fixedBlock FixedBlockAllocator
fallback Allocator
}
func CreateFallbackAllocator(fixedBlock FixedBlockAllocator, fallback Allocator) *FallbackAllocator {
return &FallbackAllocator{
fixedBlock: fixedBlock,
fallback: fallback,
}
}
func (a *FallbackAllocator) Malloc(size int) unsafe.Pointer {
if size > a.fixedBlock.assignedBlockSize() {
return a.fallback.Malloc(size)
}
return a.fixedBlock.Malloc(size)
}
func (a *FallbackAllocator) Free(ptr unsafe.Pointer) {
if !a.fixedBlock.tryFree(ptr) {
a.fallback.Free(ptr)
}
}
func (a *FallbackAllocator) Destroy() error {
err := a.fixedBlock.Destroy()
if err != nil { return err }
return a.fallback.Destroy()
}