--- libelwix/src/array.c 2019/01/22 16:16:45 1.7.2.1 +++ libelwix/src/array.c 2024/10/28 09:58:51 1.10 @@ -3,7 +3,7 @@ * by Michael Pounov * * $Author: misho $ -* $Id: array.c,v 1.7.2.1 2019/01/22 16:16:45 misho Exp $ +* $Id: array.c,v 1.10 2024/10/28 09:58:51 misho Exp $ * ************************************************************************** The ELWIX and AITNET software is distributed under the following @@ -12,7 +12,7 @@ terms: All of the documentation and software included in the ELWIX and AITNET Releases is copyrighted by ELWIX - Sofia/Bulgaria -Copyright 2004 - 2019 +Copyright 2004 - 2024 by Michael Pounov . All rights reserved. Redistribution and use in source and binary forms, with or without @@ -68,7 +68,7 @@ array_Init(int numItems) e_free(arr); return NULL; } else - array_Zero(arr); + memset(arr->arr_data, 0, array_Size(arr) * sizeof(intptr_t)); return arr; } @@ -96,7 +96,7 @@ array_Init2(array_t * __restrict arr, int numItems) e_free(arr); return NULL; } else - array_Zero(arr); + memset(arr->arr_data, 0, array_Size(arr) * sizeof(intptr_t)); return arr; } @@ -175,12 +175,10 @@ array_Free(array_t * __restrict arr) return; for (i = 0; i < array_Size(arr); i++) - if (arr->arr_data[i]) { + if (arr->arr_data[i]) e_free(arr->arr_data[i]); - arr->arr_data[i] = NULL; - } - arr->arr_last = -1; + array_Zero(arr); } /* @@ -202,10 +200,26 @@ array_Destroy(array_t ** __restrict parr) } /* + * array_Destroy2() - Free data in dynamic array + * + * @parr = Array + * return: none + */ +void +array_Destroy2(array_t * __restrict arr) +{ + if (!arr) + return; + + if (arr->arr_data) + e_free(arr->arr_data); + memset(arr, 0, sizeof(array_t)); +} +/* * array_Reset() - Reset array to initial state * * @parr = Array - * @purge = Purge data, if <>0 then will be free entire data memory + * @purge = Purge all data, if <>0 then will be free entire data memory * return: none */ void @@ -220,8 +234,8 @@ array_Reset(array_t * __restrict arr, int purge) arr->arr_data = e_calloc(array_Size(arr), sizeof(intptr_t)); } - array_Zero(arr); - arr->arr_last = -1; + if (arr->arr_data) + array_Zero(arr); } /* @@ -362,6 +376,8 @@ array_Concat(array_t * __restrict dest, array_t * __re n = array_Size(dest); if (array_Grow(dest, n + array_Size(src), 0)) return -1; + if (!dest->arr_data) + return -1; memcpy(dest->arr_data + n, src->arr_data, array_Size(src) * sizeof(intptr_t)); dest->arr_last = array_Len(dest); @@ -409,6 +425,8 @@ array_Elem(array_t * __restrict arr, int n, void *data if (n >= array_Size(arr) && array_Grow(arr, n + 1, 0)) return (void*) -1; + if (!arr->arr_data) + return (void*) -1; dat = array_Get(arr, n); if (data) @@ -437,6 +455,8 @@ array_Push(array_t * __restrict arr, void *data, int n if (nogrow && ret >= array_Size(arr)) return -1; if (!nogrow && ret >= array_Size(arr) && array_Grow(arr, ret + 1, 0)) + return -1; + if (!arr->arr_data) return -1; ret = ++arr->arr_last;