mirror of
https://frontier.innolan.net/rainlance/c-ares.git
synced 2025-10-06 10:29:43 +00:00
api: Allow injection of user-specified malloc/free functions
Add a new ares_library_init_mem() initialization function for the library which allows the library user to specify their own malloc, realloc & free equivalents for use library-wide. Store these function pointers in library-wide global variables, defaulting to libc's malloc(), realloc() and free(). Change all calls to malloc, realloc and free to use the function pointer instead. Also ensure that ares_strdup() is always available (even if the local environment includes strdup(3)), and change the library code to always use it. Convert calls to calloc() to use ares_malloc() + memset
This commit is contained in:
@ -107,7 +107,7 @@ int ares_set_servers(ares_channel channel,
|
||||
if (num_srvrs > 0)
|
||||
{
|
||||
/* Allocate storage for servers state */
|
||||
channel->servers = malloc(num_srvrs * sizeof(struct server_state));
|
||||
channel->servers = ares_malloc(num_srvrs * sizeof(struct server_state));
|
||||
if (!channel->servers)
|
||||
{
|
||||
return ARES_ENOMEM;
|
||||
@ -157,7 +157,7 @@ int ares_set_servers_csv(ares_channel channel,
|
||||
if (i == 0)
|
||||
return ARES_SUCCESS; /* blank all servers */
|
||||
|
||||
csv = malloc(i + 2);
|
||||
csv = ares_malloc(i + 2);
|
||||
if (!csv)
|
||||
return ARES_ENOMEM;
|
||||
|
||||
@ -227,7 +227,7 @@ int ares_set_servers_csv(ares_channel channel,
|
||||
goto out;
|
||||
}
|
||||
/* was ipv6, add new server */
|
||||
s = malloc(sizeof(*s));
|
||||
s = ares_malloc(sizeof(*s));
|
||||
if (!s) {
|
||||
rv = ARES_ENOMEM;
|
||||
goto out;
|
||||
@ -237,7 +237,7 @@ int ares_set_servers_csv(ares_channel channel,
|
||||
}
|
||||
else {
|
||||
/* was ipv4, add new server */
|
||||
s = malloc(sizeof(*s));
|
||||
s = ares_malloc(sizeof(*s));
|
||||
if (!s) {
|
||||
rv = ARES_ENOMEM;
|
||||
goto out;
|
||||
@ -270,11 +270,11 @@ int ares_set_servers_csv(ares_channel channel,
|
||||
|
||||
out:
|
||||
if (csv)
|
||||
free(csv);
|
||||
ares_free(csv);
|
||||
while (servers) {
|
||||
struct ares_addr_node *s = servers;
|
||||
servers = servers->next;
|
||||
free(s);
|
||||
ares_free(s);
|
||||
}
|
||||
|
||||
return rv;
|
||||
|
Reference in New Issue
Block a user