CELIX-237: refactoring of parsing. mostly using streams (FILE *) now
Project: http://git-wip-us.apache.org/repos/asf/celix/repo Commit: http://git-wip-us.apache.org/repos/asf/celix/commit/08b5bc0c Tree: http://git-wip-us.apache.org/repos/asf/celix/tree/08b5bc0c Diff: http://git-wip-us.apache.org/repos/asf/celix/diff/08b5bc0c Branch: refs/heads/feature/CELIX-237_rsa-ffi Commit: 08b5bc0ca3ba5e6524922957ce832ba4bdbc683f Parents: 5c52b21 Author: Pepijn Noltes <[email protected]> Authored: Sun Jul 12 20:14:08 2015 +0200 Committer: Pepijn Noltes <[email protected]> Committed: Sun Jul 12 20:14:08 2015 +0200 ---------------------------------------------------------------------- .../dynamic_function_interface/CMakeLists.txt | 5 +- .../dynamic_function_interface/dyn_common.c | 41 +++ .../dynamic_function_interface/dyn_common.h | 31 +++ .../dynamic_function_interface/dyn_function.c | 256 +++++++++++-------- .../dynamic_function_interface/dyn_function.h | 13 +- .../dynamic_function_interface/dyn_interface.c | 22 +- .../dynamic_function_interface/dyn_interface.h | 33 ++- .../dynamic_function_interface/dyn_type.c | 62 ++--- .../dynamic_function_interface/dyn_type.h | 4 +- .../tst/avro_descriptor_translator_tests.cpp | 2 + .../tst/dyn_closure_tests.cpp | 38 +-- .../tst/dyn_function_tests.cpp | 11 +- .../tst/dyn_type_tests.cpp | 13 +- .../tst/json_serializer_tests.cpp | 10 +- 14 files changed, 317 insertions(+), 224 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/celix/blob/08b5bc0c/remote_services/dynamic_function_interface/CMakeLists.txt ---------------------------------------------------------------------- diff --git a/remote_services/dynamic_function_interface/CMakeLists.txt b/remote_services/dynamic_function_interface/CMakeLists.txt index 0c8cccb..0c69d92 100644 --- a/remote_services/dynamic_function_interface/CMakeLists.txt +++ b/remote_services/dynamic_function_interface/CMakeLists.txt @@ -19,11 +19,12 @@ if (${CMAKE_SYSTEM_NAME} STREQUAL "Darwin") endif() add_library(dfi + dyn_common.c dyn_type.c dyn_function.c dyn_interface.c json_serializer.c - avro_descriptor_translator.c +# avro_descriptor_translator.c ${MEMSTREAM_SOURCES} ) target_link_libraries(dfi ${FFI_LIBRARIES} ${JANSSON_LIBRARY}) @@ -35,7 +36,7 @@ target_link_libraries(dfi ${FFI_LIBRARIES} ${JANSSON_LIBRARY}) tst/dyn_function_tests.cpp tst/dyn_closure_tests.cpp tst/json_serializer_tests.cpp - tst/avro_descriptor_translator_tests.cpp +# tst/avro_descriptor_translator_tests.cpp tst/run_tests.cpp ) target_link_libraries(dfi_tests dfi ${FFI_LIBRARIES} ${CPPUTEST_LIBRARY} ${JANSSON_LIBRARY}) http://git-wip-us.apache.org/repos/asf/celix/blob/08b5bc0c/remote_services/dynamic_function_interface/dyn_common.c ---------------------------------------------------------------------- diff --git a/remote_services/dynamic_function_interface/dyn_common.c b/remote_services/dynamic_function_interface/dyn_common.c new file mode 100644 index 0000000..c0d5ca0 --- /dev/null +++ b/remote_services/dynamic_function_interface/dyn_common.c @@ -0,0 +1,41 @@ +/** + * Licensed under Apache License v2. See LICENSE for more information. + */ +#include "dyn_common.h" + +#include <stdio.h> +#include <ctype.h> + +#if defined(BSD) || defined(__APPLE__) +#include "open_memstream.h" +#include "fmemopen.h" +#endif + +static const int OK = 0; +static const int ERROR = 1; + +DFI_SETUP_LOG(dynCommon) + +int dynCommon_parseName(FILE *stream, char **result) { + int status = OK; + + char *buf = NULL; + size_t size = 0; + FILE *name = open_memstream(&buf, &size); + + if (name != NULL) { + int c = getc(stream); + while (isalnum(c) || c == '_') { + fputc(c, name); + c = getc(stream); + } + fflush(name); + fclose(name); + *result = buf; + ungetc(c, stream); + } else { + status = ERROR; + LOG_ERROR("Error creating mem stream for name. %s", strerror(errno)); + } + return status; +} http://git-wip-us.apache.org/repos/asf/celix/blob/08b5bc0c/remote_services/dynamic_function_interface/dyn_common.h ---------------------------------------------------------------------- diff --git a/remote_services/dynamic_function_interface/dyn_common.h b/remote_services/dynamic_function_interface/dyn_common.h new file mode 100644 index 0000000..8a9491a --- /dev/null +++ b/remote_services/dynamic_function_interface/dyn_common.h @@ -0,0 +1,31 @@ +/** + * Licensed under Apache License v2. See LICENSE for more information. + */ +#ifndef _DYN_COMMON_H_ +#define _DYN_COMMON_H_ + +#include <errno.h> +#include <stdio.h> +#include <string.h> +#include <sys/queue.h> + +#include "dfi_log_util.h" + +//logging +DFI_SETUP_LOG_HEADER(dynCommon); + +typedef struct _dyn_annotation_list_type annotation_list_type; +TAILQ_HEAD(_dyn_annotation_list_type, _dyn_annotation_type); + +typedef struct _dyn_annotation_type dyn_annotation_type; +struct _dyn_annotation_type { + char *name; + char *value; +}; + +int dynCommon_parseName(FILE *stream, char **result); +//TODO int dynCommon_parseNameValue(FILE *stream, char **name, char **value); +//TODO int dynCommon_parseAnnotation(FILE *stream, + + +#endif http://git-wip-us.apache.org/repos/asf/celix/blob/08b5bc0c/remote_services/dynamic_function_interface/dyn_function.c ---------------------------------------------------------------------- diff --git a/remote_services/dynamic_function_interface/dyn_function.c b/remote_services/dynamic_function_interface/dyn_function.c index 8814d3c..75aa1ae 100644 --- a/remote_services/dynamic_function_interface/dyn_function.c +++ b/remote_services/dynamic_function_interface/dyn_function.c @@ -11,53 +11,61 @@ #include <ffi.h> +#include "dyn_common.h" #include "dyn_type.h" #include "dfi_log_util.h" DFI_SETUP_LOG(dynFunction) struct _dyn_function_type { - dyn_type *arguments; + char *name; + dyn_type_list_type *refTypes; //NOTE not owned + TAILQ_HEAD(,_dyn_function_argument_type) arguments; + ffi_type **ffiArguments; dyn_type *funcReturn; - void (*fn)(void); ffi_cif cif; -}; -struct _dyn_closure_type { - dyn_type *arguments; - dyn_type *funcReturn; - ffi_cif cif; + //closure part ffi_closure *ffiClosure; void (*fn)(void); + void *userData; void (*bind)(void *userData, void *args[], void *ret); - void *userData; //for bind }; +typedef struct _dyn_function_argument_type dyn_function_argument_type; +struct _dyn_function_argument_type { + int index; + char *name; + dyn_type *type; + TAILQ_ENTRY(_dyn_function_argument_type) entries; +}; -static int dynFunction_initCif(ffi_cif *cif, dyn_type *arguments, dyn_type *funcReturn); -static int dynFunction_parseDescriptor(const char *functionDescriptor, dyn_type_list_type *typeReferences, dyn_type **arguments, dyn_type **funcReturn); -static void dynClosure_ffiBind(ffi_cif *cif, void *ret, void *args[], void *userData); +static const int OK = 0; +static const int MEM_ERROR = 1; +static const int PARSE_ERROR = 2; -int dynFunction_create(FILE *descriptorStream, dyn_type_list_type *typeReferences, void (*fn)(void), dyn_function_type **dynFunc) { - //TODO - return 0; -} +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_createWithStr(const char *descriptor, dyn_type_list_type *typeReferences, void (*fn)(void), dyn_function_type **out) { - int status = 0; +int dynFunction_parse(FILE *descriptor, dyn_type_list_type *typeReferences, dyn_function_type **out) { + int status = OK; dyn_function_type *dynFunc = NULL; - LOG_DEBUG("Creating dyn function for descriptor '%s'\n", descriptor); + LOG_DEBUG("Creating dyn function", descriptor); dynFunc = calloc(1, sizeof(*dynFunc)); if (dynFunc != NULL) { - dynFunc->fn = fn; - status = dynFunction_parseDescriptor(descriptor, typeReferences, &dynFunc->arguments, &dynFunc->funcReturn); + TAILQ_INIT(&dynFunc->arguments); + dynFunc->refTypes = typeReferences; + status = dynFunction_parseDescriptor(dynFunc, descriptor); if (status == 0) { - status = dynFunction_initCif(&dynFunc->cif, dynFunc->arguments, dynFunc->funcReturn); + int rc = dynFunction_initCif(dynFunc); + status = rc != 0 ? rc : 0; } } else { - status = 2; + LOG_ERROR("Error allocationg memory for dyn functipn\n"); + status = MEM_ERROR; } if (status == 0) { @@ -72,121 +80,161 @@ int dynFunction_createWithStr(const char *descriptor, dyn_type_list_type *typeRe return status; } -static int dynFunction_parseDescriptor(const char *descriptor, dyn_type_list_type *typeReferences, dyn_type **arguments, dyn_type **funcReturn) { - int status = 0; - char *startPos = index(descriptor, '('); - char *endPos = index(descriptor, ')'); - - if (startPos != NULL && endPos != NULL) { - int len = endPos - startPos - 1; - - //TODO add names (arg001, arg002, etc) - char argDesc[len+3]; - argDesc[0] = '{'; - argDesc[len+1] = '}'; - memcpy(argDesc+1, startPos +1, len); - argDesc[len+2] = '\0'; - LOG_DEBUG("argDesc is '%s'\n", argDesc); - - len = strlen(endPos); - char returnDesc[len+1]; - memcpy(returnDesc, endPos + 1, len); - returnDesc[len] = '\0'; - LOG_DEBUG("returnDesc is '%s'\n", returnDesc); - - status = dynType_createWithStr(argDesc, NULL, typeReferences, arguments); +int dynFunction_parseWithStr(const char *descriptor, dyn_type_list_type *typeReferences, dyn_function_type **out) { + int status = OK; + FILE *stream = fmemopen((char *)descriptor, strlen(descriptor), "r"); + if (stream != NULL) { + status = dynFunction_parse(stream, typeReferences, out); + fclose(stream); + } else { + status = MEM_ERROR; + LOG_ERROR("Error creating mem stream for descriptor string. %s", strerror(errno)); + } + return status; +} + +static int dynFunction_parseDescriptor(dyn_function_type *dynFunc, FILE *descriptor) { + int status = OK; + char *name = NULL; + + status = dynCommon_parseName(descriptor, &name); + + if (status == OK) { + dynFunc->name = name; + } + + if (status == OK) { + int c = fgetc(descriptor); + if ( c != '(') { + status = PARSE_ERROR; + LOG_ERROR("Expected '(' token got '%c'", c); + } + } + + int nextChar = fgetc(descriptor); + int index = 0; + dyn_type *type = NULL; + while (nextChar != ')' && status == 0) { + type = NULL; + ungetc(nextChar, descriptor); + status = dynType_parse(descriptor, NULL, dynFunc->refTypes, &type); if (status == 0) { - status = dynType_createWithStr(returnDesc, NULL, typeReferences, funcReturn); + dyn_function_argument_type *arg = calloc(1, sizeof(*arg)); + arg->index = index++; + arg->type = type; + arg->name = NULL; //TODO + if (arg != NULL) { + TAILQ_INSERT_TAIL(&dynFunc->arguments, arg, entries); + } else { + LOG_ERROR("Error allocating memory"); + status = MEM_ERROR; + } } - } else { - status = 1; + nextChar = fgetc(descriptor); + } + + if (status == 0) { + status = dynType_parse(descriptor, NULL, dynFunc->refTypes, &dynFunc->funcReturn); } return status; } -static int dynFunction_initCif(ffi_cif *cif, dyn_type *arguments, dyn_type *returnValue) { - int result = 0; +static int dynFunction_initCif(dyn_function_type *dynFunc) { + int status = 0; int count = 0; - int i; - for (i = 0; arguments->ffiType->elements[i] != NULL; i += 1) { - count += 1; + dyn_function_argument_type *entry = NULL; + TAILQ_FOREACH(entry, &dynFunc->arguments, entries) { + count +=1; } - ffi_type **args = arguments->ffiType->elements; - ffi_type *returnType = returnValue->ffiType; + dynFunc->ffiArguments = calloc(count, sizeof(ffi_type)); + + TAILQ_FOREACH(entry, &dynFunc->arguments, entries) { + dynFunc->ffiArguments[entry->index] = entry->type->ffiType; + } + + ffi_type **args = dynFunc->ffiArguments; + ffi_type *returnType = dynFunc->funcReturn->ffiType; - int ffiResult = ffi_prep_cif(cif, FFI_DEFAULT_ABI, count, returnType, args); + int ffiResult = ffi_prep_cif(&dynFunc->cif, FFI_DEFAULT_ABI, count, returnType, args); if (ffiResult != FFI_OK) { - result = 1; + status = 1; } - return result; + return status; } void dynFunction_destroy(dyn_function_type *dynFunc) { if (dynFunc != NULL) { - if (dynFunc->arguments != NULL) { - dynType_destroy(dynFunc->arguments); - } if (dynFunc->funcReturn != NULL) { - dynType_destroy(dynFunc->funcReturn); - } - free(dynFunc); + dynType_destroy(dynFunc->funcReturn); + } + if (dynFunc->ffiClosure != NULL) { + ffi_closure_free(dynFunc->ffiClosure); + } + if (dynFunc->name != NULL) { + free(dynFunc->name); + } + if (dynFunc->ffiArguments != NULL) { + free(dynFunc->ffiArguments); + } + + dyn_function_argument_type *entry = NULL; + dyn_function_argument_type *tmp = NULL; + entry = TAILQ_FIRST(&dynFunc->arguments); + while (entry != NULL) { + if (entry->name != NULL) { + free(entry->name); + } + dynType_destroy(entry->type); + tmp = entry; + entry = TAILQ_NEXT(entry, entries); + free(tmp); + } + + free(dynFunc); } } -int dynFunction_call(dyn_function_type *dynFunc, void *returnValue, void **argValues) { - ffi_call(&dynFunc->cif, dynFunc->fn, returnValue, argValues); +int dynFunction_call(dyn_function_type *dynFunc, void(*fn)(void), void *returnValue, void **argValues) { + //TODO check dynFunc arg + ffi_call(&dynFunc->cif, fn, returnValue, argValues); return 0; } -static void dynClosure_ffiBind(ffi_cif *cif, void *ret, void *args[], void *userData) { - dyn_closure_type *dynClosure = userData; - dynClosure->bind(dynClosure->userData, args, ret); +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 dynClosure_create(FILE *descriptorStream, dyn_type_list_type *typeReferences, void (*bind)(void *, void **, void*), void *userData, dyn_closure_type **out) { - //TODO - return 0; -} - -int dynClosure_createWithStr(const char *descriptor, dyn_type_list_type *typeReferences, void (*bind)(void *, void **, void*), void *userData, dyn_closure_type **out) { +int dynFunction_createClosure(dyn_function_type *dynFunc, void (*bind)(void *, void **, void*), void *userData, void(**out)(void)) { int status = 0; - dyn_closure_type *dynClosure = calloc(1, sizeof(*dynClosure)); - if (dynClosure != NULL) { - dynClosure->bind = bind; - dynClosure->userData = userData; - status = dynFunction_parseDescriptor(descriptor, typeReferences, &dynClosure->arguments, &dynClosure->funcReturn); - if (status == 0) { - status = dynFunction_initCif(&dynClosure->cif, dynClosure->arguments, dynClosure->funcReturn); - if (status == 0) { - dynClosure->ffiClosure = ffi_closure_alloc(sizeof(ffi_closure), (void **)&dynClosure->fn); - if (dynClosure->ffiClosure != NULL) { - int rc = ffi_prep_closure_loc(dynClosure->ffiClosure, &dynClosure->cif, dynClosure_ffiBind, dynClosure, dynClosure->fn); - if (rc != FFI_OK) { - status = 1; - } - } else { - status = 2; - } - } + void (*fn)(void); + dynFunc->ffiClosure = ffi_closure_alloc(sizeof(ffi_closure), (void **)&fn); + if (dynFunc->ffiClosure != NULL) { + int rc = ffi_prep_closure_loc(dynFunc->ffiClosure, &dynFunc->cif, dynFunction_ffiBind, dynFunc, fn); + if (rc != FFI_OK) { + status = 1; } } else { status = 2; } if (status == 0) { - *out = dynClosure; + dynFunc->bind = bind; + dynFunc->fn = fn; + *out =fn; } + return status; } -int dynClosure_getFnPointer(dyn_closure_type *dynClosure, void (**fn)(void)) { +int dynClosure_getFnPointer(dyn_function_type *dynFunc, void (**fn)(void)) { int status = 0; - if (dynClosure != NULL) { - (*fn) = dynClosure->fn; + if (dynFunc != NULL && dynFunc->fn != NULL) { + (*fn) = dynFunc->fn; } else { status = 1; } @@ -194,17 +242,3 @@ int dynClosure_getFnPointer(dyn_closure_type *dynClosure, void (**fn)(void)) { } -void dynClosure_destroy(dyn_closure_type *dynClosure) { - if (dynClosure != NULL) { - if (dynClosure->arguments != NULL) { - dynType_destroy(dynClosure->arguments); - } - if (dynClosure->funcReturn != NULL) { - dynType_destroy(dynClosure->funcReturn); - } - if (dynClosure->ffiClosure != NULL) { - ffi_closure_free(dynClosure->ffiClosure); - } - free(dynClosure); - } -} http://git-wip-us.apache.org/repos/asf/celix/blob/08b5bc0c/remote_services/dynamic_function_interface/dyn_function.h ---------------------------------------------------------------------- diff --git a/remote_services/dynamic_function_interface/dyn_function.h b/remote_services/dynamic_function_interface/dyn_function.h index 11994fe..3747656 100644 --- a/remote_services/dynamic_function_interface/dyn_function.h +++ b/remote_services/dynamic_function_interface/dyn_function.h @@ -19,14 +19,13 @@ typedef struct _dyn_closure_type dyn_closure_type; DFI_SETUP_LOG_HEADER(dynFunction); -int dynFunction_create(FILE *descriptorStream, dyn_type_list_type *typeReferences, void (*fn)(void), dyn_function_type **dynFunc); -int dynFunction_createWithStr(const char *descriptor, dyn_type_list_type *typeReferences, void (*fn)(void), dyn_function_type **dynFunc); +int dynFunction_parse(FILE *descriptorStream, dyn_type_list_type *typeReferences, dyn_function_type **dynFunc); +int dynFunction_parseWithStr(const char *descriptor, dyn_type_list_type *typeReferences, dyn_function_type **dynFunc); + void dynFunction_destroy(dyn_function_type *dynFunc); -int dynFunction_call(dyn_function_type *dynFunc, void *returnValue, void **argValues); +int dynFunction_call(dyn_function_type *dynFunc, void(*fn)(void), void *returnValue, void **argValues); -int dynClosure_create(FILE *descriptorStream, dyn_type_list_type *typeReferences, void (*bind)(void *, void **, void*), void *userData, dyn_closure_type **out); -int dynClosure_createWithStr(const char *descriptor, dyn_type_list_type *typeReferences, void (*bind)(void *, void **, void*), void *userData, dyn_closure_type **out); -int dynClosure_getFnPointer(dyn_closure_type *dynClosure, void(**fn)(void)); -void dynClosure_destroy(dyn_closure_type *dynClosure); +int dynFunction_createClosure(dyn_function_type *func, void (*bind)(void *, void **, void*), void *userData, void(**fn)(void)); +int dynFunction_getFnPointer(dyn_function_type *func, void (**fn)(void)); #endif http://git-wip-us.apache.org/repos/asf/celix/blob/08b5bc0c/remote_services/dynamic_function_interface/dyn_interface.c ---------------------------------------------------------------------- diff --git a/remote_services/dynamic_function_interface/dyn_interface.c b/remote_services/dynamic_function_interface/dyn_interface.c index 601669c..1fbb831 100644 --- a/remote_services/dynamic_function_interface/dyn_interface.c +++ b/remote_services/dynamic_function_interface/dyn_interface.c @@ -20,8 +20,8 @@ int dynInterface_create(const char *name, dyn_interface_type **out) { } if (status == 0) { - TAILQ_INIT(&inft->typeInfos); - TAILQ_INIT(&inft->methodInfos); + TAILQ_INIT(&inft->types); + TAILQ_INIT(&inft->methods); } *out = inft; return status; @@ -33,8 +33,8 @@ void dynInterface_destroy(dyn_interface_type *intf) { free(intf->name); } - type_info_type *tmp = NULL; - type_info_type *tInfo = TAILQ_FIRST(&intf->typeInfos); + interface_type_type *tmp = NULL; + interface_type_type *tInfo = TAILQ_FIRST(&intf->types); while (tInfo != NULL) { tmp = tInfo; tInfo = TAILQ_NEXT(tInfo, entries); @@ -42,15 +42,12 @@ void dynInterface_destroy(dyn_interface_type *intf) { if (tmp->name != NULL) { free(tmp->name); } - if (tmp->descriptor != NULL) { - free(tmp->descriptor); - } free(tmp); } - method_info_type *mTmp = NULL; - method_info_type *mInfo = TAILQ_FIRST(&intf->methodInfos); + interface_method_type *mTmp = NULL; + interface_method_type *mInfo = TAILQ_FIRST(&intf->methods); while (mInfo != NULL) { mTmp = mInfo; mInfo = TAILQ_NEXT(mInfo, entries); @@ -58,19 +55,12 @@ void dynInterface_destroy(dyn_interface_type *intf) { if (mTmp->strIdentifier != NULL) { free(mTmp->strIdentifier); } - if (mTmp->descriptor != NULL) { - free(mTmp->descriptor); - } if (mTmp->name != NULL) { free(mTmp->name); } if (mTmp->dynFunc != NULL) { dynFunction_destroy(mTmp->dynFunc); } - if (mTmp->dynClosure != NULL) { - dynClosure_destroy(mTmp->dynClosure); - } - free(mTmp); } http://git-wip-us.apache.org/repos/asf/celix/blob/08b5bc0c/remote_services/dynamic_function_interface/dyn_interface.h ---------------------------------------------------------------------- diff --git a/remote_services/dynamic_function_interface/dyn_interface.h b/remote_services/dynamic_function_interface/dyn_interface.h index 5d0b427..77eefb8 100644 --- a/remote_services/dynamic_function_interface/dyn_interface.h +++ b/remote_services/dynamic_function_interface/dyn_interface.h @@ -14,32 +14,39 @@ typedef struct _dyn_interface_type dyn_interface_type; struct _dyn_interface_type { char *name; - TAILQ_HEAD(, _type_info_type) typeInfos; - TAILQ_HEAD(, _method_info_type) methodInfos; + int versionMajor; + int versionMinor; + int versionMicro; + TAILQ_HEAD(, _interface_annotation_type) annotations; + TAILQ_HEAD(, _interface_type_type) types; + TAILQ_HEAD(, _interface_method_type) methods; }; -typedef struct _method_info_type method_info_type; -struct _method_info_type { +typedef struct _interface_annotation_type interface_annotation_type; +struct _interface_annotation_type { + char *name; + char *value; + TAILQ_ENTRY(_interface_annotation_type) entries; +}; + +typedef struct _interface_method_type interface_method_type; +struct _interface_method_type { int identifier; char *strIdentifier; - char *descriptor; char *name; dyn_function_type *dynFunc; - dyn_closure_type *dynClosure; - TAILQ_ENTRY(_method_info_type) entries; + TAILQ_ENTRY(_interface_method_type) entries; }; -typedef struct _type_info_type type_info_type; -struct _type_info_type { +typedef struct _interface_type_type interface_type_type; +struct _interface_type_type { char *name; - char *descriptor; - TAILQ_ENTRY(_type_info_type) entries; + TAILQ_ENTRY(_interface_type_type) entries; }; - -int dynInterface_create(const char *name, dyn_interface_type **out); +int dynInterface_parse(const FILE *descriptor, dyn_interface_type **out); void dynInterface_destroy(dyn_interface_type *intf); http://git-wip-us.apache.org/repos/asf/celix/blob/08b5bc0c/remote_services/dynamic_function_interface/dyn_type.c ---------------------------------------------------------------------- diff --git a/remote_services/dynamic_function_interface/dyn_type.c b/remote_services/dynamic_function_interface/dyn_type.c index 2dbb645..2915093 100644 --- a/remote_services/dynamic_function_interface/dyn_type.c +++ b/remote_services/dynamic_function_interface/dyn_type.c @@ -13,9 +13,11 @@ #include <assert.h> #include <errno.h> +#include "dyn_common.h" + DFI_SETUP_LOG(dynType) -static int dynType_createWithStream(FILE *stream, const char *name, dyn_type *parent, dyn_type_list_type *typeReferences, dyn_type **result); +static int dynType_parseWithStream(FILE *stream, const char *name, dyn_type *parent, dyn_type_list_type *typeReferences, dyn_type **result); static void dynType_clear(dyn_type *type); static void dynType_clearComplex(dyn_type *type); static void dynType_clearSequence(dyn_type *type); @@ -23,9 +25,8 @@ static void dynType_clearTypedPointer(dyn_type *type); static ffi_type * dynType_ffiTypeFor(int c); static dyn_type * dynType_findType(dyn_type *type, char *name); -static int dynType_parse(FILE *stream, dyn_type *type); +static int dynType_parseAny(FILE *stream, dyn_type *type); static int dynType_parseComplex(FILE *stream, dyn_type *type); -static int dynType_parseName(FILE *stream, char **name); static int dynType_parseNestedType(FILE *stream, dyn_type *type); static int dynType_parseReference(FILE *stream, dyn_type *type); static int dynType_parseRefByValue(FILE *stream, dyn_type *type); @@ -57,15 +58,15 @@ static const int DT_ERROR = 1; static const int DT_MEM_ERROR = 2; static const int DT_PARSE_ERROR = 3; -int dynType_create(FILE *descriptorStream, const char *name, dyn_type_list_type *typeReferences, dyn_type **type) { - return dynType_createWithStream(descriptorStream, name, NULL, typeReferences, type); +int dynType_parse(FILE *descriptorStream, const char *name, dyn_type_list_type *typeReferences, dyn_type **type) { + return dynType_parseWithStream(descriptorStream, name, NULL, typeReferences, type); } -int dynType_createWithStr(const char *descriptor, const char *name, dyn_type_list_type *typeReferences, dyn_type **type) { +int dynType_parseWithStr(const char *descriptor, const char *name, dyn_type_list_type *typeReferences, dyn_type **type) { int status = DT_OK; FILE *stream = fmemopen((char *)descriptor, strlen(descriptor), "r"); if (stream != NULL) { - status = dynType_createWithStream(stream, name, NULL, typeReferences, type); + status = dynType_parseWithStream(stream, name, NULL, typeReferences, type); if (status == DT_OK) { int c = fgetc(stream); if (c != '\0' && c != EOF) { @@ -81,7 +82,7 @@ int dynType_createWithStr(const char *descriptor, const char *name, dyn_type_lis return status; } -static int dynType_createWithStream(FILE *stream, const char *name, dyn_type *parent, dyn_type_list_type *typeReferences, dyn_type **result) { +static int dynType_parseWithStream(FILE *stream, const char *name, dyn_type *parent, dyn_type_list_type *typeReferences, dyn_type **result) { int status = DT_OK; dyn_type *type = calloc(1, sizeof(*type)); if (type != NULL) { @@ -97,7 +98,7 @@ static int dynType_createWithStream(FILE *stream, const char *name, dyn_type *pa } } if (status == DT_OK) { - status = dynType_parse(stream, type); + status = dynType_parseAny(stream, type); } if (status == DT_OK) { *result = type; @@ -111,7 +112,7 @@ static int dynType_createWithStream(FILE *stream, const char *name, dyn_type *pa return status; } -static int dynType_parse(FILE *stream, dyn_type *type) { +static int dynType_parseAny(FILE *stream, dyn_type *type) { int status = DT_OK; int c = fgetc(stream); @@ -119,7 +120,7 @@ static int dynType_parse(FILE *stream, dyn_type *type) { case 'T' : status = dynType_parseNestedType(stream, type); if (status == DT_OK) { - status = dynType_parse(stream, type); + status = dynType_parseAny(stream, type); } break; case 'L' : @@ -162,7 +163,7 @@ static int dynType_parseComplex(FILE *stream, dyn_type *type) { entry->type.type = DYN_TYPE_INVALID; TAILQ_INIT(&entry->type.nestedTypesHead); TAILQ_INSERT_TAIL(&type->complex.entriesHead, entry, entries); - status = dynType_parse(stream, &entry->type); + status = dynType_parseAny(stream, &entry->type); } else { status = DT_MEM_ERROR; LOG_ERROR("Error allocating memory for type"); @@ -173,7 +174,7 @@ static int dynType_parseComplex(FILE *stream, dyn_type *type) { entry = TAILQ_FIRST(&type->complex.entriesHead); char *name = NULL; while (c == ' ' && entry != NULL) { - status = dynType_parseName(stream, &name); + status = dynCommon_parseName(stream, &name); if (status == DT_OK) { entry->name = name; entry = TAILQ_NEXT(entry, entries); @@ -224,31 +225,6 @@ static int dynType_parseComplex(FILE *stream, dyn_type *type) { return status; } -static int dynType_parseName(FILE *stream, char **result) { - int status = DT_OK; - - char *buf = NULL; - size_t size = 0; - FILE *name = open_memstream(&buf, &size); - - if (name != NULL) { - int c = getc(stream); - while (isalnum(c) || c == '_') { - fputc(c, name); - c = getc(stream); - } - fflush(name); - fclose(name); - *result = buf; - ungetc(c, stream); - } else { - status = DT_ERROR; - LOG_ERROR("Error creating mem stream for name. %s", strerror(errno)); - } - - return status; -} - static int dynType_parseNestedType(FILE *stream, dyn_type *type) { int status = DT_OK; char *name = NULL; @@ -260,7 +236,7 @@ static int dynType_parseNestedType(FILE *stream, dyn_type *type) { entry->type.type = DYN_TYPE_INVALID; TAILQ_INIT(&entry->type.nestedTypesHead); TAILQ_INSERT_TAIL(&type->nestedTypesHead, entry, entries); - status = dynType_parseName(stream, &name); + status = dynCommon_parseName(stream, &name); entry->type.name = name; } else { status = DT_MEM_ERROR; @@ -275,7 +251,7 @@ static int dynType_parseNestedType(FILE *stream, dyn_type *type) { } if (status == DT_OK) { - status = dynType_parse(stream, &entry->type); + status = dynType_parseAny(stream, &entry->type); int c = fgetc(stream); if (c != ';') { status = DT_PARSE_ERROR; @@ -316,7 +292,7 @@ static int dynType_parseRefByValue(FILE *stream, dyn_type *type) { type->descriptor = 'l'; char *name = NULL; - status = dynType_parseName(stream, &name); + status = dynCommon_parseName(stream, &name); if (status == DT_OK) { dyn_type *ref = dynType_findType(type, name); free(name); @@ -347,7 +323,7 @@ static int dynType_parseSequence(FILE *stream, dyn_type *type) { type->descriptor = '['; type->sequence.seqType.elements = seq_types; - status = dynType_createWithStream(stream, NULL, type, NULL, &type->sequence.itemType); + status = dynType_parseWithStream(stream, NULL, type, NULL, &type->sequence.itemType); if (status == DT_OK) { type->ffiType = &type->sequence.seqType; @@ -378,7 +354,7 @@ static int dynType_parseTypedPointer(FILE *stream, dyn_type *type) { type->descriptor = '*'; type->ffiType = &ffi_type_pointer; - status = dynType_createWithStream(stream, NULL, type, NULL, &type->typedPointer.typedType); + status = dynType_parseWithStream(stream, NULL, type, NULL, &type->typedPointer.typedType); return status; } http://git-wip-us.apache.org/repos/asf/celix/blob/08b5bc0c/remote_services/dynamic_function_interface/dyn_type.h ---------------------------------------------------------------------- diff --git a/remote_services/dynamic_function_interface/dyn_type.h b/remote_services/dynamic_function_interface/dyn_type.h index a02b347..bc19836 100644 --- a/remote_services/dynamic_function_interface/dyn_type.h +++ b/remote_services/dynamic_function_interface/dyn_type.h @@ -125,8 +125,8 @@ struct nested_entry { DFI_SETUP_LOG_HEADER(dynType); //generic -int dynType_create(FILE *descriptorStream, const char *name, dyn_type_list_type *typeReferences, dyn_type **type); -int dynType_createWithStr(const char *descriptor, const char *name, dyn_type_list_type *typeReferences, dyn_type **type); +int dynType_parse(FILE *descriptorStream, const char *name, dyn_type_list_type *typeReferences, dyn_type **type); +int dynType_parseWithStr(const char *descriptor, const char *name, dyn_type_list_type *typeReferences, dyn_type **type); void dynType_destroy(dyn_type *type); int dynType_alloc(dyn_type *type, void **bufLoc); http://git-wip-us.apache.org/repos/asf/celix/blob/08b5bc0c/remote_services/dynamic_function_interface/tst/avro_descriptor_translator_tests.cpp ---------------------------------------------------------------------- diff --git a/remote_services/dynamic_function_interface/tst/avro_descriptor_translator_tests.cpp b/remote_services/dynamic_function_interface/tst/avro_descriptor_translator_tests.cpp index 8f8dbc8..63401e5 100644 --- a/remote_services/dynamic_function_interface/tst/avro_descriptor_translator_tests.cpp +++ b/remote_services/dynamic_function_interface/tst/avro_descriptor_translator_tests.cpp @@ -10,6 +10,7 @@ extern "C" { #include <assert.h> #include <string.h> +#include "dyn_common.h" #include "descriptor_translator.h" #if defined(BSD) || defined(__APPLE__) @@ -142,6 +143,7 @@ TEST_GROUP(AvroDescTranslatorTest) { descriptorTranslator_logSetup(stdLog, NULL, 3); dynInterface_logSetup(stdLog, NULL, 3); dynType_logSetup(stdLog, NULL, 3); + dynCommon_logSetup(stdLog, NULL, 3); } }; http://git-wip-us.apache.org/repos/asf/celix/blob/08b5bc0c/remote_services/dynamic_function_interface/tst/dyn_closure_tests.cpp ---------------------------------------------------------------------- diff --git a/remote_services/dynamic_function_interface/tst/dyn_closure_tests.cpp b/remote_services/dynamic_function_interface/tst/dyn_closure_tests.cpp index 072c0f3..d01b22e 100644 --- a/remote_services/dynamic_function_interface/tst/dyn_closure_tests.cpp +++ b/remote_services/dynamic_function_interface/tst/dyn_closure_tests.cpp @@ -12,6 +12,7 @@ extern "C" { #include <string.h> #include <ctype.h> +#include "dyn_common.h" #include "dyn_function.h" static int g_count; @@ -75,27 +76,27 @@ static void example3_binding(void *userData, void* args[], void *out) { } static void tests() { - dyn_closure_type *dynClosure = NULL; + dyn_function_type *dynFunction = NULL; int rc = 0; { - rc = dynClosure_createWithStr(EXAMPLE1_DESCRIPTOR, NULL, example1_binding, NULL, &dynClosure); - CHECK_EQUAL(0, rc); int32_t (*func)(int32_t a, int32_t b, int32_t c) = NULL; - int rc = dynClosure_getFnPointer(dynClosure, (void(**)(void))&func); + rc = dynFunction_parseWithStr(EXAMPLE1_DESCRIPTOR, NULL, &dynFunction); + CHECK_EQUAL(0, rc); + rc = dynFunction_createClosure(dynFunction, example1_binding, NULL, (void(**)(void))&func); CHECK_EQUAL(0, rc); int32_t ret = func(2,3,4); CHECK_EQUAL(1, g_count); - CHECK_EQUAL(9, ret); - dynClosure_destroy(dynClosure); + CHECK_EQUAL(9, ret); + dynFunction_destroy(dynFunction); } { - dynClosure = NULL; - rc = dynClosure_createWithStr(EXAMPLE2_DESCRIPTOR, NULL, example2_binding, NULL, &dynClosure); - CHECK_EQUAL(0, rc); double (*func)(int32_t a, struct example2_arg2 b, int32_t c) = NULL; - rc = dynClosure_getFnPointer(dynClosure, (void(**)(void))&func); + dynFunction = NULL; + rc = dynFunction_parseWithStr(EXAMPLE2_DESCRIPTOR, NULL, &dynFunction); + CHECK_EQUAL(0, rc); + rc = dynFunction_createClosure(dynFunction, example2_binding, NULL, (void(**)(void))&func); CHECK_EQUAL(0, rc); struct example2_arg2 b; b.val1 = 1.0; @@ -103,21 +104,21 @@ static void tests() { b.val3 = 2.0; double ret = func(2,b,4); CHECK_EQUAL(2, g_count); - CHECK_EQUAL(10.5, ret); - dynClosure_destroy(dynClosure); + CHECK_EQUAL(10.5, ret); + dynFunction_destroy(dynFunction); } { - dynClosure = NULL; - rc = dynClosure_createWithStr(EXAMPLE3_DESCRIPTOR, NULL, example3_binding, NULL, &dynClosure); - CHECK_EQUAL(0, rc); struct example3_ret * (*func)(int32_t a, int32_t b, int32_t c) = NULL; - rc = dynClosure_getFnPointer(dynClosure, (void(**)(void))&func); + dynFunction = NULL; + rc = dynFunction_parseWithStr(EXAMPLE3_DESCRIPTOR, NULL, &dynFunction); + CHECK_EQUAL(0, rc); + rc = dynFunction_createClosure(dynFunction, example3_binding, NULL, (void(**)(void))&func); CHECK_EQUAL(0, rc); struct example3_ret *ret = func(2,8,4); CHECK_EQUAL(3, g_count); - CHECK_EQUAL(14, ret->sum); - dynClosure_destroy(dynClosure); + CHECK_EQUAL(14, ret->sum); + dynFunction_destroy(dynFunction); free(ret); } } @@ -130,6 +131,7 @@ TEST_GROUP(DynClosureTests) { dynFunction_logSetup(stdLog, NULL, 3); dynType_logSetup(stdLog, NULL, 3); //TODO dynType_logSetup(stdLog, NULL, 4); + dynCommon_logSetup(stdLog, NULL, 3); g_count = 0; } }; http://git-wip-us.apache.org/repos/asf/celix/blob/08b5bc0c/remote_services/dynamic_function_interface/tst/dyn_function_tests.cpp ---------------------------------------------------------------------- diff --git a/remote_services/dynamic_function_interface/tst/dyn_function_tests.cpp b/remote_services/dynamic_function_interface/tst/dyn_function_tests.cpp index 478a70c..410b3ec 100644 --- a/remote_services/dynamic_function_interface/tst/dyn_function_tests.cpp +++ b/remote_services/dynamic_function_interface/tst/dyn_function_tests.cpp @@ -11,6 +11,8 @@ extern "C" { #include <string.h> #include <ctype.h> + + #include "dyn_common.h" #include "dyn_function.h" static void stdLog(void *handle, int level, const char *file, int line, const char *msg, ...) { @@ -35,7 +37,7 @@ extern "C" { int rc; void (*fp)(void) = (void (*)(void)) example1; - rc = dynFunction_createWithStr(EXAMPLE1_DESCRIPTOR, NULL, fp, &dynFunc); + rc = dynFunction_parseWithStr(EXAMPLE1_DESCRIPTOR, NULL, &dynFunc); CHECK_EQUAL(0, rc); int32_t a = 2; @@ -47,7 +49,7 @@ extern "C" { values[1] = &b; values[2] = &c; - rc = dynFunction_call(dynFunc, &rVal, values); + rc = dynFunction_call(dynFunc, fp, &rVal, values); CHECK_EQUAL(0, rc); CHECK_EQUAL(1, rVal); dynFunction_destroy(dynFunc); @@ -74,7 +76,7 @@ extern "C" { int rc; void (*fp)(void) = (void (*)(void)) example2; - rc = dynFunction_createWithStr(EXAMPLE2_DESCRIPTOR, NULL, fp, &dynFunc); + rc = dynFunction_parseWithStr(EXAMPLE2_DESCRIPTOR, NULL, &dynFunc); CHECK_EQUAL(0, rc); int32_t arg1 = 2; @@ -89,7 +91,7 @@ extern "C" { values[1] = &arg2; values[2] = &arg3; - rc = dynFunction_call(dynFunc, &returnVal, values); + rc = dynFunction_call(dynFunc, fp, &returnVal, values); CHECK_EQUAL(0, rc); CHECK_EQUAL(2.2, returnVal); dynFunction_destroy(dynFunc); @@ -100,6 +102,7 @@ TEST_GROUP(DynFunctionTests) { void setup() { dynFunction_logSetup(stdLog, NULL, 3); dynType_logSetup(stdLog, NULL, 3); + dynCommon_logSetup(stdLog, NULL, 3); } }; http://git-wip-us.apache.org/repos/asf/celix/blob/08b5bc0c/remote_services/dynamic_function_interface/tst/dyn_type_tests.cpp ---------------------------------------------------------------------- diff --git a/remote_services/dynamic_function_interface/tst/dyn_type_tests.cpp b/remote_services/dynamic_function_interface/tst/dyn_type_tests.cpp index bd3c333..96bebdc 100644 --- a/remote_services/dynamic_function_interface/tst/dyn_type_tests.cpp +++ b/remote_services/dynamic_function_interface/tst/dyn_type_tests.cpp @@ -8,6 +8,7 @@ extern "C" { #include <string.h> #include <stdarg.h> + #include "dyn_common.h" #include "dyn_type.h" static void stdLog(void *handle, int level, const char *file, int line, const char *msg, ...) { @@ -24,7 +25,7 @@ extern "C" { int i; type = NULL; printf("\n-- example %s with descriptor string '%s' --\n", exName, descriptorStr); - int status = dynType_createWithStr(descriptorStr, exName, NULL, &type); + int status = dynType_parseWithStr(descriptorStr, exName, NULL, &type); CHECK_EQUAL(0, status); if (status == 0) { dynType_print(type); @@ -106,7 +107,7 @@ TEST(DynTypeTests, ParseRandomGarbageTest) { //printf("ParseRandomGarbageTest iteration %i with descriptor string '%s'\n", k, descriptorStr); dyn_type *type = NULL; - int status = dynType_createWithStr(descriptorStr, NULL, NULL, &type); + int status = dynType_parseWithStr(descriptorStr, NULL, NULL, &type); if (status == 0) { dynType_destroy(type); } @@ -122,7 +123,7 @@ TEST(DynTypeTests, AssignTest1) { struct ex1 inst; const char *desc = "{III a b c}"; dyn_type *type = NULL; - int status = dynType_createWithStr(desc, NULL, NULL, &type); + int status = dynType_parseWithStr(desc, NULL, NULL, &type); CHECK_EQUAL(0, status); int32_t val1 = 2; int32_t val2 = 4; @@ -133,6 +134,8 @@ TEST(DynTypeTests, AssignTest1) { CHECK_EQUAL(4, inst.b); dynType_complex_setValueAt(type, 2, &inst, &val3); CHECK_EQUAL(8, inst.c); + + dynType_destroy(type); } TEST(DynTypeTests, AssignTest2) { @@ -146,7 +149,7 @@ TEST(DynTypeTests, AssignTest2) { struct ex inst; const char *desc = "{I{DD a b} a b}"; dyn_type *type = NULL; - int status = dynType_createWithStr(desc, NULL, NULL, &type); + int status = dynType_parseWithStr(desc, NULL, NULL, &type); CHECK_EQUAL(0, status); int32_t a = 2; double b_a = 1.1; @@ -165,6 +168,8 @@ TEST(DynTypeTests, AssignTest2) { dynType_complex_setValueAt(subType, 1, &inst.b, &b_b); CHECK_EQUAL(1.2, inst.b.b); + + dynType_destroy(type); } http://git-wip-us.apache.org/repos/asf/celix/blob/08b5bc0c/remote_services/dynamic_function_interface/tst/json_serializer_tests.cpp ---------------------------------------------------------------------- diff --git a/remote_services/dynamic_function_interface/tst/json_serializer_tests.cpp b/remote_services/dynamic_function_interface/tst/json_serializer_tests.cpp index 1ec4655..cd61e6e 100644 --- a/remote_services/dynamic_function_interface/tst/json_serializer_tests.cpp +++ b/remote_services/dynamic_function_interface/tst/json_serializer_tests.cpp @@ -13,6 +13,7 @@ extern "C" { #include <ffi.h> +#include "dyn_common.h" #include "dyn_type.h" #include "json_serializer.h" @@ -136,7 +137,7 @@ static void tests() { type = NULL; inst = NULL; - rc = dynType_createWithStr(example1_descriptor, NULL, NULL, &type); + rc = dynType_parseWithStr(example1_descriptor, NULL, NULL, &type); CHECK_EQUAL(0, rc); rc = json_deserialize(type, example1_input, &inst); CHECK_EQUAL(0, rc); @@ -146,7 +147,7 @@ static void tests() { type = NULL; inst = NULL; - rc = dynType_createWithStr(example2_descriptor, NULL, NULL, &type); + rc = dynType_parseWithStr(example2_descriptor, NULL, NULL, &type); CHECK_EQUAL(0, rc); rc = json_deserialize(type, example2_input, &inst); CHECK_EQUAL(0, rc); @@ -155,7 +156,7 @@ static void tests() { type = NULL; inst = NULL; - rc = dynType_createWithStr(example3_descriptor, NULL, NULL, &type); + rc = dynType_parseWithStr(example3_descriptor, NULL, NULL, &type); CHECK_EQUAL(0, rc); rc = json_deserialize(type, example3_input, &inst); CHECK_EQUAL(0, rc); @@ -163,7 +164,7 @@ static void tests() { type = NULL; inst = NULL; - rc = dynType_createWithStr(example4_descriptor, NULL, NULL, &type); + rc = dynType_parseWithStr(example4_descriptor, NULL, NULL, &type); CHECK_EQUAL(0, rc); rc = json_deserialize(type, example4_input, &inst); CHECK_EQUAL(0, rc); @@ -174,6 +175,7 @@ static void tests() { TEST_GROUP(JsonSerializerTests) { void setup() { dynType_logSetup(stdLog, NULL, 3); + dynCommon_logSetup(stdLog, NULL, 3); } };
