[PATCH 2/4] daemon: use safer memory growth macros

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



* daemon/libvirtd.h (qemud_server): Change types of members
tracking array sizes.
* daemon/event.c (virEventLoop): Likewise.
(virEventAddHandleImpl, virEventAddTimeoutImpl)
(virEventCleanupTimeouts, virEventCleanupHandles): Use
VIR_EXPAND_N and VIR_SHRINK_N instead of VIR_REALLOC_N.  Tweak
debug messages to match type changes.
* daemon/libvirtd.c (qemudDispatchServer, qemudRunLoop): Use
VIR_EXPAND_N and VIR_SHRINK_N.

Signed-off-by: Eric Blake <eblake@xxxxxxxxxx>
---
 daemon/event.c    |   44 +++++++++++++++++++-------------------------
 daemon/libvirtd.c |   10 ++++------
 daemon/libvirtd.h |   10 +++++-----
 3 files changed, 28 insertions(+), 36 deletions(-)

diff --git a/daemon/event.c b/daemon/event.c
index 6971409..77498e8 100644
--- a/daemon/event.c
+++ b/daemon/event.c
@@ -73,11 +73,11 @@ struct virEventLoop {
     int running;
     pthread_t 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;
 };

@@ -113,14 +113,13 @@ int virEventAddHandleImpl(int fd, int events,
     EVENT_DEBUG("Add handle fd=%d events=%d cb=%p opaque=%p", fd, events, cb, opaque);
     virEventLock();
     if (eventLoop.handlesCount == eventLoop.handlesAlloc) {
-        EVENT_DEBUG("Used %d handle slots, adding %d more",
+        EVENT_DEBUG("Used %zu handle slots, adding %d more",
                     eventLoop.handlesAlloc, EVENT_ALLOC_EXTENT);
-        if (VIR_REALLOC_N(eventLoop.handles,
-                          (eventLoop.handlesAlloc + EVENT_ALLOC_EXTENT)) < 0) {
+        if (VIR_EXPAND_N(eventLoop.handles, eventLoop.handlesAlloc,
+                         EVENT_ALLOC_EXTENT) < 0) {
             virEventUnlock();
             return -1;
         }
-        eventLoop.handlesAlloc += EVENT_ALLOC_EXTENT;
     }

     watch = nextWatch++;
@@ -214,14 +213,13 @@ int virEventAddTimeoutImpl(int frequency,

     virEventLock();
     if (eventLoop.timeoutsCount == eventLoop.timeoutsAlloc) {
-        EVENT_DEBUG("Used %d timeout slots, adding %d more",
+        EVENT_DEBUG("Used %zu timeout slots, adding %d more",
                     eventLoop.timeoutsAlloc, EVENT_ALLOC_EXTENT);
-        if (VIR_REALLOC_N(eventLoop.timeouts,
-                          (eventLoop.timeoutsAlloc + EVENT_ALLOC_EXTENT)) < 0) {
+        if (VIR_EXPAND_N(eventLoop.timeouts, eventLoop.timeoutsAlloc,
+                         EVENT_ALLOC_EXTENT) < 0) {
             virEventUnlock();
             return -1;
         }
-        eventLoop.timeoutsAlloc += EVENT_ALLOC_EXTENT;
     }

     eventLoop.timeouts[eventLoop.timeoutsCount].timer = nextTimer++;
@@ -311,7 +309,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)
@@ -492,7 +490,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
@@ -517,12 +515,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;
 }
@@ -533,7 +529,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
@@ -557,12 +553,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 711360b..f0681a7 100644
--- a/daemon/libvirtd.c
+++ b/daemon/libvirtd.c
@@ -1320,7 +1320,7 @@ static int qemudDispatchServer(struct qemud_server *server, struct qemud_socket
         return -1;
     }

-    if (VIR_REALLOC_N(server->clients, server->nclients+1) < 0) {
+    if (VIR_EXPAND_N(server->clients, server->nclients, 1) < 0) {
         VIR_ERROR0(_("Out of memory allocating clients"));
         close(fd);
         return -1;
@@ -1444,7 +1444,7 @@ static int qemudDispatchServer(struct qemud_server *server, struct qemud_socket
         }
     }

-    server->clients[server->nclients++] = client;
+    server->clients[server->nclients - 1] = client;

     if (server->nclients > server->nactiveworkers &&
         server->nactiveworkers < server->nworkers) {
@@ -1468,6 +1468,7 @@ static int qemudDispatchServer(struct qemud_server *server, struct qemud_socket
     if (client)
         VIR_FREE(client->rx);
     VIR_FREE(client);
+    VIR_SHRINK_N(server->clients, server->nclients, 1);
     return -1;
 }

@@ -2343,10 +2344,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 3f13fb1..8d7720d 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
@@ -261,12 +261,12 @@ 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;
     struct qemud_client **clients;

     int sigread;
-- 
1.7.2.1

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list


[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]