Index: core/mem.c
===================================================================
RCS file: /sources/lwip/lwip/src/core/mem.c,v
retrieving revision 1.40
diff -u -r1.40 mem.c
--- core/mem.c	22 May 2007 19:38:12 -0000	1.40
+++ core/mem.c	11 Jun 2007 16:09:46 -0000
@@ -48,7 +48,99 @@
 
 #include "lwip/stats.h"
 
-#if (MEM_LIBC_MALLOC == 0)
+#if !MEM_LIBC_MALLOC
+#if MEM_USE_POOLS
+/* lwIP head implemented with different sized pools */
+
+/**
+ * This structure is used to save the pool one element came from.
+ */
+struct memp_helper
+{
+   memp_t poolnr;
+};
+
+/**
+ * Allocate memory: determine the smallest pool that is big enough
+ * to contain an element of 'size' and get an element from that pool.
+ *
+ * @param size the size in bytes of the memory needed
+ * @return a pointer to the allocated memory or NULL if the pool is empty
+ */
+void *
+mem_malloc(mem_size_t size)
+{
+  struct memp_helper *element;
+  int poolnr = -1;
+
+  for (poolnr = MEMP_MEM_POOL; poolnr < MEMP_MEM_POOL + MEM_POOL_COUNT; poolnr++) {
+    if ((size + sizeof(struct memp_helper)) <= memp_sizes[poolnr]) {
+      break;
+    }
+  }
+  if (poolnr == -1) {
+    LWIP_ASSERT("mem_malloc(): no pool is that big!", 0);
+    return NULL;
+  }
+  element = (struct memp_helper*)memp_malloc(poolnr);
+  if (element == NULL) {
+    /* No need to DEBUGF or ASSERT: This error is already
+       taken care of in memp.c */
+    return NULL;
+  }
+
+  element->poolnr = poolnr;
+  element++;
+
+  return element;
+}
+
+/**
+ * Free memory previously allocated by mem_malloc. Loads the pool number
+ * and calls memp_free with that pool number to put the element back into
+ * its pool
+ *
+ * @param rmem the memory element to free
+ */
+void
+mem_free(void *rmem)
+{
+  struct memp_helper *hmem = (struct memp_helper*)rmem;
+
+  LWIP_ASSERT("rmem != NULL", (rmem != NULL));
+  LWIP_ASSERT("rmem == MEM_ALIGN(rmem)", (rmem == MEM_ALIGN(rmem)));
+
+  hmem--;
+
+  LWIP_ASSERT("hmem != NULL", (hmem != NULL));
+  LWIP_ASSERT("hmem == MEM_ALIGN(hmem)", (hmem == MEM_ALIGN(hmem)));
+  LWIP_ASSERT("hmem->poolnr < MEMP_MAX", (hmem->poolnr < MEMP_MAX));
+
+  memp_free(hmem->poolnr, hmem);
+}
+
+/**
+ * In contrast to its name, mem_realloc can only shrink memory, not expand it.
+ * Since the only use (for now) is in pbuf_realloc (which also can only shrink),
+ * this shouldn't be a problem!
+ *
+ * @param rmem pointer to the memory block to shrink
+ * @param newsize the size in bytes that is still needed from rmem
+ * @return the new location of rmem (for now, this _is_ rmem!)
+ */
+void *
+mem_realloc(void *rmem, mem_size_t newsize)
+{
+  /* Since mem_realloc can currently only shrink memory, we would
+   * have to allocate a chunk from a smaller pool and memcpy into
+   * the new buffer.
+   * For now we leave it as it is and waste some memory to be faster...
+   */
+  return rmem;
+}
+
+
+#else /* MEM_USE_POOLS */
 /* lwIP replacement for your libc malloc() */
 
 /* This does not have to be aligned since for getting its size,
@@ -427,5 +519,5 @@
   return NULL;
 }
 
-#endif /* MEM_LIBC_MALLOC == 0 */
-
+#endif /* MEM_USE_POOLS */
+#endif /* !MEM_LIBC_MALLOC */
Index: core/memp.c
===================================================================
RCS file: /sources/lwip/lwip/src/core/memp.c,v
retrieving revision 1.40
diff -u -r1.40 memp.c
--- core/memp.c	7 Jun 2007 21:36:20 -0000	1.40
+++ core/memp.c	11 Jun 2007 16:00:06 -0000
@@ -99,7 +99,10 @@
 
 #endif /* MEMP_OVERFLOW_CHECK */
 
-static const u16_t memp_sizes[MEMP_MAX] = {
+#if !MEM_USE_POOLS
+static
+#endif /* !MEM_USE_POOLS */
+const u16_t memp_sizes[MEMP_MAX] = {
   MEMP_ALIGN_SIZE(sizeof(struct pbuf)),
   MEMP_ALIGN_SIZE(sizeof(struct raw_pcb)),
   MEMP_ALIGN_SIZE(sizeof(struct udp_pcb)),
@@ -113,6 +116,27 @@
   MEMP_ALIGN_SIZE(sizeof(struct etharp_q_entry)),
 #endif
   MEMP_ALIGN_SIZE(sizeof(struct pbuf)) + MEMP_ALIGN_SIZE(PBUF_POOL_BUFSIZE),
+#if MEM_USE_POOLS
+  MEM_POOL_SIZE_1,
+#if MEM_POOL_COUNT > 1
+  MEM_POOL_SIZE_2,
+#if MEM_POOL_COUNT > 2
+  MEM_POOL_SIZE_3,
+#if MEM_POOL_COUNT > 3
+  MEM_POOL_SIZE_4,
+#if MEM_POOL_COUNT > 4
+  MEM_POOL_SIZE_5,
+#if MEM_POOL_COUNT > 5
+  MEM_POOL_SIZE_6,
+#if MEM_POOL_COUNT > 6
+  MEM_POOL_SIZE_7,
+#endif
+#endif
+#endif
+#endif
+#endif
+#endif
+#endif
   MEMP_ALIGN_SIZE(sizeof(struct sys_timeo))
 };
 
@@ -130,6 +154,27 @@
   MEMP_NUM_ARP_QUEUE,
 #endif
   PBUF_POOL_SIZE,
+#if MEM_USE_POOLS
+  MEM_POOL_NUM_1,
+#if MEM_POOL_COUNT > 1
+  MEM_POOL_NUM_2,
+#if MEM_POOL_COUNT > 2
+  MEM_POOL_NUM_3,
+#if MEM_POOL_COUNT > 3
+  MEM_POOL_NUM_4,
+#if MEM_POOL_COUNT > 4
+  MEM_POOL_NUM_5,
+#if MEM_POOL_COUNT > 5
+  MEM_POOL_NUM_6,
+#if MEM_POOL_COUNT > 6
+  MEM_POOL_NUM_7,
+#endif
+#endif
+#endif
+#endif
+#endif
+#endif
+#endif
   MEMP_NUM_SYS_TIMEOUT
 };
 
@@ -151,6 +196,27 @@
 #endif
   MEMP_TYPE_SIZE(PBUF_POOL_SIZE, struct pbuf) +
                  PBUF_POOL_SIZE * MEMP_ALIGN_SIZE(PBUF_POOL_BUFSIZE) +
+#if MEM_USE_POOLS
+  MEM_POOL_NUM_1 * MEMP_ALIGN_SIZE(MEM_POOL_SIZE_1) +
+#if MEM_POOL_COUNT > 1
+  MEM_POOL_NUM_2 * MEMP_ALIGN_SIZE(MEM_POOL_SIZE_2) +
+#if MEM_POOL_COUNT > 2
+  MEM_POOL_NUM_3 * MEMP_ALIGN_SIZE(MEM_POOL_SIZE_3) +
+#if MEM_POOL_COUNT > 3
+  MEM_POOL_NUM_4 * MEMP_ALIGN_SIZE(MEM_POOL_SIZE_4) +
+#if MEM_POOL_COUNT > 4
+  MEM_POOL_NUM_5 * MEMP_ALIGN_SIZE(MEM_POOL_SIZE_5) +
+#if MEM_POOL_COUNT > 5
+  MEM_POOL_NUM_6 * MEMP_ALIGN_SIZE(MEM_POOL_SIZE_6) +
+#if MEM_POOL_COUNT > 6
+  MEM_POOL_NUM_7 * MEMP_ALIGN_SIZE(MEM_POOL_SIZE_7) +
+#endif
+#endif
+#endif
+#endif
+#endif
+#endif
+#endif
   MEMP_TYPE_SIZE(MEMP_NUM_SYS_TIMEOUT, struct sys_timeo)];
 
 #if MEMP_SANITY_CHECK
Index: include/lwip/mem.h
===================================================================
RCS file: /sources/lwip/lwip/src/include/lwip/mem.h,v
retrieving revision 1.13
diff -u -r1.13 mem.h
--- include/lwip/mem.h	11 May 2007 19:42:14 -0000	1.13
+++ include/lwip/mem.h	11 Jun 2007 16:10:52 -0000
@@ -67,7 +67,11 @@
 #endif
 #else /* MEM_LIBC_MALLOC */
 /* lwIP alternative malloc */
+#if MEM_USE_POOLS
+#define mem_init()
+#else /* MEM_USE_POOLS */
 void mem_init(void);
+#endif /* MEM_USE_POOLS */
 void *mem_malloc(mem_size_t size);
 void mem_free(void *mem);
 void *mem_realloc(void *mem, mem_size_t size);
Index: include/lwip/memp.h
===================================================================
RCS file: /sources/lwip/lwip/src/include/lwip/memp.h,v
retrieving revision 1.17
diff -u -r1.17 memp.h
--- include/lwip/memp.h	18 May 2007 19:48:45 -0000	1.17
+++ include/lwip/memp.h	11 Jun 2007 16:00:43 -0000
@@ -55,6 +55,12 @@
   MEMP_ARP_QUEUE,
 #endif
   MEMP_PBUF_POOL,
+#if MEM_USE_POOLS
+  MEMP_MEM_POOL,
+#if MEM_POOL_COUNT > 1
+  MEMP_MEM_LAST_POOL = MEMP_MEM_POOL + MEM_POOL_COUNT - 1,
+#endif
+#endif
   MEMP_SYS_TIMEOUT,
 
   MEMP_MAX
@@ -70,6 +76,10 @@
 #endif
 void  memp_free(memp_t type, void *mem);
 
+#if MEM_USE_POOLS
+extern const u16_t memp_sizes[MEMP_MAX];
+#endif /* !MEM_USE_POOLS */
+
 #ifdef __cplusplus
 }
 #endif
Index: include/lwip/opt.h
===================================================================
RCS file: /sources/lwip/lwip/src/include/lwip/opt.h,v
retrieving revision 1.68
diff -u -r1.68 opt.h
--- include/lwip/opt.h	10 Jun 2007 12:02:25 -0000	1.68
+++ include/lwip/opt.h	11 Jun 2007 15:50:42 -0000
@@ -70,6 +70,10 @@
 #define MEM_LIBC_MALLOC                 0
 #endif
 
+#ifndef MEM_USE_POOLS
+#define MEM_USE_POOLS                   0
+#endif
+
 /* MEM_ALIGNMENT: should be set to the alignment of the CPU for which
    lwIP is compiled. 4 byte alignment -> define MEM_ALIGNMENT to 4, 2
    byte alignment -> define MEM_ALIGNMENT to 2. */
@@ -568,6 +572,12 @@
 #define MEM_STATS                       1
 #endif
 
+/* don't use mem-stats if mem uses memp */
+#if MEM_USE_POOLS
+#undef MEM_STATS
+#define MEM_STATS                       0
+#endif
+
 #ifndef MEMP_STATS
 #define MEMP_STATS                      1
 #endif
