Skip to content

Commit

Permalink
Add dynType_parseOfName to avoid unnecessary string duplication durin…
Browse files Browse the repository at this point in the history
…g parsing.
  • Loading branch information
PengZheng committed Dec 26, 2023
1 parent 20ef889 commit 04692bc
Show file tree
Hide file tree
Showing 12 changed files with 177 additions and 72 deletions.
2 changes: 2 additions & 0 deletions libs/dfi/gtest/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,13 @@ if (EI_TESTS)
add_executable(test_dfi_with_ei
src/dyn_interface_ei_tests.cc
src/dyn_common_ei_tests.cc
src/dyn_type_ei_tests.cc
)
target_link_libraries(test_dfi_with_ei PRIVATE
dfi_cut
Celix::malloc_ei
Celix::stdio_ei
Celix::string_ei
GTest::gtest GTest::gtest_main
)
add_test(NAME run_test_dfi_with_ei COMMAND test_dfi_with_ei)
Expand Down
70 changes: 70 additions & 0 deletions libs/dfi/gtest/src/dyn_type_ei_tests.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

#include "dyn_type.h"
#include "celix_err.h"
#include "malloc_ei.h"
#include "stdio_ei.h"
#include "string_ei.h"

#include <errno.h>
#include <gtest/gtest.h>
#include <string>

class DynTypeErrorInjectionTestSuite : public ::testing::Test {
public:
DynTypeErrorInjectionTestSuite() {
}

~DynTypeErrorInjectionTestSuite() override {
celix_ei_expect_strdup(nullptr, 0, nullptr);
celix_ei_expect_calloc(nullptr, 0, nullptr);
celix_ei_expect_fmemopen(nullptr, 0, nullptr);
}
// delete other constructors and assign operators
DynTypeErrorInjectionTestSuite(DynTypeErrorInjectionTestSuite const&) = delete;
DynTypeErrorInjectionTestSuite(DynTypeErrorInjectionTestSuite&&) = delete;
DynTypeErrorInjectionTestSuite& operator=(DynTypeErrorInjectionTestSuite const&) = delete;
DynTypeErrorInjectionTestSuite& operator=(DynTypeErrorInjectionTestSuite&&) = delete;
};

TEST_F(DynTypeErrorInjectionTestSuite, ParseTypeErrors) {
dyn_type *type = NULL;
const char* descriptor = "{D{DD b_1 b_2}I a b c}";

// fail to open memory as stream
celix_ei_expect_fmemopen((void*)dynType_parseWithStr, 0, nullptr);
int status = dynType_parseWithStr(descriptor, NULL, NULL, &type);
ASSERT_NE(0, status);
std::string msg = "Error creating mem stream for descriptor string. ";
msg += strerror(ENOMEM);
ASSERT_STREQ(msg.c_str(), celix_err_popLastError());

// fail to allocate dyn_type
celix_ei_expect_calloc((void*)dynType_parseWithStr, 2, nullptr);
status = dynType_parseWithStr(descriptor, NULL, NULL, &type);
ASSERT_NE(0, status);
ASSERT_STREQ("Error allocating memory for type", celix_err_popLastError());

// fail to duplicate type name
celix_ei_expect_strdup((void*)dynType_parseWithStr, 1, nullptr);
status = dynType_parseWithStr(descriptor, "hello", NULL, &type);
ASSERT_NE(0, status);
ASSERT_STREQ("Error strdup'ing name 'hello'", celix_err_popLastError());
}
6 changes: 6 additions & 0 deletions libs/dfi/include/dyn_type.h
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,12 @@ struct meta_entry {
*/
CELIX_DFI_EXPORT int dynType_parse(FILE* descriptorStream, const char* name, const struct types_head* refTypes, dyn_type** type);

/**
* @brief Parses a descriptor stream and creates a dyn_type (dynamic type) of a given name.
* Similar to dynType_parse except that the ownership of the given name is taken by the dyn type.
*/
CELIX_DFI_EXPORT int dynType_parseOfName(FILE* descriptorStream, 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.
Expand Down
2 changes: 1 addition & 1 deletion libs/dfi/src/dyn_interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ static int dynInterface_parseTypes(dyn_interface_type *intf, FILE *stream) {
}

celix_autoptr(dyn_type) type = NULL;
if ((status = dynType_parse(stream, name, &intf->types, &type)) != OK) {
if ((status = dynType_parseOfName(stream, celix_steal_ptr(name), &intf->types, &type)) != OK) {
return status;
}
if ((status = dynCommon_eatChar(stream, '\n')) != OK) {
Expand Down
9 changes: 3 additions & 6 deletions libs/dfi/src/dyn_message.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

#include "dyn_message.h"
#include "celix_err.h"
#include "celix_stdlib_cleanup.h"

#include <stdlib.h>
#include <string.h>
Expand Down Expand Up @@ -179,7 +180,7 @@ static int dynMessage_parseTypes(dyn_message_type *msg, FILE *stream) {
while (peek != ':' && peek != EOF) {
ungetc(peek, stream);

char *name = NULL;
celix_autofree char* name = NULL;
status = dynCommon_parseName(stream, &name);

if (status == OK) {
Expand All @@ -188,7 +189,7 @@ static int dynMessage_parseTypes(dyn_message_type *msg, FILE *stream) {

dyn_type *type = NULL;
if (status == OK) {
status = dynType_parse(stream, name, &msg->types, &type);
status = dynType_parseOfName(stream, celix_steal_ptr(name), &msg->types, &type);
}

if (status == OK) {
Expand All @@ -207,10 +208,6 @@ static int dynMessage_parseTypes(dyn_message_type *msg, FILE *stream) {
}
}

if (name != NULL) {
free(name);
}

if (status != OK) {
if (type != NULL) {
dynType_destroy(type);
Expand Down
87 changes: 46 additions & 41 deletions libs/dfi/src/dyn_type.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@

#include "dyn_type.h"
#include "dyn_type_common.h"
#include "dyn_type_common.h"
#include "dyn_common.h"
#include "celix_err.h"
#include "celix_stdio_cleanup.h"
#include "celix_stdlib_cleanup.h"

#include <stdlib.h>
#include <string.h>
Expand All @@ -35,6 +36,7 @@ static const int MEM_ERROR = 2;
static const int PARSE_ERROR = 3;

static int dynType_parseWithStream(FILE* stream, const char* name, dyn_type* parent, const struct types_head* refTypes, dyn_type** result);
static int dynType_parseWithStreamOfName(FILE* stream, 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);
Expand Down Expand Up @@ -84,56 +86,59 @@ int dynType_parse(FILE* descriptorStream, const char* name, const struct types_h
return dynType_parseWithStream(descriptorStream, name, NULL, refTypes, type);
}


int dynType_parseOfName(FILE* descriptorStream, char* name, const struct types_head* refTypes, dyn_type** type) {
return dynType_parseWithStreamOfName(descriptorStream, name, NULL, refTypes, 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");
if (stream != NULL) {
status = dynType_parseWithStream(stream, name, NULL, refTypes, type);
if (status == OK) {
int c = fgetc(stream);
if (c != '\0' && c != EOF) {
status = PARSE_ERROR;
dynType_destroy(*type);
*type = NULL;
celix_err_pushf("Expected EOF got %c", c);
}
}
fclose(stream);
} else {
status = ERROR;
celix_autoptr(FILE) stream = fmemopen((char *)descriptor, strlen(descriptor), "r");
if (stream == NULL) {
celix_err_pushf("Error creating mem stream for descriptor string. %s", strerror(errno));
return ERROR;
}
celix_autoptr(dyn_type) result = NULL;
if ((status = dynType_parseWithStream(stream, name, NULL, refTypes, &result)) != OK) {
return status;
}
if (dynCommon_eatChar(stream, EOF) != 0) {
return PARSE_ERROR;
}
*type = celix_steal_ptr(result);
return status;
}

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));
if (type != NULL) {
type->parent = parent;
type->type = DYN_TYPE_INVALID;
type->referenceTypes = refTypes;
TAILQ_INIT(&type->nestedTypesHead);
TAILQ_INIT(&type->metaProperties);
if (name != NULL) {
type->name = strdup(name);
if (type->name == NULL) {
status = MEM_ERROR;
celix_err_pushf("Error strdup'ing name '%s'\n", name);
}
}
if (status == OK) {
status = dynType_parseAny(stream, type);
}
if (status == OK) {
*result = type;
} else {
dynType_destroy(type);
char* typeName = NULL;
if (name != NULL) {
typeName = strdup(name);
if (typeName == NULL) {
celix_err_pushf("Error strdup'ing name '%s'", name);
return MEM_ERROR;
}
} else {
status = MEM_ERROR;
celix_err_pushf("Error allocating memory for type");
}
return dynType_parseWithStreamOfName(stream, typeName, parent, refTypes, result);
}

static int dynType_parseWithStreamOfName(FILE* stream, char* name, dyn_type* parent, const struct types_head* refTypes, dyn_type** result) {
int status = OK;
celix_autofree char* typeName = name;
celix_autoptr(dyn_type) type = calloc(1, sizeof(*type));
if (type == NULL) {
celix_err_push("Error allocating memory for type");
return MEM_ERROR;
}
type->parent = parent;
type->type = DYN_TYPE_INVALID;
type->referenceTypes = refTypes;
TAILQ_INIT(&type->nestedTypesHead);
TAILQ_INIT(&type->metaProperties);
type->name = celix_steal_ptr(typeName);
if ((status = dynType_parseAny(stream, type)) != OK) {
return status;
}
*result = celix_steal_ptr(type);
return status;
}

Expand Down
1 change: 1 addition & 0 deletions libs/error_injector/stdio/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,6 @@ target_link_options(stdio_ei INTERFACE
LINKER:--wrap,fputs
LINKER:--wrap,fclose
LINKER:--wrap,fgetc
LINKER:--wrap,fmemopen
)
add_library(Celix::stdio_ei ALIAS stdio_ei)
2 changes: 2 additions & 0 deletions libs/error_injector/stdio/include/stdio_ei.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ CELIX_EI_DECLARE(fclose, int);

CELIX_EI_DECLARE(fgetc, int);

CELIX_EI_DECLARE(fmemopen, FILE *);

#ifdef __cplusplus
}
#endif
Expand Down
Loading

0 comments on commit 04692bc

Please sign in to comment.