-
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.
feat: pump purego version feat: pump purego version
- Loading branch information
Youssef ahmed
committed
May 10, 2023
1 parent
ec615c0
commit 38cd911
Showing
4 changed files
with
52 additions
and
13 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
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
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,20 +1,51 @@ | ||
package malloc | ||
|
||
// #include <stdlib.h> | ||
import "C" | ||
import "unsafe" | ||
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 := purego.Dlopen(getSystemLibrary(), purego.RTLD_NOW|purego.RTLD_GLOBAL) | ||
if err != nil { | ||
panic(err) | ||
} | ||
purego.RegisterLibFunc(&calloc, libc, "calloc") | ||
purego.RegisterLibFunc(&realloc, libc, "realloc") | ||
purego.RegisterLibFunc(&free, libc, "free") | ||
} | ||
|
||
// CMalloc raw binding to c calloc(1, size) | ||
func Malloc(size int) unsafe.Pointer { | ||
return C.calloc(1, C.size_t(size)) | ||
return calloc(1, size) | ||
} | ||
|
||
// CMalloc raw binding to c free | ||
func Free(ptr unsafe.Pointer) { | ||
C.free(ptr) | ||
free(ptr) | ||
} | ||
|
||
// CMalloc raw binding to c realloc | ||
func Realloc(ptr unsafe.Pointer, size int) unsafe.Pointer { | ||
return C.realloc(ptr, C.size_t(size)) | ||
return realloc(ptr, size) | ||
} |