fix: allocate new item in push_front before shifting

This commit is contained in:
aZholtikov committed 2026-09-25 18:49:41 +03:00
1 parent 288aca9905
commit 1891599e66
4 files changed
+17 -16

No files matched your search

+1 -1
View File
@@ -1,6 +1,6 @@
description: ESP32 ESP-IDF component for vector (dynamic array)
url: https://github.com/aZholtikov/zh_vector
version: "2.5.0"
version: "2.5.1"
license: "Apache-2.0"
maintainers:
- "Alexey Zholtikov <alexey.zholtikov@yandex.ru>"
+12 -12
View File
@@ -74,7 +74,7 @@ extern "C"
/**
* @brief Get the current number of elements in the vector.
*
* @param[in] vector Pointer to the vector (must not be NULL)
* @param[in] vector Pointer to the vector handle (must not be NULL)
* @param[out] size Pointer to receive the current size (must not be NULL)
*
* @return ESP_OK on success
@@ -86,7 +86,7 @@ extern "C"
/**
* @brief Get the current capacity of the vector.
*
* @param[in] vector Pointer to the vector (must not be NULL)
* @param[in] vector Pointer to the vector handle (must not be NULL)
* @param[out] capacity Pointer to receive the current capacity (must not be NULL)
*
* @return ESP_OK on success
@@ -102,7 +102,7 @@ extern "C"
* a copy of the provided item at index 0. The vector capacity
* will be increased if necessary.
*
* @param[in,out] vector Pointer to the vector (must not be NULL)
* @param[in,out] vector Pointer to the vector handle (must not be NULL)
* @param[in] item Pointer to the item to insert (must not be NULL)
*
* @return ESP_OK on success
@@ -118,7 +118,7 @@ extern "C"
* Creates a copy of the provided item and adds it to the end of the vector.
* The vector capacity will be increased if necessary.
*
* @param[in,out] vector Pointer to the vector (must not be NULL)
* @param[in,out] vector Pointer to the vector handle (must not be NULL)
* @param[in] item Pointer to the item to append (must not be NULL)
*
* @return ESP_OK on success
@@ -134,7 +134,7 @@ extern "C"
* Copies the provided item into the existing element at the given index.
* The element must already exist and be non-NULL.
*
* @param[in,out] vector Pointer to the vector (must not be NULL)
* @param[in,out] vector Pointer to the vector handle (must not be NULL)
* @param[in] index Index of the element to replace (must be < size)
* @param[in] item Pointer to the new item data (must not be NULL)
*
@@ -147,7 +147,7 @@ extern "C"
/**
* @brief Retrieve a copy of the element at the specified index.
*
* @param[in] vector Pointer to the vector (must not be NULL)
* @param[in] vector Pointer to the vector handle (must not be NULL)
* @param[in] index Index of the element to retrieve (must be < size)
* @param[out] item Pointer to buffer where the element will be copied (must not be NULL)
*
@@ -167,7 +167,7 @@ extern "C"
* Frees the element's memory, shifts remaining elements, and updates size.
* Capacity may be reduced if it exceeds twice the current size.
*
* @param[in,out] vector Pointer to the vector (must not be NULL)
* @param[in,out] vector Pointer to the vector handle (must not be NULL)
* @param[in] index Index of the element to delete (must be < size)
*
* @return ESP_OK on success
@@ -182,7 +182,7 @@ extern "C"
* Frees the last element's memory and updates size.
* Capacity may be reduced if it exceeds twice the current size.
*
* @param[in,out] vector Pointer to the vector (must not be NULL)
* @param[in,out] vector Pointer to the vector handle (must not be NULL)
*
* @return ESP_OK on success
* @return ESP_ERR_INVALID_ARG if vector or *vector is NULL or vector is empty
@@ -196,7 +196,7 @@ extern "C"
* Frees the first element's memory, shifts remaining elements, and updates size.
* Capacity may be reduced if it exceeds twice the current size.
*
* @param[in,out] vector Pointer to the vector (must not be NULL)
* @param[in,out] vector Pointer to the vector handle (must not be NULL)
*
* @return ESP_OK on success
* @return ESP_ERR_INVALID_ARG if vector or *vector is NULL or vector is empty
@@ -210,7 +210,7 @@ extern "C"
* Compares elements using memcmp and removes subsequent duplicates.
* Elements are compared based on their raw binary content.
*
* @param[in,out] vector Pointer to the vector (must not be NULL)
* @param[in,out] vector Pointer to the vector handle (must not be NULL)
*
* @return ESP_OK on success
* @return ESP_ERR_INVALID_ARG if vector or *vector is NULL
@@ -224,7 +224,7 @@ extern "C"
* Searches for an element whose binary content matches the provided item
* using memcmp. Returns the index of the first match or -1 if not found.
*
* @param[in] vector Pointer to the vector (must not be NULL)
* @param[in] vector Pointer to the vector handle (must not be NULL)
* @param[in] item Pointer to the item to search for (must not be NULL)
* @param[out] index Pointer to receive the found index (-1 if not found) (must not be NULL)
*
@@ -242,7 +242,7 @@ extern "C"
* and size) matches the provided value. Comparison starts from the
* specified index.
*
* @param[in] vector Pointer to the vector containing structures (must not be NULL)
* @param[in] vector Pointer to the vector handle containing structures (must not be NULL)
* @param[in] sample_struct Pointer to a sample structure (used for field offset calculation) (must not be NULL)
* @param[in] item Pointer to the field within the structure (e.g., `&sample.id`). Used to calculate the field offset (must not be NULL)
* @param[in] size Size of the field in bytes (must be > 0)
+1 -1
View File
@@ -1 +1 @@
2.5.0
2.5.1
+3 -2
View File
@@ -152,12 +152,13 @@ esp_err_t zh_vector_push_front(zh_vector_t **vector, const void *item)
uint16_t new_capacity = _calc_new_capacity((*vector)->capacity);
ZH_ERROR_CHECK(_resize(*vector, new_capacity) == ESP_OK, ESP_ERR_NO_MEM, xSemaphoreGive((*vector)->mutex), "Adding item to beginning of vector failed. Memory reallocation failed.");
}
void *new_item = heap_caps_calloc(1, (*vector)->unit, MALLOC_CAP_8BIT);
ZH_ERROR_CHECK(new_item != NULL, ESP_ERR_NO_MEM, xSemaphoreGive((*vector)->mutex), "Adding item to beginning of vector failed. Element allocation failed.");
for (uint16_t i = (*vector)->size; i > 0; --i)
{
(*vector)->items[i] = (*vector)->items[i - 1];
}
(*vector)->items[0] = heap_caps_calloc(1, (*vector)->unit, MALLOC_CAP_8BIT);
ZH_ERROR_CHECK((*vector)->items[0] != NULL, ESP_ERR_NO_MEM, xSemaphoreGive((*vector)->mutex), "Adding item to beginning of vector failed. Element allocation failed.");
(*vector)->items[0] = new_item;
memcpy((*vector)->items[0], item, (*vector)->unit);
(*vector)->size++;
xSemaphoreGive((*vector)->mutex);