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:
Joel Cunningham
2017-04-05 13:38:20 -05:00
parent b34f2d5605
commit 557a11047d
6 changed files with 9 additions and 24 deletions

View File

@@ -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;
}

View File

@@ -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;

View File

@@ -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));
}