daemon: use safer memory growth macros

* daemon/libvirtd.h (qemud_server): Change types of members
tracking array sizes, and add allocation trackers.
* daemon/event.c (virEventLoop): Likewise.
(virEventAddHandleImpl, virEventAddTimeoutImpl)
(virEventCleanupTimeouts, virEventCleanupHandles): Use
VIR_RESIZE_N instead of VIR_REALLOC_N.  Tweak debug messages to
match type changes.
* daemon/libvirtd.c (qemudDispatchServer, qemudRunLoop): Likewise.
This commit is contained in:
Eric Blake 2010-08-13 16:19:54 -06:00
parent 269d3b72f6
commit e6b68d7479
3 changed files with 28 additions and 35 deletions

View File

@ -73,11 +73,11 @@ struct virEventLoop {
int running; int running;
virThread leader; virThread leader;
int wakeupfd[2]; int wakeupfd[2];
int handlesCount; size_t handlesCount;
int handlesAlloc; size_t handlesAlloc;
struct virEventHandle *handles; struct virEventHandle *handles;
int timeoutsCount; size_t timeoutsCount;
int timeoutsAlloc; size_t timeoutsAlloc;
struct virEventTimeout *timeouts; struct virEventTimeout *timeouts;
}; };
@ -103,14 +103,13 @@ int virEventAddHandleImpl(int fd, int events,
EVENT_DEBUG("Add handle fd=%d events=%d cb=%p opaque=%p", fd, events, cb, opaque); EVENT_DEBUG("Add handle fd=%d events=%d cb=%p opaque=%p", fd, events, cb, opaque);
virMutexLock(&eventLoop.lock); virMutexLock(&eventLoop.lock);
if (eventLoop.handlesCount == eventLoop.handlesAlloc) { if (eventLoop.handlesCount == eventLoop.handlesAlloc) {
EVENT_DEBUG("Used %d handle slots, adding %d more", EVENT_DEBUG("Used %zu handle slots, adding at least %d more",
eventLoop.handlesAlloc, EVENT_ALLOC_EXTENT); eventLoop.handlesAlloc, EVENT_ALLOC_EXTENT);
if (VIR_REALLOC_N(eventLoop.handles, if (VIR_RESIZE_N(eventLoop.handles, eventLoop.handlesAlloc,
(eventLoop.handlesAlloc + EVENT_ALLOC_EXTENT)) < 0) { eventLoop.handlesCount, EVENT_ALLOC_EXTENT) < 0) {
virMutexUnlock(&eventLoop.lock); virMutexUnlock(&eventLoop.lock);
return -1; return -1;
} }
eventLoop.handlesAlloc += EVENT_ALLOC_EXTENT;
} }
watch = nextWatch++; watch = nextWatch++;
@ -204,14 +203,13 @@ int virEventAddTimeoutImpl(int frequency,
virMutexLock(&eventLoop.lock); virMutexLock(&eventLoop.lock);
if (eventLoop.timeoutsCount == eventLoop.timeoutsAlloc) { if (eventLoop.timeoutsCount == eventLoop.timeoutsAlloc) {
EVENT_DEBUG("Used %d timeout slots, adding %d more", EVENT_DEBUG("Used %zu timeout slots, adding at least %d more",
eventLoop.timeoutsAlloc, EVENT_ALLOC_EXTENT); eventLoop.timeoutsAlloc, EVENT_ALLOC_EXTENT);
if (VIR_REALLOC_N(eventLoop.timeouts, if (VIR_RESIZE_N(eventLoop.timeouts, eventLoop.timeoutsAlloc,
(eventLoop.timeoutsAlloc + EVENT_ALLOC_EXTENT)) < 0) { eventLoop.timeoutsCount, EVENT_ALLOC_EXTENT) < 0) {
virMutexUnlock(&eventLoop.lock); virMutexUnlock(&eventLoop.lock);
return -1; return -1;
} }
eventLoop.timeoutsAlloc += EVENT_ALLOC_EXTENT;
} }
eventLoop.timeouts[eventLoop.timeoutsCount].timer = nextTimer++; eventLoop.timeouts[eventLoop.timeoutsCount].timer = nextTimer++;
@ -301,7 +299,7 @@ int virEventRemoveTimeoutImpl(int timer) {
static int virEventCalculateTimeout(int *timeout) { static int virEventCalculateTimeout(int *timeout) {
unsigned long long then = 0; unsigned long long then = 0;
int i; int i;
EVENT_DEBUG("Calculate expiry of %d timers", eventLoop.timeoutsCount); EVENT_DEBUG("Calculate expiry of %zu timers", eventLoop.timeoutsCount);
/* Figure out if we need a timeout */ /* Figure out if we need a timeout */
for (i = 0 ; i < eventLoop.timeoutsCount ; i++) { for (i = 0 ; i < eventLoop.timeoutsCount ; i++) {
if (eventLoop.timeouts[i].frequency < 0) if (eventLoop.timeouts[i].frequency < 0)
@ -482,7 +480,7 @@ static int virEventDispatchHandles(int nfds, struct pollfd *fds) {
*/ */
static int virEventCleanupTimeouts(void) { static int virEventCleanupTimeouts(void) {
int i; int i;
DEBUG("Cleanup %d", eventLoop.timeoutsCount); DEBUG("Cleanup %zu", eventLoop.timeoutsCount);
/* Remove deleted entries, shuffling down remaining /* Remove deleted entries, shuffling down remaining
* entries as needed to form contiguous series * entries as needed to form contiguous series
@ -507,12 +505,10 @@ static int virEventCleanupTimeouts(void) {
/* Release some memory if we've got a big chunk free */ /* Release some memory if we've got a big chunk free */
if ((eventLoop.timeoutsAlloc - EVENT_ALLOC_EXTENT) > eventLoop.timeoutsCount) { if ((eventLoop.timeoutsAlloc - EVENT_ALLOC_EXTENT) > eventLoop.timeoutsCount) {
EVENT_DEBUG("Releasing %d out of %d timeout slots used, releasing %d", EVENT_DEBUG("Releasing %zu out of %zu timeout slots used, releasing %d",
eventLoop.timeoutsCount, eventLoop.timeoutsAlloc, EVENT_ALLOC_EXTENT); eventLoop.timeoutsCount, eventLoop.timeoutsAlloc, EVENT_ALLOC_EXTENT);
if (VIR_REALLOC_N(eventLoop.timeouts, VIR_SHRINK_N(eventLoop.timeouts, eventLoop.timeoutsAlloc,
(eventLoop.timeoutsAlloc - EVENT_ALLOC_EXTENT)) < 0) EVENT_ALLOC_EXTENT);
return -1;
eventLoop.timeoutsAlloc -= EVENT_ALLOC_EXTENT;
} }
return 0; return 0;
} }
@ -523,7 +519,7 @@ static int virEventCleanupTimeouts(void) {
*/ */
static int virEventCleanupHandles(void) { static int virEventCleanupHandles(void) {
int i; int i;
DEBUG("Cleanupo %d", eventLoop.handlesCount); DEBUG("Cleanup %zu", eventLoop.handlesCount);
/* Remove deleted entries, shuffling down remaining /* Remove deleted entries, shuffling down remaining
* entries as needed to form contiguous series * entries as needed to form contiguous series
@ -547,12 +543,10 @@ static int virEventCleanupHandles(void) {
/* Release some memory if we've got a big chunk free */ /* Release some memory if we've got a big chunk free */
if ((eventLoop.handlesAlloc - EVENT_ALLOC_EXTENT) > eventLoop.handlesCount) { if ((eventLoop.handlesAlloc - EVENT_ALLOC_EXTENT) > eventLoop.handlesCount) {
EVENT_DEBUG("Releasing %d out of %d handles slots used, releasing %d", EVENT_DEBUG("Releasing %zu out of %zu handles slots used, releasing %d",
eventLoop.handlesCount, eventLoop.handlesAlloc, EVENT_ALLOC_EXTENT); eventLoop.handlesCount, eventLoop.handlesAlloc, EVENT_ALLOC_EXTENT);
if (VIR_REALLOC_N(eventLoop.handles, VIR_SHRINK_N(eventLoop.handles, eventLoop.handlesAlloc,
(eventLoop.handlesAlloc - EVENT_ALLOC_EXTENT)) < 0) EVENT_ALLOC_EXTENT);
return -1;
eventLoop.handlesAlloc -= EVENT_ALLOC_EXTENT;
} }
return 0; return 0;
} }

View File

@ -1333,7 +1333,8 @@ static int qemudDispatchServer(struct qemud_server *server, struct qemud_socket
goto error; goto error;
} }
if (VIR_REALLOC_N(server->clients, server->nclients+1) < 0) { if (VIR_RESIZE_N(server->clients, server->nclients_max,
server->nclients, 1) < 0) {
VIR_ERROR0(_("Out of memory allocating clients")); VIR_ERROR0(_("Out of memory allocating clients"));
goto error; goto error;
} }
@ -2361,10 +2362,7 @@ static void *qemudRunLoop(void *opaque) {
server->clients + i + 1, server->clients + i + 1,
sizeof (*server->clients) * (server->nclients - i)); sizeof (*server->clients) * (server->nclients - i));
if (VIR_REALLOC_N(server->clients, VIR_SHRINK_N(server->clients, server->nclients, 0);
server->nclients) < 0) {
; /* ignore */
}
goto reprocess; goto reprocess;
} }
} }

View File

@ -1,7 +1,7 @@
/* /*
* libvirtd.h: daemon data structure definitions * libvirtd.h: daemon data structure definitions
* *
* Copyright (C) 2006-2009 Red Hat, Inc. * Copyright (C) 2006-2010 Red Hat, Inc.
* Copyright (C) 2006 Daniel P. Berrange * Copyright (C) 2006 Daniel P. Berrange
* *
* This library is free software; you can redistribute it and/or * This library is free software; you can redistribute it and/or
@ -283,12 +283,13 @@ struct qemud_server {
int privileged; int privileged;
int nworkers; size_t nworkers;
int nactiveworkers; size_t nactiveworkers;
struct qemud_worker *workers; struct qemud_worker *workers;
int nsockets; size_t nsockets;
struct qemud_socket *sockets; struct qemud_socket *sockets;
int nclients; size_t nclients;
size_t nclients_max;
struct qemud_client **clients; struct qemud_client **clients;
int sigread; int sigread;