From 81f25787b31d1ce313b69793db779f4ed16dbb04 Mon Sep 17 00:00:00 2001 From: David Hendriks <davidhendriks93@gmail.com> Date: Thu, 13 Aug 2020 18:31:50 +0100 Subject: [PATCH] trying to get the memory transferred to int for the persistent_data --- include/binary_c_python.h | 6 +- src/binary_c_python.c | 65 +++++++++++++--- src/binary_c_python_api.c | 82 +++++++++++++++----- tests/python_API_test.py | 2 +- tests/test_return_persistent_data_memaddr.py | 17 ++++ tests/test_return_store_memaddr.py | 13 ++++ 6 files changed, 154 insertions(+), 31 deletions(-) create mode 100644 tests/test_return_persistent_data_memaddr.py create mode 100644 tests/test_return_store_memaddr.py diff --git a/include/binary_c_python.h b/include/binary_c_python.h index b6bbb86e1..b75b13825 100644 --- a/include/binary_c_python.h +++ b/include/binary_c_python.h @@ -45,11 +45,15 @@ int return_version_info(char ** const outstring, /* =================================================================== */ /* Functions to call other functionality */ /* =================================================================== */ -long int return_store(char * argstring, // can we do this without argstring? +long int return_store_memaddr(char * argstring, // TODO can we do this without argstring? char ** const buffer, char ** const error_buffer, size_t * const nbytes); +long int return_persistent_data_memaddr(char * argstring, // TODO can we do this without argstring? + char ** const buffer, + char ** const error_buffer, + size_t * const nbytes); /* C macros */ #define BINARY_C_APITEST_VERSION 0.1 diff --git a/src/binary_c_python.c b/src/binary_c_python.c index ac0e8a726..7fd7f0f05 100644 --- a/src/binary_c_python.c +++ b/src/binary_c_python.c @@ -30,8 +30,7 @@ /* Preparing all the functions of the module */ // Docstrings static char module_docstring[] MAYBE_UNUSED = - "This module is a python wrapper around binary_c"; - + "This module is a python3 wrapper around binary_c"; #ifdef __DEPRECATED static char create_binary_docstring[] = @@ -57,13 +56,15 @@ static char return_version_info_docstring[] = "Return the version information of the used binary_c build"; // other functionality -static char return_store_docstring[] = +static char return_store_memaddr_docstring[] = + "Return the store memory adress that will be passed to run_population"; +static char return_persistent_data_memaddr_docstring[] = "Return the store memory adress that will be passed to run_population"; static struct libbinary_c_store_t *store = NULL; /* Initialize pyobjects */ -// +// Old functions. Can be removed I think #ifdef __DEPRECATED static PyObject* binary_c_create_binary(PyObject *self, PyObject *args); #endif @@ -80,7 +81,9 @@ static PyObject* binary_c_return_help_all_info(PyObject *self, PyObject *args); static PyObject* binary_c_return_version_info(PyObject *self, PyObject *args); // Other function headers -static PyObject* binary_c_return_store(PyObject *self, PyObject *args); +static PyObject* binary_c_return_store_memaddr(PyObject *self, PyObject *args); +static PyObject* binary_c_return_persistent_data_memaddr(PyObject *self, PyObject *args); + /* Set the module functions */ @@ -103,7 +106,8 @@ static PyMethodDef module_methods[] = { {"return_help_all", binary_c_return_help_all_info, METH_VARARGS, return_help_all_info_docstring}, {"return_version_info", binary_c_return_version_info, METH_VARARGS, return_version_info_docstring}, - {"return_store", binary_c_return_store, METH_VARARGS, return_store_docstring}, + {"return_store_memaddr", binary_c_return_store_memaddr, METH_VARARGS, return_store_memaddr_docstring}, + {"return_persistent_data_memaddr", binary_c_return_persistent_data_memaddr, METH_VARARGS, return_persistent_data_memaddr_docstring}, {NULL, NULL, 0, NULL} }; @@ -388,7 +392,7 @@ static PyObject* binary_c_return_version_info(PyObject *self, PyObject *args) /* Wrappers to functions that call other functionality */ /* ============================================================================== */ -static PyObject* binary_c_return_store(PyObject *self, PyObject *args) +static PyObject* binary_c_return_store_memaddr(PyObject *self, PyObject *args) { /* Parse the input tuple */ char *argstring; @@ -401,7 +405,47 @@ static PyObject* binary_c_return_store(PyObject *self, PyObject *args) char * buffer; char * error_buffer; size_t nbytes; - long int out MAYBE_UNUSED = return_store(argstring, + long int out MAYBE_UNUSED = return_store_memaddr(argstring, + &buffer, + &error_buffer, + &nbytes); + + /* copy the buffer to a python string */ + PyObject * return_string MAYBE_UNUSED = Py_BuildValue("s", buffer); + PyObject * return_error_string MAYBE_UNUSED = Py_BuildValue("s", error_buffer); + + PyObject * store_memaddr = Py_BuildValue("l", out); + printf("store_memaddr: %ld\n", out); + + if(error_buffer != NULL && strlen(error_buffer)>0) + { + fprintf(stderr, + "Error in binary_c run : %s\n", + error_buffer); + } + + Safe_free(buffer); + Safe_free(error_buffer); + + return store_memaddr; +} + +static PyObject* binary_c_return_persistent_data_memaddr(PyObject *self, PyObject *args) +{ + /* Python binding that wraps the c function which calls the binary_c api endpoint. */ + + /* Parse the input tuple */ + char *argstring; + + if(!PyArg_ParseTuple(args, "s", &argstring)) + { + return NULL; + } + + char * buffer; + char * error_buffer; + size_t nbytes; + long int out MAYBE_UNUSED = return_persistent_data_memaddr(argstring, &buffer, &error_buffer, &nbytes); @@ -410,7 +454,8 @@ static PyObject* binary_c_return_store(PyObject *self, PyObject *args) PyObject * return_string MAYBE_UNUSED = Py_BuildValue("s", buffer); PyObject * return_error_string MAYBE_UNUSED = Py_BuildValue("s", error_buffer); - PyObject * return_store_memaddr = Py_BuildValue("l", out); + PyObject * persistent_data_memaddr = Py_BuildValue("l", out); + printf("persistent_data_memaddr: %ld\n", out); if(error_buffer != NULL && strlen(error_buffer)>0) { @@ -422,5 +467,5 @@ static PyObject* binary_c_return_store(PyObject *self, PyObject *args) Safe_free(buffer); Safe_free(error_buffer); - return return_store_memaddr; + return persistent_data_memaddr; } \ No newline at end of file diff --git a/src/binary_c_python_api.c b/src/binary_c_python_api.c index d71cb8f47..5686c8c0f 100644 --- a/src/binary_c_python_api.c +++ b/src/binary_c_python_api.c @@ -227,22 +227,20 @@ int return_help_info(char * argstring, binary_c_help(stardata, argstring); /* get buffer pointer */ - binary_c_buffer_info(stardata,buffer,nbytes); + binary_c_buffer_info(stardata, buffer, nbytes); /* get error buffer pointer */ - binary_c_error_buffer(stardata,error_buffer); + binary_c_error_buffer(stardata, error_buffer); /* free stardata (except the buffer) */ binary_c_free_memory(&stardata, // Stardata TRUE, // free_preferences TRUE, // free_stardata - FALSE, // free_store + TRUE, // free_store FALSE, // free_raw_buffer TRUE // free_persistent TODO: check if this is correct here ); - // Ask rob whether this can be replaced with setting the thing above to true - binary_c_free_store_contents(store); return 0; } @@ -275,22 +273,20 @@ int return_help_all_info(char ** const buffer, binary_c_help_all(stardata); /* get buffer pointer */ - binary_c_buffer_info(stardata,buffer,nbytes); + binary_c_buffer_info(stardata, buffer, nbytes); /* get error buffer pointer */ - binary_c_error_buffer(stardata,error_buffer); + binary_c_error_buffer(stardata, error_buffer); /* free stardata (except the buffer) */ binary_c_free_memory(&stardata, // Stardata TRUE, // free_preferences TRUE, // free_stardata - FALSE, // free_store + TRUE, // free_store FALSE, // free_raw_buffer TRUE // free_persistent TODO: check if this is correct here ); - - binary_c_free_store_contents(store); return 0; } @@ -310,7 +306,7 @@ int return_version_info(char ** const buffer, NULL, // previous_stardatas NULL, // preferences &store, // store - NULL, // persistent_data TODO: see if this use is correct + NULL, // persistent_data &empty_str, // argv -1 // argc ); @@ -323,21 +319,20 @@ int return_version_info(char ** const buffer, binary_c_version(stardata); /* get buffer pointer */ - binary_c_buffer_info(stardata,buffer,nbytes); + binary_c_buffer_info(stardata, buffer, nbytes); /* get error buffer pointer */ - binary_c_error_buffer(stardata,error_buffer); + binary_c_error_buffer(stardata, error_buffer); /* free stardata (except the buffer) */ binary_c_free_memory(&stardata, // Stardata TRUE, // free_preferences TRUE, // free_stardata - FALSE, // free_store + TRUE, // free_store FALSE, // free_raw_buffer - TRUE // free_persistent TODO: check if this is correct here + TRUE // free_persistent ); - binary_c_free_store_contents(store); return 0; } @@ -345,7 +340,7 @@ int return_version_info(char ** const buffer, /* Functions to call other functionality */ /* =================================================================== */ -long int return_store(char * argstring, +long int return_store_memaddr(char * argstring, char ** const buffer, char ** const error_buffer, size_t * const nbytes) @@ -381,13 +376,62 @@ long int return_store(char * argstring, TRUE, // free_preferences TRUE, // free_stardata FALSE, // free_store - FALSE, // free_raw_buffer - TRUE // free_persistent TODO: check if this is correct here + FALSE, // free_raw_buffer: TODO: possibly we have to do this yes + TRUE // free_persistent ); /* convert the pointer */ uintptr_t store_memaddr_int = (uintptr_t)store; // C Version converting ptr to int + printf("store is at address: %p\n", (void*)&store); + printf("store_memaddr_int: %ld\n", store_memaddr_int); /* Return the memaddr as an int */ return store_memaddr_int; +} + + +long int return_persistent_data_memaddr(char * argstring, + char ** const buffer, + char ** const error_buffer, + size_t * const nbytes) +{ + /* Function to allocate the persistent_data_memaddr */ + + struct libbinary_c_stardata_t *stardata = NULL; + struct libbinary_c_store_t * store = NULL; + struct libbinary_c_persistent_data_t * persistent_data = NULL; // TODO: change persistent_data_t to libbinary_c_persistent_data_t + + /* make new stardata */ + stardata = NULL; + binary_c_new_system(&stardata, // stardata + NULL, // previous_stardatas + NULL, // preferences + &store, // store + &persistent_data, // persistent_data + &argstring, // argv + -1 // argc + ); + + /* get buffer pointer */ + binary_c_buffer_info(stardata, buffer, nbytes); + + /* get error buffer pointer */ + binary_c_error_buffer(stardata, error_buffer); + + /* convert the pointer */ + uintptr_t persistent_data_memaddr_int = (uintptr_t)persistent_data; // C Version converting ptr to int + printf("persistent_data is at address: %p\n", (void*)&persistent_data); + printf("persistent_data_memaddr_int: %lu\n", persistent_data_memaddr_int); + + /* free stardata (except the buffer) */ + binary_c_free_memory(&stardata, // Stardata + TRUE, // free_preferences + TRUE, // free_stardata + TRUE, // free_store + FALSE, // free_raw_buffer: TODO: possibly we have to do this yes + FALSE // free_persistent + ); + + /* Return the memaddr as an int */ + return persistent_data_memaddr_int; } \ No newline at end of file diff --git a/tests/python_API_test.py b/tests/python_API_test.py index 982e6abbc..fa53c93e7 100755 --- a/tests/python_API_test.py +++ b/tests/python_API_test.py @@ -157,7 +157,7 @@ def test_return_version_info(): # Testing other functions def test_return_store(): - output = binary_c_python_api.return_store("") + output = binary_c_python_api.return_store_memaddr("") print("function: test_return_store") print("store memory adress:") diff --git a/tests/test_return_persistent_data_memaddr.py b/tests/test_return_persistent_data_memaddr.py new file mode 100644 index 000000000..52a6a92fd --- /dev/null +++ b/tests/test_return_persistent_data_memaddr.py @@ -0,0 +1,17 @@ +import binary_c_python_api + + +import textwrap + + +# Evolution functions +def test_return_persistent_data_memaddr(): + output = binary_c_python_api.return_persistent_data_memaddr("") + + print("function: test_run_system") + print("Binary_c output:") + print(textwrap.indent(str(output), "\t")) + +#### +if __name__ == "__main__": + test_return_persistent_data_memaddr() diff --git a/tests/test_return_store_memaddr.py b/tests/test_return_store_memaddr.py new file mode 100644 index 000000000..ad2025697 --- /dev/null +++ b/tests/test_return_store_memaddr.py @@ -0,0 +1,13 @@ +import binary_c_python_api +import textwrap + +def test_return_store_memaddr(): + output = binary_c_python_api.return_store_memaddr("") + + print("function: test_return_store") + print("store memory adress:") + print(textwrap.indent(str(output), "\t")) + +#### +if __name__ == "__main__": + test_return_store_memaddr() -- GitLab