improve zarray_iter
This commit is contained in:
parent
05abed1964
commit
e85ce0e5ad
12 changed files with 72 additions and 75 deletions
|
|
@ -44,6 +44,9 @@ static void* kvmgr_lock_(kvmgr_t* mgr, void* key, int op)
|
|||
}
|
||||
|
||||
ptr = (void*)zarray_get(&(mgr->data), idx);
|
||||
if (ptr == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return obj_ref(ptr, op);
|
||||
}
|
||||
|
|
@ -57,14 +60,10 @@ void* kvmgr_lock(kvmgr_t* mgr, void* key, int op)
|
|||
return NULL;
|
||||
}
|
||||
|
||||
/* clang-format off */
|
||||
if ((op != OBJREF_NOP) &&
|
||||
(op != OBJREF_READ) &&
|
||||
(op != OBJREF_UPDATE)) {
|
||||
SET_ERROR(EINVAL);
|
||||
return NULL;
|
||||
if ((op != OBJREF_NOP) && (op != OBJREF_READ) && (op != OBJREF_UPDATE)) {
|
||||
SET_ERROR(EINVAL);
|
||||
return NULL;
|
||||
}
|
||||
/* clang-format on */
|
||||
|
||||
mylock_lock(&(mgr->lock));
|
||||
ptr = kvmgr_lock_(mgr, key, op);
|
||||
|
|
@ -73,6 +72,36 @@ void* kvmgr_lock(kvmgr_t* mgr, void* key, int op)
|
|||
return ptr;
|
||||
}
|
||||
|
||||
void* kvmgr_lock_byid(kvmgr_t* mgr, int idx, int op)
|
||||
{
|
||||
void* ptr = NULL;
|
||||
|
||||
if (mgr == NULL) {
|
||||
SET_ERROR(-EINVAL);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if ((idx < 0) || (idx >= mgr->data.caps)) {
|
||||
SET_ERROR(-EINVAL);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
if ((op != OBJREF_NOP) && (op != OBJREF_READ) && (op != OBJREF_UPDATE)) {
|
||||
SET_ERROR(-EINVAL);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
mylock_lock(&(mgr->lock));
|
||||
ptr = (void*)zarray_get(&(mgr->data), idx);
|
||||
mylock_unlock(&(mgr->lock));
|
||||
|
||||
if (ptr == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
return obj_ref(ptr, op);
|
||||
}
|
||||
|
||||
static int32_t kvmgr_add_(kvmgr_t* mgr, void* key, void* p)
|
||||
{
|
||||
hent_t* e = NULL;
|
||||
|
|
@ -143,48 +172,13 @@ void kvmgr_del(kvmgr_t* mgr, void* key)
|
|||
mylock_unlock(&(mgr->lock));
|
||||
}
|
||||
|
||||
static int add_config_(xlist_t* l, void* data)
|
||||
{
|
||||
xce_config_t* config;
|
||||
|
||||
if (data == NULL) {
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
config = (xce_config_t*)obj_ref(data, OBJREF_READ);
|
||||
return xlist_store(l, config);
|
||||
}
|
||||
|
||||
int kvmgr_publish(kvmgr_t* mgr, xlist_action_fn_t fn, void* arg)
|
||||
{
|
||||
xlist_t l;
|
||||
int rc = 0;
|
||||
int i;
|
||||
|
||||
xlist_init(&l, 128);
|
||||
|
||||
mylock_lock(&(mgr->lock));
|
||||
zarray_iter(&(mgr->data), (zarray_iter_fn_t)add_config_, &l);
|
||||
mylock_unlock(&(mgr->lock));
|
||||
|
||||
rc = (*fn)(&l, arg);
|
||||
|
||||
for (i = 0; i < l.count; i++) {
|
||||
xce_config_t* config = l.data[i];
|
||||
|
||||
DEREF_OBJ(config);
|
||||
}
|
||||
|
||||
xlist_fini(&l);
|
||||
return rc;
|
||||
}
|
||||
|
||||
int kvmgr_iter(kvmgr_t* mgr, xlist_t* l, void* arg)
|
||||
int kvmgr_iter(kvmgr_t* mgr, zarray_iter_skip_fn_t skipfn, xlist_t* l,
|
||||
void* arg)
|
||||
{
|
||||
int rc = 0;
|
||||
|
||||
mylock_lock(&(mgr->lock));
|
||||
rc = zarray_iter_ex(&(mgr->data), l, arg);
|
||||
rc = zarray_iter_ex(&(mgr->data), skipfn, l, arg);
|
||||
mylock_unlock(&(mgr->lock));
|
||||
|
||||
return rc;
|
||||
|
|
|
|||
|
|
@ -136,7 +136,8 @@ void zarray_fini(zarray_t* array)
|
|||
}
|
||||
}
|
||||
|
||||
int zarray_iter_ex(zarray_t* array, xlist_t* l, void* arg)
|
||||
int zarray_iter_ex(zarray_t* array, zarray_iter_skip_fn_t skipfn, xlist_t* l,
|
||||
void* arg)
|
||||
{
|
||||
int i, j;
|
||||
|
||||
|
|
@ -154,6 +155,14 @@ int zarray_iter_ex(zarray_t* array, xlist_t* l, void* arg)
|
|||
}
|
||||
|
||||
for (i = 0; i < array->caps; i++) {
|
||||
if (skipfn != NULL) {
|
||||
if (skipfn(array->data[i])) {
|
||||
continue;
|
||||
}
|
||||
} else if (array->data[i] == NULL) {
|
||||
continue;
|
||||
}
|
||||
|
||||
(*f)(arg, array->data[i]);
|
||||
}
|
||||
}
|
||||
|
|
@ -174,7 +183,7 @@ int zarray_iter(zarray_t* array, zarray_iter_fn_t f, void* arg)
|
|||
xlist_init(&l, 2);
|
||||
xlist_store(&l, f);
|
||||
|
||||
rc = zarray_iter_ex(array, &l, arg);
|
||||
rc = zarray_iter_ex(array, NULL, &l, arg);
|
||||
|
||||
xlist_fini(&l);
|
||||
return rc;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue