]> git.ipfire.org Git - thirdparty/dhcpcd.git/commitdiff
Introduce xzalloc, which zeros memory as well as allocates it. This makes us smaller.
authorRoy Marples <roy@marples.name>
Mon, 28 Jan 2008 15:32:04 +0000 (15:32 +0000)
committerRoy Marples <roy@marples.name>
Mon, 28 Jan 2008 15:32:04 +0000 (15:32 +0000)
arp.c
client.c
common.c
common.h
configure.c
dhcp.c
info.c
interface.c

diff --git a/arp.c b/arp.c
index e427fa992b3a7851ac3c164d1c1005c11dc94803..01ce1e786689ad615a93d3e4ea118e1a2d83c1b3 100644 (file)
--- a/arp.c
+++ b/arp.c
@@ -81,9 +81,7 @@ static int send_arp (const interface_t *iface, int op, struct in_addr sip,
        caddr_t tha;
        int retval;
 
-       arp = xmalloc (arpsize);
-       memset (arp, 0, arpsize);
-
+       arp = xzalloc (arpsize);
        arp->ar_hrd = htons (iface->family);
        arp->ar_pro = htons (ETHERTYPE_IP);
        arp->ar_hln = iface->hwlen;
index 9b298ef5e6a9246f6cbf10e074df1fca34657894..8fd1c8138f2daf5ebf5c9774aca0e8f9a61e1d32 100644 (file)
--- a/client.c
+++ b/client.c
@@ -984,12 +984,8 @@ int dhcp_run (const options_t *options, int *pidfd)
        if (! iface)
                goto eexit;
 
-       state = xmalloc (sizeof (state_t));
-       memset (state, 0, sizeof (state_t));
-       
-       state->dhcp = xmalloc (sizeof (dhcp_t));
-       memset (state->dhcp, 0, sizeof (dhcp_t));
-
+       state = xzalloc (sizeof (state_t));
+       state->dhcp = xzalloc (sizeof (dhcp_t));
        state->pidfd = pidfd;
        state->interface = iface;
 
index 14e88541956835d53e4c1c713b4a29158f18291f..161b891efc640cea3b11fa5de34974d600b8b30b 100644 (file)
--- a/common.c
+++ b/common.c
@@ -204,6 +204,13 @@ void *xmalloc (size_t s)
        /* NOTREACHED */
 }
 
+void *xzalloc (size_t s)
+{
+       void *value = xmalloc (s);
+       memset (value, 0, s);
+       return (value);
+}
+
 void *xrealloc (void *ptr, size_t s)
 {
        void *value = realloc (ptr, s);
index 61732bb62ac912f1154244dca7086740ffabe2c4..57e4e79ef1dac92a9fce8ef62108568a395911de 100644 (file)
--- a/common.h
+++ b/common.h
@@ -59,6 +59,7 @@ time_t uptime (void);
 void writepid (int fd, pid_t pid);
 void *xrealloc (void *ptr, size_t size);
 void *xmalloc (size_t size);
+void *xzalloc (size_t size);
 char *xstrdup (const char *str);
 
 #endif
index 8a65530214205109cb017ddf1994aff8ef5c2aae..6e5b8ef8c8593339942a83baebfb4b2fa0de42a9 100644 (file)
@@ -641,7 +641,6 @@ int configure (const options_t *options, interface_t *iface,
                if (remember >= 0) {
                        if (! new_routes) {
                                new_routes = xmalloc (sizeof (route_t));
-                               memset (new_routes, 0, sizeof (route_t));
                                new_route = new_routes;
                        } else {
                                new_route->next = xmalloc (sizeof (route_t));
@@ -695,16 +694,15 @@ int configure (const options_t *options, interface_t *iface,
                if (remember >= 0) {
                        if (! new_routes) {
                                new_routes = xmalloc (sizeof (route_t));
-                               memset (new_routes, 0, sizeof (route_t));
                                new_route = new_routes;
                        } else {
                                new_route->next = xmalloc (sizeof (route_t));
                                new_route = new_route->next;
-                               new_route->next = NULL;
                        }
                        new_route->destination.s_addr = dest.s_addr;
                        new_route->netmask.s_addr = mask.s_addr;
                        new_route->gateway.s_addr = gate.s_addr;
+                       new_route->next = NULL;
                }
        }
 #endif
diff --git a/dhcp.c b/dhcp.c
index 1137013808a7a94e22ea59de92dd7618df055107..c5630470004a0333d24864a2c6f43d9800e8295f 100644 (file)
--- a/dhcp.c
+++ b/dhcp.c
@@ -105,8 +105,7 @@ ssize_t send_message (const interface_t *iface, const dhcp_t *dhcp,
        if (type == DHCP_RELEASE)
                to.s_addr = dhcp->serveraddress.s_addr;
 
-       message = xmalloc (sizeof (dhcpmessage_t));
-       memset (message, 0, sizeof (dhcpmessage_t));
+       message = xzalloc (sizeof (dhcpmessage_t));
        m = (unsigned char *) message;
        p = (unsigned char *) &message->options;
 
@@ -378,8 +377,7 @@ ssize_t send_message (const interface_t *iface, const dhcp_t *dhcp,
 
        message_length = p - m;
 
-       packet = xmalloc (sizeof (struct udp_dhcp_packet));
-       memset (packet, 0, sizeof (struct udp_dhcp_packet));
+       packet = xzalloc (sizeof (struct udp_dhcp_packet));
        make_dhcp_packet (packet, (unsigned char *) message, message_length,
                          from, to);
        free (message);
@@ -465,7 +463,7 @@ static route_t *decode_CSR(const unsigned char *p, int len)
        if (len < 5)
                return NULL;
 
-       first = xmalloc (sizeof (route_t));
+       first = xzalloc (sizeof (route_t));
        route = first;
 
        while (q - p < len) {
@@ -500,7 +498,7 @@ static route_t *decode_CSR(const unsigned char *p, int len)
 
                /* We have another route */
                if (q - p < len) {
-                       route->next = xmalloc (sizeof (route_t));
+                       route->next = xzalloc (sizeof (route_t));
                        route = route->next;
                }
        }
@@ -537,13 +535,12 @@ static bool dhcp_add_address (address_t **address,
 
        for (i = 0; i < length; i += 4) {
                if (*address == NULL) {
-                       *address = xmalloc (sizeof (address_t));
+                       *address = xzalloc (sizeof (address_t));
                        p = *address;
                } else {
-                       p->next = xmalloc (sizeof (address_t));
+                       p->next = xzalloc (sizeof (address_t));
                        p = p->next;
                }
-               memset (p, 0, sizeof (address_t));
 
                /* Sanity check */
                if (i + 4 > length) {
@@ -640,11 +637,10 @@ static route_t *decode_routes (const unsigned char *data, int length)
        
        for (i = 0; i < length; i += 8) {
                if (routes) {
-                       routes->next = xmalloc (sizeof (route_t));
+                       routes->next = xzalloc (sizeof (route_t));
                        routes = routes->next;
                } else
-                       head = routes = xmalloc (sizeof (route_t));
-               memset (routes, 0, sizeof (route_t));
+                       head = routes = xzalloc (sizeof (route_t));
                memcpy (&routes->destination.s_addr, data + i, 4);
                memcpy (&routes->gateway.s_addr, data + i + 4, 4);
                routes->netmask.s_addr =
@@ -662,11 +658,10 @@ static route_t *decode_routers (const unsigned char *data, int length)
 
        for (i = 0; i < length; i += 4) {
                if (routes) {
-                       routes->next = xmalloc (sizeof (route_t));
+                       routes->next = xzalloc (sizeof (route_t));
                        routes = routes->next;
                } else
-                       head = routes = xmalloc (sizeof (route_t));
-               memset (routes, 0, sizeof (route_t));
+                       head = routes = xzalloc (sizeof (route_t));
                memcpy (&routes->gateway.s_addr, data + i, 4);
        }
 
diff --git a/info.c b/info.c
index 95065523a48ba4e5897fa3452b4307e2e52cbda8..0e8f3baa97b53e90b5629637039158b8d350c7ee 100644 (file)
--- a/info.c
+++ b/info.c
@@ -322,8 +322,7 @@ static bool parse_addresses (address_t **address, char *value, const char *var)
        bool retval = true;
 
        while ((token = strsep (&p, " "))) {
-               address_t *a = xmalloc (sizeof (address_t));
-               memset (a, 0, sizeof (address_t));
+               address_t *a = xzalloc (sizeof (address_t));
 
                if (inet_aton (token, &a->address) == 0) {
                        logger (LOG_ERR, "%s: invalid address `%s'", var, token);
@@ -425,8 +424,7 @@ bool read_info (const interface_t *iface, dhcp_t *dhcp)
                                }
 
                                /* See if we can create a route */
-                               route = xmalloc (sizeof (route_t));
-                               memset (route, 0, sizeof (route_t));
+                               route = xzalloc (sizeof (route_t));
                                if (inet_aton (dest, &route->destination) == 0) {
                                        logger (LOG_ERR, "read_info ROUTES `%s': not a valid destination address",
                                                dest);
@@ -458,8 +456,7 @@ bool read_info (const interface_t *iface, dhcp_t *dhcp)
                } else if (strcmp (var, "GATEWAYS") == 0) {
                        p = value;
                        while ((value = strsep (&p, " "))) {
-                               route_t *route = xmalloc (sizeof (route_t));
-                               memset (route, 0, sizeof (route_t));
+                               route_t *route = xzalloc (sizeof (route_t));
                                if (parse_address (&route->gateway, value, "GATEWAYS")) {
                                        if (dhcp->routes) {
                                                route_t *r = dhcp->routes;
index 3cb4952ca99b729a91ef3dd8e036b1f08b411b75..b748a37ba7e50c9a4516547a0868767c19ba5472 100644 (file)
@@ -406,8 +406,7 @@ interface_t *read_interface (const char *ifname, _unused int metric)
                }
        }
 
-       iface = xmalloc (sizeof (interface_t));
-       memset (iface, 0, sizeof (interface_t));
+       iface = xzalloc (sizeof (interface_t));
        strlcpy (iface->name, ifname, IF_NAMESIZE);
 #ifdef ENABLE_INFO
        snprintf (iface->infofile, PATH_MAX, INFOFILE, ifname);
@@ -738,8 +737,7 @@ static int send_netlink(struct nlmsghdr *hdr)
                return -1;
        }
 
-       buffer = xmalloc (sizeof (char) * BUFFERLEN);
-       memset (buffer, 0, BUFFERLEN);
+       buffer = xzalloc (sizeof (char) * BUFFERLEN);
        iov.iov_base = buffer;
 
        for (;;) {
@@ -890,9 +888,7 @@ static int do_address(const char *ifname,
        if (!ifname)
                return -1;
 
-       nlm = xmalloc (sizeof (struct nlma));
-       memset (nlm, 0, sizeof (struct nlma));
-
+       nlm = xzalloc (sizeof (struct nlma));
        nlm->hdr.nlmsg_len = NLMSG_LENGTH (sizeof (struct ifaddrmsg));
        nlm->hdr.nlmsg_flags = NLM_F_REQUEST;
        if (! del)
@@ -944,9 +940,7 @@ static int do_route (const char *ifname,
                return -1;
        }
 
-       nlm = xmalloc (sizeof (struct nlmr));
-       memset (nlm, 0, sizeof (struct nlmr));
-
+       nlm = xzalloc (sizeof (struct nlmr));
        nlm->hdr.nlmsg_len = NLMSG_LENGTH (sizeof (struct rtmsg));
        if (change)
                nlm->hdr.nlmsg_flags = NLM_F_REPLACE;