-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bf51e14
commit 11a94c7
Showing
8 changed files
with
294 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package allocator | ||
|
||
import ( | ||
"os" | ||
"unsafe" | ||
) | ||
|
||
var pageSize = os.Getpagesize() * 1 | ||
|
||
var chunkSize = int(unsafe.Sizeof(Chunk{})) | ||
|
||
type Chunk struct { | ||
data unsafe.Pointer | ||
allocated int | ||
size int | ||
ptrCount int | ||
next *Chunk | ||
prev *Chunk | ||
} | ||
|
||
type ArenaAllocator struct { | ||
tail *Chunk | ||
allocator Allocator | ||
} | ||
|
||
var arenaAllocatorSize = int(unsafe.Sizeof(ArenaAllocator{})) | ||
|
||
func NewArena(allocator Allocator) Allocator { | ||
a := (*ArenaAllocator)(allocator.Alloc(arenaAllocatorSize)) | ||
a.allocator = allocator | ||
|
||
return a | ||
} | ||
|
||
func (a *ArenaAllocator) newChunk(n int) *Chunk { | ||
ch := (*Chunk)(a.allocator.Alloc(chunkSize)) | ||
nPages := (n / pageSize) + 1 | ||
ch.size = nPages * pageSize | ||
ch.data = a.allocator.Alloc(nPages * pageSize) | ||
|
||
return ch | ||
} | ||
|
||
type Meta struct { | ||
chunk *Chunk | ||
size int | ||
} | ||
|
||
var metaSize = int(unsafe.Sizeof(Meta{})) | ||
|
||
func (a *ArenaAllocator) Alloc(n int) unsafe.Pointer { | ||
if a.tail == nil { | ||
a.tail = a.newChunk(n) | ||
} | ||
|
||
if a.tail.allocated+n+metaSize > a.tail.size { | ||
ch := a.newChunk(n + metaSize) | ||
ch.prev = a.tail | ||
a.tail.next = ch | ||
a.tail = ch | ||
} | ||
meta := (*Meta)(unsafe.Pointer(uintptr(a.tail.data) + uintptr(a.tail.allocated))) | ||
meta.chunk = a.tail | ||
meta.size = n | ||
ptr := unsafe.Pointer(uintptr(unsafe.Pointer(meta)) + uintptr(metaSize)) | ||
a.tail.allocated += n + metaSize | ||
a.tail.ptrCount++ | ||
|
||
return ptr | ||
} | ||
|
||
func (a *ArenaAllocator) freeChunk(ch *Chunk) { | ||
if ch.prev != nil { | ||
ch.prev.next = ch.next | ||
} | ||
if ch.next != nil { | ||
ch.next.prev = ch.prev | ||
} | ||
|
||
if ch == a.tail { | ||
a.tail = ch.prev | ||
} | ||
|
||
a.allocator.Free(ch.data) | ||
a.allocator.Free(unsafe.Pointer(ch)) | ||
} | ||
|
||
func (a *ArenaAllocator) Free(ptr unsafe.Pointer) { | ||
meta := (*Meta)(unsafe.Pointer(uintptr(ptr) - uintptr(metaSize))) | ||
meta.chunk.ptrCount-- | ||
} | ||
|
||
func (a *ArenaAllocator) Destroy() { | ||
for ch := a.tail; ch != nil; ch = ch.next { | ||
a.freeChunk(ch) | ||
} | ||
} | ||
|
||
func (a *ArenaAllocator) Realloc(ptr unsafe.Pointer, size int) unsafe.Pointer { | ||
newPtr := a.Alloc(size) | ||
|
||
oldMeta := (*Meta)(unsafe.Pointer(uintptr(ptr) - uintptr(metaSize))) | ||
newPtrData := unsafe.Slice((*byte)(newPtr), size) | ||
oldPtrData := unsafe.Slice((*byte)(ptr), oldMeta.size) | ||
copy(newPtrData, oldPtrData) | ||
|
||
return newPtr | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,23 @@ | ||
package malloc | ||
|
||
import ( | ||
"fmt" | ||
"runtime" | ||
"unsafe" | ||
|
||
"github.com/ebitengine/purego" | ||
) | ||
|
||
var calloc func(n int, size int) unsafe.Pointer | ||
var realloc func(ptr unsafe.Pointer, size int) unsafe.Pointer | ||
var free func(ptr unsafe.Pointer) | ||
|
||
func getSystemLibrary() string { | ||
switch runtime.GOOS { | ||
case "darwin": | ||
return "/usr/lib/libSystem.B.dylib" | ||
case "linux": | ||
return "libc.so.6" | ||
case "windows": | ||
return "ucrtbase.dll" | ||
default: | ||
panic(fmt.Errorf("GOOS=%s is not supported", runtime.GOOS)) | ||
} | ||
} | ||
|
||
func init() { | ||
libc, err := openLibrary(getSystemLibrary()) | ||
if err != nil { | ||
panic(err) | ||
} | ||
purego.RegisterLibFunc(&calloc, libc, "calloc") | ||
purego.RegisterLibFunc(&realloc, libc, "realloc") | ||
purego.RegisterLibFunc(&free, libc, "free") | ||
} | ||
//#include <stdlib.h> | ||
import "C" | ||
|
||
// CMalloc raw binding to c calloc(1, size) | ||
func Malloc(size int) unsafe.Pointer { | ||
return calloc(1, size) | ||
return C.calloc(1, C.size_t(size)) | ||
} | ||
|
||
// CMalloc raw binding to c free | ||
func Free(ptr unsafe.Pointer) { | ||
free(ptr) | ||
C.free(ptr) | ||
} | ||
|
||
// CMalloc raw binding to c realloc | ||
func Realloc(ptr unsafe.Pointer, size int) unsafe.Pointer { | ||
return realloc(ptr, size) | ||
return C.realloc(ptr, C.size_t(size)) | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.