Skip to content

Commit

Permalink
use custom malloc function to alloc memory for converting go string t…
Browse files Browse the repository at this point in the history
…o c string
  • Loading branch information
learnforpractice committed Jun 3, 2022
1 parent f5f2473 commit f073bde
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 5 deletions.
4 changes: 4 additions & 0 deletions pysrc/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
from . import mixin_api
from . import mixin_bot_api
from . import _mixin

__VERSION__ = '0.2.4'

_mixin.init()

default_api = mixin_api.MixinApi()

def get_mixin_api():
Expand Down
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ add_library(_mixin MODULE
if (WIN32)
add_compile_definitions(MS_WIN64)
SET(BUILD_TARGET mixin.lib)
SET(BUILD_LIB_CMD go build -o mixin.dll -buildmode=c-shared && cmake -E copy mixin.dll ${CMAKE_CURRENT_SOURCE_DIR}/../pysrc/mixin.dll && gendef mixin.dll && lib /def:mixin.def /machine:x64 /out:mixin.lib && cmake -E copy libmixin.bk.h libmixin.h)
SET(BUILD_LIB_CMD go build -o mixin.dll -buildmode=c-shared && cmake -E copy mixin.dll ${CMAKE_CURRENT_SOURCE_DIR}/../pysrc/mixin.dll && gendef mixin.dll && lib /def:mixin.def /machine:x64 /out:mixin.lib)
else()
SET(BUILD_TARGET libmixin.a)
SET(BUILD_LIB_CMD go build -o ${BUILD_TARGET} -buildmode=c-archive)
Expand Down
10 changes: 7 additions & 3 deletions src/_mixin.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ cdef extern from "<Python.h>":
object PyBytes_FromStringAndSize(const char* str, int size)
int _PyLong_AsByteArray(PyLongObject* v, unsigned char* bytes, size_t n, int little_endian, int is_signed)

cdef extern from "libmixin.h" nogil:
void Init();
cdef extern from "wrapper.h" nogil:
ctypedef char *(*fn_malloc)(uint64_t size)
void Init(fn_malloc fn)
char* GetMixinVersion()
int MixinMain(char* args)
char* CreateAddress(char* _params);
Expand Down Expand Up @@ -63,8 +64,11 @@ def main(_args):
with nogil:
MixinMain(args)

cdef char *user_malloc(uint64_t size):
return <char *>malloc(size)

def init():
Init()
Init(<fn_malloc>user_malloc)

def get_mixin_version():
cdef char* ret
Expand Down
2 changes: 1 addition & 1 deletion src/mixin
Submodule mixin updated 3 files
+0 −114 libmixin.bk.h
+23 −5 mixin_api.go
+17 −0 wrapper.h

0 comments on commit f073bde

Please sign in to comment.