diff --git a/libs/dfi/include/dyn_function.h b/libs/dfi/include/dyn_function.h index c9f34d5d4..3f8002840 100644 --- a/libs/dfi/include/dyn_function.h +++ b/libs/dfi/include/dyn_function.h @@ -66,7 +66,7 @@ enum dyn_function_argument_meta { * @retval 1 If there is not enough memory to create dyn_function_type. * @retval 2 Errors other than out-of-memory. */ -CELIX_DFI_EXPORT int dynFunction_parse(FILE *descriptorStream, struct types_head *refTypes, dyn_function_type **dynFunc); +CELIX_DFI_EXPORT int dynFunction_parse(FILE* descriptorStream, struct types_head* refTypes, dyn_function_type** dynFunc); /** * @brief Creates a dyn_function_type according to the given function descriptor string. @@ -82,14 +82,14 @@ CELIX_DFI_EXPORT int dynFunction_parse(FILE *descriptorStream, struct types_head * @retval 1 If there is not enough memory to create dyn_function_type. * @retval 2 Errors other than out-of-memory. */ -CELIX_DFI_EXPORT int dynFunction_parseWithStr(const char *descriptor, struct types_head *refTypes, dyn_function_type **dynFunc); +CELIX_DFI_EXPORT int dynFunction_parseWithStr(const char* descriptor, struct types_head* refTypes, dyn_function_type** dynFunc); /** * @brief Returns the number of arguments of the given dynamic function type instance. * @param[in] dynFunc The dynamic type instance for function. * @return The number of arguments. */ -CELIX_DFI_EXPORT int dynFunction_nrOfArguments(dyn_function_type *dynFunc); +CELIX_DFI_EXPORT int dynFunction_nrOfArguments(const dyn_function_type* dynFunc); /** * @brief Returns the argument type for the given argument index. @@ -97,7 +97,7 @@ CELIX_DFI_EXPORT int dynFunction_nrOfArguments(dyn_function_type *dynFunc); * @param[in] argumentNr The argument index. * @return The argument type. */ -CELIX_DFI_EXPORT const dyn_type* dynFunction_argumentTypeForIndex(dyn_function_type* dynFunc, int argumentNr); +CELIX_DFI_EXPORT const dyn_type* dynFunction_argumentTypeForIndex(const dyn_function_type* dynFunc, int argumentNr); /** * @brief Returns the argument meta for the given argument index. @@ -105,20 +105,20 @@ CELIX_DFI_EXPORT const dyn_type* dynFunction_argumentTypeForIndex(dyn_function_t * @param[in] argumentNr The argument index. * @return The argument meta. */ -CELIX_DFI_EXPORT enum dyn_function_argument_meta dynFunction_argumentMetaForIndex(dyn_function_type *dynFunc, int argumentNr); +CELIX_DFI_EXPORT enum dyn_function_argument_meta dynFunction_argumentMetaForIndex(const dyn_function_type* dynFunc, int argumentNr); /** * @brief Returns the return value type for the given dynamic function type instance. * @param[in] dynFunc The dynamic type instance for function. * @return The return value type. */ -CELIX_DFI_EXPORT const dyn_type* dynFunction_returnType(dyn_function_type *dynFunction); +CELIX_DFI_EXPORT const dyn_type* dynFunction_returnType(const dyn_function_type* dynFunction); /** * @brief Destroys the given dynamic function type instance. * @param[in] dynFunc The dynamic type instance for function. */ -CELIX_DFI_EXPORT void dynFunction_destroy(dyn_function_type *dynFunc); +CELIX_DFI_EXPORT void dynFunction_destroy(dyn_function_type* dynFunc); CELIX_DEFINE_AUTOPTR_CLEANUP_FUNC(dyn_function_type, dynFunction_destroy); @@ -130,7 +130,7 @@ CELIX_DEFINE_AUTOPTR_CLEANUP_FUNC(dyn_function_type, dynFunction_destroy); * @param[in] argValues The argument values. * @return 0 */ -CELIX_DFI_EXPORT int dynFunction_call(dyn_function_type *dynFunc, void(*fn)(void), void *returnValue, void **argValues); +CELIX_DFI_EXPORT int dynFunction_call(const dyn_function_type* dynFunc, void(*fn)(void), void* returnValue, void** argValues); /** * @brief Creates a closure for the given dynamic function type instance. @@ -142,7 +142,7 @@ CELIX_DFI_EXPORT int dynFunction_call(dyn_function_type *dynFunc, void(*fn)(void * @retval 1 If there is not enough memory to create the closure. * @retval 2 Errors other than out-of-memory. */ -CELIX_DFI_EXPORT int dynFunction_createClosure(dyn_function_type *func, void (*bind)(void *, void **, void*), void *userData, void(**fn)(void)); +CELIX_DFI_EXPORT int dynFunction_createClosure(dyn_function_type* func, void (*bind)(void*, void**, void*), void* userData, void(**fn)(void)); /** * @brief Returns the function pointer for the given dynamic function type instance. @@ -150,18 +150,18 @@ CELIX_DFI_EXPORT int dynFunction_createClosure(dyn_function_type *func, void (*b * @param[out] fn The function pointer. * @return 0 If successful, 1 if the dynamic function type instance has no function pointer. */ -CELIX_DFI_EXPORT int dynFunction_getFnPointer(dyn_function_type *func, void (**fn)(void)); +CELIX_DFI_EXPORT int dynFunction_getFnPointer(const dyn_function_type* func, void (**fn)(void)); /** * Returns whether the function has a return type. * Will return false if return is void. */ -CELIX_DFI_EXPORT bool dynFunction_hasReturn(dyn_function_type *dynFunction); +CELIX_DFI_EXPORT bool dynFunction_hasReturn(const dyn_function_type* dynFunction); /** * @brief Returns the name of the given dynamic function type instance. */ -CELIX_DFI_EXPORT const char* dynFunction_getName(const dyn_function_type *func); +CELIX_DFI_EXPORT const char* dynFunction_getName(const dyn_function_type* func); #ifdef __cplusplus } diff --git a/libs/dfi/src/dyn_function.c b/libs/dfi/src/dyn_function.c index de045797a..2365eb3f9 100644 --- a/libs/dfi/src/dyn_function.c +++ b/libs/dfi/src/dyn_function.c @@ -31,13 +31,13 @@ static const int MEM_ERROR = 1; static const int PARSE_ERROR = 2; static const int ERROR = 2; -static int dynFunction_initCif(dyn_function_type *dynFunc); -static int dynFunction_parseDescriptor(dyn_function_type *dynFunc, FILE *descriptor); -static void dynFunction_ffiBind(ffi_cif *cif, void *ret, void *args[], void *userData); +static int dynFunction_initCif(dyn_function_type* dynFunc); +static int dynFunction_parseDescriptor(dyn_function_type* dynFunc, FILE* descriptor); +static void dynFunction_ffiBind(ffi_cif* cif, void* ret, void* args[], void* userData); -int dynFunction_parse(FILE *descriptor, struct types_head *refTypes, dyn_function_type **out) { +int dynFunction_parse(FILE* descriptor, struct types_head* refTypes, dyn_function_type** out) { int status = OK; - dyn_function_type *dynFunc = NULL; + dyn_function_type* dynFunc = NULL; dynFunc = calloc(1, sizeof(*dynFunc)); @@ -58,9 +58,9 @@ int dynFunction_parse(FILE *descriptor, struct types_head *refTypes, dyn_functio } if (status == OK) { - dyn_function_argument_type *arg = NULL; + dyn_function_argument_type* arg = NULL; TAILQ_FOREACH(arg, &dynFunc->arguments, entries) { - const char *meta = dynType_getMetaInfo(arg->type, "am"); + const char* meta = dynType_getMetaInfo(arg->type, "am"); if (meta == NULL) { arg->argumentMeta = DYN_FUNCTION_ARGUMENT_META__STD; } else if (strcmp(meta, "handle") == 0) { @@ -88,9 +88,9 @@ int dynFunction_parse(FILE *descriptor, struct types_head *refTypes, dyn_functio return status; } -int dynFunction_parseWithStr(const char *descriptor, struct types_head *refTypes, dyn_function_type **out) { +int dynFunction_parseWithStr(const char* descriptor, struct types_head* refTypes, dyn_function_type** out) { int status = OK; - FILE *stream = fmemopen((char *)descriptor, strlen(descriptor) + 1, "r"); + FILE* stream = fmemopen((char* )descriptor, strlen(descriptor) + 1, "r"); if (stream != NULL) { status = dynFunction_parse(stream, refTypes, out); fclose(stream); @@ -101,9 +101,9 @@ int dynFunction_parseWithStr(const char *descriptor, struct types_head *refTypes return status; } -static int dynFunction_parseDescriptor(dyn_function_type *dynFunc, FILE *descriptor) { +static int dynFunction_parseDescriptor(dyn_function_type* dynFunc, FILE* descriptor) { int status = OK; - char *name = NULL; + char* name = NULL; status = dynCommon_parseName(descriptor, &name); @@ -121,13 +121,13 @@ static int dynFunction_parseDescriptor(dyn_function_type *dynFunc, FILE *descrip int nextChar = fgetc(descriptor); int index = 0; - dyn_type *type = NULL; + dyn_type* type = NULL; char argName[32]; while (nextChar != ')' && status == 0) { ungetc(nextChar, descriptor); type = NULL; - dyn_function_argument_type *arg = NULL; + dyn_function_argument_type* arg = NULL; status = dynType_parse(descriptor, NULL, dynFunc->refTypes, &type); if (status == OK) { @@ -159,9 +159,9 @@ static int dynFunction_parseDescriptor(dyn_function_type *dynFunc, FILE *descrip return status; } -enum dyn_function_argument_meta dynFunction_argumentMetaForIndex(dyn_function_type *dynFunc, int argumentNr) { +enum dyn_function_argument_meta dynFunction_argumentMetaForIndex(const dyn_function_type* dynFunc, int argumentNr) { enum dyn_function_argument_meta result = 0; - dyn_function_argument_type *arg = NULL; + dyn_function_argument_type* arg = NULL; int index = 0; TAILQ_FOREACH(arg, &dynFunc->arguments, entries) { if (index == argumentNr) { @@ -174,11 +174,11 @@ enum dyn_function_argument_meta dynFunction_argumentMetaForIndex(dyn_function_ty } -static int dynFunction_initCif(dyn_function_type *dynFunc) { +static int dynFunction_initCif(dyn_function_type* dynFunc) { int status = 0; unsigned int nargs = 0; - dyn_function_argument_type *entry = NULL; + dyn_function_argument_type* entry = NULL; TAILQ_FOREACH(entry, &dynFunc->arguments, entries) { nargs +=1; } @@ -189,8 +189,8 @@ static int dynFunction_initCif(dyn_function_type *dynFunc) { dynFunc->ffiArguments[entry->index] = dynType_ffiType(entry->type); } - ffi_type **args = dynFunc->ffiArguments; - ffi_type *returnType = dynType_ffiType(dynFunc->funcReturn); + ffi_type** args = dynFunc->ffiArguments; + ffi_type* returnType = dynType_ffiType(dynFunc->funcReturn); int ffiResult = ffi_prep_cif(&dynFunc->cif, FFI_DEFAULT_ABI, nargs, returnType, args); if (ffiResult != FFI_OK) { @@ -200,7 +200,7 @@ static int dynFunction_initCif(dyn_function_type *dynFunc) { return status; } -void dynFunction_destroy(dyn_function_type *dynFunc) { +void dynFunction_destroy(dyn_function_type* dynFunc) { if (dynFunc != NULL) { if (dynFunc->funcReturn != NULL) { dynType_destroy(dynFunc->funcReturn); @@ -215,8 +215,8 @@ void dynFunction_destroy(dyn_function_type *dynFunc) { free(dynFunc->ffiArguments); } - dyn_function_argument_type *entry = NULL; - dyn_function_argument_type *tmp = NULL; + dyn_function_argument_type* entry = NULL; + dyn_function_argument_type* tmp = NULL; entry = TAILQ_FIRST(&dynFunc->arguments); while (entry != NULL) { if (entry->name != NULL) { @@ -232,17 +232,17 @@ void dynFunction_destroy(dyn_function_type *dynFunc) { } } -int dynFunction_call(dyn_function_type *dynFunc, void(*fn)(void), void *returnValue, void **argValues) { - ffi_call(&dynFunc->cif, fn, returnValue, argValues); +int dynFunction_call(const dyn_function_type* dynFunc, void(*fn)(void), void* returnValue, void** argValues) { + ffi_call((ffi_cif*)&dynFunc->cif, fn, returnValue, argValues); return 0; } -static void dynFunction_ffiBind(ffi_cif *cif, void *ret, void *args[], void *userData) { - dyn_function_type *dynFunc = userData; +static void dynFunction_ffiBind(ffi_cif* cif, void* ret, void* args[], void* userData) { + dyn_function_type* dynFunc = userData; dynFunc->bind(dynFunc->userData, args, ret); } -int dynFunction_createClosure(dyn_function_type *dynFunc, void (*bind)(void *, void **, void*), void *userData, void(**out)(void)) { +int dynFunction_createClosure(dyn_function_type* dynFunc, void (*bind)(void*, void**, void*), void* userData, void(**out)(void)) { int status = 0; void (*fn)(void); dynFunc->ffiClosure = ffi_closure_alloc(sizeof(ffi_closure), (void **)&fn); @@ -265,7 +265,7 @@ int dynFunction_createClosure(dyn_function_type *dynFunc, void (*bind)(void *, v return status; } -int dynFunction_getFnPointer(dyn_function_type *dynFunc, void (**fn)(void)) { +int dynFunction_getFnPointer(const dyn_function_type* dynFunc, void (**fn)(void)) { int status = 0; if (dynFunc != NULL && dynFunc->fn != NULL) { (*fn) = dynFunc->fn; @@ -275,7 +275,7 @@ int dynFunction_getFnPointer(dyn_function_type *dynFunc, void (**fn)(void)) { return status; } -int dynFunction_nrOfArguments(dyn_function_type *dynFunc) { +int dynFunction_nrOfArguments(const dyn_function_type* dynFunc) { int count = 0; dyn_function_argument_type *entry = NULL; TAILQ_FOREACH(entry, &dynFunc->arguments, entries) { @@ -284,7 +284,7 @@ int dynFunction_nrOfArguments(dyn_function_type *dynFunc) { return count; } -const dyn_type* dynFunction_argumentTypeForIndex(dyn_function_type* dynFunc, int argumentNr) { +const dyn_type* dynFunction_argumentTypeForIndex(const dyn_function_type* dynFunc, int argumentNr) { dyn_type* result = NULL; int index = 0; dyn_function_argument_type* entry = NULL; @@ -298,11 +298,11 @@ const dyn_type* dynFunction_argumentTypeForIndex(dyn_function_type* dynFunc, int return result; } -const dyn_type* dynFunction_returnType(dyn_function_type *dynFunction) { +const dyn_type* dynFunction_returnType(const dyn_function_type *dynFunction) { return dynFunction->funcReturn; } -bool dynFunction_hasReturn(dyn_function_type* dynFunction) { +bool dynFunction_hasReturn(const dyn_function_type* dynFunction) { const dyn_type* t = dynFunction_returnType(dynFunction); return t->descriptor != 'V'; }