From: Roy Marples Date: Mon, 28 Jan 2008 15:32:04 +0000 (+0000) Subject: Introduce xzalloc, which zeros memory as well as allocates it. This makes us smaller. X-Git-Tag: v3.2.3~54 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7ab8b9f184628ec9bd7466df72e61d621185fa55;p=thirdparty%2Fdhcpcd.git Introduce xzalloc, which zeros memory as well as allocates it. This makes us smaller. --- diff --git a/arp.c b/arp.c index e427fa99..01ce1e78 100644 --- 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; diff --git a/client.c b/client.c index 9b298ef5..8fd1c813 100644 --- 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; diff --git a/common.c b/common.c index 14e88541..161b891e 100644 --- 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); diff --git a/common.h b/common.h index 61732bb6..57e4e79e 100644 --- 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 diff --git a/configure.c b/configure.c index 8a655302..6e5b8ef8 100644 --- a/configure.c +++ b/configure.c @@ -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 11370138..c5630470 100644 --- 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 95065523..0e8f3baa 100644 --- 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; diff --git a/interface.c b/interface.c index 3cb4952c..b748a37b 100644 --- a/interface.c +++ b/interface.c @@ -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;