diff --git a/WIKI_EN.md b/WIKI_EN.md index 70b2911..c184344 100644 --- a/WIKI_EN.md +++ b/WIKI_EN.md @@ -384,4 +384,4 @@ limitations under the License. --- -*Generated for zh_vector v1.1.1* +*Generated for zh_vector v1.1.2* diff --git a/WIKI_RU.md b/WIKI_RU.md index 0759476..55d55c8 100644 --- a/WIKI_RU.md +++ b/WIKI_RU.md @@ -384,4 +384,4 @@ void app_main(void) --- -*Сгенерировано для zh_vector v1.1.1* +*Сгенерировано для zh_vector v1.1.2* diff --git a/idf_component.yml b/idf_component.yml index f541e55..f6d9c94 100644 --- a/idf_component.yml +++ b/idf_component.yml @@ -1,6 +1,6 @@ description: ESP32 ESP-IDF component for vector (dynamic array) url: https://github.com/aZholtikov/zh_vector -version: "1.1.1" +version: "1.1.2" license: "Apache-2.0" maintainers: - "Alexey Zholtikov " diff --git a/version.txt b/version.txt index 8cfbc90..8428158 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -1.1.1 \ No newline at end of file +1.1.2 \ No newline at end of file diff --git a/zh_vector.c b/zh_vector.c index 256a710..2d73ceb 100644 --- a/zh_vector.c +++ b/zh_vector.c @@ -20,11 +20,11 @@ esp_err_t zh_vector_init(zh_vector_t *vector, uint16_t unit, ...) ZH_LOGI("Vector initialization begin."); ZH_ERROR_CHECK(vector != NULL && unit != 0, ESP_ERR_INVALID_ARG, NULL, "Vector initialization failed. Invalid argument."); ZH_ERROR_CHECK(vector->is_initialized == false, ESP_ERR_INVALID_STATE, NULL, "Vector initialization failed. Vector is already initialized."); + vector->items = NULL; vector->capacity = 0; vector->size = 0; vector->unit = unit; vector->is_initialized = true; - vector->items = heap_caps_calloc(1, sizeof(vector->items), MALLOC_CAP_8BIT); ZH_LOGI("Vector initialization success."); return ESP_OK; } @@ -37,8 +37,12 @@ esp_err_t zh_vector_free(zh_vector_t *vector) for (uint16_t i = 0; i < vector->size; ++i) { heap_caps_free(vector->items[i]); + vector->items[i] = NULL; } heap_caps_free(vector->items); + vector->items = NULL; + vector->size = 0; + vector->capacity = 0; vector->is_initialized = false; ZH_LOGI("Vector deletion success."); return ESP_OK; @@ -93,16 +97,13 @@ void *zh_vector_get_item(zh_vector_t *vector, uint16_t index) ZH_LOGE("Getting item from vector fail. Vector not initialized.", ESP_ERR_INVALID_STATE); return NULL; } - if (index < vector->size) - { - ZH_LOGI("Getting item from vector success."); - return vector->items[index]; - } - else + if (index >= vector->size) { ZH_LOGE("Getting item from vector fail. Index does not exist.", ESP_FAIL); return NULL; } + ZH_LOGI("Getting item from vector success."); + return vector->items[index]; } esp_err_t zh_vector_delete_item(zh_vector_t *vector, uint16_t index) // -V2008 @@ -112,24 +113,31 @@ esp_err_t zh_vector_delete_item(zh_vector_t *vector, uint16_t index) // -V2008 ZH_ERROR_CHECK(vector->is_initialized == true, ESP_ERR_INVALID_STATE, NULL, "Deleting item in vector fail. Vector not initialized."); ZH_ERROR_CHECK(index < vector->size, ESP_FAIL, NULL, "Deleting item in vector fail. Index does not exist."); heap_caps_free(vector->items[index]); - for (uint8_t i = index; i < (vector->size - 1); ++i) + for (uint16_t i = index; i < (vector->size - 1); ++i) { vector->items[i] = vector->items[i + 1]; - vector->items[i + 1] = NULL; } - --vector->size; - ZH_ERROR_CHECK(_resize(vector, vector->capacity - 1) == ESP_OK, ESP_ERR_NO_MEM, NULL, "Deleting item in vector fail. Memory allocation fail or no free memory in the heap."); + vector->items[--vector->size] = NULL; + if (vector->capacity > vector->size) + { + ZH_ERROR_CHECK(_resize(vector, vector->size) == ESP_OK, ESP_ERR_NO_MEM, NULL, "Deleting item in vector fail. Memory allocation fail or no free memory in the heap."); + } ZH_LOGI("Deleting item in vector success."); return ESP_OK; } static esp_err_t _resize(zh_vector_t *vector, uint16_t capacity) { - if (capacity != 0) + if (capacity == 0) + { + heap_caps_free(vector->items); + vector->items = NULL; + } + else { vector->items = heap_caps_realloc(vector->items, sizeof(void *) * capacity, MALLOC_CAP_8BIT); ZH_ERROR_CHECK(vector->items != NULL, ESP_ERR_NO_MEM, NULL, "Memory allocation fail or no free memory in the heap."); } vector->capacity = capacity; return ESP_OK; -} \ No newline at end of file +}