destroy takes **vec instead of *vec, NULLs on op
This commit is contained in:
@@ -141,7 +141,7 @@ static inline const void* vector_as_c_array(const vector_t* vec);
|
||||
static inline void* vector_as_c_array_mutable(vector_t* vec);
|
||||
static inline int vector_move(vector_t* dest, vector_t** src);
|
||||
static inline vector_t* vector_deep_copy(const vector_t* vec);
|
||||
static inline int vector_destroy(vector_t* vec);
|
||||
static inline int vector_destroy(vector_t** vec);
|
||||
|
||||
/*
|
||||
@brief Checks whether a pointer points inside the vector's own data array. Used internally to make the write functions safe against self-referential input.
|
||||
@@ -820,7 +820,7 @@ static inline vector_t* vector_deep_copy(const vector_t* vec) {
|
||||
|
||||
int r = vector_reserve(new_vec, vec->capacity);
|
||||
if (r != 0) {
|
||||
vector_destroy(new_vec);
|
||||
vector_destroy(&new_vec);
|
||||
return NULL; // Allocation failed
|
||||
}
|
||||
|
||||
@@ -835,24 +835,32 @@ static inline vector_t* vector_deep_copy(const vector_t* vec) {
|
||||
/*
|
||||
@brief Destroys the vector and frees its memory.
|
||||
@param vec A pointer to the vector to be destroyed.
|
||||
@return 0 on success, -1 if the vector is NULL.
|
||||
@attention After calling this function, the vector pointer should not be used again. Accessing it after destruction will lead to undefined behavior.
|
||||
@return 0 on success or if the pointer is already NULL (no-op), -1 if the underlying vector is NULL.
|
||||
@attention After calling this function, the vector pointer should not be used again (It will be set to NULL). Accessing it after destruction will lead to undefined behavior.
|
||||
@attention If stored elements own memory of their own, set a destructor with vector_set_destructor() to have it cleaned up here. Otherwise, the vector will only free the memory allocated for the data array and the vector structure itself, but not any dynamically allocated memory within the elements.
|
||||
*/
|
||||
static inline int vector_destroy(vector_t* vec) {
|
||||
static inline int vector_destroy(vector_t** vec) {
|
||||
if (!vec) {
|
||||
return -1; // Invalid vector
|
||||
}
|
||||
|
||||
if (vec->destructor) {
|
||||
for (size_t i = 0; i < vec->size; ++i) {
|
||||
void* element = (char*)vec->data + (i * vec->element_size);
|
||||
vec->destructor(element); // Call the destructor for each element
|
||||
if (!(*vec)) {
|
||||
return 0; // Already NULL, nothing to destroy
|
||||
}
|
||||
|
||||
vector_t* target = *vec;
|
||||
|
||||
if (target->destructor) {
|
||||
for (size_t i = 0; i < target->size; ++i) {
|
||||
void* element = (char*)target->data + (i * target->element_size);
|
||||
target->destructor(element); // Call the destructor for each element
|
||||
}
|
||||
}
|
||||
|
||||
free(vec->data);
|
||||
free(vec);
|
||||
free(target->data);
|
||||
free(target);
|
||||
*vec = NULL; // Set the pointer to NULL to avoid dangling references
|
||||
|
||||
return 0; // Success
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user