event: use newer array management macros

We might as well take advantage of viralloc.h instead of open-coding
array management ourselves.  While at it, I simplified several
places that were doing repetitive pointer chasing to use an
intermediate variable for legibility (some other places remain,
but they will disapper in later refactoring patches).

* src/conf/object_event_private.h (_virObjectEventCallbackList):
Use size_t for count.
* src/conf/object_event.c (_virObjectEventQueue): Likewise.
(virObjectEventCallbackListRemoveID): Use VIR_DELETE_ELEMENT.
(virObjectEventQueuePush, virObjectEventCallbackListAddID): Use
VIR_APPEND_ELEMENT.
(virObjectEventCallbackListEventID)
(virObjectEventStateDispatchCallbacks): Simplify code.

Signed-off-by: Eric Blake <eblake@redhat.com>
This commit is contained in:
Eric Blake 2014-01-03 13:31:48 -07:00
parent 22e82aa596
commit 94a26c7e88
2 changed files with 44 additions and 66 deletions

View File

@ -37,7 +37,7 @@
#define VIR_FROM_THIS VIR_FROM_NONE #define VIR_FROM_THIS VIR_FROM_NONE
struct _virObjectEventQueue { struct _virObjectEventQueue {
unsigned int count; size_t count;
virObjectEventPtr *events; virObjectEventPtr *events;
}; };
@ -97,7 +97,7 @@ virObjectEventCallbackListFree(virObjectEventCallbackListPtr list)
if (!list) if (!list)
return; return;
for (i=0; i<list->count; i++) { for (i = 0; i < list->count; i++) {
virFreeCallback freecb = list->callbacks[i]->freecb; virFreeCallback freecb = list->callbacks[i]->freecb;
if (freecb) if (freecb)
(*freecb)(list->callbacks[i]->opaque); (*freecb)(list->callbacks[i]->opaque);
@ -123,29 +123,19 @@ virObjectEventCallbackListRemoveID(virConnectPtr conn,
{ {
int ret = 0; int ret = 0;
size_t i; size_t i;
for (i = 0; i < cbList->count; i++) { for (i = 0; i < cbList->count; i++) {
if (cbList->callbacks[i]->callbackID == callbackID && virObjectEventCallbackPtr cb = cbList->callbacks[i];
cbList->callbacks[i]->conn == conn) {
virFreeCallback freecb = cbList->callbacks[i]->freecb;
if (freecb)
(*freecb)(cbList->callbacks[i]->opaque);
virObjectUnref(cbList->callbacks[i]->conn);
if (cbList->callbacks[i]->meta)
VIR_FREE(cbList->callbacks[i]->meta->name);
VIR_FREE(cbList->callbacks[i]->meta);
VIR_FREE(cbList->callbacks[i]);
if (i < (cbList->count - 1)) if (cb->callbackID == callbackID && cb->conn == conn) {
memmove(cbList->callbacks + i, if (cb->freecb)
cbList->callbacks + i + 1, (*cb->freecb)(cb->opaque);
sizeof(*(cbList->callbacks)) * virObjectUnref(cb->conn);
(cbList->count - (i + 1))); if (cb->meta)
VIR_FREE(cb->meta->name);
if (VIR_REALLOC_N(cbList->callbacks, VIR_FREE(cb->meta);
cbList->count - 1) < 0) { VIR_FREE(cb);
; /* Failure to reduce memory allocation isn't fatal */ VIR_DELETE_ELEMENT(cbList->callbacks, i, cbList->count);
}
cbList->count--;
for (i = 0; i < cbList->count; i++) { for (i = 0; i < cbList->count; i++) {
if (!cbList->callbacks[i]->deleted) if (!cbList->callbacks[i]->deleted)
@ -168,6 +158,7 @@ virObjectEventCallbackListMarkDeleteID(virConnectPtr conn,
{ {
int ret = 0; int ret = 0;
size_t i; size_t i;
for (i = 0; i < cbList->count; i++) { for (i = 0; i < cbList->count; i++) {
if (cbList->callbacks[i]->callbackID == callbackID && if (cbList->callbacks[i]->callbackID == callbackID &&
cbList->callbacks[i]->conn == conn) { cbList->callbacks[i]->conn == conn) {
@ -247,7 +238,7 @@ virObjectEventCallbackListAddID(virConnectPtr conn,
{ {
virObjectEventCallbackPtr event; virObjectEventCallbackPtr event;
size_t i; size_t i;
int ret = 0; int ret = -1;
VIR_DEBUG("conn=%p cblist=%p uuid=%p name=%s id=%d " VIR_DEBUG("conn=%p cblist=%p uuid=%p name=%s id=%d "
"klass=%p eventID=%d callback=%p opaque=%p", "klass=%p eventID=%d callback=%p opaque=%p",
@ -276,8 +267,9 @@ virObjectEventCallbackListAddID(virConnectPtr conn,
} }
/* Allocate new event */ /* Allocate new event */
if (VIR_ALLOC(event) < 0) if (VIR_ALLOC(event) < 0)
goto error; goto cleanup;
event->conn = conn; event->conn = virObjectRef(conn);
event->callbackID = cbList->nextID++;
event->cb = callback; event->cb = callback;
event->klass = klass; event->klass = klass;
event->eventID = eventID; event->eventID = eventID;
@ -286,25 +278,20 @@ virObjectEventCallbackListAddID(virConnectPtr conn,
if (name && uuid && id > 0) { if (name && uuid && id > 0) {
if (VIR_ALLOC(event->meta) < 0) if (VIR_ALLOC(event->meta) < 0)
goto error; goto cleanup;
if (VIR_STRDUP(event->meta->name, name) < 0) if (VIR_STRDUP(event->meta->name, name) < 0)
goto error; goto cleanup;
memcpy(event->meta->uuid, uuid, VIR_UUID_BUFLEN); memcpy(event->meta->uuid, uuid, VIR_UUID_BUFLEN);
event->meta->id = id; event->meta->id = id;
} }
/* Make space on list */ if (callbackID)
if (VIR_REALLOC_N(cbList->callbacks, cbList->count + 1) < 0) *callbackID = event->callbackID;
goto error;
virObjectRef(event->conn); if (VIR_APPEND_ELEMENT(cbList->callbacks, cbList->count, event) < 0)
goto cleanup;
cbList->callbacks[cbList->count] = event; for (ret = 0, i = 0; i < cbList->count; i++) {
cbList->count++;
event->callbackID = cbList->nextID++;
for (i = 0; i < cbList->count; i++) {
if (cbList->callbacks[i]->klass == klass && if (cbList->callbacks[i]->klass == klass &&
cbList->callbacks[i]->eventID == eventID && cbList->callbacks[i]->eventID == eventID &&
cbList->callbacks[i]->conn == conn && cbList->callbacks[i]->conn == conn &&
@ -312,19 +299,15 @@ virObjectEventCallbackListAddID(virConnectPtr conn,
ret++; ret++;
} }
if (callbackID) cleanup:
*callbackID = event->callbackID;
return ret;
error:
if (event) { if (event) {
virObjectUnref(event->conn);
if (event->meta) if (event->meta)
VIR_FREE(event->meta->name); VIR_FREE(event->meta->name);
VIR_FREE(event->meta); VIR_FREE(event->meta);
} }
VIR_FREE(event); VIR_FREE(event);
return -1; return ret;
} }
@ -336,12 +319,13 @@ virObjectEventCallbackListEventID(virConnectPtr conn,
size_t i; size_t i;
for (i = 0; i < cbList->count; i++) { for (i = 0; i < cbList->count; i++) {
if (cbList->callbacks[i]->deleted) virObjectEventCallbackPtr cb = cbList->callbacks[i];
if (cb->deleted)
continue; continue;
if (cbList->callbacks[i]->callbackID == callbackID && if (cb->callbackID == callbackID && cb->conn == conn)
cbList->callbacks[i]->conn == conn) return cb->eventID;
return cbList->callbacks[i]->eventID;
} }
return -1; return -1;
@ -562,17 +546,11 @@ static int
virObjectEventQueuePush(virObjectEventQueuePtr evtQueue, virObjectEventQueuePush(virObjectEventQueuePtr evtQueue,
virObjectEventPtr event) virObjectEventPtr event)
{ {
if (!evtQueue) { if (!evtQueue)
return -1;
}
/* Make space on queue */
if (VIR_REALLOC_N(evtQueue->events,
evtQueue->count + 1) < 0)
return -1; return -1;
evtQueue->events[evtQueue->count] = event; if (VIR_APPEND_ELEMENT(evtQueue->events, evtQueue->count, event) < 0)
evtQueue->count++; return -1;
return 0; return 0;
} }
@ -612,18 +590,17 @@ virObjectEventStateDispatchCallbacks(virObjectEventStatePtr state,
/* Cache this now, since we may be dropping the lock, /* Cache this now, since we may be dropping the lock,
and have more callbacks added. We're guaranteed not and have more callbacks added. We're guaranteed not
to have any removed */ to have any removed */
int cbCount = callbacks->count; size_t cbCount = callbacks->count;
for (i = 0; i < cbCount; i++) { for (i = 0; i < cbCount; i++) {
if (!virObjectEventDispatchMatchCallback(event, callbacks->callbacks[i])) virObjectEventCallbackPtr cb = callbacks->callbacks[i];
if (!virObjectEventDispatchMatchCallback(event, cb))
continue; continue;
/* Drop the lock whle dispatching, for sake of re-entrancy */ /* Drop the lock whle dispatching, for sake of re-entrancy */
virObjectEventStateUnlock(state); virObjectEventStateUnlock(state);
event->dispatch(callbacks->callbacks[i]->conn, event->dispatch(cb->conn, event, cb->cb, cb->opaque);
event,
callbacks->callbacks[i]->cb,
callbacks->callbacks[i]->opaque);
virObjectEventStateLock(state); virObjectEventStateLock(state);
} }
} }
@ -637,7 +614,8 @@ virObjectEventStateQueueDispatch(virObjectEventStatePtr state,
size_t i; size_t i;
for (i = 0; i < queue->count; i++) { for (i = 0; i < queue->count; i++) {
virObjectEventStateDispatchCallbacks(state, queue->events[i], callbacks); virObjectEventStateDispatchCallbacks(state, queue->events[i],
callbacks);
virObjectUnref(queue->events[i]); virObjectUnref(queue->events[i]);
} }
VIR_FREE(queue->events); VIR_FREE(queue->events);

View File

@ -37,7 +37,7 @@ typedef virObjectMeta *virObjectMetaPtr;
struct _virObjectEventCallbackList { struct _virObjectEventCallbackList {
unsigned int nextID; unsigned int nextID;
unsigned int count; size_t count;
virObjectEventCallbackPtr *callbacks; virObjectEventCallbackPtr *callbacks;
}; };
typedef struct _virObjectEventCallbackList virObjectEventCallbackList; typedef struct _virObjectEventCallbackList virObjectEventCallbackList;