diff --git a/include/binary_c_python.h b/include/binary_c_python.h index 7568bfcfe592db3d9cbc1efdac8a941400bed878..333a5d30390a97bfaca1da49d3b66220d8fc0ef6 100644 --- a/include/binary_c_python.h +++ b/include/binary_c_python.h @@ -6,8 +6,6 @@ * Include binary_C's API */ #include "binary_c.h" -//#include "binary_c_API.h" -//#include "binary_c_API_prototypes.h" /* Binary_c's python API prototypes */ int run_system(char * argstring, @@ -42,7 +40,7 @@ int return_version_info(char ** const outstring, size_t * const nbytes); /* =================================================================== */ -/* Functions to call other functionality */ +/* Functions to handle memory */ /* =================================================================== */ long int return_store_memaddr(char ** const buffer, diff --git a/src/binary_c_python.c b/src/binary_c_python.c index f433267b590d87b77d49eefbd8aa3f4ed41ce410..8b8478da263c6d1df359ae208819a5f48d9f75dc 100644 --- a/src/binary_c_python.c +++ b/src/binary_c_python.c @@ -1,47 +1,63 @@ #include <Python.h> #include "binary_c_python.h" +#include <time.h> +#include <sys/timeb.h> +#include <stdarg.h> +#include <stdio.h> +#include <stdlib.h> +#include <unistd.h> +#include <stdint.h> /* * binary_c/PYTHON API interface functions * + * This module will be available as _binary_c_bindings, as a part of the binarycpython package. + * + * The first section contains the functions that will be available + * to python as part of the submodule _binary_c_bindings + * + * The second section is composed of the functions that interface with the binary_c API + * + * Written by David Hendriks (davidhendriks93@gmail.com), Robert Izzard (r.izzard@surrey.ac.uk). + * Based on initial work of Jeff Andrews * Remember: variables must be passed by references * (i.e. as pointers). * - * See apitest.py for an example of how to use these functions. + * See tests/python_API_test.py for an example of how to use these functions. * - * See also + * Backup reading material for making C-extensions: * http://www-h.eng.cam.ac.uk/help/tpl/languages/mixinglanguages.html * https://realpython.com/build-python-c-extension-module/ * https://docs.python.org/3/extending/extending.html * https://docs.python.org/3/c-api/arg.html#c.PyArg_ParseTuple * https://realpython.com/python-bindings-overview/ * http://scipy-lectures.org/advanced/interfacing_with_c/interfacing_with_c.html + * + * Open tasks for the Extension: + * TODO: Put in clear debug statements + * TODO: properly return stderr + * TODO: describe all functions with docstrings */ -// TODO: Put in clear debug statements -/* list of variables used in the Py<>C interface */ -/************************************************************/ -/* +/************************************************************ + ************************************************************ + ** Section 1: Python module functions and creation of module + ************************************************************ + ************************************************************/ + +/************************************************************ + * * function prototypes : these are the functions * called by PYTHON code, without the trailing underscore. - */ -/************************************************************/ + * + ************************************************************/ /* Preparing all the functions of the module */ // Docstrings static char module_docstring[] MAYBE_UNUSED = "This module is a python3 wrapper around binary_c"; -#ifdef __DEPRECATED -static char create_binary_docstring[] = - "Allocate memory for a binary"; -#endif -static char function_prototype_docstring[] = - "The prototype for a binary_c python function"; -static char new_binary_system_docstring[] = - "Return an object containing a binary, ready for evolution"; - // Evolution function docstrings static char run_system_docstring[] = "Function to run a system. This is a general function that will be able to handle different kinds of situations: single system run with different settings, population run with different settings, etc. To avoid having too many functions doing slightly different things. \n\nArguments:\n\targstring: argument string for binary_c\n\t(opt) custom_logging_func_memaddr: memory address value for custom logging function. Default = -1 (None)\n\t(opt) store_memaddr: memory adress of the store. Default = -1 (None)\n\t(opt) write_logfile: Boolean (in int form) for whether to enable the writing of the log function. Default = 0\n\t(opt) population: Boolean (in int form) for whether this system is part of a population run. Default = 0."; @@ -70,12 +86,6 @@ static char free_store_memaddr_docstring[] = 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 -static PyObject* binary_c_function_prototype(PyObject *self, PyObject *args); -static PyObject* binary_c_new_binary_system(PyObject *self, PyObject *args); // Evolution function headers static PyObject* binary_c_run_system(PyObject *self, PyObject *args, PyObject *kwargs); @@ -96,30 +106,24 @@ static PyObject* binary_c_free_store_memaddr(PyObject *self, PyObject *args); /* Set the module functions */ static PyMethodDef module_methods[] = { -#ifdef __DEPRECATED - {"create_binary", - binary_c_create_binary, - METH_VARARGS, - create_binary_docstring - }, -#endif - {"function_prototype", binary_c_function_prototype, METH_VARARGS, function_prototype_docstring}, - {"new_system", binary_c_new_binary_system, METH_VARARGS, new_binary_system_docstring}, - - {"run_system", (PyCFunction)binary_c_run_system, METH_VARARGS|METH_KEYWORDS, run_system_docstring}, // Wierdly, this casting to a PyCFunction, which usually takes only 2 args, now works when giving keywords. See https://stackoverflow.com/q/10264080 + {"run_system", (PyCFunction)binary_c_run_system, METH_VARARGS|METH_KEYWORDS, run_system_docstring}, + // {"return_arglines", binary_c_return_arglines, METH_VARARGS, return_arglines_docstring}, {"return_help", binary_c_return_help_info, METH_VARARGS, return_help_info_docstring}, {"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_memaddr", binary_c_return_store_memaddr, METH_VARARGS, return_store_memaddr_docstring}, {"return_persistent_data_memaddr", binary_c_return_persistent_data_memaddr, METH_NOARGS, return_persistent_data_memaddr_docstring}, + // {"free_persistent_data_memaddr_and_return_json_output", binary_c_free_persistent_data_memaddr_and_return_json_output, METH_VARARGS, free_persistent_data_memaddr_and_return_json_output_docstring}, {"free_store_memaddr", binary_c_free_store_memaddr, METH_VARARGS, free_store_memaddr_docstring}, + // {NULL, NULL, 0, NULL} }; @@ -133,7 +137,7 @@ static struct PyModuleDef Py__binary_c_bindings = { PyModuleDef_HEAD_INIT, "_binary_c_bindings", /* name of module */ - "TODO", /* module documentation, may be NULL */ + "Module to interface the Binary_c API with python.", /* module documentation, may be NULL */ -1, /* size of per-interpreter state of the module, or -1 if the module keeps state in global variables. */ module_methods }; @@ -147,73 +151,6 @@ PyMODINIT_FUNC PyInit__binary_c_bindings(void) /* Some function that we started out with. Unused now. */ /* ============================================================================== */ -#ifdef __DEPRECATED -static PyObject* binary_c_create_binary(PyObject *self, PyObject *args) -{ - - double var1, var2; - char * empty_str = ""; - int i; - const int N = 1; - - /* Parse the input tuple */ - if(!PyArg_ParseTuple(args, "dd", &var1, &var2)) - return NULL; - - - /* Binary structures */ - struct libbinary_c_stardata_t *stardata[N]; - struct libbinary_c_store_t *store = NULL; - - /* Allocate memory for binaries */ - for(i=0;i<N;i++){ - stardata[i] = NULL; - binary_c_new_system(&stardata[i], NULL, NULL, &store, NULL, &empty_str, -1); - } - - /* Return the evolved binary */ - PyObject *ret = Py_BuildValue(""); - - return ret; -} -#endif //__DEPRECATED - - -static PyObject* binary_c_new_binary_system(PyObject *self, PyObject *args) -{ - /* Binary structures */ - struct libbinary_c_stardata_t *stardata; - - /* Allocate memory for binaries */ - char * empty_str = ""; - stardata = NULL; - binary_c_new_system(&stardata, NULL, NULL, &store, NULL, &empty_str, -1); - - /* Return an object containing the stardata */ - PyObject *ret = Py_BuildValue(""); - return ret; -} - -static PyObject* binary_c_function_prototype(PyObject *self, PyObject *args) -{ - - // This function is an very bare example of how a function would look like. - - double var1, var2; - - /* Parse the input tuple */ - if(!PyArg_ParseTuple(args, "dd", &var1, &var2)) - { - return NULL; - } - else - { - /* Return the evolved binary */ - PyObject *ret = Py_BuildValue(""); - return ret; - } -} - /* Below are the real functions: binary_c_run_population @@ -280,7 +217,6 @@ static PyObject* binary_c_run_system(PyObject *self, PyObject *args, PyObject *k Safe_free(buffer); Safe_free(error_buffer); - // TODO: fix that the return_error_string is returned. return return_string; } @@ -419,7 +355,6 @@ static PyObject* binary_c_return_store_memaddr(PyObject *self, PyObject *args) 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) { @@ -511,7 +446,6 @@ static PyObject* binary_c_free_store_memaddr(PyObject *self, PyObject *args) /* Parse the input tuple */ if(!PyArg_ParseTuple(args, "l", &store_memaddr)) { - // printf("Error: got a bad input\n"); fprintf(stderr, "Error (in function: binary_c_free_store_memaddr): Got a bad input\n"); return NULL; @@ -532,7 +466,6 @@ static PyObject* binary_c_free_store_memaddr(PyObject *self, PyObject *args) if(error_buffer != NULL && strlen(error_buffer)>0) { - // printf("Error (in function: binary_c_free_store_memaddr): %s", error_buffer); fprintf(stderr, "Error (in function: binary_c_free_store_memaddr): %s\n", error_buffer); @@ -541,38 +474,40 @@ static PyObject* binary_c_free_store_memaddr(PyObject *self, PyObject *args) Safe_free(buffer); Safe_free(error_buffer); - return Py_BuildValue(""); + Py_RETURN_NONE; } +///////////////////////////////////////////////////////////////////////////////////////////////////////// - -#include "binary_c_python.h" -#include <time.h> -#include <sys/timeb.h> -#include <stdarg.h> -#include <stdio.h> -#include <stdlib.h> -#include <unistd.h> -#include <stdint.h> +/************************************************************ + ************************************************************ + ** Section 2: binary_c interfacing functions + ** + ** These functions call the binary_c API + ************************************************************ + ************************************************************/ /* Binary_c python API * Set of c-functions that interface with the binary_c api. - * These functions are called by python, first through binary_c_python.c, - * and there further instructions are given on how to interface + * These functions are called by python, through the functions defined above. * * Contains several functions: - * // evolution + * // evolution functions: * run_system - * // utility + * // utility functions: * return_arglines * return_help_info * return_help_all_info * return_version_info - * // other - * create_store - * + * // memory allocating functions: + * return_store_memaddr + * free_persistent_data_memaddr_and_return_json_output + * + * // Memory freeing functions: + * free_store_memaddr + * free_persistent_data_memaddr_and_return_json_output */ // #define _CAPTURE @@ -899,7 +834,7 @@ int return_version_info(char ** const buffer, } /* =================================================================== */ -/* Functions set up memoery adresses */ +/* Functions to set up memory */ /* =================================================================== */ long int return_store_memaddr(char ** const buffer, @@ -921,8 +856,8 @@ long int return_store_memaddr(char ** const buffer, ); /* output to strings */ - // stardata->preferences->internal_buffering = INTERNAL_BUFFERING_STORE; - // stardata->preferences->batchmode = BATCHMODE_LIBRARY; + stardata->preferences->internal_buffering = INTERNAL_BUFFERING_STORE; + stardata->preferences->batchmode = BATCHMODE_LIBRARY; /* get buffer pointer */ binary_c_buffer_info(stardata, buffer, nbytes); @@ -930,7 +865,7 @@ long int return_store_memaddr(char ** const buffer, /* get error buffer pointer */ binary_c_error_buffer(stardata, error_buffer); - /* free stardata (except the buffer) */ + /* free stardata (except the buffer and the store) */ binary_c_free_memory(&stardata, // Stardata TRUE, // free_preferences TRUE, // free_stardata @@ -978,7 +913,7 @@ long int return_persistent_data_memaddr(char ** const buffer, uintptr_t persistent_data_memaddr_int = (uintptr_t)stardata->persistent_data; // C Version converting ptr to int debug_printf("persistent_data is at address: %p persistent_data_memaddr_int: %ld\n", (void*)&stardata->persistent_data, persistent_data_memaddr_int); - /* free stardata (except the buffer) */ + /* free stardata (except the buffer and the persistent data) */ binary_c_free_memory(&stardata, // Stardata TRUE, // free_preferences TRUE, // free_stardata @@ -992,7 +927,7 @@ long int return_persistent_data_memaddr(char ** const buffer, } /* =================================================================== */ -/* Functions free memory */ +/* Functions to free memory */ /* =================================================================== */ int free_persistent_data_memaddr_and_return_json_output(long int persistent_data_memaddr, @@ -1029,6 +964,9 @@ int free_persistent_data_memaddr_and_return_json_output(long int persistent_data -1 // argc ); + /* Set the model hash (usually done internally but we're not evolving a system here */ + stardata->model.ensemble_hash = stardata->persistent_data->ensemble_hash; + /* output to strings */ stardata->preferences->internal_buffering = INTERNAL_BUFFERING_STORE; stardata->preferences->batchmode = BATCHMODE_LIBRARY; @@ -1048,7 +986,7 @@ int free_persistent_data_memaddr_and_return_json_output(long int persistent_data TRUE, // free_stardata TRUE, // free_store FALSE, // free_raw_buffer - FALSE // free_persistent + FALSE // free_persistent. It already ); return 0; @@ -1087,7 +1025,8 @@ int free_store_memaddr(long int store_memaddr, -1 // argc ); - printf("freed store memaddr\n"); + debug_printf("freed store memaddr\n"); + /* output to strings */ stardata->preferences->internal_buffering = INTERNAL_BUFFERING_STORE; stardata->preferences->batchmode = BATCHMODE_LIBRARY; diff --git a/src/binary_c_python_api.c b/src/binary_c_python_api.c deleted file mode 100644 index 974d35471686ae209a018542f54e7636737bd7cd..0000000000000000000000000000000000000000 --- a/src/binary_c_python_api.c +++ /dev/null @@ -1,563 +0,0 @@ -#include "binary_c_python.h" -#include <time.h> -#include <sys/timeb.h> -#include <stdarg.h> -#include <stdio.h> -#include <stdlib.h> -#include <unistd.h> -#include <stdint.h> - -/* Binary_c python API - * Set of c-functions that interface with the binary_c api. - * These functions are called by python, first through binary_c_python.c, - * and there further instructions are given on how to interface - * - * Contains several functions: - * // evolution - * run_system - - * // utility - * return_arglines - * return_help_info - * return_help_all_info - * return_version_info - - * // other - * create_store - * - */ - -// #define _CAPTURE -#ifdef _CAPTURE -static void show_stdout(void); -static void capture_stdout(void); -#endif - -/* global variables */ -int out_pipe[2]; -int stdoutwas; - -/* =================================================================== */ -/* Functions to evolve systems */ -/* =================================================================== */ - -/* -Function that runs a system. Has multiple input parameters: -Big function. Takes several arguments. See binary_c_python.c docstring. -TODO: Describe each input -*/ -int run_system(char * argstring, - long int custom_logging_func_memaddr, - long int store_memaddr, - long int persistent_data_memaddr, - int write_logfile, - int population, - char ** const buffer, - char ** const error_buffer, - size_t * const nbytes) -{ - /* memory for system */ - struct libbinary_c_stardata_t *stardata = NULL; - - // Store: - /* Check the value of the store_memaddr */ - struct libbinary_c_store_t *store; - if(store_memaddr != -1) - { - // load the store from the integer that has been passed - store = (void*)store_memaddr; - } - else - { - // struct libbinary_c_store_t * store = NULL; - store = NULL; - } - - // persistent_data: - struct libbinary_c_persistent_data_t *persistent_data; - if(persistent_data_memaddr != -1) - { - // load the persistent data from the long int that has been passed - persistent_data = (void*)persistent_data_memaddr; - debug_printf("Took long int memaddr %ld and loaded it to %p\n", persistent_data_memaddr, (void*)&persistent_data); - } - else - { - persistent_data = NULL; - debug_printf("persistent_data memory adress was -1, now setting it to NULL\n"); - } - - /* Set up new system */ - binary_c_new_system(&stardata, // stardata - NULL, // previous_stardatas - NULL, // preferences - &store, // store - &persistent_data, // persistent_data - &argstring, // argv - -1 // argc - ); - - // Add flag to enable - /* disable logging */ - if(write_logfile != 1) - { - snprintf(stardata->preferences->log_filename, - STRING_LENGTH-1, - "%s", - "/dev/null"); - snprintf(stardata->preferences->api_log_filename_prefix, - STRING_LENGTH-1, - "%s", - "/dev/null"); - } - - /* output to strings */ - stardata->preferences->internal_buffering = INTERNAL_BUFFERING_STORE; - stardata->preferences->batchmode = BATCHMODE_LIBRARY; - - /* Check the value of the custom_logging_memaddr */ - if(custom_logging_func_memaddr != -1) - { - stardata->preferences->custom_output_function = (void*)(struct stardata_t *)custom_logging_func_memaddr; - } - - debug_printf("ensemble_defer: %d\n", stardata->preferences->ensemble_defer); - - /* do binary evolution */ - binary_c_evolve_for_dt(stardata, - stardata->model.max_evolution_time); - - /* get buffer pointer */ - binary_c_buffer_info(stardata, buffer, nbytes); - - /* get error buffer pointer */ - binary_c_error_buffer(stardata, error_buffer); - - /* Determine whether to free the store memory adress*/ - Boolean free_store = FALSE; - if (store_memaddr == -1) - { - Boolean free_store = TRUE; - } - - /* Determine whether to free the persistent data memory adress*/ - Boolean free_persistent_data = FALSE; - if (persistent_data_memaddr == -1) - { - debug_printf("Decided to free the persistent_data memaddr\n"); - Boolean free_persistent_data = TRUE; - } - - /* free stardata (except the buffer) */ - binary_c_free_memory(&stardata, // Stardata - TRUE, // free_preferences - TRUE, // free_stardata - free_store, // free_store - FALSE, // free_raw_buffer - free_persistent_data // free_persistent - ); - - return 0; -} - -/* =================================================================== */ -/* Functions to call other API functionality like help and arglines */ -/* =================================================================== */ - -int return_arglines(char ** const buffer, - char ** const error_buffer, - size_t * const nbytes) -{ - /* memory for N binary systems */ - struct libbinary_c_stardata_t *stardata = NULL; - struct libbinary_c_store_t *store = NULL; - char *empty_str = ""; - - /* Set up new system */ - binary_c_new_system(&stardata, // stardata - NULL, // previous_stardatas - NULL, // preferences - &store, // store - NULL, // persistent_data - &empty_str, // argv - -1 // argc - ); - - /* disable logging */ - snprintf(stardata->preferences->log_filename, - STRING_LENGTH-1, - "%s", - "/dev/null"); - snprintf(stardata->preferences->api_log_filename_prefix, - STRING_LENGTH-1, - "%s", - "/dev/null"); - - /* output to strings */ - stardata->preferences->internal_buffering = INTERNAL_BUFFERING_STORE; - stardata->preferences->batchmode = BATCHMODE_LIBRARY; - - /* List available arguments */ - binary_c_list_args(stardata); - - /* get buffer pointer */ - binary_c_buffer_info(stardata,buffer,nbytes); - - /* get error buffer pointer */ - binary_c_error_buffer(stardata,error_buffer); - - /* free stardata (except the buffer) */ - binary_c_free_memory(&stardata, // Stardata - TRUE, // free_preferences - TRUE, // free_stardata - TRUE, // free_store - FALSE, // free_raw_buffer - TRUE // free_persistent - ); - - return 0; -} - - -int return_help_info(char * argstring, - char ** const buffer, - char ** const error_buffer, - size_t * const nbytes) -{ - struct libbinary_c_stardata_t *stardata = NULL; - struct libbinary_c_store_t *store = NULL; - - /* Set up new system */ - binary_c_new_system(&stardata, // stardata - NULL, // previous_stardatas - NULL, // preferences - &store, // store - NULL, // persistent_data - &argstring, // argv - -1 // argc - ); - - /* output to strings */ - stardata->preferences->internal_buffering = INTERNAL_BUFFERING_STORE; - stardata->preferences->batchmode = BATCHMODE_LIBRARY; - - /* Ask the help api */ - binary_c_help(stardata, argstring); - - /* get buffer pointer */ - binary_c_buffer_info(stardata, buffer, nbytes); - - /* get error buffer pointer */ - binary_c_error_buffer(stardata, error_buffer); - - /* free stardata (except the buffer) */ - binary_c_free_memory(&stardata, // Stardata - TRUE, // free_preferences - TRUE, // free_stardata - TRUE, // free_store - FALSE, // free_raw_buffer - TRUE // free_persistent - ); - - return 0; -} - - -int return_help_all_info(char ** const buffer, - char ** const error_buffer, - size_t * const nbytes) -{ - struct libbinary_c_stardata_t *stardata = NULL; - struct libbinary_c_store_t *store = NULL; - char * empty_str = ""; - - /* Set up new system */ - binary_c_new_system(&stardata, // stardata - NULL, // previous_stardatas - NULL, // preferences - &store, // store - NULL, // persistent_data - &empty_str, // argv - -1 // argc - ); - - /* output to strings */ - stardata->preferences->internal_buffering = INTERNAL_BUFFERING_STORE; - stardata->preferences->batchmode = BATCHMODE_LIBRARY; - - /* Ask the help api */ - binary_c_help_all(stardata); - - /* get buffer pointer */ - binary_c_buffer_info(stardata, buffer, nbytes); - - /* get error buffer pointer */ - binary_c_error_buffer(stardata, error_buffer); - - /* free stardata (except the buffer) */ - binary_c_free_memory(&stardata, // Stardata - TRUE, // free_preferences - TRUE, // free_stardata - TRUE, // free_store - FALSE, // free_raw_buffer - TRUE // free_persistent - ); - - return 0; -} - - -int return_version_info(char ** const buffer, - char ** const error_buffer, - size_t * const nbytes) -{ - struct libbinary_c_stardata_t *stardata = NULL; - struct libbinary_c_store_t * store = NULL; - char * empty_str = ""; - - /* Set up new system */ - binary_c_new_system(&stardata, // stardata - NULL, // previous_stardatas - NULL, // preferences - &store, // store - NULL, // persistent_data - &empty_str, // argv - -1 // argc - ); - - /* output to strings */ - stardata->preferences->internal_buffering = INTERNAL_BUFFERING_STORE; - stardata->preferences->batchmode = BATCHMODE_LIBRARY; - - /* Ask the help api */ - binary_c_version(stardata); - - /* get buffer pointer */ - binary_c_buffer_info(stardata, buffer, nbytes); - - /* get error buffer pointer */ - binary_c_error_buffer(stardata, error_buffer); - - /* free stardata (except the buffer) */ - binary_c_free_memory(&stardata, // Stardata - TRUE, // free_preferences - TRUE, // free_stardata - TRUE, // free_store - FALSE, // free_raw_buffer - TRUE // free_persistent - ); - - return 0; -} - -/* =================================================================== */ -/* Functions set up memoery adresses */ -/* =================================================================== */ - -long int return_store_memaddr(char ** const buffer, - char ** const error_buffer, - size_t * const nbytes) -{ - struct libbinary_c_stardata_t * stardata = NULL; - struct libbinary_c_store_t * store = NULL; - char * empty_str = ""; - - /* Set up new system */ - binary_c_new_system(&stardata, // stardata - NULL, // previous_stardatas - NULL, // preferences - &store, // store - NULL, // persistent_data - &empty_str, // argv - -1 // argc - ); - - /* output to strings */ - // stardata->preferences->internal_buffering = INTERNAL_BUFFERING_STORE; - // stardata->preferences->batchmode = BATCHMODE_LIBRARY; - - /* get buffer pointer */ - binary_c_buffer_info(stardata, buffer, nbytes); - - /* get error buffer pointer */ - 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 - FALSE, // free_raw_buffer - TRUE // free_persistent - ); - - /* convert the pointer */ - uintptr_t store_memaddr_int = (uintptr_t)store; // C Version converting ptr to int - debug_printf("store is at address: %p store_memaddr_int: %ld\n", (void*)&store, store_memaddr_int); - - /* Return the memaddr as an int */ - return store_memaddr_int; -} - - -long int return_persistent_data_memaddr(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; - char * empty_str = ""; - - /* Set up new system */ - binary_c_new_system(&stardata, // stardata - NULL, // previous_stardatas - NULL, // preferences - &store, // store - &persistent_data, // persistent_data - &empty_str, // 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)stardata->persistent_data; // C Version converting ptr to int - debug_printf("persistent_data is at address: %p persistent_data_memaddr_int: %ld\n", (void*)&stardata->persistent_data, 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 - FALSE // free_persistent - ); - - /* Return the memaddr as an int */ - return persistent_data_memaddr_int; -} - -/* =================================================================== */ -/* Functions free memory */ -/* =================================================================== */ - -int free_persistent_data_memaddr_and_return_json_output(long int persistent_data_memaddr, - char ** const buffer, - char ** const error_buffer, - size_t * const nbytes) -{ - struct libbinary_c_store_t *store = NULL; - struct libbinary_c_stardata_t *stardata = NULL; - char * empty_str = ""; - - // persistent_data: - struct libbinary_c_persistent_data_t *persistent_data; - if(persistent_data_memaddr != -1) - { - // load the persistent data from the long int that has been passed - persistent_data = (void*)persistent_data_memaddr; - debug_printf("Took long int memaddr %ld and loaded it to %p\n", persistent_data_memaddr, (void*)&persistent_data); - } - else - { - printf("ERROR: this function needs a valid persistent_data_memaddr value. not -1\n"); - // persistent_data = NULL; - // TODO: put break in the function here. - } - - /* Set up new system */ - binary_c_new_system(&stardata, // stardata - NULL, // previous_stardatas - NULL, // preferences - &store, // store - &persistent_data, // persistent_data - &empty_str, // argv - -1 // argc - ); - - /* output to strings */ - stardata->preferences->internal_buffering = INTERNAL_BUFFERING_STORE; - stardata->preferences->batchmode = BATCHMODE_LIBRARY; - - /* get output and free memory */ - binary_c_output_to_json(stardata); - - /* get buffer pointer */ - binary_c_buffer_info(stardata, buffer, nbytes); - - /* get error buffer pointer */ - binary_c_error_buffer(stardata, error_buffer); - - /* free the reststardata (except the buffer) */ - binary_c_free_memory(&stardata, // Stardata - TRUE, // free_preferences - TRUE, // free_stardata - TRUE, // free_store - FALSE, // free_raw_buffer - FALSE // free_persistent - ); - - return 0; -} - -int free_store_memaddr(long int store_memaddr, - char ** const buffer, - char ** const error_buffer, - size_t * const nbytes) -{ - struct libbinary_c_stardata_t *stardata = NULL; - struct libbinary_c_persistent_data_t *persistent_data = NULL; - char * empty_str = ""; - - // Store: - /* Check the value of the store_memaddr */ - struct libbinary_c_store_t *store; - if(store_memaddr != -1) - { - // load the store from the integer that has been passed - store = (void*)store_memaddr; - debug_printf("Took long int store_memaddr %ld and loaded it to %p\n", store_memaddr, (void*)&store); - } - else - { - store = NULL; - } - - /* Set up new system */ - binary_c_new_system(&stardata, // stardata - NULL, // previous_stardatas - NULL, // preferences - &store, // store - &persistent_data, // persistent_data - &empty_str, // argv - -1 // argc - ); - - printf("freed store memaddr\n"); - /* output to strings */ - stardata->preferences->internal_buffering = INTERNAL_BUFFERING_STORE; - stardata->preferences->batchmode = BATCHMODE_LIBRARY; - - /* get buffer pointer */ - binary_c_buffer_info(stardata, buffer, nbytes); - - /* get error buffer pointer */ - binary_c_error_buffer(stardata, error_buffer); - - /* free the reststardata (except the buffer) */ - binary_c_free_memory(&stardata, // Stardata - TRUE, // free_preferences - TRUE, // free_stardata - TRUE, // free_store - FALSE, // free_raw_buffer - TRUE // free_persistent - ); - - return 0; -}