lib_mem.c 81 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825
  1. /*
  2. *********************************************************************************************************
  3. * uC/LIB
  4. * CUSTOM LIBRARY MODULES
  5. *
  6. * (c) Copyright 2004-2011; Micrium, Inc.; Weston, FL
  7. *
  8. * All rights reserved. Protected by international copyright laws.
  9. *
  10. * uC/LIB is provided in source form to registered licensees ONLY. It is
  11. * illegal to distribute this source code to any third party unless you receive
  12. * written permission by an authorized Micrium representative. Knowledge of
  13. * the source code may NOT be used to develop a similar product.
  14. *
  15. * Please help us continue to provide the Embedded community with the finest
  16. * software available. Your honesty is greatly appreciated.
  17. *
  18. * You can contact us at www.micrium.com.
  19. *********************************************************************************************************
  20. */
  21. /*
  22. *********************************************************************************************************
  23. *
  24. * STANDARD MEMORY OPERATIONS
  25. *
  26. * Filename : lib_mem.c
  27. * Version : V1.35.00
  28. * Programmer(s) : ITJ
  29. * FGK
  30. *********************************************************************************************************
  31. * Note(s) : (1) NO compiler-supplied standard library functions are used in library or product software.
  32. *
  33. * (a) ALL standard library functions are implemented in the custom library modules :
  34. *
  35. * (1) \<Custom Library Directory>\lib_*.*
  36. *
  37. * (2) \<Custom Library Directory>\Ports\<cpu>\<compiler>\lib*_a.*
  38. *
  39. * where
  40. * <Custom Library Directory> directory path for custom library software
  41. * <cpu> directory name for specific processor (CPU)
  42. * <compiler> directory name for specific compiler
  43. *
  44. * (b) Product-specific library functions are implemented in individual products.
  45. *********************************************************************************************************
  46. */
  47. /*
  48. *********************************************************************************************************
  49. * INCLUDE FILES
  50. *********************************************************************************************************
  51. */
  52. #define LIB_MEM_MODULE
  53. #include <lib_mem.h>
  54. /*$PAGE*/
  55. /*
  56. *********************************************************************************************************
  57. * LOCAL DEFINES
  58. *********************************************************************************************************
  59. */
  60. /*
  61. *********************************************************************************************************
  62. * LOCAL CONSTANTS
  63. *********************************************************************************************************
  64. */
  65. /*
  66. *********************************************************************************************************
  67. * LOCAL DATA TYPES
  68. *********************************************************************************************************
  69. */
  70. /*
  71. *********************************************************************************************************
  72. * LOCAL TABLES
  73. *********************************************************************************************************
  74. */
  75. /*
  76. *********************************************************************************************************
  77. * LOCAL GLOBAL VARIABLES
  78. *********************************************************************************************************
  79. */
  80. #if (LIB_MEM_CFG_ALLOC_EN == DEF_ENABLED)
  81. MEM_POOL *Mem_PoolTbl; /* Mem pool/seg tbl. */
  82. MEM_POOL Mem_PoolHeap; /* Mem heap pool/seg. */
  83. #ifndef LIB_MEM_CFG_HEAP_BASE_ADDR
  84. CPU_INT08U Mem_Heap[LIB_MEM_CFG_HEAP_SIZE]; /* Mem heap. */
  85. #endif
  86. #endif
  87. /*
  88. *********************************************************************************************************
  89. * LOCAL FUNCTION PROTOTYPES
  90. *********************************************************************************************************
  91. */
  92. #if (LIB_MEM_CFG_ALLOC_EN == DEF_ENABLED) /* -------------- MEM POOL FNCTS -------------- */
  93. #if (LIB_MEM_CFG_ARG_CHK_EXT_EN == DEF_ENABLED)
  94. static CPU_BOOLEAN Mem_PoolBlkIsValidAddr(MEM_POOL *pmem_pool,
  95. void *pmem_blk);
  96. #endif
  97. static CPU_SIZE_T Mem_PoolSegCalcTotSize(void *pmem_addr,
  98. CPU_SIZE_T blk_nbr,
  99. CPU_SIZE_T blk_size,
  100. CPU_SIZE_T blk_align);
  101. static void *Mem_PoolSegAlloc (MEM_POOL *pmem_pool,
  102. CPU_SIZE_T size,
  103. CPU_SIZE_T align);
  104. #endif
  105. /*
  106. *********************************************************************************************************
  107. * LOCAL CONFIGURATION ERRORS
  108. *********************************************************************************************************
  109. */
  110. /*$PAGE*/
  111. /*
  112. *********************************************************************************************************
  113. * Mem_Init()
  114. *
  115. * Description : (1) Initialize Memory Management Module :
  116. *
  117. * (a) Initialize heap memory pool
  118. * (b) Initialize memory pool table
  119. *
  120. *
  121. * Argument(s) : none.
  122. *
  123. * Return(s) : none.
  124. *
  125. * Caller(s) : Application.
  126. *
  127. * Note(s) : none.
  128. *********************************************************************************************************
  129. */
  130. void Mem_Init (void)
  131. {
  132. #if (LIB_MEM_CFG_ALLOC_EN == DEF_ENABLED)
  133. MEM_POOL *pmem_pool;
  134. /* --------- INIT MEM HEAP SEG / POOL --------- */
  135. pmem_pool = (MEM_POOL *)&Mem_PoolHeap;
  136. pmem_pool->Type = (LIB_MEM_TYPE) LIB_MEM_TYPE_HEAP;
  137. pmem_pool->SegPrevPtr = (MEM_POOL *) 0;
  138. pmem_pool->SegNextPtr = (MEM_POOL *) 0;
  139. pmem_pool->PoolPrevPtr = (MEM_POOL *) 0;
  140. pmem_pool->PoolNextPtr = (MEM_POOL *) 0;
  141. pmem_pool->PoolAddrStart = (void *) 0;
  142. pmem_pool->PoolAddrEnd = (void *) 0;
  143. pmem_pool->PoolPtrs = (void **) 0;
  144. pmem_pool->BlkSize = (CPU_SIZE_T ) 0u;
  145. pmem_pool->BlkNbr = (CPU_SIZE_T ) 0u;
  146. pmem_pool->BlkIx = (MEM_POOL_IX ) 0u;
  147. #ifdef LIB_MEM_CFG_HEAP_BASE_ADDR
  148. pmem_pool->SegAddr = (void *) LIB_MEM_CFG_HEAP_BASE_ADDR;
  149. pmem_pool->SegAddrNextAvail = (void *) LIB_MEM_CFG_HEAP_BASE_ADDR;
  150. #else
  151. pmem_pool->SegAddr = (void *)&Mem_Heap[0];
  152. pmem_pool->SegAddrNextAvail = (void *)&Mem_Heap[0];
  153. #endif
  154. pmem_pool->SegSizeTot = (CPU_SIZE_T ) LIB_MEM_CFG_HEAP_SIZE;
  155. pmem_pool->SegSizeRem = (CPU_SIZE_T ) LIB_MEM_CFG_HEAP_SIZE;
  156. /* ------------ INIT MEM POOL TBL ------------- */
  157. Mem_PoolTbl = &Mem_PoolHeap;
  158. #endif
  159. }
  160. /*$PAGE*/
  161. /*
  162. *********************************************************************************************************
  163. * Mem_Clr()
  164. *
  165. * Description : Clear data buffer (see Note #2).
  166. *
  167. * Argument(s) : pmem Pointer to memory buffer to clear.
  168. *
  169. * size Number of data buffer octets to clear (see Note #1).
  170. *
  171. * Return(s) : none.
  172. *
  173. * Caller(s) : Application.
  174. *
  175. * Note(s) : (1) Null clears allowed (i.e. zero-length clears).
  176. *
  177. * See also 'Mem_Set() Note #1'.
  178. *
  179. * (2) Clear data by setting each data octet to 0.
  180. *********************************************************************************************************
  181. */
  182. void Mem_Clr (void *pmem,
  183. CPU_SIZE_T size)
  184. {
  185. Mem_Set(pmem,
  186. 0u, /* See Note #2. */
  187. size);
  188. }
  189. /*$PAGE*/
  190. /*
  191. *********************************************************************************************************
  192. * Mem_Set()
  193. *
  194. * Description : Fill data buffer with specified data octet.
  195. *
  196. * Argument(s) : pmem Pointer to memory buffer to fill with specified data octet.
  197. *
  198. * data_val Data fill octet value.
  199. *
  200. * size Number of data buffer octets to fill (see Note #1).
  201. *
  202. * Return(s) : none.
  203. *
  204. * Caller(s) : Application.
  205. *
  206. * Note(s) : (1) Null sets allowed (i.e. zero-length sets).
  207. *
  208. * (2) For best CPU performance, optimized to fill data buffer using 'CPU_ALIGN'-sized data
  209. * words.
  210. *
  211. * (a) Since many word-aligned processors REQUIRE that multi-octet words be accessed on
  212. * word-aligned addresses, 'CPU_ALIGN'-sized words MUST be accessed on 'CPU_ALIGN'd
  213. * addresses.
  214. *
  215. * (3) Modulo arithmetic is used to determine whether a memory buffer starts on a 'CPU_ALIGN'
  216. * address boundary.
  217. *
  218. * Modulo arithmetic in ANSI-C REQUIREs operations performed on integer values. Thus,
  219. * address values MUST be cast to an appropriately-sized integer value PRIOR to any
  220. * 'mem_align_mod' arithmetic operation.
  221. *********************************************************************************************************
  222. */
  223. void Mem_Set (void *pmem,
  224. CPU_INT08U data_val,
  225. CPU_SIZE_T size)
  226. {
  227. CPU_SIZE_T size_rem;
  228. CPU_ALIGN data_align;
  229. CPU_ALIGN *pmem_align;
  230. CPU_INT08U *pmem_08;
  231. CPU_DATA mem_align_mod;
  232. CPU_DATA i;
  233. if (size < 1) { /* See Note #1. */
  234. return;
  235. }
  236. if (pmem == (void *)0) {
  237. return;
  238. }
  239. data_align = 0u;
  240. for (i = 0u; i < sizeof(CPU_ALIGN); i++) { /* Fill each data_align octet with data val. */
  241. data_align <<= DEF_OCTET_NBR_BITS;
  242. data_align |= (CPU_ALIGN)data_val;
  243. }
  244. size_rem = size;
  245. mem_align_mod = (CPU_INT08U)((CPU_ADDR)pmem % sizeof(CPU_ALIGN)); /* See Note #3. */
  246. pmem_08 = (CPU_INT08U *)pmem;
  247. if (mem_align_mod != 0u) { /* If leading octets avail, ... */
  248. i = mem_align_mod;
  249. while ((size_rem > 0) && /* ... start mem buf fill with leading octets ... */
  250. (i < sizeof(CPU_ALIGN ))) { /* ... until next CPU_ALIGN word boundary. */
  251. *pmem_08++ = data_val;
  252. size_rem -= sizeof(CPU_INT08U);
  253. i++;
  254. }
  255. }
  256. pmem_align = (CPU_ALIGN *)pmem_08; /* See Note #2a. */
  257. while (size_rem >= sizeof(CPU_ALIGN)) { /* While mem buf aligned on CPU_ALIGN word boundaries, */
  258. *pmem_align++ = data_align; /* ... fill mem buf with CPU_ALIGN-sized data. */
  259. size_rem -= sizeof(CPU_ALIGN);
  260. }
  261. pmem_08 = (CPU_INT08U *)pmem_align;
  262. while (size_rem > 0) { /* Finish mem buf fill with trailing octets. */
  263. *pmem_08++ = data_val;
  264. size_rem -= sizeof(CPU_INT08U);
  265. }
  266. }
  267. /*$PAGE*/
  268. /*
  269. *********************************************************************************************************
  270. * Mem_Copy()
  271. *
  272. * Description : Copy data octets from one memory buffer to another memory buffer.
  273. *
  274. * Argument(s) : pdest Pointer to destination memory buffer.
  275. *
  276. * psrc Pointer to source memory buffer.
  277. *
  278. * size Number of data buffer octets to copy (see Note #1).
  279. *
  280. * Return(s) : none.
  281. *
  282. * Caller(s) : Application.
  283. *
  284. * Note(s) : (1) Null copies allowed (i.e. zero-length copies).
  285. *
  286. * (2) Memory buffers NOT checked for overlapping.
  287. *
  288. * (a) IEEE Std 1003.1, 2004 Edition, Section 'memcpy() : DESCRIPTION' states that "if
  289. * copying takes place between objects that overlap, the behavior is undefined".
  290. *
  291. * (b) However, data octets from a source memory buffer at a higher address value SHOULD
  292. * successfully copy to a destination memory buffer at a lower address value even
  293. * if any octets of the memory buffers overlap as long as no individual, atomic CPU
  294. * word copy overlaps.
  295. *
  296. * Since Mem_Copy() performs the data octet copy via 'CPU_ALIGN'-sized words &/or
  297. * octets; & since 'CPU_ALIGN'-sized words MUST be accessed on word-aligned addresses
  298. * (see Note #3b), neither 'CPU_ALIGN'-sized words nor octets at unique addresses can
  299. * ever overlap.
  300. *
  301. * Therefore, Mem_Copy() SHOULD be able to successfully copy overlapping memory
  302. * buffers as long as the source memory buffer is at a higher address value than the
  303. * destination memory buffer.
  304. *
  305. * (3) For best CPU performance, optimized to copy data buffer using 'CPU_ALIGN'-sized data
  306. * words.
  307. *
  308. * (a) Since many word-aligned processors REQUIRE that multi-octet words be accessed on
  309. * word-aligned addresses, 'CPU_ALIGN'-sized words MUST be accessed on 'CPU_ALIGN'd
  310. * addresses.
  311. *
  312. * (4) Modulo arithmetic is used to determine whether a memory buffer starts on a 'CPU_ALIGN'
  313. * address boundary.
  314. *
  315. * Modulo arithmetic in ANSI-C REQUIREs operations performed on integer values. Thus,
  316. * address values MUST be cast to an appropriately-sized integer value PRIOR to any
  317. * 'mem_align_mod' arithmetic operation.
  318. *********************************************************************************************************
  319. */
  320. /*$PAGE*/
  321. #if (LIB_MEM_CFG_OPTIMIZE_ASM_EN != DEF_ENABLED)
  322. void Mem_Copy ( void *pdest,
  323. const void *psrc,
  324. CPU_SIZE_T size)
  325. {
  326. CPU_SIZE_T size_rem;
  327. CPU_ALIGN *pmem_align_dest;
  328. const CPU_ALIGN *pmem_align_src;
  329. CPU_INT08U *pmem_08_dest;
  330. const CPU_INT08U *pmem_08_src;
  331. CPU_DATA i;
  332. CPU_DATA mem_align_mod_dest;
  333. CPU_DATA mem_align_mod_src;
  334. CPU_BOOLEAN mem_aligned;
  335. if (size < 1) { /* See Note #1. */
  336. return;
  337. }
  338. if (pdest == (void *)0) {
  339. return;
  340. }
  341. if (psrc == (void *)0) {
  342. return;
  343. }
  344. size_rem = size;
  345. pmem_08_dest = ( CPU_INT08U *)pdest;
  346. pmem_08_src = (const CPU_INT08U *)psrc;
  347. /* See Note #4. */
  348. mem_align_mod_dest = (CPU_INT08U)((CPU_ADDR)pmem_08_dest % sizeof(CPU_ALIGN));
  349. mem_align_mod_src = (CPU_INT08U)((CPU_ADDR)pmem_08_src % sizeof(CPU_ALIGN));
  350. mem_aligned = (mem_align_mod_dest == mem_align_mod_src) ? DEF_YES : DEF_NO;
  351. if (mem_aligned == DEF_YES) { /* If mem bufs' alignment offset equal, ... */
  352. /* ... optimize copy for mem buf alignment. */
  353. if (mem_align_mod_dest != 0u) { /* If leading octets avail, ... */
  354. i = mem_align_mod_dest;
  355. while ((size_rem > 0) && /* ... start mem buf copy with leading octets ... */
  356. (i < sizeof(CPU_ALIGN ))) { /* ... until next CPU_ALIGN word boundary. */
  357. *pmem_08_dest++ = *pmem_08_src++;
  358. size_rem -= sizeof(CPU_INT08U);
  359. i++;
  360. }
  361. }
  362. pmem_align_dest = ( CPU_ALIGN *)pmem_08_dest; /* See Note #3a. */
  363. pmem_align_src = (const CPU_ALIGN *)pmem_08_src;
  364. while (size_rem >= sizeof(CPU_ALIGN)) { /* While mem bufs aligned on CPU_ALIGN word boundaries, */
  365. *pmem_align_dest++ = *pmem_align_src++; /* ... copy psrc to pdest with CPU_ALIGN-sized words. */
  366. size_rem -= sizeof(CPU_ALIGN);
  367. }
  368. pmem_08_dest = ( CPU_INT08U *)pmem_align_dest;
  369. pmem_08_src = (const CPU_INT08U *)pmem_align_src;
  370. }
  371. while (size_rem > 0) { /* For unaligned mem bufs or trailing octets, ... */
  372. *pmem_08_dest++ = *pmem_08_src++; /* ... copy psrc to pdest by octets. */
  373. size_rem -= sizeof(CPU_INT08U);
  374. }
  375. }
  376. #endif
  377. /*$PAGE*/
  378. /*
  379. *********************************************************************************************************
  380. * Mem_Cmp()
  381. *
  382. * Description : Verify that ALL data octets in two memory buffers are identical in sequence.
  383. *
  384. * Argument(s) : p1_mem Pointer to first memory buffer.
  385. *
  386. * p2_mem Pointer to second memory buffer.
  387. *
  388. * size Number of data buffer octets to compare (see Note #1).
  389. *
  390. * Return(s) : DEF_YES, if 'size' number of data octets are identical in both memory buffers.
  391. *
  392. * DEF_NO, otherwise.
  393. *
  394. * Caller(s) : Application.
  395. *
  396. * Note(s) : (1) Null compares allowed (i.e. zero-length compares); 'DEF_YES' returned to indicate
  397. * identical null compare.
  398. *
  399. * (2) Many memory buffer comparisons vary ONLY in the least significant octets -- e.g.
  400. * network address buffers. Consequently, memory buffer comparison is more efficient
  401. * if the comparison starts from the end of the memory buffers which will abort sooner
  402. * on dissimilar memory buffers that vary only in the least significant octets.
  403. *
  404. * (3) For best CPU performance, optimized to compare data buffers using 'CPU_ALIGN'-sized
  405. * data words.
  406. *
  407. * (a) Since many word-aligned processors REQUIRE that multi-octet words be accessed on
  408. * word-aligned addresses, 'CPU_ALIGN'-sized words MUST be accessed on 'CPU_ALIGN'd
  409. * addresses.
  410. *
  411. * (4) Modulo arithmetic is used to determine whether a memory buffer starts on a 'CPU_ALIGN'
  412. * address boundary.
  413. *
  414. * Modulo arithmetic in ANSI-C REQUIREs operations performed on integer values. Thus,
  415. * address values MUST be cast to an appropriately-sized integer value PRIOR to any
  416. * 'mem_align_mod' arithmetic operation.
  417. ********************************************************************************************************
  418. */
  419. /*$PAGE*/
  420. CPU_BOOLEAN Mem_Cmp (const void *p1_mem,
  421. const void *p2_mem,
  422. CPU_SIZE_T size)
  423. {
  424. CPU_SIZE_T size_rem;
  425. CPU_ALIGN *p1_mem_align;
  426. CPU_ALIGN *p2_mem_align;
  427. const CPU_INT08U *p1_mem_08;
  428. const CPU_INT08U *p2_mem_08;
  429. CPU_DATA i;
  430. CPU_DATA mem_align_mod_1;
  431. CPU_DATA mem_align_mod_2;
  432. CPU_BOOLEAN mem_aligned;
  433. CPU_BOOLEAN mem_cmp;
  434. if (size < 1) { /* See Note #1. */
  435. return (DEF_YES);
  436. }
  437. if (p1_mem == (void *)0) {
  438. return (DEF_NO);
  439. }
  440. if (p2_mem == (void *)0) {
  441. return (DEF_NO);
  442. }
  443. mem_cmp = DEF_YES; /* Assume mem bufs are identical until cmp fails. */
  444. size_rem = size;
  445. /* Start @ end of mem bufs (see Note #2). */
  446. p1_mem_08 = (const CPU_INT08U *)p1_mem + size;
  447. p2_mem_08 = (const CPU_INT08U *)p2_mem + size;
  448. /* See Note #4. */
  449. mem_align_mod_1 = (CPU_INT08U)((CPU_ADDR)p1_mem_08 % sizeof(CPU_ALIGN));
  450. mem_align_mod_2 = (CPU_INT08U)((CPU_ADDR)p2_mem_08 % sizeof(CPU_ALIGN));
  451. mem_aligned = (mem_align_mod_1 == mem_align_mod_2) ? DEF_YES : DEF_NO;
  452. if (mem_aligned == DEF_YES) { /* If mem bufs' alignment offset equal, ... */
  453. /* ... optimize cmp for mem buf alignment. */
  454. if (mem_align_mod_1 != 0u) { /* If trailing octets avail, ... */
  455. i = mem_align_mod_1;
  456. while ((mem_cmp == DEF_YES) && /* ... cmp mem bufs while identical & ... */
  457. (size_rem > 0) && /* ... start mem buf cmp with trailing octets ... */
  458. (i > 0)) { /* ... until next CPU_ALIGN word boundary. */
  459. p1_mem_08--;
  460. p2_mem_08--;
  461. if (*p1_mem_08 != *p2_mem_08) { /* If ANY data octet(s) NOT identical, cmp fails. */
  462. mem_cmp = DEF_NO;
  463. }
  464. size_rem -= sizeof(CPU_INT08U);
  465. i--;
  466. }
  467. }
  468. if (mem_cmp == DEF_YES) { /* If cmp still identical, cmp aligned mem bufs. */
  469. p1_mem_align = (CPU_ALIGN *)p1_mem_08; /* See Note #3a. */
  470. p2_mem_align = (CPU_ALIGN *)p2_mem_08;
  471. while ((mem_cmp == DEF_YES) && /* Cmp mem bufs while identical & ... */
  472. (size_rem >= sizeof(CPU_ALIGN))) { /* ... mem bufs aligned on CPU_ALIGN word boundaries. */
  473. p1_mem_align--;
  474. p2_mem_align--;
  475. if (*p1_mem_align != *p2_mem_align) { /* If ANY data octet(s) NOT identical, cmp fails. */
  476. mem_cmp = DEF_NO;
  477. }
  478. size_rem -= sizeof(CPU_ALIGN);
  479. }
  480. p1_mem_08 = (CPU_INT08U *)p1_mem_align;
  481. p2_mem_08 = (CPU_INT08U *)p2_mem_align;
  482. }
  483. }
  484. while ((mem_cmp == DEF_YES) && /* Cmp mem bufs while identical ... */
  485. (size_rem > 0)) { /* ... for unaligned mem bufs or trailing octets. */
  486. p1_mem_08--;
  487. p2_mem_08--;
  488. if (*p1_mem_08 != *p2_mem_08) { /* If ANY data octet(s) NOT identical, cmp fails. */
  489. mem_cmp = DEF_NO;
  490. }
  491. size_rem -= sizeof(CPU_INT08U);
  492. }
  493. return (mem_cmp);
  494. }
  495. /*$PAGE*/
  496. /*
  497. *********************************************************************************************************
  498. * Mem_HeapAlloc()
  499. *
  500. * Description : Allocate a memory block from the heap memory pool.
  501. *
  502. * Argument(s) : size Size of memory block to allocate (in octets).
  503. *
  504. * align Alignment of memory block to specific word boundary (in octets).
  505. *
  506. * poctets_reqd Optional pointer to a variable to ... :
  507. *
  508. * (a) Return the number of octets required to successfully
  509. * allocate the memory block, if any error(s);
  510. * (b) Return 0, otherwise.
  511. *
  512. * perr Pointer to variable that will receive the return error code from this function :
  513. *
  514. * LIB_MEM_ERR_NONE Memory block successfully returned.
  515. * LIB_MEM_ERR_INVALID_MEM_SIZE Invalid memory size.
  516. * LIB_MEM_ERR_INVALID_MEM_ALIGN Invalid memory alignment.
  517. * LIB_MEM_ERR_HEAP_EMPTY Heap segment empty; NOT enough available
  518. * memory from heap.
  519. * LIB_MEM_ERR_HEAP_OVF Requested memory overflows heap memory.
  520. *
  521. * Return(s) : Pointer to memory block, if NO error(s).
  522. *
  523. * Pointer to NULL, otherwise.
  524. *
  525. * Caller(s) : Application.
  526. *
  527. * Note(s) : (1) Pointers to variables that return values MUST be initialized PRIOR to all other
  528. * validation or function handling in case of any error(s).
  529. *
  530. * (2) 'pmem_pool' variables MUST ALWAYS be accessed exclusively in critical sections.
  531. *********************************************************************************************************
  532. */
  533. /*$PAGE*/
  534. #if (LIB_MEM_CFG_ALLOC_EN == DEF_ENABLED)
  535. void *Mem_HeapAlloc (CPU_SIZE_T size,
  536. CPU_SIZE_T align,
  537. CPU_SIZE_T *poctets_reqd,
  538. LIB_ERR *perr)
  539. {
  540. MEM_POOL *pmem_pool_heap;
  541. void *pmem_addr;
  542. void *pmem_blk;
  543. CPU_SIZE_T octets_reqd_unused;
  544. CPU_SIZE_T size_rem;
  545. CPU_SIZE_T size_req;
  546. CPU_SR_ALLOC();
  547. #if (LIB_MEM_CFG_ARG_CHK_EXT_EN == DEF_ENABLED) /* ------------- VALIDATE RTN ERR PTR ------------- */
  548. if (perr == (LIB_ERR *)0) {
  549. CPU_SW_EXCEPTION((void *)0);
  550. }
  551. #endif
  552. /* ------------ VALIDATE RTN OCTETS PTR ----------- */
  553. if (poctets_reqd == (CPU_SIZE_T *) 0) { /* If NOT avail, ... */
  554. poctets_reqd = (CPU_SIZE_T *)&octets_reqd_unused; /* ... re-cfg NULL rtn ptr to unused local var. */
  555. (void)&octets_reqd_unused; /* Prevent possible 'variable unused' warning. */
  556. }
  557. *poctets_reqd = 0u; /* Init octets req'd for err (see Note #1). */
  558. #if (LIB_MEM_CFG_ARG_CHK_EXT_EN == DEF_ENABLED) /* ------------ VALIDATE HEAP MEM ALLOC ----------- */
  559. if (size < 1) {
  560. *perr = LIB_MEM_ERR_INVALID_MEM_SIZE;
  561. return ((void *)0);
  562. }
  563. if (align < 1) {
  564. *perr = LIB_MEM_ERR_INVALID_MEM_ALIGN;
  565. return ((void *)0);
  566. }
  567. #endif
  568. /* -------------- ALLOC HEAP MEM BLK -------------- */
  569. pmem_pool_heap = &Mem_PoolHeap;
  570. CPU_CRITICAL_ENTER();
  571. pmem_addr = pmem_pool_heap->SegAddrNextAvail;
  572. size_rem = pmem_pool_heap->SegSizeRem;
  573. size_req = Mem_PoolSegCalcTotSize(pmem_addr,
  574. 1u, /* Calc alloc for single mem blk from heap. */
  575. size,
  576. align);
  577. #if (LIB_MEM_CFG_ARG_CHK_EXT_EN == DEF_ENABLED)
  578. if (size_req < 1) { /* If req'd size ovf, ... */
  579. CPU_CRITICAL_EXIT();
  580. *poctets_reqd = size; /* ... rtn add'l heap size needed. */
  581. *perr = LIB_MEM_ERR_HEAP_OVF;
  582. return ((void *)0);
  583. }
  584. #endif
  585. if (size_req > size_rem) { /* If req'd size > rem heap size, ... */
  586. CPU_CRITICAL_EXIT();
  587. *poctets_reqd = size_req - size_rem; /* ... rtn add'l heap size needed. */
  588. *perr = LIB_MEM_ERR_HEAP_EMPTY;
  589. return ((void *)0);
  590. }
  591. pmem_blk = Mem_PoolSegAlloc(pmem_pool_heap, size, align);
  592. if (pmem_blk == (void *)0) { /* If mem blk NOT avail from heap, ... */
  593. CPU_CRITICAL_EXIT();
  594. *poctets_reqd = size_req; /* ... rtn add'l heap size needed. */
  595. *perr = LIB_MEM_ERR_HEAP_EMPTY;
  596. return ((void *)0);
  597. }
  598. CPU_CRITICAL_EXIT();
  599. *perr = LIB_MEM_ERR_NONE;
  600. return (pmem_blk);
  601. }
  602. #endif
  603. /*$PAGE*/
  604. /*
  605. *********************************************************************************************************
  606. * Mem_PoolClr()
  607. *
  608. * Description : Clear a memory pool (see Note #1).
  609. *
  610. * Argument(s) : pmem_pool Pointer to a memory pool structure to clear (see Note #2).
  611. *
  612. * perr Pointer to variable that will receive the return error code from this function :
  613. *
  614. * LIB_MEM_ERR_NONE Memory pool successfully cleared.
  615. * LIB_MEM_ERR_NULL_PTR Argument 'pmem_pool' passed a NULL pointer.
  616. *
  617. * Return(s) : none.
  618. *
  619. * Caller(s) : Application,
  620. * Mem_PoolCreate().
  621. *
  622. * Note(s) : (1) (a) Mem_PoolClr() ONLY clears a memory pool structure's variables & should ONLY be
  623. * called to initialize a memory pool structure prior to calling Mem_PoolCreate().
  624. *
  625. * (b) Mem_PoolClr() does NOT deallocate memory from the memory pool or deallocate the
  626. * memory pool itself & MUST NOT be called after calling Mem_PoolCreate() since
  627. * this will likely corrupt the memory pool management.
  628. *
  629. * (2) Assumes 'pmem_pool' points to a valid memory pool (if non-NULL).
  630. *********************************************************************************************************
  631. */
  632. #if (LIB_MEM_CFG_ALLOC_EN == DEF_ENABLED)
  633. void Mem_PoolClr (MEM_POOL *pmem_pool,
  634. LIB_ERR *perr)
  635. {
  636. #if (LIB_MEM_CFG_ARG_CHK_EXT_EN == DEF_ENABLED) /* -------------- VALIDATE RTN ERR PTR --------------- */
  637. if (perr == (LIB_ERR *)0) {
  638. CPU_SW_EXCEPTION(;);
  639. }
  640. #endif
  641. /* -------------- VALIDATE MEM POOL PTR --------------- */
  642. if (pmem_pool == (MEM_POOL *)0) {
  643. *perr = LIB_MEM_ERR_NULL_PTR;
  644. return;
  645. }
  646. pmem_pool->Type = (LIB_MEM_TYPE)LIB_MEM_TYPE_NONE;
  647. pmem_pool->SegPrevPtr = (MEM_POOL *)0;
  648. pmem_pool->SegNextPtr = (MEM_POOL *)0;
  649. pmem_pool->PoolPrevPtr = (MEM_POOL *)0;
  650. pmem_pool->PoolNextPtr = (MEM_POOL *)0;
  651. pmem_pool->PoolAddrStart = (void *)0;
  652. pmem_pool->PoolAddrEnd = (void *)0;
  653. pmem_pool->PoolPtrs = (void **)0;
  654. pmem_pool->PoolSize = (CPU_SIZE_T )0u;
  655. pmem_pool->BlkAlign = (CPU_SIZE_T )0u;
  656. pmem_pool->BlkSize = (CPU_SIZE_T )0u;
  657. pmem_pool->BlkNbr = (CPU_SIZE_T )0u;
  658. pmem_pool->BlkIx = (MEM_POOL_IX )0u;
  659. pmem_pool->SegAddr = (void *)0;
  660. pmem_pool->SegAddrNextAvail = (void *)0;
  661. pmem_pool->SegSizeTot = (CPU_SIZE_T )0u;
  662. pmem_pool->SegSizeRem = (CPU_SIZE_T )0u;
  663. *perr = LIB_MEM_ERR_NONE;
  664. }
  665. #endif
  666. /*$PAGE*/
  667. /*
  668. *********************************************************************************************************
  669. * Mem_PoolCreate()
  670. *
  671. * Description : (1) Create a memory pool :
  672. *
  673. * (a) Create memory pool from heap or dedicated memory
  674. * (b) Allocate memory pool memory blocks
  675. * (c) Update memory pool table
  676. * (d) Configure memory pool
  677. *
  678. *
  679. * (2) Memory pools are indexed by the Memory Segments they use.
  680. *
  681. * (a) The memory pool table is composed by a two-dimensional list :
  682. *
  683. * (1) Memory segments manage the following memory segment/pool information :
  684. *
  685. * (A) Memory segment base address
  686. * (B) Memory segment next available address
  687. * (C) Memory segment total size
  688. * (D) Memory segment remaining size
  689. *
  690. * (2) Memory pools share memory from memory segments but do NOT manage any memory
  691. * segment information. To access the memory segment information, the head
  692. * memory segment must be accessed.
  693. *
  694. * (b) In the diagram below, memory pools in vertical columns represent they share the same
  695. * memory segment for the memory blocks they have. The heads of the memory pool are
  696. * linked horizontally to form a memory pool table.
  697. *
  698. * (1) 'Mem_PoolTbl' points to the head of the Memory Pool table.
  699. *
  700. * (2) Memory Pools' 'SegPrevPtr' & 'SegNextPtr' doubly-link each memory segment to
  701. * form the list of memory segments.
  702. *
  703. * (3) Memory Pools' 'PoolPrevPtr' & 'PoolNextPtr' doubly-link the memory pools of
  704. * each memory segment.
  705. *
  706. * (c) New memory pools, which do not share a memory segment, are inserted in the Memory
  707. * Segments Primary List. The point of insertion is such to keep ascended order by
  708. * memory segment base address.
  709. *
  710. * (d) Memory pool pointers to memory blocks 'PoolPtrs' must be allocated for each created
  711. * memory pool. These pointers are stored in the memory pool heap segment 'Mem_PoolHeap'.
  712. *
  713. * (1) A memory pool can also have its memory blocks allocated from the memory pool heap.
  714. * 'pmem_base_addr' must be set to NULL & 'mem_size' must be set to (0) to create the
  715. * memory pool.
  716. *
  717. *
  718. * | |
  719. * |<----------------------- Memory Segments ----------------------->|
  720. * | (see Note #2a1) |
  721. *
  722. * Lowest Memory Segment Highest Memory Segment
  723. * Base Address Base Address
  724. * (see Note #2c) (see Note #2c)
  725. *
  726. * | SegNextPtr Heap Memory Pool |
  727. * | (see Note #2b2) (see Note #2d) |
  728. * | | |
  729. * v | | v
  730. * | v
  731. * --- Head of Memory ------- ------- v ------- ------- -------
  732. * ^ Pool Table --->| |------->| |------->| |------->| |------->| |
  733. * | (see Note #2b1) | | | | | | | H | | |
  734. * | | |<-------| |<-------| |<-------| E |<-------| |
  735. * | | | | | ^ | | | A | | |
  736. * | | | | | | | | | P | | |
  737. * | | | | | | | | | | | |
  738. * | ------- ------- | ------- ------- -------
  739. * | | ^ | | ^
  740. * | | | SegPrevPtr | |
  741. * | v | (see Note #2b2) v |
  742. * | ------- -------
  743. * | | | |
  744. * Memory Pools | | | |
  745. * (see Note #2a2) | | | |
  746. * | | | |
  747. * | | | | |
  748. * | ------- -------
  749. * | | ^
  750. * | PoolNextPtr ---> | | <--- PoolPrevPtr
  751. * | (see Note #2b3) v | (see Note #2b3)
  752. * | -------
  753. * | | |
  754. * | | |
  755. * | | |
  756. * | | |
  757. * v | |
  758. * --- -------
  759. *
  760. *$PAGE*
  761. * Argument(s) : pmem_pool Pointer to a memory pool structure to create (see Note #3).
  762. *
  763. * pmem_base_addr Memory pool base address :
  764. *
  765. * (a) Null address Memory pool allocated from general-purpose heap.
  766. * (b) Non-null address Memory pool allocated from dedicated memory
  767. * specified by its base address.
  768. *
  769. * mem_size Size of memory pool segment (in octets).
  770. *
  771. * blk_nbr Number of memory pool blocks to create.
  772. *
  773. * blk_size Size of memory pool blocks to create (in octets).
  774. *
  775. * blk_align Alignment of memory pool blocks to specific word boundary (in octets).
  776. *
  777. * poctets_reqd Optional pointer to a variable to ... :
  778. *
  779. * (a) Return the number of octets required to successfully
  780. * allocate the memory pool, if any error(s);
  781. * (b) Return 0, otherwise.
  782. *
  783. * perr Pointer to variable that will receive the return error code from this function :
  784. *
  785. * LIB_MEM_ERR_NONE Memory pool successfully created.
  786. *
  787. * LIB_MEM_ERR_HEAP_NOT_FOUND Heap segment NOT found.
  788. * LIB_MEM_ERR_HEAP_EMPTY Heap segment empty; NOT enough available
  789. * memory from heap.
  790. * LIB_MEM_ERR_HEAP_OVF Requested memory overflows heap memory.
  791. * LIB_MEM_ERR_SEG_EMPTY Memory segment empty; NOT enough available
  792. * memory from segment for memory pools.
  793. * LIB_MEM_ERR_SEG_OVF Requested memory overflows segment memory.
  794. *
  795. * LIB_MEM_ERR_INVALID_SEG_SIZE Invalid memory segment size.
  796. * LIB_MEM_ERR_INVALID_SEG_OVERLAP Memory segment overlaps other memory
  797. * segment(s) in memory pool table.
  798. * LIB_MEM_ERR_INVALID_BLK_NBR Invalid memory pool number of blocks.
  799. * LIB_MEM_ERR_INVALID_BLK_SIZE Invalid memory pool block size.
  800. * LIB_MEM_ERR_INVALID_BLK_ALIGN Invalid memory pool block alignment.
  801. *
  802. * ------- RETURNED BY Mem_PoolClr() : -------
  803. * LIB_MEM_ERR_NULL_PTR Argument 'pmem_pool' passed a NULL pointer.
  804. *
  805. * Return(s) : none.
  806. *
  807. * Caller(s) : Application.
  808. *
  809. * Note(s) : (3) Assumes 'pmem_pool' points to a valid memory pool (if non-NULL).
  810. *
  811. * (4) Pointers to variables that return values MUST be initialized PRIOR to all other
  812. * validation or function handling in case of any error(s).
  813. *
  814. * (5) 'pmem_pool' variables MUST ALWAYS be accessed exclusively in critical sections.
  815. *********************************************************************************************************
  816. */
  817. /*$PAGE*/
  818. #if (LIB_MEM_CFG_ALLOC_EN == DEF_ENABLED)
  819. void Mem_PoolCreate (MEM_POOL *pmem_pool,
  820. void *pmem_base_addr,
  821. CPU_SIZE_T mem_size,
  822. CPU_SIZE_T blk_nbr,
  823. CPU_SIZE_T blk_size,
  824. CPU_SIZE_T blk_align,
  825. CPU_SIZE_T *poctets_reqd,
  826. LIB_ERR *perr)
  827. {
  828. MEM_POOL *pmem_pool_heap;
  829. MEM_POOL *pmem_pool_prev;
  830. MEM_POOL *pmem_pool_next;
  831. MEM_POOL *pmem_pool_blk;
  832. void **ppool_ptr;
  833. void *pmem_blk;
  834. CPU_INT08U *pmem_addr_ptrs;
  835. CPU_INT08U *pmem_addr_pool;
  836. CPU_INT08U *pmem_base_addr_start;
  837. CPU_INT08U *pmem_base_addr_end;
  838. CPU_INT08U *pmem_seg_addr_start;
  839. CPU_INT08U *pmem_seg_addr_end;
  840. CPU_SIZE_T octets_reqd_unused;
  841. CPU_SIZE_T size_tot;
  842. CPU_SIZE_T size_tot_ptrs;
  843. CPU_SIZE_T size_tot_pool;
  844. CPU_SIZE_T size_rem;
  845. CPU_SIZE_T size_pool_ptrs;
  846. CPU_SIZE_T blk_rem;
  847. CPU_SIZE_T i;
  848. CPU_SR_ALLOC();
  849. #if (LIB_MEM_CFG_ARG_CHK_EXT_EN == DEF_ENABLED) /* ------------- VALIDATE RTN ERR PTR ------------- */
  850. if (perr == (LIB_ERR *)0) {
  851. CPU_SW_EXCEPTION(;);
  852. }
  853. #endif
  854. /* ------------ VALIDATE RTN OCTETS PTR ----------- */
  855. if (poctets_reqd == (CPU_SIZE_T *) 0) { /* If NOT avail, ... */
  856. poctets_reqd = (CPU_SIZE_T *)&octets_reqd_unused; /* ... re-cfg NULL rtn ptr to unused local var. */
  857. (void)&octets_reqd_unused; /* Prevent possible 'variable unused' warning. */
  858. }
  859. *poctets_reqd = 0u; /* Init octets req'd for err (see Note #4). */
  860. Mem_PoolClr(pmem_pool, perr); /* Init mem pool for err (see Note #4). */
  861. if (*perr != LIB_MEM_ERR_NONE) {
  862. return;
  863. }
  864. /* ----------- VALIDATE MEM POOL CREATE ----------- */
  865. #if (LIB_MEM_CFG_ARG_CHK_EXT_EN == DEF_ENABLED)
  866. if (pmem_base_addr != (void *)0) {
  867. if (mem_size < 1) {
  868. *perr = LIB_MEM_ERR_INVALID_SEG_SIZE;
  869. return;
  870. }
  871. }
  872. if (blk_nbr < 1) {
  873. *perr = LIB_MEM_ERR_INVALID_BLK_NBR;
  874. return;
  875. }
  876. if (blk_size < 1) {
  877. *perr = LIB_MEM_ERR_INVALID_BLK_SIZE;
  878. return;
  879. }
  880. if (blk_align < 1) {
  881. *perr = LIB_MEM_ERR_INVALID_BLK_ALIGN;
  882. return;
  883. }
  884. #endif
  885. /* ------------ VALIDATE MEM POOL TBL ------------- */
  886. if (Mem_PoolTbl == (MEM_POOL *)0) {
  887. *perr = LIB_MEM_ERR_HEAP_NOT_FOUND;
  888. return;
  889. }
  890. /*$PAGE*/
  891. /* ---------------- CREATE MEM POOL --------------- */
  892. pmem_pool_heap = (MEM_POOL *)&Mem_PoolHeap;
  893. size_tot = (CPU_SIZE_T) 0u;
  894. CPU_CRITICAL_ENTER();
  895. if (pmem_base_addr == (void *)0) { /* If no base addr, cfg mem pool from heap. */
  896. pmem_pool_blk = pmem_pool_heap;
  897. pmem_pool_prev = pmem_pool_heap;
  898. pmem_pool_next = pmem_pool_heap;
  899. /* --------------- VALIDATE MEM SEG --------------- */
  900. /* Calc tot mem size for mem pool ptrs. */
  901. pmem_addr_ptrs = (CPU_INT08U *)pmem_pool_heap->SegAddrNextAvail;
  902. size_tot_ptrs = Mem_PoolSegCalcTotSize((void *)pmem_addr_ptrs,
  903. (CPU_SIZE_T)blk_nbr,
  904. (CPU_SIZE_T)sizeof(void *),
  905. (CPU_SIZE_T)sizeof(void *));
  906. #if (LIB_MEM_CFG_ARG_CHK_EXT_EN == DEF_ENABLED)
  907. if (size_tot_ptrs < 1) { /* If heap ovf, ... */
  908. CPU_CRITICAL_EXIT();
  909. *perr = LIB_MEM_ERR_HEAP_OVF; /* ... rtn err but add'l heap size NOT avail. */
  910. return;
  911. }
  912. #endif
  913. /* Calc tot mem size for mem blks. */
  914. pmem_addr_pool = pmem_addr_ptrs + size_tot_ptrs; /* Adj next avail addr for mem pool blks. */
  915. size_tot_pool = Mem_PoolSegCalcTotSize((void *)pmem_addr_pool,
  916. (CPU_SIZE_T)blk_nbr,
  917. (CPU_SIZE_T)blk_size,
  918. (CPU_SIZE_T)blk_align);
  919. #if (LIB_MEM_CFG_ARG_CHK_EXT_EN == DEF_ENABLED)
  920. if (size_tot_pool < 1) { /* If heap ovf, ... */
  921. CPU_CRITICAL_EXIT();
  922. *perr = LIB_MEM_ERR_HEAP_OVF; /* ... rtn err but add'l heap size NOT avail. */
  923. return;
  924. }
  925. #endif
  926. size_tot = size_tot_ptrs + size_tot_pool;
  927. #if (LIB_MEM_CFG_ARG_CHK_EXT_EN == DEF_ENABLED)
  928. if ((size_tot < size_tot_ptrs) || /* If heap ovf, ... */
  929. (size_tot < size_tot_pool)) {
  930. CPU_CRITICAL_EXIT();
  931. *perr = LIB_MEM_ERR_HEAP_OVF; /* ... rtn err but add'l heap size NOT avail. */
  932. return;
  933. }
  934. #endif
  935. size_rem = pmem_pool_heap->SegSizeRem;
  936. if (size_tot > size_rem) { /* If tot size > rem size, ... */
  937. CPU_CRITICAL_EXIT();
  938. *poctets_reqd = size_tot - size_rem; /* ... rtn add'l heap size needed. */
  939. *perr = LIB_MEM_ERR_HEAP_EMPTY;
  940. return;
  941. }
  942. /*$PAGE*/
  943. } else { /* Else cfg mem pool from dedicated mem. */
  944. /* -------- SRCH ALL MEM SEGS FOR MEM POOL -------- */
  945. pmem_base_addr_start = (CPU_INT08U *)pmem_base_addr;
  946. pmem_base_addr_end = (CPU_INT08U *)pmem_base_addr + mem_size - 1;
  947. #if (LIB_MEM_CFG_ARG_CHK_EXT_EN == DEF_ENABLED)
  948. if (pmem_base_addr_end < pmem_base_addr_start) { /* Chk ovf of end addr. */
  949. CPU_CRITICAL_EXIT();
  950. *perr = LIB_MEM_ERR_INVALID_BLK_ADDR;
  951. return;
  952. }
  953. #endif
  954. pmem_pool_blk = (MEM_POOL *)0;
  955. pmem_pool_prev = (MEM_POOL *)0;
  956. pmem_pool_next = Mem_PoolTbl;
  957. while (pmem_pool_next != (MEM_POOL *)0) { /* Srch tbl for mem seg with same base addr/size. */
  958. if ((pmem_base_addr == pmem_pool_next->SegAddr) && /* If same base addr/size found, ... */
  959. (mem_size == pmem_pool_next->SegSizeTot)) {
  960. pmem_pool_blk = pmem_pool_next;
  961. pmem_pool_prev = pmem_pool_next;
  962. break; /* ... prev mem seg found in tbl. */
  963. } else {
  964. pmem_seg_addr_start = (CPU_INT08U *)pmem_pool_next->SegAddr;
  965. pmem_seg_addr_end = (CPU_INT08U *)pmem_pool_next->SegAddr + pmem_pool_next->SegSizeTot - 1;
  966. if (pmem_base_addr_end < pmem_seg_addr_start) { /* If mem seg addr/size prior to next mem seg, ... */
  967. break; /* ... new mem seg NOT avail in tbl. */
  968. /* If mem seg overlaps prev mem seg(s) in tbl, ... */
  969. } else if (((pmem_base_addr_start <= pmem_seg_addr_start) &&
  970. (pmem_base_addr_end >= pmem_seg_addr_start)) ||
  971. ((pmem_base_addr_start >= pmem_seg_addr_start) &&
  972. (pmem_base_addr_end <= pmem_seg_addr_end )) ||
  973. ((pmem_base_addr_start <= pmem_seg_addr_end ) &&
  974. (pmem_base_addr_end >= pmem_seg_addr_end ))) {
  975. CPU_CRITICAL_EXIT();
  976. *perr = LIB_MEM_ERR_INVALID_SEG_OVERLAP; /* ... rtn err. */
  977. return;
  978. }
  979. }
  980. /* If mem seg NOT found, adv to next mem seg. */
  981. pmem_pool_prev = pmem_pool_next;
  982. pmem_pool_next = pmem_pool_next->SegNextPtr;
  983. }
  984. if (pmem_pool_blk == (MEM_POOL *)0) { /* If mem seg NOT found, add new mem seg. */
  985. pmem_pool_blk = pmem_pool;
  986. pmem_pool->SegAddr = pmem_base_addr;
  987. pmem_pool->SegAddrNextAvail = pmem_base_addr;
  988. pmem_pool->SegSizeTot = mem_size;
  989. pmem_pool->SegSizeRem = mem_size;
  990. }
  991. /*$PAGE*/
  992. /* --------------- VALIDATE MEM SEG --------------- */
  993. /* Calc tot mem size for mem pool ptrs. */
  994. pmem_addr_ptrs = (CPU_INT08U *)pmem_pool_heap->SegAddrNextAvail;
  995. size_tot_ptrs = Mem_PoolSegCalcTotSize((void *)pmem_addr_ptrs,
  996. (CPU_SIZE_T)blk_nbr,
  997. (CPU_SIZE_T)sizeof(void *),
  998. (CPU_SIZE_T)sizeof(void *));
  999. #if (LIB_MEM_CFG_ARG_CHK_EXT_EN == DEF_ENABLED)
  1000. if (size_tot_ptrs < 1) { /* If heap ovf, ... */
  1001. CPU_CRITICAL_EXIT();
  1002. *perr = LIB_MEM_ERR_HEAP_OVF; /* ... rtn err but add'l heap size NOT avail. */
  1003. return;
  1004. }
  1005. #endif
  1006. size_rem = pmem_pool_heap->SegSizeRem;
  1007. if (size_tot_ptrs > size_rem) { /* If ptr size > rem size, ... */
  1008. CPU_CRITICAL_EXIT();
  1009. *poctets_reqd = size_tot_ptrs - size_rem; /* ... rtn add'l heap size needed. */
  1010. *perr = LIB_MEM_ERR_HEAP_EMPTY;
  1011. return;
  1012. }
  1013. /* Calc tot mem size for mem blks. */
  1014. pmem_addr_pool = (CPU_INT08U *)pmem_pool_blk->SegAddrNextAvail;
  1015. size_tot_pool = Mem_PoolSegCalcTotSize((void *)pmem_addr_pool,
  1016. (CPU_SIZE_T)blk_nbr,
  1017. (CPU_SIZE_T)blk_size,
  1018. (CPU_SIZE_T)blk_align);
  1019. #if (LIB_MEM_CFG_ARG_CHK_EXT_EN == DEF_ENABLED)
  1020. if (size_tot_pool < 1) { /* If seg ovf, ... */
  1021. CPU_CRITICAL_EXIT();
  1022. *perr = LIB_MEM_ERR_SEG_OVF; /* ... rtn err but add'l seg size NOT avail. */
  1023. return;
  1024. }
  1025. #endif
  1026. size_rem = pmem_pool_blk->SegSizeRem;
  1027. if (size_tot_pool > size_rem) { /* If tot size > rem size, ... */
  1028. CPU_CRITICAL_EXIT();
  1029. *poctets_reqd = size_tot_pool - size_rem; /* ... rtn add'l seg size needed. */
  1030. *perr = LIB_MEM_ERR_SEG_EMPTY;
  1031. return;
  1032. }
  1033. }
  1034. /*$PAGE*/
  1035. /* ---------------- ALLOC MEM BLKs ---------------- */
  1036. size_pool_ptrs = blk_nbr * sizeof(void *);
  1037. /* Alloc stk of ptrs for mem blks from heap. */
  1038. ppool_ptr = (void **)Mem_PoolSegAlloc((MEM_POOL *)pmem_pool_heap,
  1039. (CPU_SIZE_T)size_pool_ptrs,
  1040. (CPU_SIZE_T)sizeof(void *));
  1041. if (ppool_ptr == (void **)0) { /* If mem pool ptrs alloc failed, ... */
  1042. size_rem = pmem_pool_heap->SegSizeRem;
  1043. CPU_CRITICAL_EXIT();
  1044. /* ... rtn add'l heap size needed. */
  1045. if (pmem_base_addr == (void *)0) {
  1046. if (size_tot > size_rem) {
  1047. *poctets_reqd = size_tot - size_rem;
  1048. } else {
  1049. *poctets_reqd = size_tot;
  1050. }
  1051. } else {
  1052. if (size_pool_ptrs > size_rem) {
  1053. *poctets_reqd = size_pool_ptrs - size_rem;
  1054. } else {
  1055. *poctets_reqd = size_pool_ptrs;
  1056. }
  1057. }
  1058. *perr = LIB_MEM_ERR_HEAP_EMPTY;
  1059. return;
  1060. }
  1061. for (i = 0u; i < blk_nbr; i++) { /* Alloc mem blks from blk seg ptr. */
  1062. pmem_blk = (void *)Mem_PoolSegAlloc(pmem_pool_blk, blk_size, blk_align);
  1063. if (pmem_blk == (void *)0) { /* If mem blks alloc failed, ... */
  1064. pmem_addr_pool = (CPU_INT08U *)pmem_pool_blk->SegAddrNextAvail;
  1065. size_rem = (CPU_SIZE_T )pmem_pool_blk->SegSizeRem;
  1066. CPU_CRITICAL_EXIT();
  1067. blk_rem = blk_nbr - i;
  1068. size_tot = Mem_PoolSegCalcTotSize((void *)pmem_addr_pool,
  1069. (CPU_SIZE_T)blk_rem,
  1070. (CPU_SIZE_T)blk_size,
  1071. (CPU_SIZE_T)blk_align);
  1072. /* ... rtn add'l seg size needed. */
  1073. if (size_tot > size_rem) {
  1074. *poctets_reqd = size_tot - size_rem;
  1075. } else {
  1076. *poctets_reqd = size_tot;
  1077. }
  1078. *perr = LIB_MEM_ERR_SEG_EMPTY;
  1079. return;
  1080. }
  1081. ppool_ptr[i] = pmem_blk;
  1082. }
  1083. /*$PAGE*/
  1084. /* ------------- UPDATE MEM POOL TBL -------------- */
  1085. if (pmem_pool_prev == pmem_pool_next) { /* Add new mem seg to list. */
  1086. pmem_pool_next = pmem_pool_blk->PoolNextPtr;
  1087. pmem_pool->PoolPrevPtr = pmem_pool_blk;
  1088. pmem_pool->PoolNextPtr = pmem_pool_next;
  1089. pmem_pool_blk->PoolNextPtr = pmem_pool;
  1090. if (pmem_pool_next != (MEM_POOL *)0) {
  1091. pmem_pool_next->PoolPrevPtr = pmem_pool;
  1092. }
  1093. } else { /* Add new mem pool to mem seg. */
  1094. pmem_pool->SegPrevPtr = pmem_pool_prev;
  1095. pmem_pool->SegNextPtr = pmem_pool_next;
  1096. if (pmem_pool_prev != (MEM_POOL *)0) { /* Update prev mem pool link. */
  1097. pmem_pool_prev->SegNextPtr = pmem_pool;
  1098. } else {
  1099. Mem_PoolTbl = pmem_pool; /* Update mem pool head. */
  1100. }
  1101. if (pmem_pool_next != (MEM_POOL *)0) { /* Update next mem pool link. */
  1102. pmem_pool_next->SegPrevPtr = pmem_pool;
  1103. }
  1104. }
  1105. /* ----------------- CFG MEM POOL ----------------- */
  1106. pmem_pool->Type = (LIB_MEM_TYPE) LIB_MEM_TYPE_POOL;
  1107. pmem_pool->PoolAddrStart = (void *) pmem_addr_pool;
  1108. pmem_pool->PoolAddrEnd = (void *)(pmem_addr_pool + size_tot_pool - 1);
  1109. pmem_pool->PoolPtrs = (void **) ppool_ptr;
  1110. pmem_pool->PoolSize = (CPU_SIZE_T ) size_tot_pool;
  1111. pmem_pool->BlkAlign = (CPU_SIZE_T ) blk_align;
  1112. pmem_pool->BlkSize = (CPU_SIZE_T ) blk_size;
  1113. pmem_pool->BlkNbr = (CPU_SIZE_T ) blk_nbr;
  1114. pmem_pool->BlkIx = (MEM_POOL_IX ) blk_nbr;
  1115. CPU_CRITICAL_EXIT();
  1116. *perr = LIB_MEM_ERR_NONE;
  1117. }
  1118. #endif
  1119. /*$PAGE*/
  1120. /*
  1121. *********************************************************************************************************
  1122. * Mem_PoolBlkGet()
  1123. *
  1124. * Description : Get a memory block from memory pool.
  1125. *
  1126. * Argument(s) : pmem_pool Pointer to memory pool to get memory block from.
  1127. *
  1128. * size Size of requested memory (in octets).
  1129. *
  1130. * perr Pointer to variable that will receive the return error code from this function :
  1131. *
  1132. * LIB_MEM_ERR_NONE Memory block successfully returned.
  1133. * LIB_MEM_ERR_POOL_EMPTY NO memory blocks available in memory pool.
  1134. *
  1135. * LIB_MEM_ERR_NULL_PTR Argument 'pmem_pool' passed a NULL pointer.
  1136. * LIB_MEM_ERR_INVALID_POOL Invalid memory pool type.
  1137. * LIB_MEM_ERR_INVALID_BLK_SIZE Invalid memory pool block size requested.
  1138. * LIB_MEM_ERR_INVALID_BLK_IX Invalid memory pool block index.
  1139. *
  1140. * Return(s) : Pointer to memory block, if NO error(s).
  1141. *
  1142. * Pointer to NULL, otherwise.
  1143. *
  1144. * Caller(s) : Application.
  1145. *
  1146. * Note(s) : (1) 'pmem_pool' variables MUST ALWAYS be accessed exclusively in critical sections.
  1147. *********************************************************************************************************
  1148. */
  1149. /*$PAGE*/
  1150. #if (LIB_MEM_CFG_ALLOC_EN == DEF_ENABLED)
  1151. void *Mem_PoolBlkGet (MEM_POOL *pmem_pool,
  1152. CPU_SIZE_T size,
  1153. LIB_ERR *perr)
  1154. {
  1155. void *pmem_blk;
  1156. CPU_SR_ALLOC();
  1157. #if (LIB_MEM_CFG_ARG_CHK_EXT_EN == DEF_ENABLED) /* ------------- VALIDATE RTN ERR PTR ------------- */
  1158. if (perr == (LIB_ERR *)0) {
  1159. CPU_SW_EXCEPTION((void *)0);
  1160. }
  1161. #endif
  1162. /* ------------ VALIDATE MEM POOL GET ------------- */
  1163. #if (LIB_MEM_CFG_ARG_CHK_EXT_EN == DEF_ENABLED)
  1164. if (pmem_pool == (MEM_POOL *)0) { /* Validate mem ptr. */
  1165. *perr = LIB_MEM_ERR_NULL_PTR;
  1166. return ((void *)0);
  1167. }
  1168. if (size < 1) { /* Validate req'd size as non-NULL. */
  1169. *perr = LIB_MEM_ERR_INVALID_BLK_SIZE;
  1170. return ((void *)0);
  1171. }
  1172. #endif
  1173. CPU_CRITICAL_ENTER();
  1174. #if (LIB_MEM_CFG_ARG_CHK_EXT_EN == DEF_ENABLED)
  1175. if (pmem_pool->Type != LIB_MEM_TYPE_POOL) { /* Validate mem pool type. */
  1176. CPU_CRITICAL_EXIT();
  1177. *perr = LIB_MEM_ERR_INVALID_POOL;
  1178. return ((void *)0);
  1179. }
  1180. if (size > pmem_pool->BlkSize) { /* Validate req'd size <= mem pool blk size. */
  1181. CPU_CRITICAL_EXIT();
  1182. *perr = LIB_MEM_ERR_INVALID_BLK_SIZE;
  1183. return ((void *)0);
  1184. }
  1185. #endif
  1186. (void)&size; /* Prevent possible 'variable unused' warning. */
  1187. if (pmem_pool->BlkIx < 1) { /* Validate mem pool as NOT empty. */
  1188. CPU_CRITICAL_EXIT();
  1189. *perr = LIB_MEM_ERR_POOL_EMPTY;
  1190. return ((void *)0);
  1191. }
  1192. if (pmem_pool->BlkIx > pmem_pool->BlkNbr) { /* Validate mem pool ix NOT corrupt. */
  1193. CPU_CRITICAL_EXIT();
  1194. *perr = LIB_MEM_ERR_INVALID_BLK_IX;
  1195. return ((void *)0);
  1196. }
  1197. /* ------------ GET MEM BLK FROM POOL ------------- */
  1198. pmem_pool->BlkIx--;
  1199. pmem_blk = pmem_pool->PoolPtrs[pmem_pool->BlkIx];
  1200. CPU_CRITICAL_EXIT();
  1201. *perr = LIB_MEM_ERR_NONE;
  1202. return (pmem_blk);
  1203. }
  1204. #endif
  1205. /*$PAGE*/
  1206. /*
  1207. *********************************************************************************************************
  1208. * Mem_PoolBlkFree()
  1209. *
  1210. * Description : Free a memory block to memory pool.
  1211. *
  1212. * Argument(s) : pmem_pool Pointer to memory pool to free memory block.
  1213. *
  1214. * pmem_blk Pointer to memory block address to free.
  1215. *
  1216. * perr Pointer to variable that will receive the return error code from this function :
  1217. *
  1218. * LIB_MEM_ERR_NONE Memory block successfully freed.
  1219. * LIB_MEM_ERR_POOL_FULL ALL memory blocks already available in
  1220. * memory pool.
  1221. *
  1222. * LIB_MEM_ERR_NULL_PTR Argument 'pmem_pool'/'pmem_blk' passed
  1223. * a NULL pointer.
  1224. * LIB_MEM_ERR_INVALID_POOL Invalid memory pool type.
  1225. * LIB_MEM_ERR_INVALID_BLK_ADDR Invalid memory block address.
  1226. * LIB_MEM_ERR_INVALID_BLK_ADDR_IN_POOL Memory block address already
  1227. * in memory pool.
  1228. *
  1229. * Return(s) : none.
  1230. *
  1231. * Caller(s) : Application.
  1232. *
  1233. * Note(s) : (1) 'pmem_pool' variables MUST ALWAYS be accessed exclusively in critical sections.
  1234. *********************************************************************************************************
  1235. */
  1236. /*$PAGE*/
  1237. #if (LIB_MEM_CFG_ALLOC_EN == DEF_ENABLED)
  1238. void Mem_PoolBlkFree (MEM_POOL *pmem_pool,
  1239. void *pmem_blk,
  1240. LIB_ERR *perr)
  1241. {
  1242. #if (LIB_MEM_CFG_ARG_CHK_EXT_EN == DEF_ENABLED)
  1243. CPU_BOOLEAN addr_valid;
  1244. MEM_POOL_IX i;
  1245. #endif
  1246. CPU_SR_ALLOC();
  1247. #if (LIB_MEM_CFG_ARG_CHK_EXT_EN == DEF_ENABLED) /* ------------- VALIDATE RTN ERR PTR ------------- */
  1248. if (perr == (LIB_ERR *)0) {
  1249. CPU_SW_EXCEPTION(;);
  1250. }
  1251. #endif
  1252. /* ------------ VALIDATE MEM POOL FREE ------------ */
  1253. #if (LIB_MEM_CFG_ARG_CHK_EXT_EN == DEF_ENABLED) /* Validate mem ptrs. */
  1254. if (pmem_pool == (MEM_POOL *)0) {
  1255. *perr = LIB_MEM_ERR_NULL_PTR;
  1256. return;
  1257. }
  1258. if (pmem_blk == (void *)0) {
  1259. *perr = LIB_MEM_ERR_NULL_PTR;
  1260. return;
  1261. }
  1262. #endif
  1263. CPU_CRITICAL_ENTER();
  1264. #if (LIB_MEM_CFG_ARG_CHK_EXT_EN == DEF_ENABLED)
  1265. if (pmem_pool->Type != LIB_MEM_TYPE_POOL) { /* Validate mem pool type. */
  1266. CPU_CRITICAL_EXIT();
  1267. *perr = LIB_MEM_ERR_INVALID_POOL;
  1268. return;
  1269. }
  1270. addr_valid = Mem_PoolBlkIsValidAddr(pmem_pool, pmem_blk); /* Validate mem blk as valid pool blk addr. */
  1271. if (addr_valid != DEF_OK) {
  1272. CPU_CRITICAL_EXIT();
  1273. *perr = LIB_MEM_ERR_INVALID_BLK_ADDR;
  1274. return;
  1275. }
  1276. for (i = 0u; i < pmem_pool->BlkIx; i++) { /* Validate mem blk NOT already in pool. */
  1277. if (pmem_blk == pmem_pool->PoolPtrs[i]) {
  1278. CPU_CRITICAL_EXIT();
  1279. *perr = LIB_MEM_ERR_INVALID_BLK_ADDR_IN_POOL;
  1280. return;
  1281. }
  1282. }
  1283. #endif
  1284. if (pmem_pool->BlkIx >= pmem_pool->BlkNbr) { /* Validate mem pool NOT already full. */
  1285. CPU_CRITICAL_EXIT();
  1286. *perr = LIB_MEM_ERR_POOL_FULL;
  1287. return;
  1288. }
  1289. /* ------------- FREE MEM BLK TO POOL ------------- */
  1290. pmem_pool->PoolPtrs[pmem_pool->BlkIx] = pmem_blk;
  1291. pmem_pool->BlkIx++;
  1292. CPU_CRITICAL_EXIT();
  1293. *perr = LIB_MEM_ERR_NONE;
  1294. }
  1295. #endif
  1296. /*$PAGE*/
  1297. /*
  1298. *********************************************************************************************************
  1299. *********************************************************************************************************
  1300. * LOCAL FUNCTIONS
  1301. *********************************************************************************************************
  1302. *********************************************************************************************************
  1303. */
  1304. /*
  1305. *********************************************************************************************************
  1306. * Mem_PoolBlkIsValidAddr()
  1307. *
  1308. * Description : Calculates if a given memory block address is valid for the memory pool.
  1309. *
  1310. * Argument(s) : pmem_pool Pointer to memory pool structure to validate memory block address.
  1311. * --------- Argument validated in Mem_PoolBlkFree().
  1312. *
  1313. * pmem_blk Pointer to memory block address to validate.
  1314. * -------- Argument validated in Mem_PoolBlkFree().
  1315. *
  1316. * Return(s) : DEF_YES, if valid memory pool block address.
  1317. *
  1318. * DEF_NO, otherwise.
  1319. *
  1320. * Caller(s) : Mem_PoolBlkFree().
  1321. *
  1322. * Note(s) : none.
  1323. *********************************************************************************************************
  1324. */
  1325. #if ((LIB_MEM_CFG_ALLOC_EN == DEF_ENABLED) && \
  1326. (LIB_MEM_CFG_ARG_CHK_EXT_EN == DEF_ENABLED))
  1327. static CPU_BOOLEAN Mem_PoolBlkIsValidAddr (MEM_POOL *pmem_pool,
  1328. void *pmem_blk)
  1329. {
  1330. CPU_INT08U *ppool_addr_first;
  1331. void *ppool_addr_start;
  1332. void *ppool_addr_end;
  1333. CPU_SIZE_T align_offset;
  1334. CPU_SIZE_T blk_align;
  1335. CPU_SIZE_T blk_align_offset;
  1336. CPU_SIZE_T blk_size;
  1337. CPU_SIZE_T mem_align;
  1338. CPU_SIZE_T mem_align_offset;
  1339. CPU_SIZE_T mem_diff;
  1340. CPU_BOOLEAN addr_valid;
  1341. ppool_addr_start = pmem_pool->PoolAddrStart;
  1342. ppool_addr_end = pmem_pool->PoolAddrEnd;
  1343. if ((pmem_blk < ppool_addr_start) ||
  1344. (pmem_blk > ppool_addr_end)) {
  1345. return (DEF_NO);
  1346. }
  1347. blk_align = (CPU_SIZE_T)pmem_pool->BlkAlign;
  1348. align_offset = (CPU_SIZE_T)((CPU_ADDR)ppool_addr_start % blk_align);
  1349. if (align_offset != 0u) {
  1350. mem_align_offset = blk_align - align_offset;
  1351. } else {
  1352. mem_align_offset = 0u;
  1353. }
  1354. blk_size = pmem_pool->BlkSize;
  1355. align_offset = blk_size % blk_align;
  1356. if (align_offset != 0u) {
  1357. blk_align_offset = blk_align - align_offset;
  1358. } else {
  1359. blk_align_offset = 0u;
  1360. }
  1361. ppool_addr_first = (CPU_INT08U *)((CPU_INT08U *)ppool_addr_start + mem_align_offset);
  1362. mem_diff = (CPU_SIZE_T )((CPU_INT08U *)pmem_blk - ppool_addr_first);
  1363. mem_align = (CPU_SIZE_T )( blk_size + blk_align_offset);
  1364. addr_valid = ((mem_diff % mem_align) == 0u) ? DEF_YES : DEF_NO;
  1365. return (addr_valid);
  1366. }
  1367. #endif
  1368. /*$PAGE*/
  1369. /*
  1370. *********************************************************************************************************
  1371. * Mem_PoolSegCalcTotSize()
  1372. *
  1373. * Description : (1) Calculates total memory segment size for number of blocks with specific size & alignment :
  1374. *
  1375. *
  1376. * ----- ====================== ---
  1377. * ^ Mem Addr ---> | / / / / / / | ^
  1378. * | (see Note #1a) | / / / / / / /| | Mem Align Offset
  1379. * | |/ / / / / / / | | (see Notes #1e & #2a)
  1380. * | | / / / / / / | v
  1381. * | ====================== ---
  1382. * | | | ^
  1383. * | | | |
  1384. * | | Mem Blk #1 | | Blk Size
  1385. * | | | | (see Note #1c)
  1386. * | | | v
  1387. * | ---------------------- ---
  1388. * | | / / / / / / | ^
  1389. * | | / / / / / / /| | Blk Align Offset
  1390. * | |/ / / / / / / | | (see Notes #1f & #2b)
  1391. * | | / / / / / / | v
  1392. * | ====================== ---
  1393. * | . |
  1394. * Total Size | . |
  1395. * (see Note #2c) | . |
  1396. * ====================== ---
  1397. * | | | ^
  1398. * | | | |
  1399. * | | Mem Blk #N - 1 | | Blk Size
  1400. * | | | | (see Note #1c)
  1401. * | | | v
  1402. * | ---------------------- ---
  1403. * | | / / / / / / | ^
  1404. * | | / / / / / / /| | Blk Align Offset
  1405. * | |/ / / / / / / | | (see Notes #1f & #2b)
  1406. * | | / / / / / / | v
  1407. * | ====================== ---
  1408. * | | | ^
  1409. * | | | |
  1410. * | | Mem Blk #N | | Blk Size
  1411. * | | | | (see Note #1c)
  1412. * v | | v
  1413. * ----- ====================== ---
  1414. *
  1415. * where
  1416. *
  1417. * (a) Mem Addr Memory address of the beginning of the memory block ('pmem_addr')
  1418. *
  1419. * (b) N Number of memory blocks to allocate ('blk_nbr')
  1420. *
  1421. * (c) Blk Size Size of memory block to allocate ('blk_size')
  1422. *
  1423. * (d) Align Required block memory alignment ('blk_align')
  1424. *
  1425. * (e) Mem Align Offset Offset required to align first memory block
  1426. *
  1427. * (f) Blk Align Offset Offset required to align every memory block
  1428. *
  1429. *
  1430. * (2) The total size is calculated based on the following equations :
  1431. *
  1432. * { (1) Align - (Mem Addr % Align) , if memory address is not aligned
  1433. * (a) Mem Align Offset = {
  1434. * { (2) 0 , if memory address is aligned
  1435. *
  1436. *
  1437. * { (1) Align - (Size % Align) , if memory block is not aligned
  1438. * (b) Blk Align Offset = {
  1439. * { (2) 0 , if memory block is aligned
  1440. *
  1441. *
  1442. * (c) Total Size = Mem Align Offset
  1443. * + ((Blk Size + Blk Align Offset) * (N - 1))
  1444. * + Blk Size
  1445. *
  1446. *
  1447. * Argument(s) : pmem_addr Memory address of the beginning of the memory block.
  1448. *
  1449. * blk_nbr Number of memory blocks to allocate.
  1450. * ------- Argument checked in Mem_HeapAlloc(),
  1451. * Mem_PoolCreate().
  1452. *
  1453. * blk_size Size of memory block to allocate.
  1454. * -------- Argument checked in Mem_HeapAlloc(),
  1455. * Mem_PoolCreate().
  1456. *
  1457. * blk_align Required block word-boundary memory alignment (in octets).
  1458. * --------- Argument checked in Mem_HeapAlloc(),
  1459. * Mem_PoolCreate().
  1460. *
  1461. * Return(s) : Total size of memory segment used to allocate the number of blocks, if NO error(s).
  1462. *
  1463. * 0, otherwise.
  1464. *$PAGE*
  1465. * Caller(s) : Mem_HeapAlloc(),
  1466. * Mem_PoolCreate().
  1467. *
  1468. * Note(s) : none.
  1469. *********************************************************************************************************
  1470. */
  1471. #if (LIB_MEM_CFG_ALLOC_EN == DEF_ENABLED)
  1472. static CPU_SIZE_T Mem_PoolSegCalcTotSize (void *pmem_addr,
  1473. CPU_SIZE_T blk_nbr,
  1474. CPU_SIZE_T blk_size,
  1475. CPU_SIZE_T blk_align)
  1476. {
  1477. #if (LIB_MEM_CFG_ARG_CHK_EXT_EN == DEF_ENABLED)
  1478. CPU_SIZE_T blk_size_mem_aligned;
  1479. CPU_SIZE_T blk_size_aligned;
  1480. CPU_SIZE_T blk_size_aligned_nbr;
  1481. CPU_SIZE_T blk_size_tot;
  1482. #endif
  1483. CPU_SIZE_T align_offset;
  1484. CPU_SIZE_T mem_align_offset;
  1485. CPU_SIZE_T blk_align_offset;
  1486. CPU_SIZE_T size_tot;
  1487. /* Calc mem align (see Note #2a). */
  1488. align_offset = (CPU_ADDR)pmem_addr % blk_align;
  1489. if (align_offset != 0u) {
  1490. mem_align_offset = blk_align - align_offset;
  1491. } else {
  1492. mem_align_offset = 0u;
  1493. }
  1494. /* Calc blk align (see Note #2b). */
  1495. align_offset = blk_size % blk_align;
  1496. if (align_offset != 0u) {
  1497. blk_align_offset = blk_align - align_offset;
  1498. } else {
  1499. blk_align_offset = 0u;
  1500. }
  1501. /* Calc tot size (see Note #2c). */
  1502. size_tot = mem_align_offset + ((blk_size + blk_align_offset) * (blk_nbr - 1)) + blk_size;
  1503. #if (LIB_MEM_CFG_ARG_CHK_EXT_EN == DEF_ENABLED) /* Chk ovf of tot size = A + [(B + C) * D] + E */
  1504. blk_size_mem_aligned = mem_align_offset + blk_size; /* Chk ovf of A + E : */
  1505. if ((blk_size_mem_aligned < mem_align_offset) ||
  1506. (blk_size_mem_aligned < blk_size)) {
  1507. return (0u);
  1508. }
  1509. if (blk_nbr > 1) {
  1510. blk_size_aligned = blk_size + blk_align_offset;
  1511. if ((blk_size_aligned < blk_align_offset) || /* Chk ovf of (B + C) : */
  1512. (blk_size_aligned < blk_size)) {
  1513. return (0u);
  1514. }
  1515. blk_size_aligned_nbr = blk_size_aligned * (blk_nbr - 1);
  1516. if ((blk_size_aligned_nbr < blk_size_aligned) || /* Chk ovf of [(B + C) * D] : */
  1517. (blk_size_aligned_nbr < blk_align_offset) ||
  1518. (blk_size_aligned_nbr < blk_size)) {
  1519. return (0u);
  1520. }
  1521. blk_size_tot = blk_size_aligned_nbr + blk_size;
  1522. if ((blk_size_tot < blk_size_aligned_nbr) || /* Chk ovf of [(B + C) * D] + E : */
  1523. (blk_size_tot < blk_size)) {
  1524. return (0u);
  1525. }
  1526. if ((size_tot < blk_size_mem_aligned) || /* Chk ovf of A + [(B + C) * D] + E : */
  1527. (size_tot < blk_size_aligned_nbr) ||
  1528. (size_tot < blk_size_tot)) {
  1529. return (0u);
  1530. }
  1531. }
  1532. #endif
  1533. return (size_tot);
  1534. }
  1535. #endif
  1536. /*$PAGE*/
  1537. /*
  1538. *********************************************************************************************************
  1539. * Mem_PoolSegAlloc()
  1540. *
  1541. * Description : Allocates memory from specific segment.
  1542. *
  1543. * Argument(s) : pmem_pool Pointer to memory pool structure containing segment information.
  1544. * --------- Argument validated in Mem_HeapAlloc(),
  1545. * Mem_PoolCreate().
  1546. *
  1547. * size Size of memory to allocate.
  1548. * ---- Argument validated in Mem_HeapAlloc(),
  1549. * Mem_PoolCreate().
  1550. *
  1551. * align Required starting word-boundary memory alignment (in octets).
  1552. * ----- Argument validated in Mem_HeapAlloc(),
  1553. * Mem_PoolCreate().
  1554. *
  1555. * Return(s) : Pointer to allocated memory, if NO error(s).
  1556. *
  1557. * Pointer to NULL, otherwise.
  1558. *
  1559. * Caller(s) : Mem_HeapAlloc(),
  1560. * Mem_PoolCreate().
  1561. *
  1562. * Note(s) : (1) Allocated memory from the specific segment is NEVER freed after allocation.
  1563. *
  1564. * (2) 'pmem_pool' variables MUST ALWAYS be accessed exclusively in critical sections.
  1565. *
  1566. * (a) However, this function is already called within critical sections.
  1567. *********************************************************************************************************
  1568. */
  1569. #if (LIB_MEM_CFG_ALLOC_EN == DEF_ENABLED)
  1570. static void *Mem_PoolSegAlloc (MEM_POOL *pmem_pool,
  1571. CPU_SIZE_T size,
  1572. CPU_SIZE_T align)
  1573. {
  1574. CPU_INT08U *pmem_addr;
  1575. CPU_INT08U *pmem_addr_next;
  1576. CPU_SIZE_T mem_align;
  1577. CPU_SIZE_T align_offset;
  1578. CPU_SIZE_T size_tot;
  1579. pmem_addr = (CPU_INT08U *)pmem_pool->SegAddrNextAvail;
  1580. mem_align = (CPU_SIZE_T)((CPU_ADDR)pmem_addr % align); /* Calc mem align. */
  1581. if (mem_align != 0u) {
  1582. align_offset = align - mem_align;
  1583. } else {
  1584. align_offset = 0u;
  1585. }
  1586. size_tot = align_offset + size;
  1587. if (size_tot > pmem_pool->SegSizeRem) { /* If insufficiemt mem seg size rem, ... */
  1588. return ((void *)0); /* ... rtn NULL. */
  1589. }
  1590. #if (LIB_MEM_CFG_ARG_CHK_EXT_EN == DEF_ENABLED)
  1591. if ((size_tot < align_offset) || /* If size ovf, ... */
  1592. (size_tot < size)) {
  1593. return ((void *)0); /* ... rtn NULL. */
  1594. }
  1595. #endif
  1596. pmem_addr_next = pmem_addr + size_tot;
  1597. #if (LIB_MEM_CFG_ARG_CHK_EXT_EN == DEF_ENABLED)
  1598. if (pmem_addr_next < pmem_addr) { /* If addr ovf, ... */
  1599. return ((void *)0); /* ... rtn NULL. */
  1600. }
  1601. #endif
  1602. pmem_addr += align_offset; /* Align mem addr. */
  1603. pmem_pool->SegAddrNextAvail = (void *)pmem_addr_next; /* Adv next avail addr. */
  1604. pmem_pool->SegSizeRem -= (CPU_SIZE_T)size_tot; /* Adj rem mem seg size. */
  1605. return ((void *)pmem_addr);
  1606. }
  1607. #endif