enhance zarray library function

This commit is contained in:
Rongsong Shen 2025-12-26 16:23:04 +08:00
parent ae658c4a23
commit a27c8bfe6e
3 changed files with 100 additions and 29 deletions

View file

@ -221,53 +221,101 @@ int zarray_del(zarray_t* array, int idx)
return 0;
}
int zarray_put(zarray_t* array, int idx, void* data)
int zarray_put_ex(zarray_t* array, int count, int* idxs, void** data)
{
int i, maxid;
int rc = 0;
/* clang-format off */
if ((array == NULL) ||
(idx < 0) ||
(data == NULL)) {
return -EINVAL;
if ((array == NULL) || (count <= 0) || (idxs == NULL) || (data == NULL)) {
return -EINVAL;
}
maxid = 0;
for (i = 0; i < count; i++) {
if (idxs[i] < 0) {
return -EINVAL;
}
if (idxs[i] > maxid) {
maxid = idxs[i];
}
}
/* clang-format on */
mylock_lock(&(array->lock));
rc = zarray_ensure_items(array, idx + 1);
rc = zarray_ensure_items(array, maxid + 1);
if (rc >= 0) {
void* ptr = array->data[idx];
array->data[idx] = data;
rc = 0;
if (ptr != NULL) {
if (array->killer != NULL) {
array->killer(ptr);
for (i = 0; i < count; i++) {
int idx = idxs[i];
void* ptr = array->data[idx];
array->data[idx] = data[i];
if (ptr != NULL) {
if (array->killer != NULL) {
array->killer(ptr);
}
} else {
array->count++;
}
} else {
array->count++;
}
}
mylock_unlock(&(array->lock));
return rc;
return 0;
}
int zarray_put(zarray_t* array, int idx, void* data)
{
int idxs[] = { idx };
void* datas[] = { data };
return zarray_put_ex(array, 1, &idxs[0], &datas[0]);
}
int zarray_get_ex(zarray_t* array, int count, int* idxs, void** data)
{
int i, maxid;
if ((array == NULL) || (count <= 0) || (idxs == NULL) || (data == NULL)) {
return -EINVAL;
}
maxid = 0;
for (i = 0; i < count; i++) {
if (idxs[i] < 0) {
return -EINVAL;
}
if (idxs[i] > maxid) {
maxid = idxs[i];
}
}
if (maxid >= array->caps) {
return -EINVAL;
}
mylock_lock(&(array->lock));
for (i = 0; i < count; i++) {
int idx = idxs[i];
data[i] = array->data[idx];
}
mylock_unlock(&(array->lock));
return 0;
}
void* zarray_get(zarray_t* array, int idx)
{
void* ptr = NULL;
int idxs[] = { idx };
void* datas[] = { NULL };
int rc = 0;
/* clang-format off */
if ((array == NULL) ||
(idx < 0) ||
(idx >= array->caps)) {
SET_ERROR(EINVAL);
rc = zarray_get_ex(array, 1, &idxs[0], &datas[0]);
if (rc < 0) {
SET_ERROR(-rc);
return NULL;
}
/* clang-format on */
mylock_lock(&(array->lock));
ptr = array->data[idx];
mylock_unlock(&(array->lock));
return ptr;
return datas[0];
}