Let netdb use a memp pool for allocating memory when getaddrinfo() is called.

This commit is contained in:
goldsimon 2010-02-01 21:04:29 +00:00
parent 81c5d9e983
commit a66039b86c
6 changed files with 31 additions and 8 deletions

View File

@ -19,6 +19,10 @@ HISTORY
++ New features:
2010-02-01: Simon Goldschmidt
* opt.h, memp_std.h, dns.h, netdb.c, memp.c: Let netdb use a memp pool
for allocating memory when getaddrinfo() is called.
2010-01-31: Simon Goldschmidt
* dhcp.h, dhcp.c: Reworked the code that parses DHCP options: parse
them once instead of parsing for every option. This also removes

View File

@ -39,8 +39,10 @@
#include "lwip/err.h"
#include "lwip/mem.h"
#include "lwip/memp.h"
#include "lwip/ip_addr.h"
#include "lwip/api.h"
#include "lwip/dns.h"
#include <string.h>
#include <stdlib.h>
@ -235,7 +237,7 @@ lwip_freeaddrinfo(struct addrinfo *ai)
while (ai != NULL) {
next = ai->ai_next;
mem_free(ai);
memp_free(MEMP_NETDB, ai);
ai = next;
}
}
@ -305,7 +307,10 @@ lwip_getaddrinfo(const char *nodename, const char *servname,
LWIP_ASSERT("namelen is too long", (namelen + 1) <= (mem_size_t)-1);
total_size += namelen + 1;
}
ai = mem_malloc(total_size);
/* If this fails, please report to lwip-devel! :-) */
LWIP_ASSERT("total_size <= NETDB_ELEM_SIZE: please report this!",
total_size <= NETDB_ELEM_SIZE);
ai = memp_malloc(MEMP_NETDB);
if (ai == NULL) {
goto memerr;
}
@ -338,7 +343,7 @@ lwip_getaddrinfo(const char *nodename, const char *servname,
return 0;
memerr:
if (ai != NULL) {
mem_free(ai);
memp_free(MEMP_NETDB, ai);
}
return EAI_MEMORY;
}

View File

@ -56,6 +56,7 @@
#include "lwip/ip_frag.h"
#include "lwip/snmp_structs.h"
#include "lwip/snmp_msg.h"
#include "lwip/dns.h"
#include <string.h>

View File

@ -66,6 +66,13 @@
#define DNS_RRCLASS_HS 4 /* Hesiod [Dyer 87] */
#define DNS_RRCLASS_FLUSH 0x800 /* Flush bit */
/* The size used for the next line is rather a hack, but it prevents including socket.h in all files
that include memp.h, and that would possibly break portability (since socket.h defines some types
and constants possibly already define by the OS).
Calculation rule:
sizeof(struct addrinfo) + sizeof(struct sockaddr_in) + DNS_MAX_NAME_LENGTH + 1 byte zero-termination */
#define NETDB_ELEM_SIZE (32 + 16 + DNS_MAX_NAME_LENGTH + 1)
/** Callback which is invoked when a hostname is found.
* A function of this type must be implemented by the application using the DNS resolver.
* @param name pointer to the name that was looked up.
@ -75,15 +82,10 @@
*/
typedef void (*dns_found_callback)(const char *name, struct ip_addr *ipaddr, void *callback_arg);
void dns_init(void);
void dns_tmr(void);
void dns_setserver(u8_t numdns, struct ip_addr *dnsserver);
struct ip_addr dns_getserver(u8_t numdns);
err_t dns_gethostbyname(const char *hostname, struct ip_addr *addr,
dns_found_callback found, void *callback_arg);

View File

@ -74,6 +74,9 @@ LWIP_MEMPOOL(SNMP_NODE, MEMP_NUM_SNMP_NODE, sizeof(struct mib_list_no
LWIP_MEMPOOL(SNMP_VARBIND, MEMP_NUM_SNMP_VARBIND, sizeof(struct snmp_varbind), "SNMP_VARBIND")
LWIP_MEMPOOL(SNMP_VALUE, MEMP_NUM_SNMP_VALUE, SNMP_MAX_VALUE_SIZE, "SNMP_VALUE")
#endif /* LWIP_SNMP */
#if LWIP_DNS && LWIP_SOCKET
LWIP_MEMPOOL(NETDB, MEMP_NUM_NETDB, NETDB_ELEM_SIZE, "NETDB")
#endif /* LWIP_DNS && LWIP_SOCKET */
/*
* A list of pools of pbuf's used by LWIP.

View File

@ -362,6 +362,14 @@
#define MEMP_NUM_SNMP_VALUE 3
#endif
/**
* MEMP_NUM_NETDB: the number of concurrently running lwip_addrinfo() calls
* (before freeing the corresponding memory using lwip_freeaddrinfo()).
*/
#ifndef MEMP_NUM_NETDB
#define MEMP_NUM_NETDB 1
#endif
/**
* PBUF_POOL_SIZE: the number of buffers in the pbuf pool.
*/