CELIX-237: refactoring of dyn_type. moved struct to c file (private). Removed 
mem leaks with valgrind


Project: http://git-wip-us.apache.org/repos/asf/celix/repo
Commit: http://git-wip-us.apache.org/repos/asf/celix/commit/f9ed9b33
Tree: http://git-wip-us.apache.org/repos/asf/celix/tree/f9ed9b33
Diff: http://git-wip-us.apache.org/repos/asf/celix/diff/f9ed9b33

Branch: refs/heads/feature/CELIX-237_rsa-ffi
Commit: f9ed9b33b1893bfa34ac7516be42a82fed152a2d
Parents: e995474
Author: Pepijn Noltes <pepijnnol...@gmail.com>
Authored: Fri Jul 31 15:43:27 2015 +0200
Committer: Pepijn Noltes <pepijnnol...@gmail.com>
Committed: Fri Jul 31 15:43:27 2015 +0200

----------------------------------------------------------------------
 .../dynamic_function_interface/dyn_function.c   |  10 +-
 .../dynamic_function_interface/dyn_function.h   |   4 +-
 .../dynamic_function_interface/dyn_interface.h  |   2 +-
 .../dynamic_function_interface/dyn_type.c       | 184 +++++++++++++++----
 .../dynamic_function_interface/dyn_type.h       |  54 ++----
 .../json_serializer.c                           |  18 +-
 .../tst/dyn_interface_tests.cpp                 |   3 +-
 .../tst/dyn_type_tests.cpp                      |  17 +-
 .../tst/json_serializer_tests.cpp               |  20 +-
 9 files changed, 203 insertions(+), 109 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/celix/blob/f9ed9b33/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 68c3a11..0ae2a8a 100644
--- a/remote_services/dynamic_function_interface/dyn_function.c
+++ b/remote_services/dynamic_function_interface/dyn_function.c
@@ -19,7 +19,7 @@ DFI_SETUP_LOG(dynFunction)
 
 struct _dyn_function_type {
     char *name;
-    struct reference_types_head *refTypes; //NOTE not owned
+    struct types_head *refTypes; //NOTE not owned
     TAILQ_HEAD(,_dyn_function_argument_type) arguments;
     ffi_type **ffiArguments;
     dyn_type *funcReturn;
@@ -49,7 +49,7 @@ 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 reference_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;
     LOG_DEBUG("Creating dyn function", descriptor);
@@ -79,7 +79,7 @@ int dynFunction_parse(FILE *descriptor, struct 
reference_types_head *refTypes, d
     return status;
 }
 
-int dynFunction_parseWithStr(const char *descriptor, struct 
reference_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), "r");
     if (stream != NULL) {
@@ -151,11 +151,11 @@ static int dynFunction_initCif(dyn_function_type 
*dynFunc) {
     dynFunc->ffiArguments = calloc(count, sizeof(ffi_type));
 
     TAILQ_FOREACH(entry, &dynFunc->arguments, entries) {
-        dynFunc->ffiArguments[entry->index] = entry->type->ffiType;
+        dynFunc->ffiArguments[entry->index] = dynType_ffiType(entry->type);
     }
     
     ffi_type **args = dynFunc->ffiArguments;
-    ffi_type *returnType = dynFunc->funcReturn->ffiType;
+    ffi_type *returnType = dynType_ffiType(dynFunc->funcReturn);
 
     int ffiResult = ffi_prep_cif(&dynFunc->cif, FFI_DEFAULT_ABI, count, 
returnType, args);
     if (ffiResult != FFI_OK) {

http://git-wip-us.apache.org/repos/asf/celix/blob/f9ed9b33/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 098c76e..2d5d6bb 100644
--- a/remote_services/dynamic_function_interface/dyn_function.h
+++ b/remote_services/dynamic_function_interface/dyn_function.h
@@ -19,8 +19,8 @@ typedef struct _dyn_closure_type dyn_closure_type;
 
 DFI_SETUP_LOG_HEADER(dynFunction);
 
-int dynFunction_parse(FILE *descriptorStream, struct reference_types_head 
*refTypes, dyn_function_type **dynFunc);
-int dynFunction_parseWithStr(const char *descriptor, struct 
reference_types_head *refTypes, dyn_function_type **dynFunc);
+int dynFunction_parse(FILE *descriptorStream, struct types_head *refTypes, 
dyn_function_type **dynFunc);
+int dynFunction_parseWithStr(const char *descriptor, struct types_head 
*refTypes, dyn_function_type **dynFunc);
 
 void dynFunction_destroy(dyn_function_type *dynFunc);
 int dynFunction_call(dyn_function_type *dynFunc, void(*fn)(void), void 
*returnValue, void **argValues);

http://git-wip-us.apache.org/repos/asf/celix/blob/f9ed9b33/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 41f239f..0b0898f 100644
--- a/remote_services/dynamic_function_interface/dyn_interface.h
+++ b/remote_services/dynamic_function_interface/dyn_interface.h
@@ -32,7 +32,7 @@ typedef struct _dyn_interface_type dyn_interface_type;
 struct _dyn_interface_type {
     struct namvals_head header;
     struct namvals_head annotations;
-    struct reference_types_head types;
+    struct types_head types;
     struct methods_head methods;
 };
 

http://git-wip-us.apache.org/repos/asf/celix/blob/f9ed9b33/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 87ebb5d..af98fe9 100644
--- a/remote_services/dynamic_function_interface/dyn_type.c
+++ b/remote_services/dynamic_function_interface/dyn_type.c
@@ -17,12 +17,14 @@
 
 DFI_SETUP_LOG(dynType)
 
-static int dynType_parseWithStream(FILE *stream, const char *name, dyn_type 
*parent, struct reference_types_head *refTypes, dyn_type **result);
+static int dynType_parseWithStream(FILE *stream, const char *name, dyn_type 
*parent, struct types_head *refTypes, dyn_type **result);
 static void dynType_clear(dyn_type *type);
 static void dynType_clearComplex(dyn_type *type);
 static void dynType_clearSequence(dyn_type *type);
 static void dynType_clearTypedPointer(dyn_type *type);
 
+static struct type_entry *dynType_allocTypeEntry(void);
+
 static ffi_type * dynType_ffiTypeFor(int c);
 static dyn_type * dynType_findType(dyn_type *type, char *name);
 static int dynType_parseAny(FILE *stream, dyn_type *type);
@@ -48,6 +50,9 @@ static void dynType_printComplexType(dyn_type *type, FILE 
*stream);
 static void dynType_printSimpleType(dyn_type *type, FILE *stream);
 
 static int dynType_parseText(FILE *stream, dyn_type *type);
+void dynType_freeComplexType(dyn_type *type, void *loc);
+void dynType_deepFree(dyn_type *type, void *loc, bool alsoDeleteSelf);
+void dynType_freeSequenceType(dyn_type *type, void *seqLoc);
 
 struct generic_sequence {
     uint32_t cap;
@@ -55,16 +60,43 @@ struct generic_sequence {
     void *buf;
 };
 
+struct _dyn_type {
+    char *name;
+    char descriptor;
+    int type;
+    ffi_type *ffiType;
+    dyn_type *parent;
+    struct types_head *referenceTypes; //NOTE: not owned
+    struct types_head nestedTypesHead;
+    union {
+        struct {
+            struct complex_type_entries_head entriesHead;
+            ffi_type structType; //dyn_type.ffiType points to this
+            dyn_type **types; //based on entriesHead for fast access
+        } complex;
+        struct {
+            ffi_type seqType; //dyn_type.ffiType points to this
+            dyn_type *itemType;
+        } sequence;
+        struct {
+            dyn_type *typedType;
+        } typedPointer;
+        struct {
+            dyn_type *ref;
+        } ref;
+    };
+};
+
 static const int OK = 0;
 static const int ERROR = 1;
 static const int MEM_ERROR = 2;
 static const int PARSE_ERROR = 3;
 
-int dynType_parse(FILE *descriptorStream, const char *name, struct 
reference_types_head *refTypes, dyn_type **type) {
+int dynType_parse(FILE *descriptorStream, const char *name, struct types_head 
*refTypes, dyn_type **type) {
     return dynType_parseWithStream(descriptorStream, name, NULL, refTypes, 
type);
 }
 
-int dynType_parseWithStr(const char *descriptor, const char *name, struct 
reference_types_head *refTypes, dyn_type **type) {
+int dynType_parseWithStr(const char *descriptor, const char *name, struct 
types_head *refTypes, dyn_type **type) {
     int status = OK;
     FILE *stream = fmemopen((char *)descriptor, strlen(descriptor), "r");
     if (stream != NULL) {
@@ -84,7 +116,7 @@ int dynType_parseWithStr(const char *descriptor, const char 
*name, struct refere
     return status;
 }
 
-static int dynType_parseWithStream(FILE *stream, const char *name, dyn_type 
*parent, struct reference_types_head *refTypes, dyn_type **result) {
+static int dynType_parseWithStream(FILE *stream, const char *name, dyn_type 
*parent, struct types_head *refTypes, dyn_type **result) {
     int status = OK;
     dyn_type *type = calloc(1, sizeof(*type));
     if (type != NULL) {
@@ -172,12 +204,18 @@ static int dynType_parseComplex(FILE *stream, dyn_type 
*type) {
         ungetc(c,stream);
         entry = calloc(1, sizeof(*entry));
         if (entry != NULL) {
-            entry->type.parent = type;
-            entry->type.type = DYN_TYPE_INVALID;
-            TAILQ_INIT(&entry->type.nestedTypesHead);
+            entry->type = calloc(1, sizeof(*entry->type));
+        }
+        if (entry != NULL && entry->type != NULL) {
+            entry->type->parent = type;
+            entry->type->type = DYN_TYPE_INVALID;
+            TAILQ_INIT(&entry->type->nestedTypesHead);
             TAILQ_INSERT_TAIL(&type->complex.entriesHead, entry, entries);
-            status = dynType_parseAny(stream, &entry->type);
+            status = dynType_parseAny(stream, entry->type);
         } else {
+            if (entry != NULL) {
+                free(entry);
+            }
             status = MEM_ERROR;
             LOG_ERROR("Error allocating memory for type");
         }
@@ -209,7 +247,7 @@ static int dynType_parseComplex(FILE *stream, dyn_type 
*type) {
         if (type->complex.structType.elements != NULL) {
             int index = 0;
             TAILQ_FOREACH(entry, &type->complex.entriesHead, entries) {
-                type->complex.structType.elements[index++] = 
entry->type.ffiType;
+                type->complex.structType.elements[index++] = 
entry->type->ffiType;
             }
         } else {
             status = MEM_ERROR;
@@ -222,7 +260,7 @@ static int dynType_parseComplex(FILE *stream, dyn_type 
*type) {
         if (type != NULL) {
             int index = 0;
             TAILQ_FOREACH(entry, &type->complex.entriesHead, entries) {
-                type->complex.types[index++] = &entry->type;
+                type->complex.types[index++] = entry->type;
             }
         } else {
             status = MEM_ERROR;
@@ -241,18 +279,19 @@ static int dynType_parseComplex(FILE *stream, dyn_type 
*type) {
 static int dynType_parseNestedType(FILE *stream, dyn_type *type) {
     int status = OK;
     char *name = NULL;
-    struct nested_entry *entry = NULL; 
+    struct type_entry *entry = NULL;
 
-    entry = calloc(1, sizeof(*entry));
+    entry = dynType_allocTypeEntry();
     if (entry != NULL) {
-        entry->type.parent = type;
-        entry->type.type = DYN_TYPE_INVALID;
-        TAILQ_INIT(&entry->type.nestedTypesHead);
+        entry->type->parent = type;
+        entry->type->type = DYN_TYPE_INVALID;
+        TAILQ_INIT(&entry->type->nestedTypesHead);
         TAILQ_INSERT_TAIL(&type->nestedTypesHead, entry, entries);
         status = dynCommon_parseName(stream, &name);
-        entry->type.name = name;
+        entry->type->name = name;
     } else {
         status = MEM_ERROR;
+        LOG_ERROR("Error allocating entry");
     }     
 
     if (status == OK) {
@@ -264,7 +303,7 @@ static int dynType_parseNestedType(FILE *stream, dyn_type 
*type) {
     }
 
     if (status == OK) {
-        status = dynType_parseAny(stream, &entry->type);
+        status = dynType_parseAny(stream, entry->type);
         int c = fgetc(stream);
         if (c != ';') {
             status = PARSE_ERROR;
@@ -328,6 +367,18 @@ static int dynType_parseRefByValue(FILE *stream, dyn_type 
*type) {
     return status;
 }
 
+static struct type_entry *dynType_allocTypeEntry(void) {
+    struct type_entry *entry = calloc(1, sizeof(*entry));
+    if (entry != NULL) {
+        entry->type = calloc(1, sizeof(*entry->type));
+        if (entry->type == NULL) {
+            free(entry);
+            entry = NULL;
+        }
+    }
+    return entry;
+}
+
 static ffi_type *seq_types[] = {&ffi_type_uint32, &ffi_type_uint32, 
&ffi_type_pointer, NULL};
 
 static int dynType_parseSequence(FILE *stream, dyn_type *type) {
@@ -387,12 +438,15 @@ void dynType_destroy(dyn_type *type) {
 }
 
 static void dynType_clear(dyn_type *type) {
-    struct nested_entry *entry = TAILQ_FIRST(&type->nestedTypesHead);
-    struct nested_entry *tmp = NULL;
+    struct type_entry *entry = TAILQ_FIRST(&type->nestedTypesHead);
+    struct type_entry *tmp = NULL;
     while (entry != NULL) {
         tmp = entry;
         entry = TAILQ_NEXT(entry, entries);
-        dynType_clear(&tmp->type);
+        if (tmp->type != NULL) {
+            dynType_destroy(tmp->type);
+            tmp->type = NULL;
+        }
         free(tmp);
     }
 
@@ -418,7 +472,7 @@ static void dynType_clearComplex(dyn_type *type) {
     struct complex_type_entry *entry = TAILQ_FIRST(&type->complex.entriesHead);
     struct complex_type_entry *tmp = NULL;
     while (entry != NULL) {
-        dynType_clear(&entry->type);
+        dynType_destroy(entry->type);
         if (entry->name != NULL) {
             free(entry->name);
         }
@@ -512,7 +566,7 @@ int dynType_complex_entries(dyn_type *type, struct 
complex_type_entries_head **e
 }
 
 //sequence
-int dynType_sequence_alloc(dyn_type *type, void *inst, int cap, void **out) {
+int dynType_sequence_alloc(dyn_type *type, void *inst, uint32_t cap) {
     assert(type->type == DYN_TYPE_SEQUENCE);
     int status = OK;
     struct generic_sequence *seq = inst;
@@ -521,8 +575,7 @@ int dynType_sequence_alloc(dyn_type *type, void *inst, int 
cap, void **out) {
         seq->buf = calloc(cap, size);
         if (seq->buf != NULL) {
             seq->cap = cap;
-            seq->len = 0;
-            *out = seq->buf;
+            seq->len = 0;;
         } else {
             seq->cap = 0;
             status = MEM_ERROR;
@@ -536,10 +589,59 @@ int dynType_sequence_alloc(dyn_type *type, void *inst, 
int cap, void **out) {
 }
 
 void dynType_free(dyn_type *type, void *loc) {
-    //TODO
-    LOG_INFO("TODO dynType_free");
+    dynType_deepFree(type, loc, true);
+}
+
+void dynType_deepFree(dyn_type *type, void *loc, bool alsoDeleteSelf) {
+    if (loc != NULL) {
+        dyn_type *subType = NULL;
+        const char *text = NULL;
+        switch (type->type) {
+            case DYN_TYPE_COMPLEX :
+                dynType_freeComplexType(type, loc);
+                break;
+            case DYN_TYPE_SEQUENCE :
+                dynType_freeSequenceType(type, loc);
+                break;
+            case DYN_TYPE_TYPED_POINTER:
+                dynType_typedPointer_getTypedType(type, &subType);
+                dynType_deepFree(subType, *(void **)loc, true);
+                break;
+            case DYN_TYPE_TEXT :
+                text = *(const char **)loc;
+                free(text);
+                break;
+        }
+
+        if (alsoDeleteSelf) {
+            free(loc);
+        }
+    }
+}
+
+void dynType_freeSequenceType(dyn_type *type, void *seqLoc) {
+    struct generic_sequence *seq = seqLoc;
+    dyn_type *itemType = dynType_sequence_itemType(type);
+    void *itemLoc = NULL;
+    int i;
+    for (i = 0; i < seq->len; i += 1) {
+        dynType_sequence_locForIndex(type, seqLoc, i, &itemLoc);
+        dynType_deepFree(itemType, itemLoc, false);
+    }
+    free(seq->buf);
 }
 
+void dynType_freeComplexType(dyn_type *type, void *loc) {
+    struct complex_type_entry *entry = NULL;
+    int index = 0;
+    void *entryLoc = NULL;
+    TAILQ_FOREACH(entry, &type->complex.entriesHead, entries) {
+        dynType_complex_valLocAt(type, index++, loc, &entryLoc);
+        dynType_deepFree(entry->type, entryLoc, false);
+    }
+}
+
+
 uint32_t dynType_sequence_length(void *seqLoc) {
     struct generic_sequence *seq = seqLoc;
     return seq->len;
@@ -616,6 +718,13 @@ int dynType_descriptorType(dyn_type *type) {
     return type->descriptor;
 }
 
+ffi_type *dynType_ffiType(dyn_type *type) {
+    if (type->type == DYN_TYPE_REF) {
+        return type->ref.ref->ffiType;
+    }
+    return type->ffiType;
+}
+
 static ffi_type * dynType_ffiTypeFor(int c) {
     ffi_type *type = NULL;
     switch (c) {
@@ -674,11 +783,11 @@ static dyn_type * dynType_findType(dyn_type *type, char 
*name) {
     }
 
     if (result == NULL) {
-        struct nested_entry *nEntry = NULL;
+        struct type_entry *nEntry = NULL;
         TAILQ_FOREACH(nEntry, &type->nestedTypesHead, entries) {
-            LOG_DEBUG("checking nested type '%s' with name '%s'", 
nEntry->type.name, name);
-            if (strcmp(name, nEntry->type.name) == 0) {
-                result = &nEntry->type;
+            LOG_DEBUG("checking nested type '%s' with name '%s'", 
nEntry->type->name, name);
+            if (strcmp(name, nEntry->type->name) == 0) {
+                result = nEntry->type;
                 break;
             }
         }
@@ -736,6 +845,7 @@ int dynType_typedPointer_getTypedType(dyn_type *type, 
dyn_type **out) {
 
 
 int dynType_text_allocAndInit(dyn_type *type, void *textLoc, const char 
*value) {
+    assert(type->type == DYN_TYPE_TEXT);
     int status = 0;
     const char *str = strdup(value);
     char const **loc = textLoc;
@@ -811,7 +921,7 @@ static void dynType_printComplex(char *name, dyn_type 
*type, int depth, FILE *st
 
         struct complex_type_entry *entry = NULL;
         TAILQ_FOREACH(entry, &type->complex.entriesHead, entries) {
-            dynType_printAny(entry->name, &entry->type, depth + 1, stream);
+            dynType_printAny(entry->name, entry->type, depth + 1, stream);
         }
 
         dynType_printDepth(depth, stream);
@@ -857,19 +967,19 @@ static void dynType_printTypedPointer(char *name, 
dyn_type *type, int depth, FIL
 static void dynType_printTypes(dyn_type *type, FILE *stream) {
 
     dyn_type *parent = type->parent;
-    struct nested_entry *pentry = NULL;
+    struct type_entry *pentry = NULL;
     while (parent != NULL) {
         TAILQ_FOREACH(pentry, &parent->nestedTypesHead, entries) {
-            if (&pentry->type == type) {
+            if (pentry->type == type) {
                 return;
             }
         }
         parent = parent->parent;
     }
 
-    struct nested_entry *entry = NULL;
+    struct type_entry *entry = NULL;
     TAILQ_FOREACH(entry, &type->nestedTypesHead, entries) {
-        dyn_type *toPrint = &entry->type;
+        dyn_type *toPrint = entry->type;
         if (toPrint->type == DYN_TYPE_REF) {
             toPrint = toPrint->ref.ref;
         }
@@ -892,7 +1002,7 @@ static void dynType_printTypes(dyn_type *type, FILE 
*stream) {
     switch(type->type) {
         case DYN_TYPE_COMPLEX :
             TAILQ_FOREACH(centry, &type->complex.entriesHead, entries) {
-                dynType_printTypes(&centry->type, stream);
+                dynType_printTypes(centry->type, stream);
             }
             break;
         case DYN_TYPE_SEQUENCE :
@@ -909,7 +1019,7 @@ static void dynType_printComplexType(dyn_type *type, FILE 
*stream) {
 
     struct complex_type_entry *entry = NULL;
     TAILQ_FOREACH(entry, &type->complex.entriesHead, entries) {
-        dynType_printAny(entry->name, &entry->type, 2, stream);
+        dynType_printAny(entry->name, entry->type, 2, stream);
     }
 
     fprintf(stream, "}\n");

http://git-wip-us.apache.org/repos/asf/celix/blob/f9ed9b33/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 7334da3..c4f33a2 100644
--- a/remote_services/dynamic_function_interface/dyn_type.h
+++ b/remote_services/dynamic_function_interface/dyn_type.h
@@ -83,59 +83,26 @@
 
 typedef struct _dyn_type dyn_type;
 
-TAILQ_HEAD(reference_types_head, type_entry); 
-TAILQ_HEAD(nested_types_head, nested_entry);
-TAILQ_HEAD(complex_type_entries_head, complex_type_entry);
-
-struct _dyn_type {
-    char *name;
-    char descriptor;
-    int type;
-    ffi_type *ffiType;
-    dyn_type *parent;
-    struct reference_types_head *referenceTypes; //NOTE: not owned
-    struct nested_types_head  nestedTypesHead;
-    union {
-        struct {
-            struct complex_type_entries_head entriesHead;
-            ffi_type structType; //dyn_type.ffiType points to this
-            dyn_type **types; //based on entriesHead for fast access
-        } complex;
-        struct {
-            ffi_type seqType; //dyn_type.ffiType points to this
-            dyn_type *itemType;
-        } sequence;
-        struct {
-            dyn_type *typedType;
-        } typedPointer;
-        struct {
-            dyn_type *ref;
-        } ref;
-    };
-};
-
-struct complex_type_entry {
-    dyn_type type;
-    char *name;
-    TAILQ_ENTRY(complex_type_entry) entries;
-};
-
+//TODO rename
+TAILQ_HEAD(types_head, type_entry);
 struct type_entry {
     dyn_type *type;
     TAILQ_ENTRY(type_entry) entries;
 };
 
-struct nested_entry {
-    dyn_type type;
-    TAILQ_ENTRY(nested_entry) entries;
+TAILQ_HEAD(complex_type_entries_head, complex_type_entry);
+struct complex_type_entry {
+    dyn_type *type;
+    char *name;
+    TAILQ_ENTRY(complex_type_entry) entries;
 };
 
 //logging
 DFI_SETUP_LOG_HEADER(dynType);
 
 //generic
-int dynType_parse(FILE *descriptorStream, const char *name, struct 
reference_types_head *refTypes, dyn_type **type);
-int dynType_parseWithStr(const char *descriptor, const char *name, struct 
reference_types_head *refTypes, dyn_type **type);
+int dynType_parse(FILE *descriptorStream, const char *name, struct types_head 
*refTypes, dyn_type **type);
+int dynType_parseWithStr(const char *descriptor, const char *name, struct 
types_head *refTypes, dyn_type **type);
 void dynType_destroy(dyn_type *type);
 
 int dynType_alloc(dyn_type *type, void **bufLoc);
@@ -145,6 +112,7 @@ void dynType_print(dyn_type *type, FILE *stream);
 size_t dynType_size(dyn_type *type);
 int dynType_type(dyn_type *type);
 int dynType_descriptorType(dyn_type *type);
+ffi_type *dynType_ffiType(dyn_type *type);
 
 //complexType
 int dynType_complex_indexForName(dyn_type *type, const char *name);
@@ -154,7 +122,7 @@ int dynType_complex_valLocAt(dyn_type *type, int index, 
void *inst, void **valLo
 int dynType_complex_entries(dyn_type *type, struct complex_type_entries_head 
**entries);
 
 //sequence
-int dynType_sequence_alloc(dyn_type *type, void *inst, int cap, void **buf);
+int dynType_sequence_alloc(dyn_type *type, void *inst, uint32_t cap);
 int dynType_sequence_locForIndex(dyn_type *type, void *seqLoc, int index, void 
**valLoc);
 int dynType_sequence_increaseLengthAndReturnLastLoc(dyn_type *type, void 
*seqLoc, void **valLoc);
 dyn_type * dynType_sequence_itemType(dyn_type *type);

http://git-wip-us.apache.org/repos/asf/celix/blob/f9ed9b33/remote_services/dynamic_function_interface/json_serializer.c
----------------------------------------------------------------------
diff --git a/remote_services/dynamic_function_interface/json_serializer.c 
b/remote_services/dynamic_function_interface/json_serializer.c
index 3fbdda4..614e948 100644
--- a/remote_services/dynamic_function_interface/json_serializer.c
+++ b/remote_services/dynamic_function_interface/json_serializer.c
@@ -27,7 +27,7 @@ static int ERROR = 1;
 DFI_SETUP_LOG(jsonSerializer);
 
 int jsonSerializer_deserialize(dyn_type *type, const char *input, void 
**result) {
-    assert(type->type == DYN_TYPE_COMPLEX);
+    assert(dynType_type(type) == DYN_TYPE_COMPLEX);
     int status = 0;
 
     json_error_t error;
@@ -111,6 +111,7 @@ static int jsonSerializer_parseAny(dyn_type *type, void 
*loc, json_t *val) {
     int status = OK;
 
     dyn_type *subType = NULL;
+    char c = dynType_descriptorType(type);
 
     float *f;           //F
     double *d;          //D
@@ -123,7 +124,7 @@ static int jsonSerializer_parseAny(dyn_type *type, void 
*loc, json_t *val) {
     uint32_t  *ui;      //i
     uint64_t  *ul;      //j
 
-    switch (type->descriptor) {
+    switch (c) {
         case 'F' :
             f = loc;
             *f = (float) json_real_value(val);
@@ -197,7 +198,7 @@ static int jsonSerializer_parseAny(dyn_type *type, void 
*loc, json_t *val) {
             break;
         default :
             status = ERROR;
-            LOG_ERROR("Error provided type '%c' not supported for JSON\n", 
type->descriptor);
+            LOG_ERROR("Error provided type '%c' not supported for JSON\n", 
dynType_descriptorType(type));
             break;
     }
 
@@ -209,9 +210,8 @@ static int jsonSerializer_parseSequence(dyn_type *seq, 
json_t *array, void *seqL
     int status = OK;
 
     size_t size = json_array_size(array);
-    void *buf = NULL;
     LOG_DEBUG("Allocating sequence with capacity %zu", size);
-    status = dynType_sequence_alloc(seq, seqLoc, (int) size, &buf);
+    status = dynType_sequence_alloc(seq, seqLoc, (int) size);
 
     if (status == OK) {
         dyn_type *itemType = dynType_sequence_itemType(seq);
@@ -339,7 +339,7 @@ static int jsonSerializer_writeAny(dyn_type *type, void 
*input, json_t **out) {
 }
 
 static int jsonSerializer_writeSequence(dyn_type *type, void *input, json_t 
**out) {
-    assert(type->type = DYN_TYPE_SEQUENCE);
+    assert(dynType_type(type) == DYN_TYPE_SEQUENCE);
     int status = OK;
 
     json_t *array = json_array();
@@ -355,7 +355,8 @@ static int jsonSerializer_writeSequence(dyn_type *type, 
void *input, json_t **ou
         if (status == OK) {
             status = jsonSerializer_writeAny(itemType, itemLoc, &item);
             if (status == OK) {
-                json_array_append_new(array, item);
+                json_array_append(array, item);
+                json_decref(item);
             }
         }
 
@@ -372,7 +373,7 @@ static int jsonSerializer_writeSequence(dyn_type *type, 
void *input, json_t **ou
 }
 
 static int jsonSerializer_writeComplex(dyn_type *type, void *input, json_t 
**out) {
-    assert(type->type == DYN_TYPE_COMPLEX);
+    assert(dynType_type(type) == DYN_TYPE_COMPLEX);
     int status = OK;
 
     json_t *val = json_object();
@@ -396,6 +397,7 @@ static int jsonSerializer_writeComplex(dyn_type *type, void 
*input, json_t **out
             }
             if (status == OK) {
                 json_object_set(val, entry->name, subVal);
+                json_decref(subVal);
             }
 
             if (status != OK) {

http://git-wip-us.apache.org/repos/asf/celix/blob/f9ed9b33/remote_services/dynamic_function_interface/tst/dyn_interface_tests.cpp
----------------------------------------------------------------------
diff --git 
a/remote_services/dynamic_function_interface/tst/dyn_interface_tests.cpp 
b/remote_services/dynamic_function_interface/tst/dyn_interface_tests.cpp
index f60cc50..6453afd 100644
--- a/remote_services/dynamic_function_interface/tst/dyn_interface_tests.cpp
+++ b/remote_services/dynamic_function_interface/tst/dyn_interface_tests.cpp
@@ -36,6 +36,7 @@ extern "C" {
         assert(desc != NULL);
         status = dynInterface_parse(desc, &dynIntf);
         CHECK_EQUAL(0, status);
+        fclose(desc);
 
         char *name = NULL;
         status = dynInterface_getName(dynIntf, &name);
@@ -65,7 +66,7 @@ extern "C" {
 
 TEST_GROUP(DynInterfaceTests) {
     void setup() {
-        int level = 3;
+        int level = 1;
         dynCommon_logSetup(stdLog, NULL, level);
         dynType_logSetup(stdLog, NULL, level);
         dynFunction_logSetup(stdLog, NULL, level);

http://git-wip-us.apache.org/repos/asf/celix/blob/f9ed9b33/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 a152306..daee4cf 100644
--- a/remote_services/dynamic_function_interface/tst/dyn_type_tests.cpp
+++ b/remote_services/dynamic_function_interface/tst/dyn_type_tests.cpp
@@ -24,20 +24,21 @@ extern "C" {
         dyn_type *type;
         int i;
         type = NULL;
-        printf("\n-- example %s with descriptor string '%s' --\n", exName, 
descriptorStr);
+        //printf("\n-- example %s with descriptor string '%s' --\n", exName, 
descriptorStr);
         int status = dynType_parseWithStr(descriptorStr, exName, NULL, &type);
         CHECK_EQUAL(0, status);
 
         FILE *stream = fopen("/dev/null", "w");
         dynType_print(type, stream);
+        fclose(stream);
         dynType_destroy(type);
-        printf("--\n\n");
+        //printf("--\n\n");
     }
 }
 
 TEST_GROUP(DynTypeTests) {
        void setup() {
-           dynType_logSetup(stdLog, NULL, 3);
+           dynType_logSetup(stdLog, NULL, 0);
        }
 };
 
@@ -77,14 +78,14 @@ CREATE_EXAMPLES_TEST(EX13)
 CREATE_EXAMPLES_TEST(EX14)
 
 TEST(DynTypeTests, ParseRandomGarbageTest) {
-    return; //TODO enable
+    /*
     unsigned int seed = 4148;
     char *testRandom = getenv("DYN_TYPE_TEST_RANDOM");
     if (testRandom != NULL && strcmp("true", testRandom) == 0) {
         seed = (unsigned int) time(NULL);
     } 
     srandom(seed);
-    size_t nrOfTests = 10000;
+    size_t nrOfTests = 100;
 
     printf("\nStarting test with random seed %i and nrOfTests %zu.\n", seed, 
nrOfTests);
 
@@ -112,6 +113,7 @@ TEST(DynTypeTests, ParseRandomGarbageTest) {
             dynType_destroy(type);
         }
     }
+     */
 }
 
 TEST(DynTypeTests, AssignTest1) {
@@ -171,8 +173,3 @@ TEST(DynTypeTests, AssignTest2) {
 
     dynType_destroy(type);
 }
-
-
-//TODO allocating / freeing
-
-//TODO sequences 

http://git-wip-us.apache.org/repos/asf/celix/blob/f9ed9b33/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 d5c76ea..c21d594 100644
--- a/remote_services/dynamic_function_interface/tst/json_serializer_tests.cpp
+++ b/remote_services/dynamic_function_interface/tst/json_serializer_tests.cpp
@@ -198,7 +198,6 @@ static void check_example5(void *data) {
 }
 
 static void parseTests(void) {
-    printf("Starting json serializer tests\n");
     dyn_type *type;
     void *inst;
     int rc;
@@ -210,6 +209,8 @@ static void parseTests(void) {
     rc = jsonSerializer_deserialize(type, example1_input, &inst);
     CHECK_EQUAL(0, rc);
     check_example1(inst);
+    dynType_free(type, inst);
+    dynType_destroy(type);
 
     type = NULL;
     inst = NULL;
@@ -218,6 +219,8 @@ static void parseTests(void) {
     rc = jsonSerializer_deserialize(type, example2_input, &inst);
     CHECK_EQUAL(0, rc);
     check_example2(inst);
+    dynType_free(type, inst);
+    dynType_destroy(type);
 
     type = NULL;
     inst = NULL;
@@ -226,6 +229,8 @@ static void parseTests(void) {
     rc = jsonSerializer_deserialize(type, example3_input, &inst);
     CHECK_EQUAL(0, rc);
     check_example3(inst);
+    dynType_free(type, inst);
+    dynType_destroy(type);
 
     type = NULL;
     inst = NULL;
@@ -234,6 +239,8 @@ static void parseTests(void) {
     rc = jsonSerializer_deserialize(type, example4_input, &inst);
     CHECK_EQUAL(0, rc);
     check_example4(inst);
+    dynType_free(type, inst);
+    dynType_destroy(type);
 
     type = NULL;
     inst = NULL;
@@ -242,6 +249,8 @@ static void parseTests(void) {
     rc = jsonSerializer_deserialize(type, example5_input, &inst);
     CHECK_EQUAL(0, rc);
     check_example5(inst);
+    dynType_free(type, inst);
+    dynType_destroy(type);
 }
 
 const char *write_example1_descriptor = "{BSIJsijFDN a b c d e f g h i j}";
@@ -278,6 +287,8 @@ void writeTest1(void) {
     STRCMP_CONTAINS("\"i\":9.9", result);
     STRCMP_CONTAINS("\"j\":10", result);
     //printf("example 1 result: '%s'\n", result);
+    dynType_destroy(type);
+    free(result);
 }
 
 const char *write_example2_descriptor = "{*{JJ a b}{SS c d} sub1 sub2}";
@@ -312,6 +323,8 @@ void writeTest2(void) {
     STRCMP_CONTAINS("\"c\":3", result);
     STRCMP_CONTAINS("\"d\":4", result);
     //printf("example 2 result: '%s'\n", result);
+    dynType_destroy(type);
+    free(result);
 }
 
 const char *write_example3_descriptor = "Tperson={ti name age};[Lperson;";
@@ -351,13 +364,16 @@ void writeTest3(void) {
     STRCMP_CONTAINS("\"age\":55", result);
     STRCMP_CONTAINS("\"age\":66", result);
     //printf("example 3 result: '%s'\n", result);
+    free(seq.buf);
+    dynType_destroy(type);
+    free(result);
 }
 
 }
 
 TEST_GROUP(JsonSerializerTests) {
     void setup() {
-        int lvl = 3;
+        int lvl = 1;
         dynCommon_logSetup(stdLog, NULL, lvl);
         dynType_logSetup(stdLog, NULL,lvl);
         jsonSerializer_logSetup(stdLog, NULL, lvl);

Reply via email to