memp.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  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/tcp.h"
  45. #include "lwip/igmp.h"
  46. #include "lwip/api.h"
  47. #include "lwip/api_msg.h"
  48. #include "lwip/tcpip.h"
  49. #include "lwip/sys.h"
  50. #include "lwip/stats.h"
  51. #include "netif/etharp.h"
  52. #include "lwip/ip_frag.h"
  53. #include <string.h>
  54. #if !MEMP_MEM_MALLOC /* don't build if not configured for use in lwipopts.h */
  55. struct memp {
  56. struct memp *next;
  57. #if MEMP_OVERFLOW_CHECK
  58. const char *file;
  59. int line;
  60. #endif /* MEMP_OVERFLOW_CHECK */
  61. };
  62. #if MEMP_OVERFLOW_CHECK
  63. /* if MEMP_OVERFLOW_CHECK is turned on, we reserve some bytes at the beginning
  64. * and at the end of each element, initialize them as 0xcd and check
  65. * them later. */
  66. /* If MEMP_OVERFLOW_CHECK is >= 2, on every call to memp_malloc or memp_free,
  67. * every single element in each pool is checked!
  68. * This is VERY SLOW but also very helpful. */
  69. /* MEMP_SANITY_REGION_BEFORE and MEMP_SANITY_REGION_AFTER can be overridden in
  70. * lwipopts.h to change the amount reserved for checking. */
  71. #ifndef MEMP_SANITY_REGION_BEFORE
  72. #define MEMP_SANITY_REGION_BEFORE 16
  73. #endif /* MEMP_SANITY_REGION_BEFORE*/
  74. #if MEMP_SANITY_REGION_BEFORE > 0
  75. #define MEMP_SANITY_REGION_BEFORE_ALIGNED LWIP_MEM_ALIGN_SIZE(MEMP_SANITY_REGION_BEFORE)
  76. #else
  77. #define MEMP_SANITY_REGION_BEFORE_ALIGNED 0
  78. #endif /* MEMP_SANITY_REGION_BEFORE*/
  79. #ifndef MEMP_SANITY_REGION_AFTER
  80. #define MEMP_SANITY_REGION_AFTER 16
  81. #endif /* MEMP_SANITY_REGION_AFTER*/
  82. #if MEMP_SANITY_REGION_AFTER > 0
  83. #define MEMP_SANITY_REGION_AFTER_ALIGNED LWIP_MEM_ALIGN_SIZE(MEMP_SANITY_REGION_AFTER)
  84. #else
  85. #define MEMP_SANITY_REGION_AFTER_ALIGNED 0
  86. #endif /* MEMP_SANITY_REGION_AFTER*/
  87. /* MEMP_SIZE: save space for struct memp and for sanity check */
  88. #define MEMP_SIZE (LWIP_MEM_ALIGN_SIZE(sizeof(struct memp)) + MEMP_SANITY_REGION_BEFORE_ALIGNED)
  89. #define MEMP_ALIGN_SIZE(x) (LWIP_MEM_ALIGN_SIZE(x) + MEMP_SANITY_REGION_AFTER_ALIGNED)
  90. #else /* MEMP_OVERFLOW_CHECK */
  91. /* No sanity checks
  92. * We don't need to preserve the struct memp while not allocated, so we
  93. * can save a little space and set MEMP_SIZE to 0.
  94. */
  95. #define MEMP_SIZE 0
  96. #define MEMP_ALIGN_SIZE(x) (LWIP_MEM_ALIGN_SIZE(x))
  97. #endif /* MEMP_OVERFLOW_CHECK */
  98. /** This array holds the first free element of each pool.
  99. * Elements form a linked list. */
  100. static struct memp *memp_tab[MEMP_MAX];
  101. #else /* MEMP_MEM_MALLOC */
  102. #define MEMP_ALIGN_SIZE(x) (LWIP_MEM_ALIGN_SIZE(x))
  103. #endif /* MEMP_MEM_MALLOC */
  104. /** This array holds the element sizes of each pool. */
  105. #if !MEM_USE_POOLS && !MEMP_MEM_MALLOC
  106. static
  107. #endif
  108. const u16_t memp_sizes[MEMP_MAX] = {
  109. #define LWIP_MEMPOOL(name,num,size,desc) LWIP_MEM_ALIGN_SIZE(size),
  110. #include "lwip/memp_std.h"
  111. };
  112. /** This array holds a textual description of each pool. */
  113. #ifdef LWIP_DEBUG
  114. static const char *memp_desc[MEMP_MAX] = {
  115. #define LWIP_MEMPOOL(name,num,size,desc) (desc),
  116. #include "lwip/memp_std.h"
  117. };
  118. #endif /* LWIP_DEBUG */
  119. #if !MEMP_MEM_MALLOC /* don't build if not configured for use in lwipopts.h */
  120. /** This array holds the number of elements in each pool. */
  121. static const u16_t memp_num[MEMP_MAX] = {
  122. #define LWIP_MEMPOOL(name,num,size,desc) (num),
  123. #include "lwip/memp_std.h"
  124. };
  125. /** This is the actual memory used by the pools. */
  126. static u8_t memp_memory[MEM_ALIGNMENT - 1
  127. #define LWIP_MEMPOOL(name,num,size,desc) + ( (num) * (MEMP_SIZE + MEMP_ALIGN_SIZE(size) ) )
  128. #include "lwip/memp_std.h"
  129. ];
  130. #if MEMP_SANITY_CHECK
  131. /**
  132. * Check that memp-lists don't form a circle
  133. */
  134. static int
  135. memp_sanity(void)
  136. {
  137. s16_t i, c;
  138. struct memp *m, *n;
  139. for (i = 0; i < MEMP_MAX; i++) {
  140. for (m = memp_tab[i]; m != NULL; m = m->next) {
  141. c = 1;
  142. for (n = memp_tab[i]; n != NULL; n = n->next) {
  143. if (n == m && --c < 0) {
  144. return 0;
  145. }
  146. }
  147. }
  148. }
  149. return 1;
  150. }
  151. #endif /* MEMP_SANITY_CHECK*/
  152. #if MEMP_OVERFLOW_CHECK
  153. /**
  154. * Check if a memp element was victim of an overflow
  155. * (e.g. the restricted area after it has been altered)
  156. *
  157. * @param p the memp element to check
  158. * @param memp_size the element size of the pool p comes from
  159. */
  160. static void
  161. memp_overflow_check_element(struct memp *p, u16_t memp_size)
  162. {
  163. u16_t k;
  164. u8_t *m;
  165. #if MEMP_SANITY_REGION_BEFORE_ALIGNED > 0
  166. m = (u8_t*)p + MEMP_SIZE - MEMP_SANITY_REGION_BEFORE_ALIGNED;
  167. for (k = 0; k < MEMP_SANITY_REGION_BEFORE_ALIGNED; k++) {
  168. if (m[k] != 0xcd) {
  169. LWIP_ASSERT("detected memp underflow!", 0);
  170. }
  171. }
  172. #endif
  173. #if MEMP_SANITY_REGION_AFTER_ALIGNED > 0
  174. m = (u8_t*)p + MEMP_SIZE + memp_size;
  175. for (k = 0; k < MEMP_SANITY_REGION_AFTER_ALIGNED; k++) {
  176. if (m[k] != 0xcd) {
  177. LWIP_ASSERT("detected memp overflow!", 0);
  178. }
  179. }
  180. #endif
  181. }
  182. /**
  183. * Do an overflow check for all elements in every pool.
  184. *
  185. * @see memp_overflow_check_element for a description of the check
  186. */
  187. static void
  188. memp_overflow_check_all(void)
  189. {
  190. u16_t i, j;
  191. struct memp *p;
  192. p = LWIP_MEM_ALIGN(memp_memory);
  193. for (i = 0; i < MEMP_MAX; ++i) {
  194. p = p;
  195. for (j = 0; j < memp_num[i]; ++j) {
  196. memp_overflow_check_element(p, memp_sizes[i]);
  197. p = (struct memp*)((u8_t*)p + MEMP_SIZE + memp_sizes[i] + MEMP_SANITY_REGION_AFTER_ALIGNED);
  198. }
  199. }
  200. }
  201. /**
  202. * Initialize the restricted areas of all memp elements in every pool.
  203. */
  204. static void
  205. memp_overflow_init(void)
  206. {
  207. u16_t i, j;
  208. struct memp *p;
  209. u8_t *m;
  210. p = LWIP_MEM_ALIGN(memp_memory);
  211. for (i = 0; i < MEMP_MAX; ++i) {
  212. p = p;
  213. for (j = 0; j < memp_num[i]; ++j) {
  214. #if MEMP_SANITY_REGION_BEFORE_ALIGNED > 0
  215. m = (u8_t*)p + MEMP_SIZE - MEMP_SANITY_REGION_BEFORE_ALIGNED;
  216. memset(m, 0xcd, MEMP_SANITY_REGION_BEFORE_ALIGNED);
  217. #endif
  218. #if MEMP_SANITY_REGION_AFTER_ALIGNED > 0
  219. m = (u8_t*)p + MEMP_SIZE + memp_sizes[i];
  220. memset(m, 0xcd, MEMP_SANITY_REGION_AFTER_ALIGNED);
  221. #endif
  222. p = (struct memp*)((u8_t*)p + MEMP_SIZE + memp_sizes[i] + MEMP_SANITY_REGION_AFTER_ALIGNED);
  223. }
  224. }
  225. }
  226. #endif /* MEMP_OVERFLOW_CHECK */
  227. /**
  228. * Initialize this module.
  229. *
  230. * Carves out memp_memory into linked lists for each pool-type.
  231. */
  232. void
  233. memp_init(void)
  234. {
  235. struct memp *memp;
  236. u16_t i, j;
  237. for (i = 0; i < MEMP_MAX; ++i) {
  238. MEMP_STATS_AVAIL(used, i, 0);
  239. MEMP_STATS_AVAIL(max, i, 0);
  240. MEMP_STATS_AVAIL(err, i, 0);
  241. MEMP_STATS_AVAIL(avail, i, memp_num[i]);
  242. }
  243. memp = LWIP_MEM_ALIGN(memp_memory);
  244. /* for every pool: */
  245. for (i = 0; i < MEMP_MAX; ++i) {
  246. memp_tab[i] = NULL;
  247. /* create a linked list of memp elements */
  248. for (j = 0; j < memp_num[i]; ++j) {
  249. memp->next = memp_tab[i];
  250. memp_tab[i] = memp;
  251. memp = (struct memp *)((u8_t *)memp + MEMP_SIZE + memp_sizes[i]
  252. #if MEMP_OVERFLOW_CHECK
  253. + MEMP_SANITY_REGION_AFTER_ALIGNED
  254. #endif
  255. );
  256. }
  257. }
  258. #if MEMP_OVERFLOW_CHECK
  259. memp_overflow_init();
  260. /* check everything a first time to see if it worked */
  261. memp_overflow_check_all();
  262. #endif /* MEMP_OVERFLOW_CHECK */
  263. }
  264. #else
  265. /* fix time-wait tcp pcb issue */
  266. static rt_uint16_t tcp_pcbs = 0;
  267. #endif /* MEMP_MEM_MALLOC */
  268. /**
  269. * Get an element from a specific pool.
  270. *
  271. * @param type the pool to get an element from
  272. *
  273. * the debug version has two more parameters:
  274. * @param file file name calling this function
  275. * @param line number of line where this function is called
  276. *
  277. * @return a pointer to the allocated memory or a NULL pointer on error
  278. */
  279. void *
  280. #if !MEMP_OVERFLOW_CHECK
  281. memp_malloc(memp_t type)
  282. #else
  283. memp_malloc_fn(memp_t type, const char* file, const int line)
  284. #endif
  285. {
  286. #if !MEMP_MEM_MALLOC
  287. struct memp *memp;
  288. SYS_ARCH_DECL_PROTECT(old_level);
  289. LWIP_ERROR("memp_malloc: type < MEMP_MAX", (type < MEMP_MAX), return NULL;);
  290. SYS_ARCH_PROTECT(old_level);
  291. #if MEMP_OVERFLOW_CHECK >= 2
  292. memp_overflow_check_all();
  293. #endif /* MEMP_OVERFLOW_CHECK >= 2 */
  294. memp = memp_tab[type];
  295. if (memp != NULL) {
  296. memp_tab[type] = memp->next;
  297. #if MEMP_OVERFLOW_CHECK
  298. memp->next = NULL;
  299. memp->file = file;
  300. memp->line = line;
  301. #endif /* MEMP_OVERFLOW_CHECK */
  302. MEMP_STATS_INC_USED(used, type);
  303. LWIP_ASSERT("memp_malloc: memp properly aligned",
  304. ((mem_ptr_t)memp % MEM_ALIGNMENT) == 0);
  305. memp = (struct memp*)((u8_t*)memp + MEMP_SIZE);
  306. } else {
  307. LWIP_DEBUGF(MEMP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("memp_malloc: out of memory in pool %s\n", memp_desc[type]));
  308. MEMP_STATS_INC(err, type);
  309. }
  310. SYS_ARCH_UNPROTECT(old_level);
  311. return memp;
  312. #else
  313. void* ptr;
  314. rt_uint32_t size;
  315. SYS_ARCH_DECL_PROTECT(old_level);
  316. size = memp_sizes[type];
  317. LWIP_DEBUGF(MEMP_DEBUG, ("memp malloc %s, size %d, ", memp_desc[type], memp_sizes[type]));
  318. SYS_ARCH_PROTECT(old_level);
  319. if (type == MEMP_TCP_PCB)
  320. {
  321. if (tcp_pcbs >= MEMP_NUM_TCP_PCB)
  322. {
  323. SYS_ARCH_UNPROTECT(old_level);
  324. return NULL;
  325. }
  326. else
  327. {
  328. /* increased tcp pcb allocated number */
  329. tcp_pcbs ++;
  330. }
  331. }
  332. SYS_ARCH_UNPROTECT(old_level);
  333. ptr = mem_malloc(size);
  334. LWIP_DEBUGF(MEMP_DEBUG, ("mem 0x%x\n", ptr));
  335. return ptr;
  336. #endif /* MEMP_MEM_MALLOC */
  337. }
  338. /**
  339. * Put an element back into its pool.
  340. *
  341. * @param type the pool where to put mem
  342. * @param mem the memp element to free
  343. */
  344. void
  345. memp_free(memp_t type, void *mem)
  346. {
  347. #if !MEMP_MEM_MALLOC
  348. struct memp *memp;
  349. SYS_ARCH_DECL_PROTECT(old_level);
  350. if (mem == NULL) {
  351. return;
  352. }
  353. LWIP_ASSERT("memp_free: mem properly aligned",
  354. ((mem_ptr_t)mem % MEM_ALIGNMENT) == 0);
  355. memp = (struct memp *)((u8_t*)mem - MEMP_SIZE);
  356. SYS_ARCH_PROTECT(old_level);
  357. #if MEMP_OVERFLOW_CHECK
  358. #if MEMP_OVERFLOW_CHECK >= 2
  359. memp_overflow_check_all();
  360. #else
  361. memp_overflow_check_element(memp, memp_sizes[type]);
  362. #endif /* MEMP_OVERFLOW_CHECK >= 2 */
  363. #endif /* MEMP_OVERFLOW_CHECK */
  364. MEMP_STATS_DEC(used, type);
  365. memp->next = memp_tab[type];
  366. memp_tab[type] = memp;
  367. #if MEMP_SANITY_CHECK
  368. LWIP_ASSERT("memp sanity", memp_sanity());
  369. #endif /* MEMP_SANITY_CHECK */
  370. SYS_ARCH_UNPROTECT(old_level);
  371. #else
  372. SYS_ARCH_DECL_PROTECT(old_level);
  373. SYS_ARCH_PROTECT(old_level);
  374. if (type == MEMP_TCP_PCB && mem)
  375. {
  376. tcp_pcbs --;
  377. }
  378. SYS_ARCH_UNPROTECT(old_level);
  379. LWIP_DEBUGF(MEMP_DEBUG, ("memp free %s, mem 0x%x\n", memp_desc[type], mem));
  380. /* release this memory */
  381. mem_free(mem);
  382. #endif /* MEMP_MEM_MALLOC */
  383. }