From 03bd61c799d8e7489130dbb8630ccefb6fe47796 Mon Sep 17 00:00:00 2001 From: goldsimon Date: Sun, 16 May 2010 13:36:51 +0000 Subject: [PATCH] DNS_LOCAL_HOSTLIST_IS_DYNAMIC uses its own MEMP pool instead of the heap --- src/core/dns.c | 17 ++++++----------- src/include/lwip/dns.h | 17 +++++++++++++++++ src/include/lwip/memp_std.h | 3 +++ src/include/lwip/opt.h | 8 ++++++++ 4 files changed, 34 insertions(+), 11 deletions(-) diff --git a/src/core/dns.c b/src/core/dns.c index 06a1123a..205b2917 100644 --- a/src/core/dns.c +++ b/src/core/dns.c @@ -78,6 +78,7 @@ #include "lwip/udp.h" #include "lwip/mem.h" +#include "lwip/memp.h" #include "lwip/dns.h" #include @@ -180,14 +181,6 @@ struct dns_table_entry { }; #if DNS_LOCAL_HOSTLIST -/** struct used for local host-list */ -struct local_hostlist_entry { - /** static hostname */ - const char *name; - /** static host address in network byteorder */ - ip_addr_t addr; - struct local_hostlist_entry *next; -}; #if DNS_LOCAL_HOSTLIST_IS_DYNAMIC /** Local host-list. For hostnames in this list, no @@ -329,7 +322,8 @@ dns_init_local() struct local_hostlist_entry *init_entry = &local_hostlist_init[i]; LWIP_ASSERT("invalid host name (NULL)", init_entry->name != NULL); namelen = strlen(init_entry->name); - entry = mem_malloc((mem_size_t)(sizeof(struct local_hostlist_entry) + namelen + 1)); + LWIP_ASSERT("namelen <= DNS_LOCAL_HOSTLIST_MAX_NAMELEN", namelen <= DNS_LOCAL_HOSTLIST_MAX_NAMELEN); + entry = (struct local_hostlist_entry *)memp_malloc(MEMP_LOCALHOSTLIST); LWIP_ASSERT("mem-error in dns_init_local", entry != NULL); if (entry != NULL) { entry->name = (char*)entry + sizeof(struct local_hostlist_entry); @@ -398,7 +392,7 @@ dns_local_removehost(const char *hostname, const ip_addr_t *addr) } free_entry = entry; entry = entry->next; - mem_free(free_entry); + memp_free(MEMP_LOCALHOSTLIST, free_entry); removed++; } else { last_entry = entry; @@ -423,7 +417,8 @@ dns_local_addhost(const char *hostname, const ip_addr_t *addr) size_t namelen; LWIP_ASSERT("invalid host name (NULL)", hostname != NULL); namelen = strlen(hostname); - entry = mem_malloc((mem_size_t)(sizeof(struct local_hostlist_entry) + namelen + 1)); + LWIP_ASSERT("namelen <= DNS_LOCAL_HOSTLIST_MAX_NAMELEN", namelen <= DNS_LOCAL_HOSTLIST_MAX_NAMELEN); + entry = (struct local_hostlist_entry *)memp_malloc(MEMP_LOCALHOSTLIST); if (entry == NULL) { return ERR_MEM; } diff --git a/src/include/lwip/dns.h b/src/include/lwip/dns.h index 69f34b2f..6042bf75 100644 --- a/src/include/lwip/dns.h +++ b/src/include/lwip/dns.h @@ -73,6 +73,23 @@ 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) +#if DNS_LOCAL_HOSTLIST +/** struct used for local host-list */ +struct local_hostlist_entry { + /** static hostname */ + const char *name; + /** static host address in network byteorder */ + ip_addr_t addr; + struct local_hostlist_entry *next; +}; +#if DNS_LOCAL_HOSTLIST_IS_DYNAMIC +#ifndef DNS_LOCAL_HOSTLIST_MAX_NAMELEN +#define DNS_LOCAL_HOSTLIST_MAX_NAMELEN DNS_MAX_NAME_LENGTH +#endif +#define LOCALHOSTLIST_ELEM_SIZE ((sizeof(struct local_hostlist_entry) + DNS_LOCAL_HOSTLIST_MAX_NAMELEN + 1)) +#endif /* DNS_LOCAL_HOSTLIST_IS_DYNAMIC */ +#endif /* DNS_LOCAL_HOSTLIST */ + /** 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. diff --git a/src/include/lwip/memp_std.h b/src/include/lwip/memp_std.h index 30bc2f25..448d6f91 100644 --- a/src/include/lwip/memp_std.h +++ b/src/include/lwip/memp_std.h @@ -79,6 +79,9 @@ LWIP_MEMPOOL(SNMP_VALUE, MEMP_NUM_SNMP_VALUE, SNMP_MAX_VALUE_SIZE, #if LWIP_DNS && LWIP_SOCKET LWIP_MEMPOOL(NETDB, MEMP_NUM_NETDB, NETDB_ELEM_SIZE, "NETDB") #endif /* LWIP_DNS && LWIP_SOCKET */ +#if LWIP_DNS && DNS_LOCAL_HOSTLIST && DNS_LOCAL_HOSTLIST_IS_DYNAMIC +LWIP_MEMPOOL(LOCALHOSTLIST, MEMP_NUM_LOCALHOSTLIST, LOCALHOSTLIST_ELEM_SIZE, "LOCALHOSTLIST") +#endif /* LWIP_DNS && DNS_LOCAL_HOSTLIST && DNS_LOCAL_HOSTLIST_IS_DYNAMIC */ /* * A list of pools of pbuf's used by LWIP. diff --git a/src/include/lwip/opt.h b/src/include/lwip/opt.h index 1aa2c400..9617eaaa 100644 --- a/src/include/lwip/opt.h +++ b/src/include/lwip/opt.h @@ -370,6 +370,14 @@ #define MEMP_NUM_NETDB 1 #endif +/** + * MEMP_NUM_LOCALHOSTLIST: the number of host entries in the local host list + * if DNS_LOCAL_HOSTLIST_IS_DYNAMIC==1. + */ +#ifndef MEMP_NUM_LOCALHOSTLIST +#define MEMP_NUM_LOCALHOSTLIST 1 +#endif + /** * PBUF_POOL_SIZE: the number of buffers in the pbuf pool. */