[libvirt] [PATCH 03/10] daemon: use safer memory growth macros

Eric Blake eblake at redhat.com
Thu Nov 18 04:28:55 UTC 2010


* 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.
---
 daemon/event.c    |   44 +++++++++++++++++++-------------------------
 daemon/libvirtd.c |    8 +++-----
 daemon/libvirtd.h |   11 ++++++-----
 3 files changed, 28 insertions(+), 35 deletions(-)

diff --git a/daemon/event.c b/daemon/event.c
index 06f9aad..a983b35 100644
--- a/daemon/event.c
+++ b/daemon/event.c
@@ -73,11 +73,11 @@ struct virEventLoop {
     int running;
     virThread leader;
     int wakeupfd[2];
-    int handlesCount;
-    int handlesAlloc;
+    size_t handlesCount;
+    size_t handlesAlloc;
     struct virEventHandle *handles;
-    int timeoutsCount;
-    int timeoutsAlloc;
+    size_t timeoutsCount;
+    size_t timeoutsAlloc;
     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);
     virMutexLock(&eventLoop.lock);
     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);
-        if (VIR_REALLOC_N(eventLoop.handles,
-                          (eventLoop.handlesAlloc + EVENT_ALLOC_EXTENT)) < 0) {
+        if (VIR_RESIZE_N(eventLoop.handles, eventLoop.handlesAlloc,
+                         eventLoop.handlesCount, EVENT_ALLOC_EXTENT) < 0) {
             virMutexUnlock(&eventLoop.lock);
             return -1;
         }
-        eventLoop.handlesAlloc += EVENT_ALLOC_EXTENT;
     }

     watch = nextWatch++;
@@ -204,14 +203,13 @@ int virEventAddTimeoutImpl(int frequency,

     virMutexLock(&eventLoop.lock);
     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);
-        if (VIR_REALLOC_N(eventLoop.timeouts,
-                          (eventLoop.timeoutsAlloc + EVENT_ALLOC_EXTENT)) < 0) {
+        if (VIR_RESIZE_N(eventLoop.timeouts, eventLoop.timeoutsAlloc,
+                         eventLoop.timeoutsCount, EVENT_ALLOC_EXTENT) < 0) {
             virMutexUnlock(&eventLoop.lock);
             return -1;
         }
-        eventLoop.timeoutsAlloc += EVENT_ALLOC_EXTENT;
     }

     eventLoop.timeouts[eventLoop.timeoutsCount].timer = nextTimer++;
@@ -301,7 +299,7 @@ int virEventRemoveTimeoutImpl(int timer) {
 static int virEventCalculateTimeout(int *timeout) {
     unsigned long long then = 0;
     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 */
     for (i = 0 ; i < eventLoop.timeoutsCount ; i++) {
         if (eventLoop.timeouts[i].frequency < 0)
@@ -482,7 +480,7 @@ static int virEventDispatchHandles(int nfds, struct pollfd *fds) {
  */
 static int virEventCleanupTimeouts(void) {
     int i;
-    DEBUG("Cleanup %d", eventLoop.timeoutsCount);
+    DEBUG("Cleanup %zu", eventLoop.timeoutsCount);

     /* Remove deleted entries, shuffling down remaining
      * 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 */
     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);
-        if (VIR_REALLOC_N(eventLoop.timeouts,
-                          (eventLoop.timeoutsAlloc - EVENT_ALLOC_EXTENT)) < 0)
-            return -1;
-        eventLoop.timeoutsAlloc -= EVENT_ALLOC_EXTENT;
+        VIR_SHRINK_N(eventLoop.timeouts, eventLoop.timeoutsAlloc,
+                     EVENT_ALLOC_EXTENT);
     }
     return 0;
 }
@@ -523,7 +519,7 @@ static int virEventCleanupTimeouts(void) {
  */
 static int virEventCleanupHandles(void) {
     int i;
-    DEBUG("Cleanupo %d", eventLoop.handlesCount);
+    DEBUG("Cleanup %zu", eventLoop.handlesCount);

     /* Remove deleted entries, shuffling down remaining
      * 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 */
     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);
-        if (VIR_REALLOC_N(eventLoop.handles,
-                          (eventLoop.handlesAlloc - EVENT_ALLOC_EXTENT)) < 0)
-            return -1;
-        eventLoop.handlesAlloc -= EVENT_ALLOC_EXTENT;
+        VIR_SHRINK_N(eventLoop.handles, eventLoop.handlesAlloc,
+                     EVENT_ALLOC_EXTENT);
     }
     return 0;
 }
diff --git a/daemon/libvirtd.c b/daemon/libvirtd.c
index 1673e91..e544c48 100644
--- a/daemon/libvirtd.c
+++ b/daemon/libvirtd.c
@@ -1333,7 +1333,8 @@ static int qemudDispatchServer(struct qemud_server *server, struct qemud_socket
         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"));
         goto error;
     }
@@ -2361,10 +2362,7 @@ static void *qemudRunLoop(void *opaque) {
                             server->clients + i + 1,
                             sizeof (*server->clients) * (server->nclients - i));

-                if (VIR_REALLOC_N(server->clients,
-                                  server->nclients) < 0) {
-                    ; /* ignore */
-                }
+                VIR_SHRINK_N(server->clients, server->nclients, 0);
                 goto reprocess;
             }
         }
diff --git a/daemon/libvirtd.h b/daemon/libvirtd.h
index 785ac07..af20e56 100644
--- a/daemon/libvirtd.h
+++ b/daemon/libvirtd.h
@@ -1,7 +1,7 @@
 /*
  * 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
  *
  * This library is free software; you can redistribute it and/or
@@ -283,12 +283,13 @@ struct qemud_server {

     int privileged;

-    int nworkers;
-    int nactiveworkers;
+    size_t nworkers;
+    size_t nactiveworkers;
     struct qemud_worker *workers;
-    int nsockets;
+    size_t nsockets;
     struct qemud_socket *sockets;
-    int nclients;
+    size_t nclients;
+    size_t nclients_max;
     struct qemud_client **clients;

     int sigread;
-- 
1.7.3.2




More information about the libvir-list mailing list