memp.c 11 KB

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