memp.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. /**
  2. * @file
  3. * Dynamic pool memory manager
  4. *
  5. * lwIP has dedicated pools for many structures (netconn, protocol control blocks,
  6. * packet buffers, ...). All these pools are managed here.
  7. *
  8. * @defgroup mempool Memory pools
  9. * @ingroup infrastructure
  10. * Custom memory pools
  11. */
  12. /*
  13. * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
  14. * All rights reserved.
  15. *
  16. * Redistribution and use in source and binary forms, with or without modification,
  17. * are permitted provided that the following conditions are met:
  18. *
  19. * 1. Redistributions of source code must retain the above copyright notice,
  20. * this list of conditions and the following disclaimer.
  21. * 2. Redistributions in binary form must reproduce the above copyright notice,
  22. * this list of conditions and the following disclaimer in the documentation
  23. * and/or other materials provided with the distribution.
  24. * 3. The name of the author may not be used to endorse or promote products
  25. * derived from this software without specific prior written permission.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  28. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  29. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  30. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  31. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  32. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  33. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  34. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  35. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  36. * OF SUCH DAMAGE.
  37. *
  38. * This file is part of the lwIP TCP/IP stack.
  39. *
  40. * Author: Adam Dunkels <adam@sics.se>
  41. *
  42. */
  43. #include "lwip/opt.h"
  44. #include "lwip/memp.h"
  45. #include "lwip/sys.h"
  46. #include "lwip/stats.h"
  47. #include <string.h>
  48. /* Make sure we include everything we need for size calculation required by memp_std.h */
  49. #include "lwip/pbuf.h"
  50. #include "lwip/raw.h"
  51. #include "lwip/udp.h"
  52. #include "lwip/tcp.h"
  53. #include "lwip/priv/tcp_priv.h"
  54. #include "lwip/ip4_frag.h"
  55. #include "lwip/netbuf.h"
  56. #include "lwip/api.h"
  57. #include "lwip/priv/tcpip_priv.h"
  58. #include "lwip/priv/api_msg.h"
  59. #include "lwip/sockets.h"
  60. #include "lwip/netifapi.h"
  61. #include "lwip/etharp.h"
  62. #include "lwip/igmp.h"
  63. #include "lwip/timeouts.h"
  64. /* needed by default MEMP_NUM_SYS_TIMEOUT */
  65. #include "netif/ppp/ppp_opts.h"
  66. #include "lwip/netdb.h"
  67. #include "lwip/dns.h"
  68. #include "lwip/priv/nd6_priv.h"
  69. #include "lwip/ip6_frag.h"
  70. #include "lwip/mld6.h"
  71. #define LWIP_MEMPOOL(name,num,size,desc) LWIP_MEMPOOL_DECLARE(name,num,size,desc)
  72. #include "lwip/priv/memp_std.h"
  73. const struct memp_desc* const memp_pools[MEMP_MAX] = {
  74. #define LWIP_MEMPOOL(name,num,size,desc) &memp_ ## name,
  75. #include "lwip/priv/memp_std.h"
  76. };
  77. #ifdef LWIP_HOOK_FILENAME
  78. #include LWIP_HOOK_FILENAME
  79. #endif
  80. #if MEMP_MEM_MALLOC && MEMP_OVERFLOW_CHECK >= 2
  81. #undef MEMP_OVERFLOW_CHECK
  82. /* MEMP_OVERFLOW_CHECK >= 2 does not work with MEMP_MEM_MALLOC, use 1 instead */
  83. #define MEMP_OVERFLOW_CHECK 1
  84. #endif
  85. #if MEMP_SANITY_CHECK && !MEMP_MEM_MALLOC
  86. /**
  87. * Check that memp-lists don't form a circle, using "Floyd's cycle-finding algorithm".
  88. */
  89. static int
  90. memp_sanity(const struct memp_desc *desc)
  91. {
  92. struct memp *t, *h;
  93. t = *desc->tab;
  94. if (t != NULL) {
  95. for (h = t->next; (t != NULL) && (h != NULL); t = t->next,
  96. h = ((h->next != NULL) ? h->next->next : NULL)) {
  97. if (t == h) {
  98. return 0;
  99. }
  100. }
  101. }
  102. return 1;
  103. }
  104. #endif /* MEMP_SANITY_CHECK && !MEMP_MEM_MALLOC */
  105. #if MEMP_OVERFLOW_CHECK
  106. /**
  107. * Check if a memp element was victim of an overflow
  108. * (e.g. the restricted area after it has been altered)
  109. *
  110. * @param p the memp element to check
  111. * @param desc the pool p comes from
  112. */
  113. static void
  114. memp_overflow_check_element_overflow(struct memp *p, const struct memp_desc *desc)
  115. {
  116. #if MEMP_SANITY_REGION_AFTER_ALIGNED > 0
  117. u16_t k;
  118. u8_t *m;
  119. m = (u8_t*)p + MEMP_SIZE + desc->size;
  120. for (k = 0; k < MEMP_SANITY_REGION_AFTER_ALIGNED; k++) {
  121. if (m[k] != 0xcd) {
  122. char errstr[128] = "detected memp overflow in pool ";
  123. strcat(errstr, desc->desc);
  124. LWIP_ASSERT(errstr, 0);
  125. }
  126. }
  127. #else /* MEMP_SANITY_REGION_AFTER_ALIGNED > 0 */
  128. LWIP_UNUSED_ARG(p);
  129. LWIP_UNUSED_ARG(desc);
  130. #endif /* MEMP_SANITY_REGION_AFTER_ALIGNED > 0 */
  131. }
  132. /**
  133. * Check if a memp element was victim of an underflow
  134. * (e.g. the restricted area before it has been altered)
  135. *
  136. * @param p the memp element to check
  137. * @param desc the pool p comes from
  138. */
  139. static void
  140. memp_overflow_check_element_underflow(struct memp *p, const struct memp_desc *desc)
  141. {
  142. #if MEMP_SANITY_REGION_BEFORE_ALIGNED > 0
  143. u16_t k;
  144. u8_t *m;
  145. m = (u8_t*)p + MEMP_SIZE - MEMP_SANITY_REGION_BEFORE_ALIGNED;
  146. for (k = 0; k < MEMP_SANITY_REGION_BEFORE_ALIGNED; k++) {
  147. if (m[k] != 0xcd) {
  148. char errstr[128] = "detected memp underflow in pool ";
  149. strcat(errstr, desc->desc);
  150. LWIP_ASSERT(errstr, 0);
  151. }
  152. }
  153. #else /* MEMP_SANITY_REGION_BEFORE_ALIGNED > 0 */
  154. LWIP_UNUSED_ARG(p);
  155. LWIP_UNUSED_ARG(desc);
  156. #endif /* MEMP_SANITY_REGION_BEFORE_ALIGNED > 0 */
  157. }
  158. /**
  159. * Initialize the restricted area of on memp element.
  160. */
  161. static void
  162. memp_overflow_init_element(struct memp *p, const struct memp_desc *desc)
  163. {
  164. #if MEMP_SANITY_REGION_BEFORE_ALIGNED > 0 || MEMP_SANITY_REGION_AFTER_ALIGNED > 0
  165. u8_t *m;
  166. #if MEMP_SANITY_REGION_BEFORE_ALIGNED > 0
  167. m = (u8_t*)p + MEMP_SIZE - MEMP_SANITY_REGION_BEFORE_ALIGNED;
  168. memset(m, 0xcd, MEMP_SANITY_REGION_BEFORE_ALIGNED);
  169. #endif
  170. #if MEMP_SANITY_REGION_AFTER_ALIGNED > 0
  171. m = (u8_t*)p + MEMP_SIZE + desc->size;
  172. memset(m, 0xcd, MEMP_SANITY_REGION_AFTER_ALIGNED);
  173. #endif
  174. #else /* MEMP_SANITY_REGION_BEFORE_ALIGNED > 0 || MEMP_SANITY_REGION_AFTER_ALIGNED > 0 */
  175. LWIP_UNUSED_ARG(p);
  176. LWIP_UNUSED_ARG(desc);
  177. #endif /* MEMP_SANITY_REGION_BEFORE_ALIGNED > 0 || MEMP_SANITY_REGION_AFTER_ALIGNED > 0 */
  178. }
  179. #if MEMP_OVERFLOW_CHECK >= 2
  180. /**
  181. * Do an overflow check for all elements in every pool.
  182. *
  183. * @see memp_overflow_check_element for a description of the check
  184. */
  185. static void
  186. memp_overflow_check_all(void)
  187. {
  188. u16_t i, j;
  189. struct memp *p;
  190. SYS_ARCH_DECL_PROTECT(old_level);
  191. SYS_ARCH_PROTECT(old_level);
  192. for (i = 0; i < MEMP_MAX; ++i) {
  193. p = (struct memp*)LWIP_MEM_ALIGN(memp_pools[i]->base);
  194. for (j = 0; j < memp_pools[i]->num; ++j) {
  195. memp_overflow_check_element_overflow(p, memp_pools[i]);
  196. memp_overflow_check_element_underflow(p, memp_pools[i]);
  197. p = LWIP_ALIGNMENT_CAST(struct memp*, ((u8_t*)p + MEMP_SIZE + memp_pools[i]->size + MEMP_SANITY_REGION_AFTER_ALIGNED));
  198. }
  199. }
  200. SYS_ARCH_UNPROTECT(old_level);
  201. }
  202. #endif /* MEMP_OVERFLOW_CHECK >= 2 */
  203. #endif /* MEMP_OVERFLOW_CHECK */
  204. /**
  205. * Initialize custom memory pool.
  206. * Related functions: memp_malloc_pool, memp_free_pool
  207. *
  208. * @param desc pool to initialize
  209. */
  210. void
  211. memp_init_pool(const struct memp_desc *desc)
  212. {
  213. #if MEMP_MEM_MALLOC
  214. LWIP_UNUSED_ARG(desc);
  215. #else
  216. int i;
  217. struct memp *memp;
  218. *desc->tab = NULL;
  219. memp = (struct memp*)LWIP_MEM_ALIGN(desc->base);
  220. /* create a linked list of memp elements */
  221. for (i = 0; i < desc->num; ++i) {
  222. memp->next = *desc->tab;
  223. *desc->tab = memp;
  224. #if MEMP_OVERFLOW_CHECK
  225. memp_overflow_init_element(memp, desc);
  226. #endif /* MEMP_OVERFLOW_CHECK */
  227. /* cast through void* to get rid of alignment warnings */
  228. memp = (struct memp *)(void *)((u8_t *)memp + MEMP_SIZE + desc->size
  229. #if MEMP_OVERFLOW_CHECK
  230. + MEMP_SANITY_REGION_AFTER_ALIGNED
  231. #endif
  232. );
  233. }
  234. #if MEMP_STATS
  235. desc->stats->avail = desc->num;
  236. #endif /* MEMP_STATS */
  237. #endif /* !MEMP_MEM_MALLOC */
  238. #if MEMP_STATS && (defined(LWIP_DEBUG) || LWIP_STATS_DISPLAY)
  239. desc->stats->name = desc->desc;
  240. #endif /* MEMP_STATS && (defined(LWIP_DEBUG) || LWIP_STATS_DISPLAY) */
  241. }
  242. /**
  243. * Initializes lwIP built-in pools.
  244. * Related functions: memp_malloc, memp_free
  245. *
  246. * Carves out memp_memory into linked lists for each pool-type.
  247. */
  248. void
  249. memp_init(void)
  250. {
  251. u16_t i;
  252. /* for every pool: */
  253. for (i = 0; i < LWIP_ARRAYSIZE(memp_pools); i++) {
  254. memp_init_pool(memp_pools[i]);
  255. #if LWIP_STATS && MEMP_STATS
  256. lwip_stats.memp[i] = memp_pools[i]->stats;
  257. #endif
  258. }
  259. #if MEMP_OVERFLOW_CHECK >= 2
  260. /* check everything a first time to see if it worked */
  261. memp_overflow_check_all();
  262. #endif /* MEMP_OVERFLOW_CHECK >= 2 */
  263. }
  264. static void*
  265. #if !MEMP_OVERFLOW_CHECK
  266. do_memp_malloc_pool(const struct memp_desc *desc)
  267. #else
  268. do_memp_malloc_pool_fn(const struct memp_desc *desc, const char* file, const int line)
  269. #endif
  270. {
  271. struct memp *memp;
  272. SYS_ARCH_DECL_PROTECT(old_level);
  273. #if MEMP_MEM_MALLOC
  274. memp = (struct memp *)mem_malloc(MEMP_SIZE + MEMP_ALIGN_SIZE(desc->size));
  275. SYS_ARCH_PROTECT(old_level);
  276. #else /* MEMP_MEM_MALLOC */
  277. SYS_ARCH_PROTECT(old_level);
  278. memp = *desc->tab;
  279. #endif /* MEMP_MEM_MALLOC */
  280. if (memp != NULL) {
  281. #if !MEMP_MEM_MALLOC
  282. #if MEMP_OVERFLOW_CHECK == 1
  283. memp_overflow_check_element_overflow(memp, desc);
  284. memp_overflow_check_element_underflow(memp, desc);
  285. #endif /* MEMP_OVERFLOW_CHECK */
  286. *desc->tab = memp->next;
  287. #if MEMP_OVERFLOW_CHECK
  288. memp->next = NULL;
  289. #endif /* MEMP_OVERFLOW_CHECK */
  290. #endif /* !MEMP_MEM_MALLOC */
  291. #if MEMP_OVERFLOW_CHECK
  292. memp->file = file;
  293. memp->line = line;
  294. #if MEMP_MEM_MALLOC
  295. memp_overflow_init_element(memp, desc);
  296. #endif /* MEMP_MEM_MALLOC */
  297. #endif /* MEMP_OVERFLOW_CHECK */
  298. LWIP_ASSERT("memp_malloc: memp properly aligned",
  299. ((mem_ptr_t)memp % MEM_ALIGNMENT) == 0);
  300. #if MEMP_STATS
  301. desc->stats->used++;
  302. if (desc->stats->used > desc->stats->max) {
  303. desc->stats->max = desc->stats->used;
  304. }
  305. #endif
  306. SYS_ARCH_UNPROTECT(old_level);
  307. /* cast through u8_t* to get rid of alignment warnings */
  308. return ((u8_t*)memp + MEMP_SIZE);
  309. } else {
  310. LWIP_DEBUGF(MEMP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("memp_malloc: out of memory in pool %s\n", desc->desc));
  311. #if MEMP_STATS
  312. desc->stats->err++;
  313. #endif
  314. }
  315. SYS_ARCH_UNPROTECT(old_level);
  316. return NULL;
  317. }
  318. /**
  319. * Get an element from a custom pool.
  320. *
  321. * @param desc the pool to get an element from
  322. *
  323. * @return a pointer to the allocated memory or a NULL pointer on error
  324. */
  325. void *
  326. #if !MEMP_OVERFLOW_CHECK
  327. memp_malloc_pool(const struct memp_desc *desc)
  328. #else
  329. memp_malloc_pool_fn(const struct memp_desc *desc, const char* file, const int line)
  330. #endif
  331. {
  332. LWIP_ASSERT("invalid pool desc", desc != NULL);
  333. if (desc == NULL) {
  334. return NULL;
  335. }
  336. #if !MEMP_OVERFLOW_CHECK
  337. return do_memp_malloc_pool(desc);
  338. #else
  339. return do_memp_malloc_pool_fn(desc, file, line);
  340. #endif
  341. }
  342. /**
  343. * Get an element from a specific pool.
  344. *
  345. * @param type the pool to get an element from
  346. *
  347. * @return a pointer to the allocated memory or a NULL pointer on error
  348. */
  349. void *
  350. #if !MEMP_OVERFLOW_CHECK
  351. memp_malloc(memp_t type)
  352. #else
  353. memp_malloc_fn(memp_t type, const char* file, const int line)
  354. #endif
  355. {
  356. void *memp;
  357. LWIP_ERROR("memp_malloc: type < MEMP_MAX", (type < MEMP_MAX), return NULL;);
  358. #if MEMP_OVERFLOW_CHECK >= 2
  359. memp_overflow_check_all();
  360. #endif /* MEMP_OVERFLOW_CHECK >= 2 */
  361. #if !MEMP_OVERFLOW_CHECK
  362. memp = do_memp_malloc_pool(memp_pools[type]);
  363. #else
  364. memp = do_memp_malloc_pool_fn(memp_pools[type], file, line);
  365. #endif
  366. return memp;
  367. }
  368. static void
  369. do_memp_free_pool(const struct memp_desc* desc, void *mem)
  370. {
  371. struct memp *memp;
  372. SYS_ARCH_DECL_PROTECT(old_level);
  373. LWIP_ASSERT("memp_free: mem properly aligned",
  374. ((mem_ptr_t)mem % MEM_ALIGNMENT) == 0);
  375. /* cast through void* to get rid of alignment warnings */
  376. memp = (struct memp *)(void *)((u8_t*)mem - MEMP_SIZE);
  377. SYS_ARCH_PROTECT(old_level);
  378. #if MEMP_OVERFLOW_CHECK == 1
  379. memp_overflow_check_element_overflow(memp, desc);
  380. memp_overflow_check_element_underflow(memp, desc);
  381. #endif /* MEMP_OVERFLOW_CHECK */
  382. #if MEMP_STATS
  383. desc->stats->used--;
  384. #endif
  385. #if MEMP_MEM_MALLOC
  386. LWIP_UNUSED_ARG(desc);
  387. SYS_ARCH_UNPROTECT(old_level);
  388. mem_free(memp);
  389. #else /* MEMP_MEM_MALLOC */
  390. memp->next = *desc->tab;
  391. *desc->tab = memp;
  392. #if MEMP_SANITY_CHECK
  393. LWIP_ASSERT("memp sanity", memp_sanity(desc));
  394. #endif /* MEMP_SANITY_CHECK */
  395. SYS_ARCH_UNPROTECT(old_level);
  396. #endif /* !MEMP_MEM_MALLOC */
  397. }
  398. /**
  399. * Put a custom pool element back into its pool.
  400. *
  401. * @param desc the pool where to put mem
  402. * @param mem the memp element to free
  403. */
  404. void
  405. memp_free_pool(const struct memp_desc* desc, void *mem)
  406. {
  407. LWIP_ASSERT("invalid pool desc", desc != NULL);
  408. if ((desc == NULL) || (mem == NULL)) {
  409. return;
  410. }
  411. do_memp_free_pool(desc, mem);
  412. }
  413. /**
  414. * Put an element back into its pool.
  415. *
  416. * @param type the pool where to put mem
  417. * @param mem the memp element to free
  418. */
  419. void
  420. memp_free(memp_t type, void *mem)
  421. {
  422. #ifdef LWIP_HOOK_MEMP_AVAILABLE
  423. struct memp *old_first;
  424. #endif
  425. LWIP_ERROR("memp_free: type < MEMP_MAX", (type < MEMP_MAX), return;);
  426. if (mem == NULL) {
  427. return;
  428. }
  429. #if MEMP_OVERFLOW_CHECK >= 2
  430. memp_overflow_check_all();
  431. #endif /* MEMP_OVERFLOW_CHECK >= 2 */
  432. #ifdef LWIP_HOOK_MEMP_AVAILABLE
  433. old_first = *memp_pools[type]->tab;
  434. #endif
  435. do_memp_free_pool(memp_pools[type], mem);
  436. #ifdef LWIP_HOOK_MEMP_AVAILABLE
  437. if (old_first == NULL) {
  438. LWIP_HOOK_MEMP_AVAILABLE(type);
  439. }
  440. #endif
  441. }