diff --git a/libs/dfi/gtest/src/dyn_function_tests.cpp b/libs/dfi/gtest/src/dyn_function_tests.cpp index 6164cbcc7..0af571e42 100644 --- a/libs/dfi/gtest/src/dyn_function_tests.cpp +++ b/libs/dfi/gtest/src/dyn_function_tests.cpp @@ -114,15 +114,15 @@ static bool func_acc() { int nrOfArgs = 0; bool isStruct = false; bool isVoid = false; - dyn_type *nonExist = nullptr; + const dyn_type *nonExist = nullptr; if (rc == 0) { nrOfArgs = dynFunction_nrOfArguments(dynFunc); - dyn_type *arg1 = dynFunction_argumentTypeForIndex(dynFunc, 1); + const dyn_type *arg1 = dynFunction_argumentTypeForIndex(dynFunc, 1); if (arg1 != nullptr) { isStruct = '{' == dynType_descriptorType(arg1); } nonExist = dynFunction_argumentTypeForIndex(dynFunc, 10); - dyn_type *returnType = dynFunction_returnType(dynFunc); + const dyn_type* returnType = dynFunction_returnType(dynFunc); if (returnType != nullptr) { isVoid = 'V' == dynType_descriptorType(returnType); } diff --git a/libs/dfi/gtest/src/dyn_type_tests.cpp b/libs/dfi/gtest/src/dyn_type_tests.cpp index cfa01ee22..ed4bbef49 100644 --- a/libs/dfi/gtest/src/dyn_type_tests.cpp +++ b/libs/dfi/gtest/src/dyn_type_tests.cpp @@ -188,10 +188,8 @@ TEST_F(DynTypeTests, AssignTest2) { dynType_complex_setValueAt(type, 0, &inst, &a); ASSERT_EQ(2, inst.a); - void *loc = NULL; - dyn_type *subType = NULL; - dynType_complex_valLocAt(type, 1, (void *)&inst, &loc); - dynType_complex_dynTypeAt(type, 1, &subType); + (void)dynType_complex_valLocAt(type, 1, (void *)&inst); + const dyn_type* subType = dynType_complex_dynTypeAt(type, 1); dynType_complex_setValueAt(subType, 0, &inst.b, &b_a); ASSERT_EQ(1.1, inst.b.a); diff --git a/libs/dfi/include/dyn_function.h b/libs/dfi/include/dyn_function.h index 7d506945c..c9f34d5d4 100644 --- a/libs/dfi/include/dyn_function.h +++ b/libs/dfi/include/dyn_function.h @@ -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 dyn_type *dynFunction_argumentTypeForIndex(dyn_function_type *dynFunc, int argumentNr); +CELIX_DFI_EXPORT const dyn_type* dynFunction_argumentTypeForIndex(dyn_function_type* dynFunc, int argumentNr); /** * @brief Returns the argument meta for the given argument index. @@ -112,7 +112,7 @@ CELIX_DFI_EXPORT enum dyn_function_argument_meta dynFunction_argumentMetaForInde * @param[in] dynFunc The dynamic type instance for function. * @return The return value type. */ -CELIX_DFI_EXPORT dyn_type * dynFunction_returnType(dyn_function_type *dynFunction); +CELIX_DFI_EXPORT const dyn_type* dynFunction_returnType(dyn_function_type *dynFunction); /** * @brief Destroys the given dynamic function type instance. diff --git a/libs/dfi/include/dyn_type.h b/libs/dfi/include/dyn_type.h index f56b08e9a..1ab2da416 100644 --- a/libs/dfi/include/dyn_type.h +++ b/libs/dfi/include/dyn_type.h @@ -59,8 +59,8 @@ extern "C" { * //Extended * b unsigned char * i uint32_t - * j uint62_t - * s uint64_t + * j uint64_t + * s uint16_t * P untyped pointer (void *) * t char* string * N native int @@ -112,64 +112,64 @@ typedef struct _dyn_type dyn_type; TAILQ_HEAD(types_head, type_entry); struct type_entry { - dyn_type *type; + dyn_type* type; TAILQ_ENTRY(type_entry) entries; }; TAILQ_HEAD(complex_type_entries_head, complex_type_entry); struct complex_type_entry { - dyn_type *type; - char *name; + dyn_type* type; + char* name; TAILQ_ENTRY(complex_type_entry) entries; }; TAILQ_HEAD(meta_properties_head, meta_entry); struct meta_entry { - char *name; - char *value; + char* name; + char* value; TAILQ_ENTRY(meta_entry) entries; }; /** * Parses a descriptor stream and creates a dyn_type (dynamic type). * If successful the type output argument points to the newly created dyn type. - * The caller is the owner of the dyn type and use dynType_destroy deallocate the memory. + * The caller is the owner of the dyn type and use dynType_destroy to deallocate the memory. * * In case of an error, an error message is added to celix_err. * * @param descriptorStream Stream to the descriptor. * @param name name for the dyn_type. This can be used in references to dyn types. - * @param refTypes A list if reference-able dyn types. + * @param refTypes A list of referable dyn types. * @param type Output argument for the parsed dyn type. * @return 0 if successful. */ -CELIX_DFI_EXPORT int dynType_parse(FILE *descriptorStream, const char *name, struct types_head *refTypes, dyn_type **type); +CELIX_DFI_EXPORT int dynType_parse(FILE* descriptorStream, const char* name, const struct types_head* refTypes, dyn_type** type); /** * Parses a descriptor string and creates a dyn_type (dynamic type). * If successful the type output argument points to the newly created dyn type. - * The caller is the owner of the dyn type and use dynType_destroy deallocate the memory. + * The caller is the owner of the dyn type and use dynType_destroy to deallocate the memory. * * In case of an error, an error message is added to celix_err. * * @param descriptor The descriptor. * @param name name for the dyn_type. This can be used in references to dyn types. - * @param refTypes A list if reference-able dyn types. + * @param refTypes A list if referable dyn types. * @param type Output argument for the parsed dyn type. * @return 0 if successful. */ -CELIX_DFI_EXPORT int dynType_parseWithStr(const char *descriptor, const char *name, struct types_head *refTypes, dyn_type **type); +CELIX_DFI_EXPORT int dynType_parseWithStr(const char* descriptor, const char* name, const struct types_head* refTypes, dyn_type** type); /** * Destroy a dyn type and de-allocates the memory. * @param type The dyn type to destroy. */ -CELIX_DFI_EXPORT void dynType_destroy(dyn_type *type); +CELIX_DFI_EXPORT void dynType_destroy(dyn_type* type); CELIX_DEFINE_AUTOPTR_CLEANUP_FUNC(dyn_type, dynType_destroy); /** - * Allocates memory for a type instance described by a dyn type. The memory will be 0 allocated (calloc). + * Allocates memory for a type instance described by a dyn type. The memory will be set to zero (calloc). * * In case of an error, an error message is added to celix_err. * @@ -177,7 +177,7 @@ CELIX_DEFINE_AUTOPTR_CLEANUP_FUNC(dyn_type, dynType_destroy); * @param instance The output argument for the allocated memory. * @return 0 on success. */ -CELIX_DFI_EXPORT int dynType_alloc(dyn_type *type, void **instance); +CELIX_DFI_EXPORT int dynType_alloc(const dyn_type* type, void** instance); /** * free the memory for a type instance described by a dyn type. @@ -186,23 +186,23 @@ CELIX_DFI_EXPORT int dynType_alloc(dyn_type *type, void **instance); * @param type The dyn type for which structure to allocate. * @param instance The memory location of the type instance. */ -CELIX_DFI_EXPORT void dynType_free(dyn_type *type, void *instance); +CELIX_DFI_EXPORT void dynType_free(const dyn_type* type, void* instance); /** * Prints the dyn type information to the provided output stream. * @param type The dyn type to print. * @param stream The output stream (e.g. stdout). */ -CELIX_DFI_EXPORT void dynType_print(dyn_type *type, FILE *stream); +CELIX_DFI_EXPORT void dynType_print(const dyn_type* type, FILE* stream); /** - * Return the size of the structure descripbed by the dyn type. - * This this _not_ includes sizes of data where is only pointed to (i.e. content of sequences, strings, etc). + * Return the size of the structure described by the dyn type. + * This does _not_ includes sizes of data which is only pointed to (i.e. content of sequences, strings, etc). * * @param type The dyn type. * @return The size of the type instance described by dyn type. */ -CELIX_DFI_EXPORT size_t dynType_size(dyn_type *type); +CELIX_DFI_EXPORT size_t dynType_size(const dyn_type* type); /** * The type of the dyn type @@ -210,7 +210,7 @@ CELIX_DFI_EXPORT size_t dynType_size(dyn_type *type); * @param type The dyn type * @return The type of the dyn type. */ -CELIX_DFI_EXPORT int dynType_type(dyn_type *type); +CELIX_DFI_EXPORT int dynType_type(const dyn_type* type); /** * Returns the char identifier of the dyn type type. @@ -218,7 +218,7 @@ CELIX_DFI_EXPORT int dynType_type(dyn_type *type); * @param type The dyn type * @return The descriptor of the dyn type. */ -CELIX_DFI_EXPORT char dynType_descriptorType(dyn_type *type); +CELIX_DFI_EXPORT char dynType_descriptorType(const dyn_type* type); /** * Get the dyn type meta information for the provided name. @@ -226,21 +226,20 @@ CELIX_DFI_EXPORT char dynType_descriptorType(dyn_type *type); * @param name The name of the requested meta information. * @return The meta information or NULL if the meta information is not present. */ -CELIX_DFI_EXPORT const char * dynType_getMetaInfo(dyn_type *type, const char *name); +CELIX_DFI_EXPORT const char* dynType_getMetaInfo(const dyn_type* type, const char* name); /** - * Returns a list of meta entries. + * Returns the list of meta entries. * Note that dyn type is owner of the entries. Traversing the entries is not thread safe. * @param type The dyn type. - * @param entries The output arguments for the meta entries. - * @return 0 on success. + * @note It always returns valid list. */ -CELIX_DFI_EXPORT int dynType_metaEntries(dyn_type *type, struct meta_properties_head **entries); +CELIX_DFI_EXPORT const struct meta_properties_head* dynType_metaEntries(const dyn_type* type); /** * Returns the name of the dyn type. Name can be NULL. */ -CELIX_DFI_EXPORT const char * dynType_getName(dyn_type *type); +CELIX_DFI_EXPORT const char* dynType_getName(const dyn_type* type); /** @@ -251,30 +250,27 @@ CELIX_DFI_EXPORT const char * dynType_getName(dyn_type *type); * @param name The field name. * @return The field index or -1 if no field with the name was found. */ -CELIX_DFI_EXPORT int dynType_complex_indexForName(dyn_type *type, const char *name); +CELIX_DFI_EXPORT int dynType_complex_indexForName(const dyn_type* type, const char* name); /** * Returns the field dyn type for a given complex type and a field index. * * @param type The dyn type. Must be a complex type. * @param index The field index for which field the dyn type should be returned. The field index must be correct. - * @param subType The field dyn type as output. - * exists. - * @return 0 if successful. */ -CELIX_DFI_EXPORT int dynType_complex_dynTypeAt(dyn_type *type, int index, dyn_type **subType); +CELIX_DFI_EXPORT const dyn_type* dynType_complex_dynTypeAt(const dyn_type* type, int index); -CELIX_DFI_EXPORT int dynType_complex_setValueAt(dyn_type *type, int index, void *inst, void *in); -CELIX_DFI_EXPORT int dynType_complex_valLocAt(dyn_type *type, int index, void *inst, void **valLoc); -CELIX_DFI_EXPORT int dynType_complex_entries(dyn_type *type, struct complex_type_entries_head **entries); -CELIX_DFI_EXPORT size_t dynType_complex_nrOfEntries(dyn_type *type); +CELIX_DFI_EXPORT int dynType_complex_setValueAt(const dyn_type* type, int index, void* inst, const void* in); +CELIX_DFI_EXPORT void* dynType_complex_valLocAt(const dyn_type* type, int index, void* inst); +CELIX_DFI_EXPORT const struct complex_type_entries_head* dynType_complex_entries(const dyn_type* type); +CELIX_DFI_EXPORT size_t dynType_complex_nrOfEntries(const dyn_type* type); //sequence /** * Initialize a sequence struct with a cap & len of 0 and the buf to NULL. */ -CELIX_DFI_EXPORT void dynType_sequence_init(dyn_type *type, void *inst); +CELIX_DFI_EXPORT void dynType_sequence_init(const dyn_type* type, void* inst); /** * Allocates memory for a sequence with capacity cap. @@ -284,7 +280,7 @@ CELIX_DFI_EXPORT void dynType_sequence_init(dyn_type *type, void *inst); * In case of an error, an error message is added to celix_err. * */ -CELIX_DFI_EXPORT int dynType_sequence_alloc(dyn_type *type, void *inst, uint32_t cap); +CELIX_DFI_EXPORT int dynType_sequence_alloc(const dyn_type* type, void* inst, uint32_t cap); /** * Reserve a sequence capacity of cap @@ -295,7 +291,7 @@ CELIX_DFI_EXPORT int dynType_sequence_alloc(dyn_type *type, void *inst, uint32_t * In case of an error, an error message is added to celix_err. * */ -CELIX_DFI_EXPORT int dynType_sequence_reserve(dyn_type *type, void *inst, uint32_t cap); +CELIX_DFI_EXPORT int dynType_sequence_reserve(const dyn_type* type, void* inst, uint32_t cap); /** * @brief Gets the value location for a specific sequence index from a sequence instance. @@ -309,7 +305,7 @@ CELIX_DFI_EXPORT int dynType_sequence_reserve(dyn_type *type, void *inst, uint32 * @return 0 if successful. * @retval 1 if the index is out of bounds. */ -CELIX_DFI_EXPORT int dynType_sequence_locForIndex(dyn_type *type, void *seqLoc, int index, void **valLoc); +CELIX_DFI_EXPORT int dynType_sequence_locForIndex(const dyn_type* type, void* seqLoc, int index, void** valLoc); /** * @brief Increase the length of the sequence by one and return the value location for the last element. @@ -322,29 +318,26 @@ CELIX_DFI_EXPORT int dynType_sequence_locForIndex(dyn_type *type, void *seqLoc, * @return 0 if successful. * @retval 1 if the sequence length is already at the max. */ -CELIX_DFI_EXPORT int dynType_sequence_increaseLengthAndReturnLastLoc(dyn_type *type, void *seqLoc, void **valLoc); +CELIX_DFI_EXPORT int dynType_sequence_increaseLengthAndReturnLastLoc(const dyn_type* type, void* seqLoc, void** valLoc); /** * @brief Get the item type of a sequence. * @param[in] type The dyn type. Must be a sequence type. * @return The item type of the sequence. */ -CELIX_DFI_EXPORT dyn_type * dynType_sequence_itemType(dyn_type *type); +CELIX_DFI_EXPORT const dyn_type* dynType_sequence_itemType(const dyn_type* type); /** * @brief Get the length of a sequence. * @param[in] seqLoc The sequence instance. * @return The length of the sequence. */ -CELIX_DFI_EXPORT uint32_t dynType_sequence_length(void *seqLoc); +CELIX_DFI_EXPORT uint32_t dynType_sequence_length(const void* seqLoc); /** * @brief Gets the typedType of a typedPointer type. - * @param[in] type The dyn type. Must be a typedPointer type. - * @param[out] typedType The typedType of the typedPointer type. - * @return 0 */ -CELIX_DFI_EXPORT int dynType_typedPointer_getTypedType(dyn_type *type, dyn_type **typedType); +CELIX_DFI_EXPORT const dyn_type* dynType_typedPointer_getTypedType(const dyn_type* type); /** * @brief Allocates and initializes a string type. @@ -357,7 +350,7 @@ CELIX_DFI_EXPORT int dynType_typedPointer_getTypedType(dyn_type *type, dyn_type * @return 0 if successful. * @retval 1 if Cannot allocate memory. */ -CELIX_DFI_EXPORT int dynType_text_allocAndInit(dyn_type *type, void *textLoc, const char *value); +CELIX_DFI_EXPORT int dynType_text_allocAndInit(const dyn_type* type, void* textLoc, const char* value); /** * @brief Sets the value of a simple type. @@ -365,7 +358,7 @@ CELIX_DFI_EXPORT int dynType_text_allocAndInit(dyn_type *type, void *textLoc, co * @param[out] inst The instance of the simple type. * @param[in] in The value to set. */ -CELIX_DFI_EXPORT void dynType_simple_setValue(dyn_type *type, void *inst, void *in); +CELIX_DFI_EXPORT void dynType_simple_setValue(const dyn_type* type, void* inst, const void* in); #ifdef __cplusplus } diff --git a/libs/dfi/include/json_serializer.h b/libs/dfi/include/json_serializer.h index 2e5d2ce93..5d38b7ebc 100644 --- a/libs/dfi/include/json_serializer.h +++ b/libs/dfi/include/json_serializer.h @@ -58,7 +58,7 @@ CELIX_DFI_EXPORT int jsonSerializer_deserialize(dyn_type *type, const char *inpu * @return 0 if successful, otherwise 1. * */ -CELIX_DFI_EXPORT int jsonSerializer_deserializeJson(dyn_type *type, json_t *input, void **result); +CELIX_DFI_EXPORT int jsonSerializer_deserializeJson(const dyn_type *type, json_t *input, void **result); /** * @brief Serialize a given type to a JSON string. @@ -88,7 +88,7 @@ CELIX_DFI_EXPORT int jsonSerializer_serialize(dyn_type *type, const void* input, * @return 0 if successful, otherwise 1. * */ -CELIX_DFI_EXPORT int jsonSerializer_serializeJson(dyn_type *type, const void* input, json_t **out); +CELIX_DFI_EXPORT int jsonSerializer_serializeJson(const dyn_type *type, const void* input, json_t **out); #ifdef __cplusplus } diff --git a/libs/dfi/src/dyn_function.c b/libs/dfi/src/dyn_function.c index 65739ae37..69a7dcf99 100644 --- a/libs/dfi/src/dyn_function.c +++ b/libs/dfi/src/dyn_function.c @@ -286,10 +286,10 @@ int dynFunction_nrOfArguments(dyn_function_type *dynFunc) { return count; } -dyn_type *dynFunction_argumentTypeForIndex(dyn_function_type *dynFunc, int argumentNr) { - dyn_type *result = NULL; +const dyn_type* dynFunction_argumentTypeForIndex(dyn_function_type* dynFunc, int argumentNr) { + dyn_type* result = NULL; int index = 0; - dyn_function_argument_type *entry = NULL; + dyn_function_argument_type* entry = NULL; TAILQ_FOREACH(entry, &dynFunc->arguments, entries) { if (index == argumentNr) { result = entry->type; @@ -300,12 +300,12 @@ dyn_type *dynFunction_argumentTypeForIndex(dyn_function_type *dynFunc, int argum return result; } -dyn_type * dynFunction_returnType(dyn_function_type *dynFunction) { +const dyn_type* dynFunction_returnType(dyn_function_type *dynFunction) { return dynFunction->funcReturn; } -bool dynFunction_hasReturn(dyn_function_type *dynFunction) { - dyn_type *t = dynFunction_returnType(dynFunction); +bool dynFunction_hasReturn(dyn_function_type* dynFunction) { + const dyn_type* t = dynFunction_returnType(dynFunction); return t->descriptor != 'V'; } diff --git a/libs/dfi/src/dyn_interface.c b/libs/dfi/src/dyn_interface.c index 8d5d5b4f5..6ac50a8a7 100644 --- a/libs/dfi/src/dyn_interface.c +++ b/libs/dfi/src/dyn_interface.c @@ -108,7 +108,7 @@ static int dynInterface_checkInterface(dyn_interface_type *intf) { struct method_entry *mEntry = NULL; TAILQ_FOREACH(mEntry, &intf->methods, entries) { - dyn_type *type = dynFunction_returnType(mEntry->dynFunc); + const dyn_type* type = dynFunction_returnType(mEntry->dynFunc); int descriptor = dynType_descriptorType(type); if (descriptor != 'N') { celix_err_pushf("Parse Error. Only method with a return type 'N' (native int) are supported. Got return type '%c'\n", descriptor); diff --git a/libs/dfi/src/dyn_type.c b/libs/dfi/src/dyn_type.c index 507f6c4a1..c533b8f39 100644 --- a/libs/dfi/src/dyn_type.c +++ b/libs/dfi/src/dyn_type.c @@ -34,59 +34,59 @@ static const int ERROR = 1; static const int MEM_ERROR = 2; static const int PARSE_ERROR = 3; -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); -ffi_type * dynType_ffiType(dyn_type *type); - -static struct type_entry *dynType_allocTypeEntry(void); - -static ffi_type * dynType_ffiTypeFor(int c); -static int dynType_parseAny(FILE *stream, dyn_type *type); -static int dynType_parseComplex(FILE *stream, dyn_type *type); -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); -static int dynType_parseSequence(FILE *stream, dyn_type *type); -static int dynType_parseSimple(int c, dyn_type *type); -static int dynType_parseTypedPointer(FILE *stream, dyn_type *type); -static unsigned short dynType_getOffset(dyn_type *type, int index); - -static void dynType_printAny(char *name, dyn_type *type, int depth, FILE *stream); -static void dynType_printComplex(char *name, dyn_type *type, int depth, FILE *stream); -static void dynType_printSequence(char *name, dyn_type *type, int depth, FILE *stream); -static void dynType_printSimple(char *name, dyn_type *type, int depth, FILE *stream); -static void dynType_printEnum(char *name, dyn_type *type, int depth, FILE *stream); -static void dynType_printTypedPointer(char *name, dyn_type *type, int depth, FILE *stream); -static void dynType_printDepth(int depth, FILE *stream); - -static void dynType_printTypes(dyn_type *type, FILE *stream); -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); -static int dynType_parseEnum(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); - -static int dynType_parseMetaInfo(FILE *stream, dyn_type *type); +static int dynType_parseWithStream(FILE* stream, const char* name, dyn_type* parent, const 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); +ffi_type* dynType_ffiType(dyn_type* type); + +static struct type_entry* dynType_allocTypeEntry(void); + +static ffi_type* dynType_ffiTypeFor(int c); +static int dynType_parseAny(FILE* stream, dyn_type* type); +static int dynType_parseComplex(FILE* stream, dyn_type* type); +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); +static int dynType_parseSequence(FILE* stream, dyn_type* type); +static int dynType_parseSimple(int c, dyn_type* type); +static int dynType_parseTypedPointer(FILE* stream, dyn_type* type); +static unsigned short dynType_getOffset(const dyn_type* type, int index); + +static void dynType_printAny(const char* name, const dyn_type* type, int depth, FILE* stream); +static void dynType_printComplex(const char* name, const dyn_type* type, int depth, FILE* stream); +static void dynType_printSequence(const char* name, const dyn_type* type, int depth, FILE* stream); +static void dynType_printSimple(const char* name, const dyn_type* type, int depth, FILE* stream); +static void dynType_printEnum(const char* name, const dyn_type* type, int depth, FILE* stream); +static void dynType_printTypedPointer(const char* name, const dyn_type* type, int depth, FILE* stream); +static void dynType_printDepth(int depth, FILE* stream); + +static void dynType_printTypes(const dyn_type* type, FILE* stream); +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); +static int dynType_parseEnum(FILE* stream, dyn_type* type); +static void dynType_freeComplexType(const dyn_type* type, void* loc); +static void dynType_deepFree(const dyn_type* type, void* loc, bool alsoDeleteSelf); +static void dynType_freeSequenceType(const dyn_type* type, void* seqLoc); + +static int dynType_parseMetaInfo(FILE* stream, dyn_type* type); struct generic_sequence { uint32_t cap; uint32_t len; - void *buf; + void* buf; }; -int dynType_parse(FILE *descriptorStream, const char *name, struct types_head *refTypes, dyn_type **type) { +int dynType_parse(FILE* descriptorStream, const char* name, const 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 types_head *refTypes, dyn_type **type) { +int dynType_parseWithStr(const char* descriptor, const char* name, const struct types_head* refTypes, dyn_type** type) { 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 = dynType_parseWithStream(stream, name, NULL, refTypes, type); if (status == OK) { @@ -106,9 +106,9 @@ int dynType_parseWithStr(const char *descriptor, const char *name, struct types_ return status; } -static int dynType_parseWithStream(FILE *stream, const char *name, dyn_type *parent, struct types_head *refTypes, dyn_type **result) { +static int dynType_parseWithStream(FILE* stream, const char* name, dyn_type* parent, const struct types_head* refTypes, dyn_type** result) { int status = OK; - dyn_type *type = calloc(1, sizeof(*type)); + dyn_type* type = calloc(1, sizeof(*type)); if (type != NULL) { type->parent = parent; type->type = DYN_TYPE_INVALID; @@ -137,7 +137,7 @@ static int dynType_parseWithStream(FILE *stream, const char *name, dyn_type *par return status; } -static int dynType_parseAny(FILE *stream, dyn_type *type) { +static int dynType_parseAny(FILE* stream, dyn_type* type) { int status = OK; int c = fgetc(stream); @@ -183,10 +183,10 @@ static int dynType_parseAny(FILE *stream, dyn_type *type) { return status; } -static int dynType_parseMetaInfo(FILE *stream, dyn_type *type) { +static int dynType_parseMetaInfo(FILE* stream, dyn_type* type) { int status = OK; - char *name = NULL; - char *value = NULL; + char* name = NULL; + char* value = NULL; struct meta_entry *entry = calloc(1, sizeof(*entry)); if (entry == NULL) { @@ -223,7 +223,7 @@ static int dynType_parseMetaInfo(FILE *stream, dyn_type *type) { return status; } -static int dynType_parseText(FILE *stream, dyn_type *type) { +static int dynType_parseText(FILE* stream, dyn_type* type) { int status = OK; type->type = DYN_TYPE_TEXT; type->descriptor = 't'; @@ -231,7 +231,7 @@ static int dynType_parseText(FILE *stream, dyn_type *type) { return status; } -static int dynType_parseEnum(FILE *stream, dyn_type *type) { +static int dynType_parseEnum(FILE* stream, dyn_type* type) { int status = OK; type->ffiType = &ffi_type_sint32; type->descriptor = 'E'; @@ -239,7 +239,7 @@ static int dynType_parseEnum(FILE *stream, dyn_type *type) { return status; } -static int dynType_parseComplex(FILE *stream, dyn_type *type) { +static int dynType_parseComplex(FILE* stream, dyn_type* type) { int status = OK; type->type = DYN_TYPE_COMPLEX; type->descriptor = '{'; @@ -247,7 +247,7 @@ static int dynType_parseComplex(FILE *stream, dyn_type *type) { TAILQ_INIT(&type->complex.entriesHead); int c = fgetc(stream); - struct complex_type_entry *entry = NULL; + struct complex_type_entry* entry = NULL; while (c != ' ' && c != '}') { ungetc(c,stream); entry = calloc(1, sizeof(*entry)); @@ -277,7 +277,7 @@ static int dynType_parseComplex(FILE *stream, dyn_type *type) { // loop over names if (status == OK) { entry = TAILQ_FIRST(&type->complex.entriesHead); - char *name = NULL; + char* name = NULL; while (c == ' ' && entry != NULL) { status = dynCommon_parseName(stream, &name); if (status == OK) { @@ -333,10 +333,10 @@ static int dynType_parseComplex(FILE *stream, dyn_type *type) { return status; } -static int dynType_parseNestedType(FILE *stream, dyn_type *type) { +static int dynType_parseNestedType(FILE* stream, dyn_type* type) { int status = OK; - char *name = NULL; - struct type_entry *entry = NULL; + char* name = NULL; + struct type_entry* entry = NULL; entry = dynType_allocTypeEntry(); if (entry != NULL) { @@ -372,7 +372,7 @@ static int dynType_parseNestedType(FILE *stream, dyn_type *type) { return status; } -static int dynType_parseReference(FILE *stream, dyn_type *type) { +static int dynType_parseReference(FILE* stream, dyn_type* type) { int status = OK; type->type = DYN_TYPE_TYPED_POINTER; type->descriptor = '*'; @@ -380,7 +380,7 @@ static int dynType_parseReference(FILE *stream, dyn_type *type) { type->ffiType = &ffi_type_pointer; type->typedPointer.typedType = NULL; - dyn_type *subType = calloc(1, sizeof(*subType)); + dyn_type* subType = calloc(1, sizeof(*subType)); if (subType != NULL) { type->typedPointer.typedType = subType; @@ -397,15 +397,15 @@ static int dynType_parseReference(FILE *stream, dyn_type *type) { return status; } -static int dynType_parseRefByValue(FILE *stream, dyn_type *type) { +static int dynType_parseRefByValue(FILE* stream, dyn_type* type) { int status = OK; type->type = DYN_TYPE_REF; type->descriptor = 'l'; - char *name = NULL; + char* name = NULL; status = dynCommon_parseName(stream, &name); if (status == OK) { - dyn_type *ref = dynType_findType(type, name); + dyn_type* ref = dynType_findType(type, name); if (ref != NULL) { type->ref.ref = ref; } else { @@ -426,8 +426,8 @@ 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)); +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) { @@ -438,9 +438,9 @@ static struct type_entry *dynType_allocTypeEntry(void) { return entry; } -static ffi_type *seq_types[] = {&ffi_type_uint32, &ffi_type_uint32, &ffi_type_pointer, NULL}; +static ffi_type* seq_types[] = {&ffi_type_uint32, &ffi_type_uint32, &ffi_type_pointer, NULL}; -static int dynType_parseSequence(FILE *stream, dyn_type *type) { +static int dynType_parseSequence(FILE* stream, dyn_type* type) { int status = OK; type->type = DYN_TYPE_SEQUENCE; type->descriptor = '['; @@ -460,9 +460,9 @@ static int dynType_parseSequence(FILE *stream, dyn_type *type) { return status; } -static int dynType_parseSimple(int c, dyn_type *type) { +static int dynType_parseSimple(int c, dyn_type* type) { int status = OK; - ffi_type *ffiType = dynType_ffiTypeFor(c); + ffi_type* ffiType = dynType_ffiTypeFor(c); if (ffiType != NULL) { type->type = DYN_TYPE_SIMPLE; type->descriptor = c; @@ -475,7 +475,7 @@ static int dynType_parseSimple(int c, dyn_type *type) { return status; } -static int dynType_parseTypedPointer(FILE *stream, dyn_type *type) { +static int dynType_parseTypedPointer(FILE* stream, dyn_type* type) { int status = OK; type->type = DYN_TYPE_TYPED_POINTER; type->descriptor = '*'; @@ -486,16 +486,16 @@ static int dynType_parseTypedPointer(FILE *stream, dyn_type *type) { return status; } -void dynType_destroy(dyn_type *type) { +void dynType_destroy(dyn_type* type) { if (type != NULL) { dynType_clear(type); free(type); } } -static void dynType_clear(dyn_type *type) { - struct type_entry *entry = TAILQ_FIRST(&type->nestedTypesHead); - struct type_entry *tmp = NULL; +static void dynType_clear(dyn_type* type) { + struct type_entry* entry = TAILQ_FIRST(&type->nestedTypesHead); + struct type_entry* tmp = NULL; while (entry != NULL) { tmp = entry; entry = TAILQ_NEXT(entry, entries); @@ -506,8 +506,8 @@ static void dynType_clear(dyn_type *type) { free(tmp); } - struct meta_entry *mEntry = TAILQ_FIRST(&type->metaProperties);; - struct meta_entry *next = NULL; + struct meta_entry* mEntry = TAILQ_FIRST(&type->metaProperties);; + struct meta_entry* next = NULL; while (mEntry != NULL) { next = TAILQ_NEXT(mEntry, entries); if (mEntry != NULL) { @@ -535,10 +535,10 @@ static void dynType_clear(dyn_type *type) { } } -static void dynType_clearComplex(dyn_type *type) { +static void dynType_clearComplex(dyn_type* type) { assert(type->type == DYN_TYPE_COMPLEX); - struct complex_type_entry *entry = TAILQ_FIRST(&type->complex.entriesHead); - struct complex_type_entry *tmp = NULL; + struct complex_type_entry* entry = TAILQ_FIRST(&type->complex.entriesHead); + struct complex_type_entry* tmp = NULL; while (entry != NULL) { dynType_destroy(entry->type); if (entry->name != NULL) { @@ -556,27 +556,27 @@ static void dynType_clearComplex(dyn_type *type) { } } -static void dynType_clearSequence(dyn_type *type) { +static void dynType_clearSequence(dyn_type* type) { assert(type->type == DYN_TYPE_SEQUENCE); if (type->sequence.itemType != NULL) { dynType_destroy(type->sequence.itemType); } } -static void dynType_clearTypedPointer(dyn_type *type) { +static void dynType_clearTypedPointer(dyn_type* type) { assert(type->type == DYN_TYPE_TYPED_POINTER); if (type->typedPointer.typedType != NULL) { dynType_destroy(type->typedPointer.typedType); } } -int dynType_alloc(dyn_type *type, void **bufLoc) { +int dynType_alloc(const dyn_type* type, void** bufLoc) { int status = OK; if (type->type == DYN_TYPE_REF) { status = dynType_alloc(type->ref.ref, bufLoc); } else { - void *inst = calloc(1, type->ffiType->size); + void* inst = calloc(1, type->ffiType->size); if (inst != NULL) { *bufLoc = inst; } else { @@ -589,14 +589,14 @@ int dynType_alloc(dyn_type *type, void **bufLoc) { } -int dynType_complex_indexForName(dyn_type *type, const char *name) { +int dynType_complex_indexForName(const dyn_type* type, const char* name) { assert(type->type == DYN_TYPE_COMPLEX); if (name == NULL) { return -1; } int i = 0; int index = -1; - struct complex_type_entry *entry = NULL; + struct complex_type_entry* entry = NULL; TAILQ_FOREACH(entry, &type->complex.entriesHead, entries) { if (entry->name != NULL && strcmp(name, entry->name) == 0) { index = i; @@ -607,63 +607,58 @@ int dynType_complex_indexForName(dyn_type *type, const char *name) { return index; } -int dynType_complex_dynTypeAt(dyn_type *type, int index, dyn_type **result) { +const dyn_type* dynType_complex_dynTypeAt(const dyn_type* type, int index) { assert(type->type == DYN_TYPE_COMPLEX); assert(index >= 0); - dyn_type *sub = type->complex.types[index]; + dyn_type* sub = type->complex.types[index]; if (sub->type == DYN_TYPE_REF) { sub = sub->ref.ref; } - *result = sub; - return 0; + return sub; } -int dynType_complex_setValueAt(dyn_type *type, int index, void *start, void *in) { +int dynType_complex_setValueAt(const dyn_type* type, int index, void* start, const void* in) { assert(type->type == DYN_TYPE_COMPLEX); - char *loc = ((char *)start) + dynType_getOffset(type, index); + char* loc = ((char*)start) + dynType_getOffset(type, index); size_t size = type->complex.structType.elements[index]->size; memcpy(loc, in, size); return 0; } -int dynType_complex_valLocAt(dyn_type *type, int index, void *inst, void **result) { +void* dynType_complex_valLocAt(const dyn_type* type, int index, void* inst) { assert(type->type == DYN_TYPE_COMPLEX); - char *l = (char *)inst; - void *loc = (void *)(l + dynType_getOffset(type, index)); - *result = loc; - return OK; + char* l = (char*)inst; + return (void* )(l + dynType_getOffset(type, index)); } -size_t dynType_complex_nrOfEntries(dyn_type *type) { +size_t dynType_complex_nrOfEntries(const dyn_type* type) { size_t count = 0; - struct complex_type_entry *entry = NULL; + struct complex_type_entry* entry = NULL; TAILQ_FOREACH(entry, &type->complex.entriesHead, entries) { ++count; } return count; } -int dynType_complex_entries(dyn_type *type, struct complex_type_entries_head **entries) { +const struct complex_type_entries_head* dynType_complex_entries(const dyn_type* type) { assert(type->type == DYN_TYPE_COMPLEX); - int status = OK; - *entries = &type->complex.entriesHead; - return status; + return &type->complex.entriesHead; } //sequence -void dynType_sequence_init(dyn_type *type, void *inst) { +void dynType_sequence_init(const dyn_type* type, void* inst) { assert(type->type == DYN_TYPE_SEQUENCE); - struct generic_sequence *seq = inst; + struct generic_sequence* seq = inst; seq->buf = NULL; seq->cap = 0; seq->len = 0; } -int dynType_sequence_alloc(dyn_type *type, void *inst, uint32_t cap) { +int dynType_sequence_alloc(const dyn_type* type, void* inst, uint32_t cap) { assert(type->type == DYN_TYPE_SEQUENCE); int status = OK; - struct generic_sequence *seq = inst; + struct generic_sequence* seq = inst; if (seq != NULL) { size_t size = dynType_size(type->sequence.itemType); seq->buf = calloc(cap, size); @@ -682,10 +677,10 @@ int dynType_sequence_alloc(dyn_type *type, void *inst, uint32_t cap) { return status; } -int dynType_sequence_reserve(dyn_type *type, void *inst, uint32_t cap) { +int dynType_sequence_reserve(const dyn_type* type, void* inst, uint32_t cap) { assert(type->type == DYN_TYPE_SEQUENCE); int status = OK; - struct generic_sequence *seq = inst; + struct generic_sequence* seq = inst; if (seq != NULL && seq->cap < cap) { size_t size = dynType_size(type->sequence.itemType); seq->buf = realloc(seq->buf, (size_t)(cap * size)); @@ -703,14 +698,14 @@ int dynType_sequence_reserve(dyn_type *type, void *inst, uint32_t cap) { return status; } -void dynType_free(dyn_type *type, void *loc) { +void dynType_free(const dyn_type* type, void* loc) { dynType_deepFree(type, loc, true); } -void dynType_deepFree(dyn_type *type, void *loc, bool alsoDeleteSelf) { +static void dynType_deepFree(const dyn_type* type, void* loc, bool alsoDeleteSelf) { if (loc != NULL) { - dyn_type *subType = NULL; - char *text = NULL; + const dyn_type* subType = NULL; + char* text = NULL; switch (type->type) { case DYN_TYPE_REF: //NOTE: do not recursively forward asloDeleteSelf, because this is already handled in this function) @@ -723,12 +718,12 @@ void dynType_deepFree(dyn_type *type, void *loc, bool alsoDeleteSelf) { dynType_freeSequenceType(type, loc); break; case DYN_TYPE_TYPED_POINTER: - dynType_typedPointer_getTypedType(type, &subType); + subType = dynType_typedPointer_getTypedType(type); void *ptrToType = *(void**)loc; dynType_deepFree(subType, ptrToType, true); break; case DYN_TYPE_TEXT : - text = *(char **)loc; + text = *(char**)loc; free(text); break; case DYN_TYPE_SIMPLE: @@ -745,10 +740,10 @@ void dynType_deepFree(dyn_type *type, void *loc, bool alsoDeleteSelf) { } } -void dynType_freeSequenceType(dyn_type *type, void *seqLoc) { - struct generic_sequence *seq = seqLoc; - dyn_type *itemType = dynType_sequence_itemType(type); - void *itemLoc = NULL; +static void dynType_freeSequenceType(const dyn_type* type, void* seqLoc) { + struct generic_sequence* seq = seqLoc; + const dyn_type* itemType = dynType_sequence_itemType(type); + void* itemLoc = NULL; int i; for (i = 0; i < seq->len; ++i) { dynType_sequence_locForIndex(type, seqLoc, i, &itemLoc); @@ -757,27 +752,25 @@ void dynType_freeSequenceType(dyn_type *type, void *seqLoc) { free(seq->buf); } -void dynType_freeComplexType(dyn_type *type, void *loc) { - struct complex_type_entry *entry = NULL; +static void dynType_freeComplexType(const 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); + dynType_deepFree(entry->type, dynType_complex_valLocAt(type, index++, loc), false); } } -uint32_t dynType_sequence_length(void *seqLoc) { - struct generic_sequence *seq = seqLoc; +uint32_t dynType_sequence_length(const void *seqLoc) { + const struct generic_sequence* seq = seqLoc; return seq->len; } -int dynType_sequence_locForIndex(dyn_type *type, void *seqLoc, int index, void **out) { +int dynType_sequence_locForIndex(const dyn_type* type, void* seqLoc, int index, void** out) { assert(type->type == DYN_TYPE_SEQUENCE); int status = OK; - struct generic_sequence *seq = seqLoc; + struct generic_sequence* seq = seqLoc; size_t itemSize = dynType_size(type->sequence.itemType); @@ -792,17 +785,17 @@ int dynType_sequence_locForIndex(dyn_type *type, void *seqLoc, int index, void * } if (status == OK) { - char *valLoc = seq->buf + (index * itemSize); + char* valLoc = seq->buf + (index * itemSize); (*out) = valLoc; } return status; } -int dynType_sequence_increaseLengthAndReturnLastLoc(dyn_type *type, void *seqLoc, void **valLoc) { +int dynType_sequence_increaseLengthAndReturnLastLoc(const dyn_type* type, void* seqLoc, void** valLoc) { assert(type->type == DYN_TYPE_SEQUENCE); int status = OK; - struct generic_sequence *seq = seqLoc; + struct generic_sequence* seq = seqLoc; int lastIndex = seq->len; if (seq->len < seq->cap) { @@ -819,7 +812,7 @@ int dynType_sequence_increaseLengthAndReturnLastLoc(dyn_type *type, void *seqLoc return status; } -dyn_type * dynType_sequence_itemType(dyn_type *type) { +const dyn_type* dynType_sequence_itemType(const dyn_type* type) { assert(type->type == DYN_TYPE_SEQUENCE); dyn_type *itemType = type->sequence.itemType; if (itemType->type == DYN_TYPE_REF) { @@ -828,18 +821,18 @@ dyn_type * dynType_sequence_itemType(dyn_type *type) { return itemType; } -void dynType_simple_setValue(dyn_type *type, void *inst, void *in) { +void dynType_simple_setValue(const dyn_type *type, void *inst, const void *in) { size_t size = dynType_size(type); memcpy(inst, in, size); } -char dynType_descriptorType(dyn_type *type) { +char dynType_descriptorType(const dyn_type* type) { return type->descriptor; } -const char * dynType_getMetaInfo(dyn_type *type, const char *name) { - const char *result = NULL; - struct meta_entry *entry = NULL; +const char* dynType_getMetaInfo(const dyn_type* type, const char* name) { + const char* result = NULL; + struct meta_entry* entry = NULL; TAILQ_FOREACH(entry, &type->metaProperties, entries) { if (strcmp(entry->name, name) == 0) { result = entry->value; @@ -849,17 +842,16 @@ const char * dynType_getMetaInfo(dyn_type *type, const char *name) { return result; } -int dynType_metaEntries(dyn_type *type, struct meta_properties_head **entries) { - *entries = &type->metaProperties; - return OK; +const struct meta_properties_head* dynType_metaEntries(const dyn_type* type) { + return &type->metaProperties; } -const char * dynType_getName(dyn_type *type) { +const char* dynType_getName(const dyn_type* type) { return type->name; } -static ffi_type * dynType_ffiTypeFor(int c) { - ffi_type *type = NULL; +static ffi_type* dynType_ffiTypeFor(int c) { + ffi_type* type = NULL; switch (c) { case 'Z' : type = &ffi_type_uint8; @@ -907,11 +899,11 @@ static ffi_type * dynType_ffiTypeFor(int c) { return type; } -static unsigned short dynType_getOffset(dyn_type *type, int index) { +static unsigned short dynType_getOffset(const dyn_type* type, int index) { assert(type->type == DYN_TYPE_COMPLEX); unsigned short offset = 0; - ffi_type *ffiType = &type->complex.structType; + const ffi_type* ffiType = &type->complex.structType; int i; for (i = 0; i <= index && ffiType->elements[i] != NULL; i += 1) { size_t size = ffiType->elements[i]->size; @@ -928,38 +920,33 @@ static unsigned short dynType_getOffset(dyn_type *type, int index) { return offset; } -size_t dynType_size(dyn_type *type) { - dyn_type *rType = type; +size_t dynType_size(const dyn_type* type) { + const dyn_type* rType = type; if (type->type == DYN_TYPE_REF) { rType = type->ref.ref; } return rType->ffiType->size; } -int dynType_type(dyn_type *type) { +int dynType_type(const dyn_type* type) { return type->type; } - -int dynType_typedPointer_getTypedType(dyn_type *type, dyn_type **out) { +const dyn_type* dynType_typedPointer_getTypedType(const dyn_type* type) { assert(type->type == DYN_TYPE_TYPED_POINTER); - int status = 0; - - dyn_type *typedType = type->typedPointer.typedType; + dyn_type* typedType = type->typedPointer.typedType; if (typedType->type == DYN_TYPE_REF) { typedType = typedType->ref.ref; } - - *out = typedType; - return status; + return typedType; } -int dynType_text_allocAndInit(dyn_type *type, void *textLoc, const char *value) { +int dynType_text_allocAndInit(const 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; + const char* str = strdup(value); + char const** loc = textLoc; if (str != NULL) { *loc = str; } else { @@ -973,7 +960,7 @@ int dynType_text_allocAndInit(dyn_type *type, void *textLoc, const char *value) -void dynType_print(dyn_type *type, FILE *stream) { +void dynType_print(const dyn_type *type, FILE *stream) { if (type != NULL) { dynType_printTypes(type, stream); @@ -991,8 +978,8 @@ static void dynType_printDepth(int depth, FILE *stream) { } } -static void dynType_printAny(char *name, dyn_type *type, int depth, FILE *stream) { - dyn_type *toPrint = type; +static void dynType_printAny(const char* name, const dyn_type* type, int depth, FILE *stream) { + const dyn_type* toPrint = type; if (toPrint->type == DYN_TYPE_REF) { toPrint = toPrint->ref.ref; } @@ -1015,12 +1002,13 @@ static void dynType_printAny(char *name, dyn_type *type, int depth, FILE *stream } } -static void dynType_printComplex(char *name, dyn_type *type, int depth, FILE *stream) { +static void dynType_printComplex(const char* name, const dyn_type* type, int depth, FILE *stream) { if (type->name == NULL) { dynType_printDepth(depth, stream); - fprintf(stream, "%s: complex type (anon), size is %zu, alignment is %i, descriptor is '%c'. fields:\n", name, type->ffiType->size, type->ffiType->alignment, type->descriptor); + fprintf(stream, "%s: complex type (anon), size is %zu, alignment is %i, descriptor is '%c'. fields:\n", + name, type->ffiType->size, type->ffiType->alignment, type->descriptor); - struct complex_type_entry *entry = NULL; + struct complex_type_entry* entry = NULL; TAILQ_FOREACH(entry, &type->complex.entriesHead, entries) { dynType_printAny(entry->name, entry->type, depth + 1, stream); } @@ -1029,49 +1017,57 @@ static void dynType_printComplex(char *name, dyn_type *type, int depth, FILE *st fprintf(stream, "}\n"); } else { dynType_printDepth(depth, stream); - fprintf(stream, "%s: complex type ('%s'), size is %zu, alignment is %i, descriptor is '%c'.\n", name, type->name, type->ffiType->size, type->ffiType->alignment, type->descriptor); + fprintf(stream, "%s: complex type ('%s'), size is %zu, alignment is %i, descriptor is '%c'.\n", + name, type->name, type->ffiType->size, type->ffiType->alignment, type->descriptor); } } -static void dynType_printSequence(char *name, dyn_type *type, int depth, FILE *stream) { +static void dynType_printSequence(const char* name, const dyn_type* type, int depth, FILE* stream) { dynType_printDepth(depth, stream); - fprintf(stream, "sequence, size is %zu, alignment is %i, descriptor is '%c'. fields:\n", type->ffiType->size, type->ffiType->alignment, type->descriptor); + fprintf(stream, "sequence, size is %zu, alignment is %i, descriptor is '%c'. fields:\n", + type->ffiType->size, type->ffiType->alignment, type->descriptor); dynType_printDepth(depth + 1, stream); - fprintf(stream, "cap: simple type, size is %zu, alignment is %i.\n", type->sequence.seqType.elements[0]->size, type->sequence.seqType.elements[0]->alignment); + fprintf(stream, "cap: simple type, size is %zu, alignment is %i.\n", + type->sequence.seqType.elements[0]->size, type->sequence.seqType.elements[0]->alignment); dynType_printDepth(depth + 1, stream); - fprintf(stream, "len: simple type, size is %zu, alignment is %i.\n", type->sequence.seqType.elements[1]->size, type->sequence.seqType.elements[1]->alignment); + fprintf(stream, "len: simple type, size is %zu, alignment is %i.\n", + type->sequence.seqType.elements[1]->size, type->sequence.seqType.elements[1]->alignment); dynType_printDepth(depth + 1, stream); - fprintf(stream, "buf: array, size is %zu, alignment is %i. points to ->\n", type->sequence.seqType.elements[2]->size, type->sequence.seqType.elements[2]->alignment); + fprintf(stream, "buf: array, size is %zu, alignment is %i. points to ->\n", + type->sequence.seqType.elements[2]->size, type->sequence.seqType.elements[2]->alignment); dynType_printAny("element", type->sequence.itemType, depth + 1, stream); } -static void dynType_printSimple(char *name, dyn_type *type, int depth, FILE *stream) { +static void dynType_printSimple(const char* name, const dyn_type* type, int depth, FILE* stream) { if (type->descriptor != 'E') { dynType_printDepth(depth, stream); - fprintf(stream, "%s: simple type, size is %zu, alignment is %i, descriptor is '%c'.\n", name, type->ffiType->size, type->ffiType->alignment, type->descriptor); + fprintf(stream, "%s: simple type, size is %zu, alignment is %i, descriptor is '%c'.\n", + name, type->ffiType->size, type->ffiType->alignment, type->descriptor); } else { dynType_printEnum(name, type, depth, stream); } } -static void dynType_printEnum(char *name, dyn_type *type, int depth, FILE *stream) { +static void dynType_printEnum(const char* name, const dyn_type* type, int depth, FILE* stream) { dynType_printDepth(depth, stream); - fprintf(stream, "%s: enum type, size is %zu, alignment is %i, descriptor is '%c'. values:", name, type->ffiType->size, type->ffiType->alignment, type->descriptor); - struct meta_entry * m_entry; + fprintf(stream, "%s: enum type, size is %zu, alignment is %i, descriptor is '%c'. values:", + name, type->ffiType->size, type->ffiType->alignment, type->descriptor); + struct meta_entry* m_entry; TAILQ_FOREACH(m_entry, &type->metaProperties, entries) { fprintf(stream, " (\"%s\":\"%s\")", m_entry->name, m_entry->value); } fprintf(stream, "\n"); } -static void dynType_printTypedPointer(char *name, dyn_type *type, int depth, FILE *stream) { +static void dynType_printTypedPointer(const char* name, const dyn_type* type, int depth, FILE* stream) { dynType_printDepth(depth, stream); - fprintf(stream, "%s: typed pointer, size is %zu, alignment is %i, points to ->\n", name, type->ffiType->size, type->ffiType->alignment); - char *subName = NULL; + fprintf(stream, "%s: typed pointer, size is %zu, alignment is %i, points to ->\n", + name, type->ffiType->size, type->ffiType->alignment); + char* subName = NULL; char buf[128]; memset(buf,0,128); if (name != NULL) { @@ -1081,10 +1077,10 @@ static void dynType_printTypedPointer(char *name, dyn_type *type, int depth, FIL dynType_printAny(subName, type->typedPointer.typedType, depth + 1, stream); } -static void dynType_printTypes(dyn_type *type, FILE *stream) { +static void dynType_printTypes(const dyn_type* type, FILE* stream) { - dyn_type *parent = type->parent; - struct type_entry *pentry = NULL; + dyn_type* parent = type->parent; + struct type_entry* pentry = NULL; while (parent != NULL) { TAILQ_FOREACH(pentry, &parent->nestedTypesHead, entries) { if (pentry->type == type) { @@ -1094,9 +1090,9 @@ static void dynType_printTypes(dyn_type *type, FILE *stream) { parent = parent->parent; } - struct type_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; } @@ -1115,7 +1111,7 @@ static void dynType_printTypes(dyn_type *type, FILE *stream) { } - struct complex_type_entry *centry = NULL; + struct complex_type_entry* centry = NULL; switch(type->type) { case DYN_TYPE_COMPLEX : TAILQ_FOREACH(centry, &type->complex.entriesHead, entries) { diff --git a/libs/dfi/src/dyn_type_common.h b/libs/dfi/src/dyn_type_common.h index 45e8098ab..8638b63a7 100644 --- a/libs/dfi/src/dyn_type_common.h +++ b/libs/dfi/src/dyn_type_common.h @@ -31,36 +31,36 @@ extern "C" { #endif struct _dyn_type { - char *name; + char* name; char descriptor; int type; - ffi_type *ffiType; - dyn_type *parent; - struct types_head *referenceTypes; //NOTE: not owned + ffi_type* ffiType; + dyn_type* parent; + const struct types_head* referenceTypes; //NOTE: not owned struct types_head nestedTypesHead; struct meta_properties_head metaProperties; 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 + dyn_type** types; //based on entriesHead for fast access } complex; struct { ffi_type seqType; //dyn_type.ffiType points to this - dyn_type *itemType; + dyn_type* itemType; } sequence; struct { - dyn_type *typedType; + dyn_type* typedType; } typedPointer; struct { - dyn_type *ref; + dyn_type* ref; } ref; }; }; -dyn_type * dynType_findType(dyn_type *type, char *name); -ffi_type * dynType_ffiType(dyn_type * type); -void dynType_prepCif(ffi_type *type); +dyn_type * dynType_findType(dyn_type* type, char* name); +ffi_type * dynType_ffiType(dyn_type* type); +void dynType_prepCif(ffi_type* type); #ifdef __cplusplus } diff --git a/libs/dfi/src/json_rpc.c b/libs/dfi/src/json_rpc.c index ef90f5275..541cd9c3c 100644 --- a/libs/dfi/src/json_rpc.c +++ b/libs/dfi/src/json_rpc.c @@ -44,7 +44,7 @@ struct generic_service_layout { int jsonRpc_call(dyn_interface_type *intf, void *service, const char *request, char **out) { int status = OK; - dyn_type* returnType = NULL; + const dyn_type* returnType = NULL; json_error_t error; json_t *js_request = json_loads(request, 0, &error); @@ -108,7 +108,7 @@ int jsonRpc_call(dyn_interface_type *intf, void *service, const char *request, c //setup and deserialize input for (i = 0; i < nrOfArgs; ++i) { - dyn_type *argType = dynFunction_argumentTypeForIndex(func, i); + const dyn_type *argType = dynFunction_argumentTypeForIndex(func, i); enum dyn_function_argument_meta meta = dynFunction_argumentMetaForIndex(func, i); if (meta == DYN_FUNCTION_ARGUMENT_META__STD) { value = json_array_get(arguments, index++); @@ -118,8 +118,7 @@ int jsonRpc_call(dyn_interface_type *intf, void *service, const char *request, c } else if (meta == DYN_FUNCTION_ARGUMENT_META__PRE_ALLOCATED_OUTPUT) { void **instPtr = calloc(1, sizeof(void*)); void *inst = NULL; - dyn_type *subType = NULL; - dynType_typedPointer_getTypedType(argType, &subType); + const dyn_type *subType = dynType_typedPointer_getTypedType(argType); dynType_alloc(subType, &inst); *instPtr = inst; args[i] = instPtr; @@ -154,7 +153,7 @@ int jsonRpc_call(dyn_interface_type *intf, void *service, const char *request, c //free input args json_t *jsonResult = NULL; for(i = 0; i < nrOfArgs; ++i) { - dyn_type *argType = dynFunction_argumentTypeForIndex(func, i); + const dyn_type *argType = dynFunction_argumentTypeForIndex(func, i); enum dyn_function_argument_meta meta = dynFunction_argumentMetaForIndex(func, i); if (meta == DYN_FUNCTION_ARGUMENT_META__STD) { if (dynType_descriptorType(argType) == 't') { @@ -174,30 +173,29 @@ int jsonRpc_call(dyn_interface_type *intf, void *service, const char *request, c //serialize and free output for (i = 0; i < nrOfArgs; i += 1) { - dyn_type *argType = dynFunction_argumentTypeForIndex(func, i); + const dyn_type *argType = dynFunction_argumentTypeForIndex(func, i); enum dyn_function_argument_meta meta = dynFunction_argumentMetaForIndex(func, i); if (meta == DYN_FUNCTION_ARGUMENT_META__PRE_ALLOCATED_OUTPUT) { if (funcCallStatus == 0 && status == OK) { status = jsonSerializer_serializeJson(argType, args[i], &jsonResult); } - dyn_type *subType = NULL; - dynType_typedPointer_getTypedType(argType, &subType); + const dyn_type* subType = dynType_typedPointer_getTypedType(argType); void **ptrToInst = (void**)args[i]; dynType_free(subType, *ptrToInst); free(ptrToInst); } else if (meta == DYN_FUNCTION_ARGUMENT_META__OUTPUT) { if (funcCallStatus == 0 && ptr != NULL) { - dyn_type *typedType = NULL; + const dyn_type *typedType = NULL; if (status == OK) { - status = dynType_typedPointer_getTypedType(argType, &typedType); + typedType = dynType_typedPointer_getTypedType(argType); } if (status == OK && dynType_descriptorType(typedType) == 't') { status = jsonSerializer_serializeJson(typedType, (void*) &ptr, &jsonResult); free(ptr); } else { - dyn_type *typedTypedType = NULL; + const dyn_type *typedTypedType = NULL; if (status == OK) { - status = dynType_typedPointer_getTypedType(typedType, &typedTypedType); + typedTypedType = dynType_typedPointer_getTypedType(typedType); } if(status == OK){ @@ -255,7 +253,7 @@ int jsonRpc_prepareInvokeRequest(dyn_function_type *func, const char *id, void * int i; int nrOfArgs = dynFunction_nrOfArguments(func); for (i = 0; i < nrOfArgs; i +=1) { - dyn_type *type = dynFunction_argumentTypeForIndex(func, i); + const dyn_type *type = dynFunction_argumentTypeForIndex(func, i); enum dyn_function_argument_meta meta = dynFunction_argumentMetaForIndex(func, i); if (meta == DYN_FUNCTION_ARGUMENT_META__STD) { json_t *val = NULL; @@ -345,7 +343,7 @@ int jsonRpc_handleReply(dyn_function_type *func, const char *reply, void *args[] if (status == OK && !replyHasError) { int i; for (i = 0; i < nrOfArgs; i += 1) { - dyn_type *argType = dynFunction_argumentTypeForIndex(func, i); + const dyn_type *argType = dynFunction_argumentTypeForIndex(func, i); enum dyn_function_argument_meta meta = dynFunction_argumentMetaForIndex(func, i); if (meta == DYN_FUNCTION_ARGUMENT_META__PRE_ALLOCATED_OUTPUT) { void *tmp = NULL; @@ -359,7 +357,7 @@ int jsonRpc_handleReply(dyn_function_type *func, const char *reply, void *args[] memcpy(*out, *(void**) tmp, size); } } else { - dynType_typedPointer_getTypedType(argType, &argType); + argType = dynType_typedPointer_getTypedType(argType); status = jsonSerializer_deserializeJson(argType, result, &tmp); if (tmp != NULL) { size = dynType_size(argType); @@ -369,9 +367,7 @@ int jsonRpc_handleReply(dyn_function_type *func, const char *reply, void *args[] dynType_free(argType, tmp); } else if (meta == DYN_FUNCTION_ARGUMENT_META__OUTPUT) { - dyn_type *subType = NULL; - - dynType_typedPointer_getTypedType(argType, &subType); + const dyn_type *subType = dynType_typedPointer_getTypedType(argType); if (dynType_descriptorType(subType) == 't') { char ***out = (char ***) args[i]; @@ -381,8 +377,7 @@ int jsonRpc_handleReply(dyn_function_type *func, const char *reply, void *args[] free(ptrToString); **out = (void*)s; } else { - dyn_type *subSubType = NULL; - dynType_typedPointer_getTypedType(subType, &subSubType); + const dyn_type *subSubType = dynType_typedPointer_getTypedType(subType); void ***out = (void ***) args[i]; status = jsonSerializer_deserializeJson(subSubType, result, *out); } diff --git a/libs/dfi/src/json_serializer.c b/libs/dfi/src/json_serializer.c index dbbc5b742..34d4f577b 100644 --- a/libs/dfi/src/json_serializer.c +++ b/libs/dfi/src/json_serializer.c @@ -28,17 +28,17 @@ #include #include -static int jsonSerializer_createType(dyn_type *type, json_t *object, void **result); -static int jsonSerializer_parseObject(dyn_type *type, json_t *object, void *inst); -static int jsonSerializer_parseObjectMember(dyn_type *type, const char *name, json_t *val, void *inst); -static int jsonSerializer_parseSequence(dyn_type *seq, json_t *array, void *seqLoc); -static int jsonSerializer_parseAny(dyn_type *type, void *input, json_t *val); -static int jsonSerializer_parseEnum(dyn_type *type, const char* enum_name, int32_t *out); +static int jsonSerializer_createType(const dyn_type *type, json_t *object, void **result); +static int jsonSerializer_parseObject(const dyn_type *type, json_t *object, void *inst); +static int jsonSerializer_parseObjectMember(const dyn_type *type, const char *name, json_t *val, void *inst); +static int jsonSerializer_parseSequence(const dyn_type *seq, json_t *array, void *seqLoc); +static int jsonSerializer_parseAny(const dyn_type *type, void *input, json_t *val); +static int jsonSerializer_parseEnum(const dyn_type *type, const char* enum_name, int32_t *out); -static int jsonSerializer_writeAny(dyn_type *type, void *input, json_t **val); -static int jsonSerializer_writeComplex(dyn_type *type, void *input, json_t **val); -static int jsonSerializer_writeSequence(dyn_type *type, void *input, json_t **out); -static int jsonSerializer_writeEnum(dyn_type *type, int32_t enum_value, json_t **out); +static int jsonSerializer_writeAny(const dyn_type *type, void *input, json_t **val); +static int jsonSerializer_writeComplex(const dyn_type *type, void *input, json_t **val); +static int jsonSerializer_writeSequence(const dyn_type *type, void *input, json_t **out); +static int jsonSerializer_writeEnum(const dyn_type *type, int32_t enum_value, json_t **out); static int OK = 0; @@ -65,11 +65,11 @@ int jsonSerializer_deserialize(dyn_type *type, const char *input, size_t length, return status; } -int jsonSerializer_deserializeJson(dyn_type *type, json_t *input, void **out) { +int jsonSerializer_deserializeJson(const dyn_type *type, json_t *input, void **out) { return jsonSerializer_createType(type, input, out); } -static int jsonSerializer_createType(dyn_type *type, json_t *val, void **result) { +static int jsonSerializer_createType(const dyn_type *type, json_t *val, void **result) { assert(val != NULL); int status = OK; void *inst = NULL; @@ -104,7 +104,7 @@ static int jsonSerializer_createType(dyn_type *type, json_t *val, void **result) return status; } -static int jsonSerializer_parseObject(dyn_type *type, json_t *object, void *inst) { +static int jsonSerializer_parseObject(const dyn_type *type, json_t *object, void *inst) { assert(object != NULL); int status = 0; json_t *value; @@ -120,35 +120,25 @@ static int jsonSerializer_parseObject(dyn_type *type, json_t *object, void *inst return status; } -static int jsonSerializer_parseObjectMember(dyn_type *type, const char *name, json_t *val, void *inst) { - int status = OK; +static int jsonSerializer_parseObjectMember(const dyn_type *type, const char *name, json_t *val, void *inst) { void *valp = NULL; - dyn_type *valType = NULL; + const dyn_type* valType = NULL; int index = dynType_complex_indexForName(type, name); if (index < 0) { return OK;//We should ignore unknown name in request or response. Satisfy forward compatibility for responses. } - if (status == OK) { - status = dynType_complex_valLocAt(type, index, inst, &valp); - } - - if (status == OK ) { - status = dynType_complex_dynTypeAt(type, index, &valType); - } + valp = dynType_complex_valLocAt(type, index, inst); + valType = dynType_complex_dynTypeAt(type, index); - if (status == OK) { - status = jsonSerializer_parseAny(valType, valp, val); - } - - return status; + return jsonSerializer_parseAny(valType, valp, val); } -static int jsonSerializer_parseAny(dyn_type *type, void *loc, json_t *val) { +static int jsonSerializer_parseAny(const dyn_type *type, void *loc, json_t *val) { int status = OK; - dyn_type *subType = NULL; + const dyn_type *subType = NULL; char c = dynType_descriptorType(type); /* @@ -255,10 +245,8 @@ static int jsonSerializer_parseAny(dyn_type *type, void *loc, json_t *val) { } break; case '*' : - status = dynType_typedPointer_getTypedType(type, &subType); - if (status == OK) { - status = jsonSerializer_createType(subType, val, (void **) loc); - } + subType = dynType_typedPointer_getTypedType(type); + status = jsonSerializer_createType(subType, val, (void **) loc); break; case 'P' : status = ERROR; @@ -276,7 +264,7 @@ static int jsonSerializer_parseAny(dyn_type *type, void *loc, json_t *val) { return status; } -static int jsonSerializer_parseSequence(dyn_type *seq, json_t *array, void *seqLoc) { +static int jsonSerializer_parseSequence(const dyn_type *seq, json_t *array, void *seqLoc) { assert(dynType_type(seq) == DYN_TYPE_SEQUENCE); int status = OK; @@ -284,7 +272,7 @@ static int jsonSerializer_parseSequence(dyn_type *seq, json_t *array, void *seqL status = dynType_sequence_alloc(seq, seqLoc, (int) size); if (status == OK) { - dyn_type *itemType = dynType_sequence_itemType(seq); + const dyn_type *itemType = dynType_sequence_itemType(seq); size_t index; json_t *val; json_array_foreach(array, index, val) { @@ -316,7 +304,7 @@ int jsonSerializer_serialize(dyn_type *type, const void* input, char **output) { return status; } -static int jsonSerializer_parseEnum(dyn_type *type, const char* enum_name, int32_t *out) { +static int jsonSerializer_parseEnum(const dyn_type *type, const char* enum_name, int32_t *out) { struct meta_entry * entry; TAILQ_FOREACH(entry, &type->metaProperties, entries) { @@ -330,16 +318,16 @@ static int jsonSerializer_parseEnum(dyn_type *type, const char* enum_name, int32 return ERROR; } -int jsonSerializer_serializeJson(dyn_type *type, const void* input, json_t **out) { +int jsonSerializer_serializeJson(const dyn_type *type, const void* input, json_t **out) { return jsonSerializer_writeAny(type, (void*)input /*TODO update static function to take const void**/, out); } -static int jsonSerializer_writeAny(dyn_type *type, void* input, json_t **out) { +static int jsonSerializer_writeAny(const dyn_type *type, void* input, json_t **out) { int status = OK; int descriptor = dynType_descriptorType(type); json_t *val = NULL; - dyn_type *subType = NULL; + const dyn_type *subType = NULL; bool *z; //Z float *f; //F @@ -412,10 +400,8 @@ static int jsonSerializer_writeAny(dyn_type *type, void* input, json_t **out) { status = jsonSerializer_writeEnum(type, *e, &val); break; case '*' : - status = dynType_typedPointer_getTypedType(type, &subType); - if (status == OK) { - status = jsonSerializer_writeAny(subType, *(void **)input, &val); - } + subType = dynType_typedPointer_getTypedType(type); + status = jsonSerializer_writeAny(subType, *(void **)input, &val); break; case '{' : status = jsonSerializer_writeComplex(type, input, &val); @@ -443,12 +429,12 @@ static int jsonSerializer_writeAny(dyn_type *type, void* input, json_t **out) { return status; } -static int jsonSerializer_writeSequence(dyn_type *type, void *input, json_t **out) { +static int jsonSerializer_writeSequence(const dyn_type *type, void *input, json_t **out) { assert(dynType_type(type) == DYN_TYPE_SEQUENCE); int status = OK; json_t *array = json_array(); - dyn_type *itemType = dynType_sequence_itemType(type); + const dyn_type *itemType = dynType_sequence_itemType(type); uint32_t len = dynType_sequence_length(input); int i = 0; @@ -480,43 +466,40 @@ static int jsonSerializer_writeSequence(dyn_type *type, void *input, json_t **ou return status; } -static int jsonSerializer_writeComplex(dyn_type *type, void *input, json_t **out) { +static int jsonSerializer_writeComplex(const dyn_type *type, void *input, json_t **out) { assert(dynType_type(type) == DYN_TYPE_COMPLEX); int status = OK; json_t *val = json_object(); struct complex_type_entry *entry = NULL; - struct complex_type_entries_head *entries = NULL; + const struct complex_type_entries_head *entries = dynType_complex_entries(type); int index = -1; - status = dynType_complex_entries(type, &entries); - if (status == OK) { - TAILQ_FOREACH(entry, entries, entries) { - void *subLoc = NULL; - json_t *subVal = NULL; - dyn_type *subType = NULL; - index = dynType_complex_indexForName(type, entry->name); - if (index < 0) { - celix_err_pushf("Cannot find index for member '%s'", entry->name); - status = ERROR; - } - if(status == OK){ - status = dynType_complex_valLocAt(type, index, input, &subLoc); - } - if (status == OK) { - status = dynType_complex_dynTypeAt(type, index, &subType); - } - if (status == OK) { - status = jsonSerializer_writeAny(subType, subLoc, &subVal); - } - if (status == OK) { - json_object_set(val, entry->name, subVal); - json_decref(subVal); - } + TAILQ_FOREACH(entry, entries, entries) { + void *subLoc = NULL; + json_t *subVal = NULL; + const dyn_type* subType = NULL; + index = dynType_complex_indexForName(type, entry->name); + if (index < 0) { + celix_err_pushf("Cannot find index for member '%s'", entry->name); + status = ERROR; + } + if(status == OK){ + subLoc = dynType_complex_valLocAt(type, index, input); + } + if (status == OK) { + subType = dynType_complex_dynTypeAt(type, index); + } + if (status == OK) { + status = jsonSerializer_writeAny(subType, subLoc, &subVal); + } + if (status == OK) { + json_object_set(val, entry->name, subVal); + json_decref(subVal); + } - if (status != OK) { - break; - } + if (status != OK) { + break; } } @@ -530,7 +513,7 @@ static int jsonSerializer_writeComplex(dyn_type *type, void *input, json_t **out return status; } -static int jsonSerializer_writeEnum(dyn_type *type, int32_t enum_value, json_t **out) { +static int jsonSerializer_writeEnum(const dyn_type* type, int32_t enum_value, json_t **out) { struct meta_entry * entry; // Convert to string