memp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  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/altcp.h"
  55. #include "lwip/ip4_frag.h"
  56. #include "lwip/netbuf.h"
  57. #include "lwip/api.h"
  58. #include "lwip/priv/tcpip_priv.h"
  59. #include "lwip/priv/api_msg.h"
  60. #include "lwip/priv/sockets_priv.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 or underflow
  108. * (e.g. the restricted area after/before 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(struct memp *p, const struct memp_desc *desc)
  115. {
  116. mem_overflow_check_raw((u8_t *)p + MEMP_SIZE, desc->size, "pool ", desc->desc);
  117. }
  118. /**
  119. * Initialize the restricted area of on memp element.
  120. */
  121. static void
  122. memp_overflow_init_element(struct memp *p, const struct memp_desc *desc)
  123. {
  124. mem_overflow_init_raw((u8_t *)p + MEMP_SIZE, desc->size);
  125. }
  126. #if MEMP_OVERFLOW_CHECK >= 2
  127. /**
  128. * Do an overflow check for all elements in every pool.
  129. *
  130. * @see memp_overflow_check_element for a description of the check
  131. */
  132. static void
  133. memp_overflow_check_all(void)
  134. {
  135. u16_t i, j;
  136. struct memp *p;
  137. SYS_ARCH_DECL_PROTECT(old_level);
  138. SYS_ARCH_PROTECT(old_level);
  139. for (i = 0; i < MEMP_MAX; ++i) {
  140. p = (struct memp *)LWIP_MEM_ALIGN(memp_pools[i]->base);
  141. for (j = 0; j < memp_pools[i]->num; ++j) {
  142. memp_overflow_check_element(p, memp_pools[i]);
  143. p = LWIP_ALIGNMENT_CAST(struct memp *, ((u8_t *)p + MEMP_SIZE + memp_pools[i]->size + MEM_SANITY_REGION_AFTER_ALIGNED));
  144. }
  145. }
  146. SYS_ARCH_UNPROTECT(old_level);
  147. }
  148. #endif /* MEMP_OVERFLOW_CHECK >= 2 */
  149. #endif /* MEMP_OVERFLOW_CHECK */
  150. /**
  151. * Initialize custom memory pool.
  152. * Related functions: memp_malloc_pool, memp_free_pool
  153. *
  154. * @param desc pool to initialize
  155. */
  156. void
  157. memp_init_pool(const struct memp_desc *desc)
  158. {
  159. #if MEMP_MEM_MALLOC
  160. LWIP_UNUSED_ARG(desc);
  161. #else
  162. int i;
  163. struct memp *memp;
  164. *desc->tab = NULL;
  165. memp = (struct memp *)LWIP_MEM_ALIGN(desc->base);
  166. #if MEMP_MEM_INIT
  167. /* force memset on pool memory */
  168. memset(memp, 0, (size_t)desc->num * (MEMP_SIZE + desc->size
  169. #if MEMP_OVERFLOW_CHECK
  170. + MEM_SANITY_REGION_AFTER_ALIGNED
  171. #endif
  172. ));
  173. #endif
  174. /* create a linked list of memp elements */
  175. for (i = 0; i < desc->num; ++i) {
  176. memp->next = *desc->tab;
  177. *desc->tab = memp;
  178. #if MEMP_OVERFLOW_CHECK
  179. memp_overflow_init_element(memp, desc);
  180. #endif /* MEMP_OVERFLOW_CHECK */
  181. /* cast through void* to get rid of alignment warnings */
  182. memp = (struct memp *)(void *)((u8_t *)memp + MEMP_SIZE + desc->size
  183. #if MEMP_OVERFLOW_CHECK
  184. + MEM_SANITY_REGION_AFTER_ALIGNED
  185. #endif
  186. );
  187. }
  188. #if MEMP_STATS
  189. desc->stats->avail = desc->num;
  190. #endif /* MEMP_STATS */
  191. #endif /* !MEMP_MEM_MALLOC */
  192. #if MEMP_STATS && (defined(LWIP_DEBUG) || LWIP_STATS_DISPLAY)
  193. desc->stats->name = desc->desc;
  194. #endif /* MEMP_STATS && (defined(LWIP_DEBUG) || LWIP_STATS_DISPLAY) */
  195. }
  196. /**
  197. * Initializes lwIP built-in pools.
  198. * Related functions: memp_malloc, memp_free
  199. *
  200. * Carves out memp_memory into linked lists for each pool-type.
  201. */
  202. void
  203. memp_init(void)
  204. {
  205. u16_t i;
  206. /* for every pool: */
  207. for (i = 0; i < LWIP_ARRAYSIZE(memp_pools); i++) {
  208. memp_init_pool(memp_pools[i]);
  209. #if LWIP_STATS && MEMP_STATS
  210. lwip_stats.memp[i] = memp_pools[i]->stats;
  211. #endif
  212. }
  213. #if MEMP_OVERFLOW_CHECK >= 2
  214. /* check everything a first time to see if it worked */
  215. memp_overflow_check_all();
  216. #endif /* MEMP_OVERFLOW_CHECK >= 2 */
  217. }
  218. static void *
  219. #if !MEMP_OVERFLOW_CHECK
  220. do_memp_malloc_pool(const struct memp_desc *desc)
  221. #else
  222. do_memp_malloc_pool_fn(const struct memp_desc *desc, const char *file, const int line)
  223. #endif
  224. {
  225. struct memp *memp;
  226. SYS_ARCH_DECL_PROTECT(old_level);
  227. #if MEMP_MEM_MALLOC
  228. memp = (struct memp *)mem_malloc(MEMP_SIZE + MEMP_ALIGN_SIZE(desc->size));
  229. SYS_ARCH_PROTECT(old_level);
  230. #else /* MEMP_MEM_MALLOC */
  231. SYS_ARCH_PROTECT(old_level);
  232. memp = *desc->tab;
  233. #endif /* MEMP_MEM_MALLOC */
  234. if (memp != NULL) {
  235. #if !MEMP_MEM_MALLOC
  236. #if MEMP_OVERFLOW_CHECK == 1
  237. memp_overflow_check_element(memp, desc);
  238. #endif /* MEMP_OVERFLOW_CHECK */
  239. *desc->tab = memp->next;
  240. #if MEMP_OVERFLOW_CHECK
  241. memp->next = NULL;
  242. #endif /* MEMP_OVERFLOW_CHECK */
  243. #endif /* !MEMP_MEM_MALLOC */
  244. #if MEMP_OVERFLOW_CHECK
  245. memp->file = file;
  246. memp->line = line;
  247. #if MEMP_MEM_MALLOC
  248. memp_overflow_init_element(memp, desc);
  249. #endif /* MEMP_MEM_MALLOC */
  250. #endif /* MEMP_OVERFLOW_CHECK */
  251. LWIP_ASSERT("memp_malloc: memp properly aligned",
  252. ((mem_ptr_t)memp % MEM_ALIGNMENT) == 0);
  253. #if MEMP_STATS
  254. desc->stats->used++;
  255. if (desc->stats->used > desc->stats->max) {
  256. desc->stats->max = desc->stats->used;
  257. }
  258. #endif
  259. SYS_ARCH_UNPROTECT(old_level);
  260. /* cast through u8_t* to get rid of alignment warnings */
  261. return ((u8_t *)memp + MEMP_SIZE);
  262. } else {
  263. #if MEMP_STATS
  264. desc->stats->err++;
  265. #endif
  266. SYS_ARCH_UNPROTECT(old_level);
  267. LWIP_DEBUGF(MEMP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("memp_malloc: out of memory in pool %s\n", desc->desc));
  268. }
  269. return NULL;
  270. }
  271. /**
  272. * Get an element from a custom pool.
  273. *
  274. * @param desc the pool to get an element from
  275. *
  276. * @return a pointer to the allocated memory or a NULL pointer on error
  277. */
  278. void *
  279. #if !MEMP_OVERFLOW_CHECK
  280. memp_malloc_pool(const struct memp_desc *desc)
  281. #else
  282. memp_malloc_pool_fn(const struct memp_desc *desc, const char *file, const int line)
  283. #endif
  284. {
  285. LWIP_ASSERT("invalid pool desc", desc != NULL);
  286. if (desc == NULL) {
  287. return NULL;
  288. }
  289. #if !MEMP_OVERFLOW_CHECK
  290. return do_memp_malloc_pool(desc);
  291. #else
  292. return do_memp_malloc_pool_fn(desc, file, line);
  293. #endif
  294. }
  295. /**
  296. * Get an element from a specific pool.
  297. *
  298. * @param type the pool to get an element from
  299. *
  300. * @return a pointer to the allocated memory or a NULL pointer on error
  301. */
  302. void *
  303. #if !MEMP_OVERFLOW_CHECK
  304. memp_malloc(memp_t type)
  305. #else
  306. memp_malloc_fn(memp_t type, const char *file, const int line)
  307. #endif
  308. {
  309. void *memp;
  310. LWIP_ERROR("memp_malloc: type < MEMP_MAX", (type < MEMP_MAX), return NULL;);
  311. #if MEMP_OVERFLOW_CHECK >= 2
  312. memp_overflow_check_all();
  313. #endif /* MEMP_OVERFLOW_CHECK >= 2 */
  314. #if !MEMP_OVERFLOW_CHECK
  315. memp = do_memp_malloc_pool(memp_pools[type]);
  316. #else
  317. memp = do_memp_malloc_pool_fn(memp_pools[type], file, line);
  318. #endif
  319. return memp;
  320. }
  321. static void
  322. do_memp_free_pool(const struct memp_desc *desc, void *mem)
  323. {
  324. struct memp *memp;
  325. SYS_ARCH_DECL_PROTECT(old_level);
  326. LWIP_ASSERT("memp_free: mem properly aligned",
  327. ((mem_ptr_t)mem % MEM_ALIGNMENT) == 0);
  328. /* cast through void* to get rid of alignment warnings */
  329. memp = (struct memp *)(void *)((u8_t *)mem - MEMP_SIZE);
  330. SYS_ARCH_PROTECT(old_level);
  331. #if MEMP_OVERFLOW_CHECK == 1
  332. memp_overflow_check_element(memp, desc);
  333. #endif /* MEMP_OVERFLOW_CHECK */
  334. #if MEMP_STATS
  335. desc->stats->used--;
  336. #endif
  337. #if MEMP_MEM_MALLOC
  338. LWIP_UNUSED_ARG(desc);
  339. SYS_ARCH_UNPROTECT(old_level);
  340. mem_free(memp);
  341. #else /* MEMP_MEM_MALLOC */
  342. memp->next = *desc->tab;
  343. *desc->tab = memp;
  344. #if MEMP_SANITY_CHECK
  345. LWIP_ASSERT("memp sanity", memp_sanity(desc));
  346. #endif /* MEMP_SANITY_CHECK */
  347. SYS_ARCH_UNPROTECT(old_level);
  348. #endif /* !MEMP_MEM_MALLOC */
  349. }
  350. /**
  351. * Put a custom pool element back into its pool.
  352. *
  353. * @param desc the pool where to put mem
  354. * @param mem the memp element to free
  355. */
  356. void
  357. memp_free_pool(const struct memp_desc *desc, void *mem)
  358. {
  359. LWIP_ASSERT("invalid pool desc", desc != NULL);
  360. if ((desc == NULL) || (mem == NULL)) {
  361. return;
  362. }
  363. do_memp_free_pool(desc, mem);
  364. }
  365. /**
  366. * Put an element back into its pool.
  367. *
  368. * @param type the pool where to put mem
  369. * @param mem the memp element to free
  370. */
  371. void
  372. memp_free(memp_t type, void *mem)
  373. {
  374. #ifdef LWIP_HOOK_MEMP_AVAILABLE
  375. struct memp *old_first;
  376. #endif
  377. LWIP_ERROR("memp_free: type < MEMP_MAX", (type < MEMP_MAX), return;);
  378. if (mem == NULL) {
  379. return;
  380. }
  381. #if MEMP_OVERFLOW_CHECK >= 2
  382. memp_overflow_check_all();
  383. #endif /* MEMP_OVERFLOW_CHECK >= 2 */
  384. #ifdef LWIP_HOOK_MEMP_AVAILABLE
  385. old_first = *memp_pools[type]->tab;
  386. #endif
  387. do_memp_free_pool(memp_pools[type], mem);
  388. #ifdef LWIP_HOOK_MEMP_AVAILABLE
  389. if (old_first == NULL) {
  390. LWIP_HOOK_MEMP_AVAILABLE(type);
  391. }
  392. #endif
  393. }