]> rtime.felk.cvut.cz Git - pes-rpp/rpp-lwip.git/commitdiff
task #10140: Remove DNS_USES_STATIC_BUF (keep the implementation of DNS_USES_STATIC_B...
authorgoldsimon <goldsimon>
Sun, 21 Feb 2010 12:15:01 +0000 (12:15 +0000)
committergoldsimon <goldsimon>
Sun, 21 Feb 2010 12:15:01 +0000 (12:15 +0000)
CHANGELOG
src/core/dns.c
src/include/lwip/mem.h
src/include/lwip/opt.h

index 467fe4d983d925c5e098ad3d7faf16069d11ab7b..ab2b536b9bc9664074e23d4606e290c95e28c7a0 100644 (file)
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -129,6 +129,10 @@ HISTORY
 
   ++ Bugfixes:
 
+  2010-02-21: Simon Goldschmidt
+  * opt.h, mem.h, dns.c: task #10140: Remove DNS_USES_STATIC_BUF (keep
+    the implementation of DNS_USES_STATIC_BUF==1)
+
   2010-02-20: Simon Goldschmidt
   * tcp.h, tcp.c, tcp_in.c, tcp_out.c: Task #10088: Correctly implement
     close() vs. shutdown(). Now the application does not get any more
index 44e0e41e4a2c51c6dafaf1e1ea952c86d3742ce0..772188ef4d51a039aff18c4ff1bdfa47398de104 100644 (file)
@@ -227,10 +227,9 @@ static struct udp_pcb        *dns_pcb;
 static u8_t                   dns_seqno;
 static struct dns_table_entry dns_table[DNS_TABLE_SIZE];
 static ip_addr_t              dns_servers[DNS_MAX_SERVERS];
-
-#if (DNS_USES_STATIC_BUF == 1)
-static u8_t                   dns_payload[DNS_MSG_SIZE];
-#endif /* (DNS_USES_STATIC_BUF == 1) */
+/** Contiguous buffer for processing responses */
+static u8_t                   dns_payload_buffer[LWIP_MEM_ALIGN_BUFFER(DNS_MSG_SIZE)];
+static u8_t*                  dns_payload;
 
 /**
  * Initialize the resolver: set up the UDP pcb and configure the default server
@@ -240,6 +239,8 @@ void
 dns_init()
 {
   ip_addr_t dnsserver;
+
+  dns_payload = LWIP_MEM_ALIGN(dns_payload_buffer);
   
   /* initialize default DNS server address */
   DNS_SERVER_ADDRESS(&dnsserver);
@@ -733,12 +734,6 @@ dns_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, ip_addr_t *addr, u16_t
   struct dns_answer ans;
   struct dns_table_entry *pEntry;
   u16_t nquestions, nanswers;
-#if (DNS_USES_STATIC_BUF == 0)
-  u8_t dns_payload[DNS_MSG_SIZE];
-#endif /* (DNS_USES_STATIC_BUF == 0) */
-#if (DNS_USES_STATIC_BUF == 2)
-  u8_t* dns_payload;
-#endif /* (DNS_USES_STATIC_BUF == 2) */
 
   LWIP_UNUSED_ARG(arg);
   LWIP_UNUSED_ARG(pcb);
@@ -749,24 +744,15 @@ dns_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, ip_addr_t *addr, u16_t
   if (p->tot_len > DNS_MSG_SIZE) {
     LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: pbuf too big\n"));
     /* free pbuf and return */
-    goto memerr1;
+    goto memerr;
   }
 
   /* is the dns message big enough ? */
   if (p->tot_len < (SIZEOF_DNS_HDR + SIZEOF_DNS_QUERY + SIZEOF_DNS_ANSWER)) {
     LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: pbuf too small\n"));
     /* free pbuf and return */
-    goto memerr1;
-  }
-
-#if (DNS_USES_STATIC_BUF == 2)
-  dns_payload = mem_malloc(p->tot_len);
-  if (dns_payload == NULL) {
-    LWIP_DEBUGF(DNS_DEBUG, ("dns_recv: mem_malloc error\n"));
-    /* free pbuf and return */
-    goto memerr1;
+    goto memerr;
   }
-#endif /* (DNS_USES_STATIC_BUF == 2) */
 
   /* copy dns payload inside static buffer for processing */ 
   if (pbuf_copy_partial(p, dns_payload, p->tot_len, 0) == p->tot_len) {
@@ -804,7 +790,7 @@ dns_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, ip_addr_t *addr, u16_t
         /* Skip the name in the "question" part */
         pHostname = (char *) dns_parse_name((unsigned char *)dns_payload + SIZEOF_DNS_HDR) + SIZEOF_DNS_QUERY;
 
-        while(nanswers > 0) {
+        while (nanswers > 0) {
           /* skip answer resource record's host name */
           pHostname = (char *) dns_parse_name((unsigned char *)pHostname);
 
@@ -827,7 +813,7 @@ dns_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, ip_addr_t *addr, u16_t
               (*pEntry->found)(pEntry->name, &pEntry->ipaddr, pEntry->arg);
             }
             /* deallocate memory and return */
-            goto memerr2;
+            goto memerr;
           } else {
             pHostname = pHostname + SIZEOF_DNS_ANSWER + htons(ans.len);
           }
@@ -841,7 +827,7 @@ dns_recv(void *arg, struct udp_pcb *pcb, struct pbuf *p, ip_addr_t *addr, u16_t
   }
 
   /* deallocate memory and return */
-  goto memerr2;
+  goto memerr;
 
 responseerr:
   /* ERROR: call specified callback function with NULL as name to indicate an error */
@@ -852,13 +838,7 @@ responseerr:
   pEntry->state = DNS_STATE_UNUSED;
   pEntry->found = NULL;
 
-memerr2:
-#if (DNS_USES_STATIC_BUF == 2)
-  /* free dns buffer */
-  mem_free(dns_payload);
-#endif /* (DNS_USES_STATIC_BUF == 2) */
-
-memerr1:
+memerr:
   /* free pbuf */
   pbuf_free(p);
   return;
index 6e6c1d92536f1a94ebbb61073d89de61fc52e107..7a9a0ecb7c9c2ef90cb5876d81eb373c5c373e23 100644 (file)
@@ -90,10 +90,25 @@ void *mem_calloc(mem_size_t count, mem_size_t size);
 void  mem_free(void *mem);
 #endif /* MEM_LIBC_MALLOC */
 
+/** Calculate memory size for an aligned buffer - returns the next highest
+ * multiple of MEM_ALIGNMENT (e.g. LWIP_MEM_ALIGN_SIZE(3) and
+ * LWIP_MEM_ALIGN_SIZE(4) will both yield 4 for MEM_ALIGNMENT == 4).
+ */
 #ifndef LWIP_MEM_ALIGN_SIZE
 #define LWIP_MEM_ALIGN_SIZE(size) (((size) + MEM_ALIGNMENT - 1) & ~(MEM_ALIGNMENT-1))
 #endif
 
+/** Calculate safe memory size for an aligned buffer when using an unaligned
+ * type as storage. This includes a safety-margin on (MEM_ALIGNMENT - 1) at the
+ * start (e.g. if buffer is u8_t[] and actual data will be u32_t*)
+ */
+#ifndef LWIP_MEM_ALIGN_BUFFER
+#define LWIP_MEM_ALIGN_BUFFER(size) (((size) + MEM_ALIGNMENT - 1))
+#endif
+
+/** Align a memory pointer to the alignment defined by MEM_ALIGNMENT
+ * so that ADDR % MEM_ALIGNMENT == 0
+ */
 #ifndef LWIP_MEM_ALIGN
 #define LWIP_MEM_ALIGN(addr) ((void *)(((mem_ptr_t)(addr) + MEM_ALIGNMENT - 1) & ~(mem_ptr_t)(MEM_ALIGNMENT-1)))
 #endif
index ab5be05aa36300366635f7a6eaab0ca7158c20f0..444fe0abd1a070bae0bf2869419b7be4070a2efc 100644 (file)
 #define DNS_DOES_NAME_CHECK             1
 #endif
 
-/** DNS use a local buffer if DNS_USES_STATIC_BUF=0, a static one if
-    DNS_USES_STATIC_BUF=1, or a dynamic one if DNS_USES_STATIC_BUF=2.
-    The buffer will be of size DNS_MSG_SIZE */
-#ifndef DNS_USES_STATIC_BUF
-#define DNS_USES_STATIC_BUF             1
-#endif
-
 /** DNS message max. size. Default value is RFC compliant. */
 #ifndef DNS_MSG_SIZE
 #define DNS_MSG_SIZE                    512