mirror of
https://github.com/lwip-tcpip/lwip.git
synced 2025-04-17 02:42:29 +00:00
Patch #9307: Replace mem_malloc+memset with mem_calloc
Aside from reducing source code, on systems which use MEM_LIBC_MALLOC, this has the potential to improve performance depending on the underlying memory allocator See http://stackoverflow.com/questions/2688466/why-mallocmemset-is-slower-than-calloc
This commit is contained in:
parent
b34f2d5605
commit
557a11047d
@ -173,9 +173,8 @@ altcp_mbedtls_mem_init(void)
|
||||
altcp_mbedtls_state_t *
|
||||
altcp_mbedtls_alloc(void *conf)
|
||||
{
|
||||
altcp_mbedtls_state_t *ret = (altcp_mbedtls_state_t *)mem_malloc(sizeof(altcp_mbedtls_state_t));
|
||||
altcp_mbedtls_state_t *ret = (altcp_mbedtls_state_t *)mem_calloc(1, sizeof(altcp_mbedtls_state_t));
|
||||
if (ret != NULL) {
|
||||
memset(ret, 0, sizeof(altcp_mbedtls_state_t));
|
||||
ret->conf = conf;
|
||||
}
|
||||
return ret;
|
||||
@ -198,10 +197,7 @@ altcp_mbedtls_alloc_config(size_t size)
|
||||
/* allocation too big (mem_size_t overflow) */
|
||||
return NULL;
|
||||
}
|
||||
ret = (altcp_mbedtls_state_t *)mem_malloc((mem_size_t)size);
|
||||
if (ret != NULL) {
|
||||
memset(ret, 0, size);
|
||||
}
|
||||
ret = (altcp_mbedtls_state_t *)mem_calloc(1, (mem_size_t)size);
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
@ -1912,12 +1912,11 @@ mdns_resp_add_netif(struct netif *netif, const char *hostname, u32_t dns_ttl)
|
||||
LWIP_ERROR("mdns_resp_add_netif: Hostname too long", (strlen(hostname) <= MDNS_LABEL_MAXLEN), return ERR_VAL);
|
||||
|
||||
LWIP_ASSERT("mdns_resp_add_netif: Double add", NETIF_TO_HOST(netif) == NULL);
|
||||
mdns = (struct mdns_host *) mem_malloc(sizeof(struct mdns_host));
|
||||
mdns = (struct mdns_host *) mem_calloc(1, sizeof(struct mdns_host));
|
||||
LWIP_ERROR("mdns_resp_add_netif: Alloc failed", (mdns != NULL), return ERR_MEM);
|
||||
|
||||
netif_set_client_data(netif, mdns_netif_client_id, mdns);
|
||||
|
||||
memset(mdns, 0, sizeof(struct mdns_host));
|
||||
MEMCPY(&mdns->name, hostname, LWIP_MIN(MDNS_LABEL_MAXLEN, strlen(hostname)));
|
||||
mdns->dns_ttl = dns_ttl;
|
||||
|
||||
@ -2020,11 +2019,9 @@ mdns_resp_add_service(struct netif *netif, const char *name, const char *service
|
||||
}
|
||||
LWIP_ERROR("mdns_resp_add_service: Service list full (increase MDNS_MAX_SERVICES)", (slot >= 0), return ERR_MEM);
|
||||
|
||||
srv = (struct mdns_service*)mem_malloc(sizeof(struct mdns_service));
|
||||
srv = (struct mdns_service*)mem_calloc(1, sizeof(struct mdns_service));
|
||||
LWIP_ERROR("mdns_resp_add_service: Alloc failed", (srv != NULL), return ERR_MEM);
|
||||
|
||||
memset(srv, 0, sizeof(struct mdns_service));
|
||||
|
||||
MEMCPY(&srv->name, name, LWIP_MIN(MDNS_LABEL_MAXLEN, strlen(name)));
|
||||
MEMCPY(&srv->service, service, LWIP_MIN(MDNS_LABEL_MAXLEN, strlen(service)));
|
||||
srv->txt_fn = txt_fn;
|
||||
|
@ -1238,11 +1238,7 @@ mqtt_set_inpub_callback(mqtt_client_t *client, mqtt_incoming_publish_cb_t pub_cb
|
||||
mqtt_client_t *
|
||||
mqtt_client_new(void)
|
||||
{
|
||||
mqtt_client_t *client = (mqtt_client_t *)mem_malloc(sizeof(mqtt_client_t));
|
||||
if (client != NULL) {
|
||||
memset(client, 0, sizeof(mqtt_client_t));
|
||||
}
|
||||
return client;
|
||||
return (mqtt_client_t *)mem_calloc(1, sizeof(mqtt_client_t));
|
||||
}
|
||||
|
||||
|
||||
|
@ -270,13 +270,12 @@ autoip_start(struct netif *netif)
|
||||
/* no AutoIP client attached yet? */
|
||||
LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE,
|
||||
("autoip_start(): starting new AUTOIP client\n"));
|
||||
autoip = (struct autoip *)mem_malloc(sizeof(struct autoip));
|
||||
autoip = (struct autoip *)mem_calloc(1, sizeof(struct autoip));
|
||||
if (autoip == NULL) {
|
||||
LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE,
|
||||
("autoip_start(): could not allocate autoip\n"));
|
||||
return ERR_MEM;
|
||||
}
|
||||
memset(autoip, 0, sizeof(struct autoip));
|
||||
/* store this AutoIP client in the netif */
|
||||
netif_set_client_data(netif, LWIP_NETIF_CLIENT_DATA_INDEX_AUTOIP, autoip);
|
||||
LWIP_DEBUGF(AUTOIP_DEBUG | LWIP_DBG_TRACE, ("autoip_start(): allocated autoip"));
|
||||
|
@ -244,11 +244,10 @@ bridgeif_fdb_init(u16_t max_fdb_entries)
|
||||
bridgeif_dfdb_t *fdb;
|
||||
mem_size_t alloc_len = sizeof(bridgeif_dfdb_t) + (max_fdb_entries*sizeof(bridgeif_dfdb_entry_t));
|
||||
LWIP_DEBUGF(BRIDGEIF_DEBUG, ("bridgeif_fdb_init: allocating %d bytes for private FDB data\n", (int)alloc_len));
|
||||
fdb = (bridgeif_dfdb_t*)mem_malloc(alloc_len);
|
||||
fdb = (bridgeif_dfdb_t*)mem_calloc(1, alloc_len);
|
||||
if (fdb == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
memset(fdb, 0, alloc_len);
|
||||
fdb->max_fdb_entries = max_fdb_entries;
|
||||
fdb->fdb = (bridgeif_dfdb_entry_t *)(fdb + 1);
|
||||
return fdb;
|
||||
@ -612,12 +611,11 @@ bridgeif_init(struct netif *netif)
|
||||
|
||||
alloc_len = sizeof(bridgeif_private_t) + (init_data->max_ports*sizeof(bridgeif_port_t) + (init_data->max_fdb_static_entries*sizeof(bridgeif_fdb_static_entry_t)));
|
||||
LWIP_DEBUGF(BRIDGEIF_DEBUG, ("bridgeif_init: allocating %d bytes for private data\n", (int)alloc_len));
|
||||
br = (bridgeif_private_t*)mem_malloc(alloc_len);
|
||||
br = (bridgeif_private_t*)mem_calloc(1, alloc_len);
|
||||
if (br == NULL) {
|
||||
LWIP_DEBUGF(NETIF_DEBUG, ("bridgeif_init: out of memory\n"));
|
||||
return ERR_MEM;
|
||||
}
|
||||
memset(br, 0, alloc_len);
|
||||
memcpy(&br->ethaddr, &init_data->ethaddr, sizeof(br->ethaddr));
|
||||
br->netif = netif;
|
||||
|
||||
|
@ -280,9 +280,8 @@ static void test_sockets_msgapi_tcp(int domain)
|
||||
}
|
||||
|
||||
/* allocate a receive buffer, same size as snd_buf for easy verification */
|
||||
rcv_buf = (u8_t*)mem_malloc(BUF_SZ);
|
||||
rcv_buf = (u8_t*)mem_calloc(1, BUF_SZ);
|
||||
fail_unless(rcv_buf != NULL);
|
||||
memset(rcv_buf, 0, BUF_SZ);
|
||||
/* split across iovs */
|
||||
for (i = 0; i < 4; i++) {
|
||||
riovs[i].iov_base = &rcv_buf[i*(BUF_SZ/4)];
|
||||
|
Loading…
x
Reference in New Issue
Block a user