Skip to content

Commit

Permalink
Refactor properties createEntry to use destroyEntry
Browse files Browse the repository at this point in the history
  • Loading branch information
pnoltes committed Nov 18, 2023
1 parent 492c457 commit 03731d6
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 38 deletions.
13 changes: 7 additions & 6 deletions libs/utils/include/celix_long_hash_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,12 @@ typedef struct celix_long_hash_map celix_long_hash_map_t;
* @Brief long hash map iterator, which contains a hash map entry's keu and value.
*/
typedef struct celix_long_hash_map_iterator {
size_t index; //iterator index, starting at 0
long key;
celix_hash_map_value_t value;
size_t index; /**< The index of the iterator. Starts at 0, ends at map size (so beyond the last element). When an
entry is removed, the index value is not changed. */
long key; /**< The key of the hash map entry. */
celix_hash_map_value_t value; /**< The value of the hash map entry. */

void* _internal[2]; //internal opaque struct
void* _internal[2]; /**< internal opaque data, do not use */
} celix_long_hash_map_iterator_t;

/**
Expand Down Expand Up @@ -344,11 +345,11 @@ bool celix_longHashMapIterator_equals(
*
* Small example of how to use the celix_longHashMapIterator_remove function:
* @code{.c}
* //remove all even entries
* //remove all entries except the first 4 entries from hash map
* celix_long_hash_map_t* map = ...
* celix_long_hash_map_iterator_t iter = celix_longHashMap_begin(map);
* while (!celix_longHashMapIterator_isEnd(&iter)) {
* if (iter.index % 2 == 0) {
* if (iter.index >= 4) {
* celix_longHashMapIterator_remove(&ter);
* } else {
* celix_longHashMapIterator_next(&iter);
Expand Down
15 changes: 8 additions & 7 deletions libs/utils/include/celix_string_hash_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,15 @@ extern "C" {
typedef struct celix_string_hash_map celix_string_hash_map_t;

/**
* @Brief long hash map iterator, which contains a hash map entry's keu and value.
* @Brief string hash map iterator, which contains a hash map entry's keu and value.
*/
typedef struct celix_string_hash_map_iterator {
size_t index; //iterator index, starting at 0
const char* key;
celix_hash_map_value_t value;
size_t index; /**< The index of the iterator. Starts at 0, ends at map size (so beyond the last element). When an
entry is removed, the index value is not changed. */
const char* key; /**< The key of the hash map entry. */
celix_hash_map_value_t value; /**< The value of the hash map entry. */

void* _internal[2]; //internal opaque struct
void* _internal[2]; /**< internal opaque data, do not use */
} celix_string_hash_map_iterator_t;

/**
Expand Down Expand Up @@ -379,11 +380,11 @@ bool celix_stringHashMapIterator_equals(
*
* Small example of how to use the celix_stringHashMapIterator_remove function:
* @code{.c}
* //remove all even entries from hash map
* //remove all entries except the first 4 entries from hash map
* celix_string_hash_map_t* map = ...
* celix_string_hash_map_iterator_t iter = celix_stringHashMap_begin(map);
* while (!celix_stringHashMapIterator_isEnd(&iter)) {
* if (iter.index % 2 == 0) {
* if (iter.index >= 4) {
* celix_stringHashMapIterator_remove(&ter);
* } else {
* celix_stringHashMapIterator_next(&iter);
Expand Down
48 changes: 23 additions & 25 deletions libs/utils/src/properties.c
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,27 @@ static celix_properties_entry_t* celix_properties_createEntryWithNoCopy(celix_pr
return entry;
}

static void celix_properties_destroyEntry(celix_properties_t* properties, celix_properties_entry_t* entry) {
celix_properties_freeString(properties, (char*)entry->value);
if (entry->valueType == CELIX_PROPERTIES_VALUE_TYPE_VERSION) {
celix_version_destroy((celix_version_t*)entry->typed.versionValue);
}

if (entry >= properties->entriesBuffer &&
entry <= (properties->entriesBuffer + CELIX_PROPERTIES_OPTIMIZATION_ENTRIES_BUFFER_SIZE)) {
if (entry == (properties->entriesBuffer + properties->currentEntriesBufferIndex - 1)) {
// entry is part of the properties entries buffer -> decrease the currentEntriesBufferIndex
properties->currentEntriesBufferIndex -= 1;
} else {
// entry is part of the properties entries buffer, but not the last entry -> nop
}
} else {
free(entry);
}
}



/**
* Create entry and optionally use the short properties optimization buffers.
* Only 1 of the types values (strValue, LongValue, etc) should be provided.
Expand All @@ -257,35 +278,12 @@ static celix_properties_entry_t* celix_properties_createEntry(celix_properties_t
celix_status_t status = celix_properties_fillEntry(properties, entry, prototype);
if (status != CELIX_SUCCESS) {
celix_err_pushf("Cannot fill property entry");
if (prototype->valueType == CELIX_PROPERTIES_VALUE_TYPE_VERSION) {
celix_version_destroy((celix_version_t*)prototype->typed.versionValue);
}
if (entry >= properties->entriesBuffer &&
entry <= (properties->entriesBuffer + CELIX_PROPERTIES_OPTIMIZATION_ENTRIES_BUFFER_SIZE)) {
// entry is part of the properties entries buffer -> decrease the currentEntriesBufferIndex
properties->currentEntriesBufferIndex -= 1;
} else {
free(entry);
}
entry = NULL;
celix_properties_destroyEntry(properties, entry);
return NULL;
}
return entry;
}

static void celix_properties_destroyEntry(celix_properties_t* properties, celix_properties_entry_t* entry) {
celix_properties_freeString(properties, (char*)entry->value);
if (entry->valueType == CELIX_PROPERTIES_VALUE_TYPE_VERSION) {
celix_version_destroy((celix_version_t*)entry->typed.versionValue);
}

if (entry >= properties->entriesBuffer &&
entry <= (properties->entriesBuffer + CELIX_PROPERTIES_OPTIMIZATION_ENTRIES_BUFFER_SIZE)) {
// entry is part of the properties entries buffer -> nop.
} else {
free(entry);
}
}

/**
* Create and add entry and optionally use the short properties optimization buffers.
* The prototype is used to determine the type of the value.
Expand Down

0 comments on commit 03731d6

Please sign in to comment.