1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842 |
- /*
- *********************************************************************************************************
- * uC/LIB
- * CUSTOM LIBRARY MODULES
- *
- * (c) Copyright 2004-2014; Micrium, Inc.; Weston, FL
- *
- * All rights reserved. Protected by international copyright laws.
- *
- * uC/LIB is provided in source form to registered licensees ONLY. It is
- * illegal to distribute this source code to any third party unless you receive
- * written permission by an authorized Micrium representative. Knowledge of
- * the source code may NOT be used to develop a similar product.
- *
- * Please help us continue to provide the Embedded community with the finest
- * software available. Your honesty is greatly appreciated.
- *
- * You can find our product's user manual, API reference, release notes and
- * more information at: https://doc.micrium.com
- *
- * You can contact us at: http://www.micrium.com
- *********************************************************************************************************
- */
- /*
- *********************************************************************************************************
- *
- * STANDARD MEMORY OPERATIONS
- *
- * Filename : lib_mem.c
- * Version : V1.38.01
- * Programmer(s) : ITJ
- * FGK
- * JFD
- * FBJ
- * EJ
- *********************************************************************************************************
- * Note(s) : (1) NO compiler-supplied standard library functions are used in library or product software.
- *
- * (a) ALL standard library functions are implemented in the custom library modules :
- *
- * (1) \<Custom Library Directory>\lib_*.*
- *
- * (2) \<Custom Library Directory>\Ports\<cpu>\<compiler>\lib*_a.*
- *
- * where
- * <Custom Library Directory> directory path for custom library software
- * <cpu> directory name for specific processor (CPU)
- * <compiler> directory name for specific compiler
- *
- * (b) Product-specific library functions are implemented in individual products.
- *********************************************************************************************************
- */
- /*
- *********************************************************************************************************
- * INCLUDE FILES
- *********************************************************************************************************
- */
- #define MICRIUM_SOURCE
- #define LIB_MEM_MODULE
- #include "lib_mem.h"
- #include "lib_math.h"
- #include "lib_str.h"
- /*
- *********************************************************************************************************
- * LOCAL DEFINES
- *********************************************************************************************************
- */
- /*
- *********************************************************************************************************
- * LOCAL CONSTANTS
- *********************************************************************************************************
- */
- /*
- *********************************************************************************************************
- * LOCAL DATA TYPES
- *********************************************************************************************************
- */
- /*
- *********************************************************************************************************
- * LOCAL TABLES
- *********************************************************************************************************
- */
- /*
- *********************************************************************************************************
- * LOCAL GLOBAL VARIABLES
- *********************************************************************************************************
- */
- #if (LIB_MEM_CFG_HEAP_SIZE > 0u)
- #ifndef LIB_MEM_CFG_HEAP_BASE_ADDR
- CPU_INT08U Mem_Heap[LIB_MEM_CFG_HEAP_SIZE]; /* Mem heap. */
- #endif
- MEM_SEG Mem_SegHeap; /* Heap mem seg. */
- #endif
- MEM_SEG *Mem_SegHeadPtr; /* Ptr to head of seg list. */
- /*
- *********************************************************************************************************
- * LOCAL FUNCTION PROTOTYPES
- *********************************************************************************************************
- */
- static void Mem_SegCreateCritical (const CPU_CHAR *p_name,
- MEM_SEG *p_seg,
- CPU_ADDR seg_base_addr,
- CPU_SIZE_T padding_align,
- CPU_SIZE_T size);
- static MEM_SEG *Mem_SegOverlapChkCritical( CPU_ADDR seg_base_addr,
- CPU_SIZE_T size,
- LIB_ERR *p_err);
- static void *Mem_SegAllocInternal (const CPU_CHAR *p_name,
- MEM_SEG *p_seg,
- CPU_SIZE_T size,
- CPU_SIZE_T align,
- CPU_SIZE_T padding_align,
- CPU_SIZE_T *p_bytes_reqd,
- LIB_ERR *p_err);
- static void *Mem_SegAllocExtCritical ( MEM_SEG *p_seg,
- CPU_SIZE_T size,
- CPU_SIZE_T align,
- CPU_SIZE_T padding_align,
- CPU_SIZE_T *p_bytes_reqd,
- LIB_ERR *p_err);
- static void Mem_DynPoolCreateInternal(const CPU_CHAR *p_name,
- MEM_DYN_POOL *p_pool,
- MEM_SEG *p_seg,
- CPU_SIZE_T blk_size,
- CPU_SIZE_T blk_align,
- CPU_SIZE_T blk_padding_align,
- CPU_SIZE_T blk_qty_init,
- CPU_SIZE_T blk_qty_max,
- LIB_ERR *p_err);
- #if (LIB_MEM_CFG_DBG_INFO_EN == DEF_ENABLED)
- static void Mem_SegAllocTrackCritical(const CPU_CHAR *p_name,
- MEM_SEG *p_seg,
- CPU_SIZE_T size,
- LIB_ERR *p_err);
- #endif
- #if ((LIB_MEM_CFG_ARG_CHK_EXT_EN == DEF_ENABLED) && \
- (LIB_MEM_CFG_HEAP_SIZE > 0u))
- static CPU_BOOLEAN Mem_PoolBlkIsValidAddr ( MEM_POOL *p_pool,
- void *p_mem);
- #endif
- /*
- *********************************************************************************************************
- * LOCAL CONFIGURATION ERRORS
- *********************************************************************************************************
- */
- /*
- *********************************************************************************************************
- *********************************************************************************************************
- * GLOBAL FUNCTIONS
- *********************************************************************************************************
- *********************************************************************************************************
- */
- /*
- *********************************************************************************************************
- * Mem_Init()
- *
- * Description : (1) Initializes Memory Management Module :
- *
- * (a) Initialize heap memory pool
- * (b) Initialize memory pool table
- *
- *
- * Argument(s) : none.
- *
- * Return(s) : none.
- *
- * Caller(s) : Application.
- *
- * Note(s) : (2) Mem_Init() MUST be called ... :
- *
- * (a) ONLY ONCE from a product's application; ...
- * (b) BEFORE product's application calls any memory library module function(s)
- *********************************************************************************************************
- */
- void Mem_Init (void)
- {
- /* ------------------ INIT SEG LIST ------------------- */
- Mem_SegHeadPtr = DEF_NULL;
- #if (LIB_MEM_CFG_HEAP_SIZE > 0u)
- {
- LIB_ERR err;
- CPU_ADDR heap_base_addr;
- /* ------------------ INIT HEAP SEG ------------------- */
- #ifdef LIB_MEM_CFG_HEAP_BASE_ADDR
- heap_base_addr = LIB_MEM_CFG_HEAP_BASE_ADDR;
- #else
- heap_base_addr = (CPU_ADDR)&Mem_Heap[0u];
- #endif
- Mem_SegCreate("Heap",
- &Mem_SegHeap, /* Create heap seg. */
- heap_base_addr,
- LIB_MEM_CFG_HEAP_SIZE,
- LIB_MEM_PADDING_ALIGN_NONE,
- &err);
- if (err != LIB_MEM_ERR_NONE) {
- CPU_SW_EXCEPTION(;);
- }
- }
- #endif
- }
- /*
- *********************************************************************************************************
- * Mem_Clr()
- *
- * Description : Clears data buffer (see Note #2).
- *
- * Argument(s) : pmem Pointer to memory buffer to clear.
- *
- * size Number of data buffer octets to clear (see Note #1).
- *
- * Return(s) : none.
- *
- * Caller(s) : Application.
- *
- * Note(s) : (1) Null clears allowed (i.e. zero-length clears).
- *
- * See also 'Mem_Set() Note #1'.
- *
- * (2) Clear data by setting each data octet to 0.
- *********************************************************************************************************
- */
- void Mem_Clr (void *pmem,
- CPU_SIZE_T size)
- {
- Mem_Set(pmem,
- 0u, /* See Note #2. */
- size);
- }
- /*
- *********************************************************************************************************
- * Mem_Set()
- *
- * Description : Fills data buffer with specified data octet.
- *
- * Argument(s) : pmem Pointer to memory buffer to fill with specified data octet.
- *
- * data_val Data fill octet value.
- *
- * size Number of data buffer octets to fill (see Note #1).
- *
- * Return(s) : none.
- *
- * Caller(s) : Application.
- *
- * Note(s) : (1) Null sets allowed (i.e. zero-length sets).
- *
- * (2) For best CPU performance, optimized to fill data buffer using 'CPU_ALIGN'-sized data
- * words. Since many word-aligned processors REQUIRE that multi-octet words be accessed on
- * word-aligned addresses, 'CPU_ALIGN'-sized words MUST be accessed on 'CPU_ALIGN'd
- * addresses.
- *
- * (3) Modulo arithmetic is used to determine whether a memory buffer starts on a 'CPU_ALIGN'
- * address boundary.
- *
- * Modulo arithmetic in ANSI-C REQUIREs operations performed on integer values. Thus
- * address values MUST be cast to an appropriately-sized integer value PRIOR to any
- * 'mem_align_mod' arithmetic operation.
- *********************************************************************************************************
- */
- void Mem_Set (void *pmem,
- CPU_INT08U data_val,
- CPU_SIZE_T size)
- {
- CPU_SIZE_T size_rem;
- CPU_ALIGN data_align;
- CPU_ALIGN *pmem_align;
- CPU_INT08U *pmem_08;
- CPU_DATA mem_align_mod;
- CPU_DATA i;
- #if (LIB_MEM_CFG_ARG_CHK_EXT_EN == DEF_ENABLED)
- if (size < 1) { /* See Note #1. */
- return;
- }
- if (pmem == (void *)0) {
- return;
- }
- #endif
- data_align = 0u;
- for (i = 0u; i < sizeof(CPU_ALIGN); i++) { /* Fill each data_align octet with data val. */
- data_align <<= DEF_OCTET_NBR_BITS;
- data_align |= (CPU_ALIGN)data_val;
- }
- size_rem = size;
- mem_align_mod = (CPU_INT08U)((CPU_ADDR)pmem % sizeof(CPU_ALIGN)); /* See Note #3. */
- pmem_08 = (CPU_INT08U *)pmem;
- if (mem_align_mod != 0u) { /* If leading octets avail, ... */
- i = mem_align_mod;
- while ((size_rem > 0) && /* ... start mem buf fill with leading octets ... */
- (i < sizeof(CPU_ALIGN ))) { /* ... until next CPU_ALIGN word boundary. */
- *pmem_08++ = data_val;
- size_rem -= sizeof(CPU_INT08U);
- i++;
- }
- }
- pmem_align = (CPU_ALIGN *)pmem_08; /* See Note #2. */
- while (size_rem >= sizeof(CPU_ALIGN)) { /* While mem buf aligned on CPU_ALIGN word boundaries, */
- *pmem_align++ = data_align; /* ... fill mem buf with CPU_ALIGN-sized data. */
- size_rem -= sizeof(CPU_ALIGN);
- }
- pmem_08 = (CPU_INT08U *)pmem_align;
- while (size_rem > 0) { /* Finish mem buf fill with trailing octets. */
- *pmem_08++ = data_val;
- size_rem -= sizeof(CPU_INT08U);
- }
- }
- /*
- *********************************************************************************************************
- * Mem_Copy()
- *
- * Description : Copies data octets from one memory buffer to another memory buffer.
- *
- * Argument(s) : pdest Pointer to destination memory buffer.
- *
- * psrc Pointer to source memory buffer.
- *
- * size Number of octets to copy (see Note #1).
- *
- * Return(s) : none.
- *
- * Caller(s) : Application.
- *
- * Note(s) : (1) Null copies allowed (i.e. zero-length copies).
- *
- * (2) Memory buffers NOT checked for overlapping.
- *
- * (a) IEEE Std 1003.1, 2004 Edition, Section 'memcpy() : DESCRIPTION' states that "if
- * copying takes place between objects that overlap, the behavior is undefined".
- *
- * (b) However, data octets from a source memory buffer at a higher address value SHOULD
- * successfully copy to a destination memory buffer at a lower address value even
- * if any octets of the memory buffers overlap as long as no individual, atomic CPU
- * word copy overlaps.
- *
- * Since Mem_Copy() performs the data octet copy via 'CPU_ALIGN'-sized words &/or
- * octets; & since 'CPU_ALIGN'-sized words MUST be accessed on word-aligned addresses
- * (see Note #3b), neither 'CPU_ALIGN'-sized words nor octets at unique addresses can
- * ever overlap.
- *
- * Therefore, Mem_Copy() SHOULD be able to successfully copy overlapping memory
- * buffers as long as the source memory buffer is at a higher address value than the
- * destination memory buffer.
- *
- * (3) For best CPU performance, optimized to copy data buffer using 'CPU_ALIGN'-sized data
- * words. Since many word-aligned processors REQUIRE that multi-octet words be accessed on
- * word-aligned addresses, 'CPU_ALIGN'-sized words MUST be accessed on 'CPU_ALIGN'd
- * addresses.
- *
- * (4) Modulo arithmetic is used to determine whether a memory buffer starts on a 'CPU_ALIGN'
- * address boundary.
- *
- * Modulo arithmetic in ANSI-C REQUIREs operations performed on integer values. Thus
- * address values MUST be cast to an appropriately-sized integer value PRIOR to any
- * 'mem_align_mod' arithmetic operation.
- *********************************************************************************************************
- */
- #if (LIB_MEM_CFG_OPTIMIZE_ASM_EN != DEF_ENABLED)
- void Mem_Copy ( void *pdest,
- const void *psrc,
- CPU_SIZE_T size)
- {
- CPU_SIZE_T size_rem;
- CPU_SIZE_T mem_gap_octets;
- CPU_ALIGN *pmem_align_dest;
- const CPU_ALIGN *pmem_align_src;
- CPU_INT08U *pmem_08_dest;
- const CPU_INT08U *pmem_08_src;
- CPU_DATA i;
- CPU_DATA mem_align_mod_dest;
- CPU_DATA mem_align_mod_src;
- CPU_BOOLEAN mem_aligned;
- #if (LIB_MEM_CFG_ARG_CHK_EXT_EN == DEF_ENABLED)
- if (size < 1) { /* See Note #1. */
- return;
- }
- if (pdest == (void *)0) {
- return;
- }
- if (psrc == (void *)0) {
- return;
- }
- #endif
- size_rem = size;
- pmem_08_dest = ( CPU_INT08U *)pdest;
- pmem_08_src = (const CPU_INT08U *)psrc;
- mem_gap_octets = pmem_08_src - pmem_08_dest;
- if (mem_gap_octets >= sizeof(CPU_ALIGN)) { /* Avoid bufs overlap. */
- /* See Note #4. */
- mem_align_mod_dest = (CPU_INT08U)((CPU_ADDR)pmem_08_dest % sizeof(CPU_ALIGN));
- mem_align_mod_src = (CPU_INT08U)((CPU_ADDR)pmem_08_src % sizeof(CPU_ALIGN));
- mem_aligned = (mem_align_mod_dest == mem_align_mod_src) ? DEF_YES : DEF_NO;
- if (mem_aligned == DEF_YES) { /* If mem bufs' alignment offset equal, ... */
- /* ... optimize copy for mem buf alignment. */
- if (mem_align_mod_dest != 0u) { /* If leading octets avail, ... */
- i = mem_align_mod_dest;
- while ((size_rem > 0) && /* ... start mem buf copy with leading octets ... */
- (i < sizeof(CPU_ALIGN ))) { /* ... until next CPU_ALIGN word boundary. */
- *pmem_08_dest++ = *pmem_08_src++;
- size_rem -= sizeof(CPU_INT08U);
- i++;
- }
- }
- pmem_align_dest = ( CPU_ALIGN *)pmem_08_dest; /* See Note #3. */
- pmem_align_src = (const CPU_ALIGN *)pmem_08_src;
- while (size_rem >= sizeof(CPU_ALIGN)) { /* While mem bufs aligned on CPU_ALIGN word boundaries, */
- *pmem_align_dest++ = *pmem_align_src++; /* ... copy psrc to pdest with CPU_ALIGN-sized words. */
- size_rem -= sizeof(CPU_ALIGN);
- }
- pmem_08_dest = ( CPU_INT08U *)pmem_align_dest;
- pmem_08_src = (const CPU_INT08U *)pmem_align_src;
- }
- }
- while (size_rem > 0) { /* For unaligned mem bufs or trailing octets, ... */
- *pmem_08_dest++ = *pmem_08_src++; /* ... copy psrc to pdest by octets. */
- size_rem -= sizeof(CPU_INT08U);
- }
- }
- #endif
- /*
- *********************************************************************************************************
- * Mem_Move()
- *
- * Description : Moves data octets from one memory buffer to another memory buffer, or within the same
- * memory buffer. Overlapping is correctly handled for all move operations.
- *
- * Argument(s) : pdest Pointer to destination memory buffer.
- *
- * psrc Pointer to source memory buffer.
- *
- * size Number of octets to move (see Note #1).
- *
- * Return(s) : none.
- *
- * Caller(s) : Application.
- *
- * Note(s) : (1) Null move operations allowed (i.e. zero-length).
- *
- * (2) Memory buffers checked for overlapping.
- *
- * (3) For best CPU performance, optimized to copy data buffer using 'CPU_ALIGN'-sized data
- * words. Since many word-aligned processors REQUIRE that multi-octet words be accessed on
- * word-aligned addresses, 'CPU_ALIGN'-sized words MUST be accessed on 'CPU_ALIGN'd
- * addresses.
- *
- * (4) Modulo arithmetic is used to determine whether a memory buffer starts on a 'CPU_ALIGN'
- * address boundary.
- *
- * Modulo arithmetic in ANSI-C REQUIREs operations performed on integer values. Thus
- * address values MUST be cast to an appropriately-sized integer value PRIOR to any
- * 'mem_align_mod' arithmetic operation.
- *********************************************************************************************************
- */
- void Mem_Move ( void *pdest,
- const void *psrc,
- CPU_SIZE_T size)
- {
- CPU_SIZE_T size_rem;
- CPU_SIZE_T mem_gap_octets;
- CPU_ALIGN *pmem_align_dest;
- const CPU_ALIGN *pmem_align_src;
- CPU_INT08U *pmem_08_dest;
- const CPU_INT08U *pmem_08_src;
- CPU_INT08S i;
- CPU_DATA mem_align_mod_dest;
- CPU_DATA mem_align_mod_src;
- CPU_BOOLEAN mem_aligned;
- #if (LIB_MEM_CFG_ARG_CHK_EXT_EN == DEF_ENABLED)
- if (size < 1) {
- return;
- }
- if (pdest == (void *)0) {
- return;
- }
- if (psrc == (void *)0) {
- return;
- }
- #endif
- pmem_08_src = (const CPU_INT08U *)psrc;
- pmem_08_dest = ( CPU_INT08U *)pdest;
- if (pmem_08_src > pmem_08_dest) {
- Mem_Copy(pdest, psrc, size);
- return;
- }
- size_rem = size;
- pmem_08_dest = ( CPU_INT08U *)pdest + size - 1;
- pmem_08_src = (const CPU_INT08U *)psrc + size - 1;
- mem_gap_octets = pmem_08_dest - pmem_08_src;
- if (mem_gap_octets >= sizeof(CPU_ALIGN)) { /* Avoid bufs overlap. */
- /* See Note #4. */
- mem_align_mod_dest = (CPU_INT08U)((CPU_ADDR)pmem_08_dest % sizeof(CPU_ALIGN));
- mem_align_mod_src = (CPU_INT08U)((CPU_ADDR)pmem_08_src % sizeof(CPU_ALIGN));
- mem_aligned = (mem_align_mod_dest == mem_align_mod_src) ? DEF_YES : DEF_NO;
- if (mem_aligned == DEF_YES) { /* If mem bufs' alignment offset equal, ... */
- /* ... optimize copy for mem buf alignment. */
- if (mem_align_mod_dest != (sizeof(CPU_ALIGN) - 1)) {/* If leading octets avail, ... */
- i = mem_align_mod_dest;
- while ((size_rem > 0) && /* ... start mem buf copy with leading octets ... */
- (i >= 0)) { /* ... until next CPU_ALIGN word boundary. */
- *pmem_08_dest-- = *pmem_08_src--;
- size_rem -= sizeof(CPU_INT08U);
- i--;
- }
- }
- /* See Note #3. */
- pmem_align_dest = ( CPU_ALIGN *)((CPU_INT08U *)pmem_08_dest - sizeof(CPU_ALIGN) + 1);
- pmem_align_src = (const CPU_ALIGN *)((CPU_INT08U *)pmem_08_src - sizeof(CPU_ALIGN) + 1);
- while (size_rem >= sizeof(CPU_ALIGN)) { /* While mem bufs aligned on CPU_ALIGN word boundaries, */
- *pmem_align_dest-- = *pmem_align_src--; /* ... copy psrc to pdest with CPU_ALIGN-sized words. */
- size_rem -= sizeof(CPU_ALIGN);
- }
- pmem_08_dest = ( CPU_INT08U *)pmem_align_dest + sizeof(CPU_ALIGN) - 1;
- pmem_08_src = (const CPU_INT08U *)pmem_align_src + sizeof(CPU_ALIGN) - 1;
- }
- }
- while (size_rem > 0) { /* For unaligned mem bufs or trailing octets, ... */
- *pmem_08_dest-- = *pmem_08_src--; /* ... copy psrc to pdest by octets. */
- size_rem -= sizeof(CPU_INT08U);
- }
- }
- /*
- *********************************************************************************************************
- * Mem_Cmp()
- *
- * Description : Verifies that ALL data octets in two memory buffers are identical in sequence.
- *
- * Argument(s) : p1_mem Pointer to first memory buffer.
- *
- * p2_mem Pointer to second memory buffer.
- *
- * size Number of data buffer octets to compare (see Note #1).
- *
- * Return(s) : DEF_YES, if 'size' number of data octets are identical in both memory buffers.
- *
- * DEF_NO, otherwise.
- *
- * Caller(s) : Application.
- *
- * Note(s) : (1) Null compares allowed (i.e. zero-length compares); 'DEF_YES' returned to indicate
- * identical null compare.
- *
- * (2) Many memory buffer comparisons vary ONLY in the least significant octets -- e.g.
- * network address buffers. Consequently, memory buffer comparison is more efficient
- * if the comparison starts from the end of the memory buffers which will abort sooner
- * on dissimilar memory buffers that vary only in the least significant octets.
- *
- * (3) For best CPU performance, optimized to compare data buffers using 'CPU_ALIGN'-sized
- * data words. Since many word-aligned processors REQUIRE that multi-octet words be accessed on
- * word-aligned addresses, 'CPU_ALIGN'-sized words MUST be accessed on 'CPU_ALIGN'd
- * addresses.
- *
- * (4) Modulo arithmetic is used to determine whether a memory buffer starts on a 'CPU_ALIGN'
- * address boundary.
- *
- * Modulo arithmetic in ANSI-C REQUIREs operations performed on integer values. Thus
- * address values MUST be cast to an appropriately-sized integer value PRIOR to any
- * 'mem_align_mod' arithmetic operation.
- *********************************************************************************************************
- */
- CPU_BOOLEAN Mem_Cmp (const void *p1_mem,
- const void *p2_mem,
- CPU_SIZE_T size)
- {
- CPU_SIZE_T size_rem;
- CPU_ALIGN *p1_mem_align;
- CPU_ALIGN *p2_mem_align;
- const CPU_INT08U *p1_mem_08;
- const CPU_INT08U *p2_mem_08;
- CPU_DATA i;
- CPU_DATA mem_align_mod_1;
- CPU_DATA mem_align_mod_2;
- CPU_BOOLEAN mem_aligned;
- CPU_BOOLEAN mem_cmp;
- if (size < 1) { /* See Note #1. */
- return (DEF_YES);
- }
- if (p1_mem == (void *)0) {
- return (DEF_NO);
- }
- if (p2_mem == (void *)0) {
- return (DEF_NO);
- }
- mem_cmp = DEF_YES; /* Assume mem bufs are identical until cmp fails. */
- size_rem = size;
- /* Start @ end of mem bufs (see Note #2). */
- p1_mem_08 = (const CPU_INT08U *)p1_mem + size;
- p2_mem_08 = (const CPU_INT08U *)p2_mem + size;
- /* See Note #4. */
- mem_align_mod_1 = (CPU_INT08U)((CPU_ADDR)p1_mem_08 % sizeof(CPU_ALIGN));
- mem_align_mod_2 = (CPU_INT08U)((CPU_ADDR)p2_mem_08 % sizeof(CPU_ALIGN));
- mem_aligned = (mem_align_mod_1 == mem_align_mod_2) ? DEF_YES : DEF_NO;
- if (mem_aligned == DEF_YES) { /* If mem bufs' alignment offset equal, ... */
- /* ... optimize cmp for mem buf alignment. */
- if (mem_align_mod_1 != 0u) { /* If trailing octets avail, ... */
- i = mem_align_mod_1;
- while ((mem_cmp == DEF_YES) && /* ... cmp mem bufs while identical & ... */
- (size_rem > 0) && /* ... start mem buf cmp with trailing octets ... */
- (i > 0)) { /* ... until next CPU_ALIGN word boundary. */
- p1_mem_08--;
- p2_mem_08--;
- if (*p1_mem_08 != *p2_mem_08) { /* If ANY data octet(s) NOT identical, cmp fails. */
- mem_cmp = DEF_NO;
- }
- size_rem -= sizeof(CPU_INT08U);
- i--;
- }
- }
- if (mem_cmp == DEF_YES) { /* If cmp still identical, cmp aligned mem bufs. */
- p1_mem_align = (CPU_ALIGN *)p1_mem_08; /* See Note #3. */
- p2_mem_align = (CPU_ALIGN *)p2_mem_08;
- while ((mem_cmp == DEF_YES) && /* Cmp mem bufs while identical & ... */
- (size_rem >= sizeof(CPU_ALIGN))) { /* ... mem bufs aligned on CPU_ALIGN word boundaries. */
- p1_mem_align--;
- p2_mem_align--;
- if (*p1_mem_align != *p2_mem_align) { /* If ANY data octet(s) NOT identical, cmp fails. */
- mem_cmp = DEF_NO;
- }
- size_rem -= sizeof(CPU_ALIGN);
- }
- p1_mem_08 = (CPU_INT08U *)p1_mem_align;
- p2_mem_08 = (CPU_INT08U *)p2_mem_align;
- }
- }
- while ((mem_cmp == DEF_YES) && /* Cmp mem bufs while identical ... */
- (size_rem > 0)) { /* ... for unaligned mem bufs or trailing octets. */
- p1_mem_08--;
- p2_mem_08--;
- if (*p1_mem_08 != *p2_mem_08) { /* If ANY data octet(s) NOT identical, cmp fails. */
- mem_cmp = DEF_NO;
- }
- size_rem -= sizeof(CPU_INT08U);
- }
- return (mem_cmp);
- }
- /*
- *********************************************************************************************************
- * Mem_HeapAlloc()
- *
- * Description : Allocates a memory block from the heap memory segment.
- *
- * Argument(s) : size Size of memory block to allocate (in bytes).
- *
- * align Alignment of memory block to specific word boundary (in bytes).
- *
- * p_bytes_reqd Optional pointer to a variable to ... :
- *
- * (a) Return the number of bytes required to successfully
- * allocate the memory block, if any error(s);
- * (b) Return 0, otherwise.
- *
- * p_err Pointer to variable that will receive the return error code from this function :
- *
- * LIB_MEM_ERR_NONE Operation was successful.
- * LIB_MEM_ERR_HEAP_EMPTY No more memory available on heap.
- *
- * ---------------------RETURNED BY Mem_SegAllocInternal()---------------------
- * LIB_MEM_ERR_INVALID_MEM_ALIGN Invalid memory block alignment requested.
- * LIB_MEM_ERR_INVALID_MEM_SIZE Invalid memory block size specified.
- * LIB_MEM_ERR_NULL_PTR Error or segment data pointer NULL.
- *
- * Return(s) : Pointer to memory block, if NO error(s).
- *
- * Pointer to NULL, otherwise.
- *
- * Caller(s) : Application.
- *
- * Note(s) : (1) Pointers to variables that return values MUST be initialized PRIOR to all other
- * validation or function handling in case of any error(s).
- *
- * (2) This function is DEPRECATED and will be removed in a future version of this product.
- * Mem_SegAlloc(), Mem_SegAllocExt() or Mem_SegAllocHW() should be used instead.
- *********************************************************************************************************
- */
- #if (LIB_MEM_CFG_HEAP_SIZE > 0u)
- void *Mem_HeapAlloc (CPU_SIZE_T size,
- CPU_SIZE_T align,
- CPU_SIZE_T *p_bytes_reqd,
- LIB_ERR *p_err)
- {
- void *p_mem;
- p_mem = Mem_SegAllocInternal(DEF_NULL,
- &Mem_SegHeap,
- size,
- align,
- LIB_MEM_CFG_HEAP_PADDING_ALIGN,
- p_bytes_reqd,
- p_err);
- if (*p_err == LIB_MEM_ERR_SEG_OVF) {
- *p_err = LIB_MEM_ERR_HEAP_OVF;
- }
- return (p_mem);
- }
- #endif
- /*
- *********************************************************************************************************
- * Mem_HeapGetSizeRem()
- *
- * Description : Gets remaining heap memory size available to allocate.
- *
- * Argument(s) : align Desired word boundary alignment (in bytes) to return remaining memory size from.
- *
- * p_err Pointer to variable that will receive the return error code from this function
- *
- * LIB_MEM_ERR_NONE Operation was successful.
- *
- * --------------------RETURNED BY Mem_SegRemSizeGet()--------------------
- * LIB_MEM_ERR_NULL_PTR Segment data pointer NULL.
- * LIB_MEM_ERR_INVALID_MEM_ALIGN Invalid memory alignment.
- *
- * Return(s) : Remaining heap memory size (in bytes), if NO error(s).
- *
- * 0, otherwise.
- *
- * Caller(s) : Application.
- *
- * Note(s) : (1) This function is DEPRECATED and will be removed in a future version of this product.
- * Mem_SegRemSizeGet() should be used instead.
- *********************************************************************************************************
- */
- #if (LIB_MEM_CFG_HEAP_SIZE > 0u)
- CPU_SIZE_T Mem_HeapGetSizeRem (CPU_SIZE_T align,
- LIB_ERR *p_err)
- {
- CPU_SIZE_T rem_size;
- rem_size = Mem_SegRemSizeGet(&Mem_SegHeap,
- align,
- DEF_NULL,
- p_err);
- if (*p_err != LIB_MEM_ERR_NONE) {
- return (0u);
- }
- return (rem_size);
- }
- #endif
- /*
- *********************************************************************************************************
- * Mem_SegCreate()
- *
- * Description : Creates a new memory segment to be used for runtime memory allocation.
- *
- * Argument(s) : p_name Pointer to segment name.
- *
- * p_seg Pointer to segment data. Must be allocated by caller.
- *
- * seg_base_addr Address of segment's first byte.
- *
- * size Total size of segment, in bytes.
- *
- * padding_align Padding alignment, in bytes, that will be added to any allocated buffer from
- * this memory segment. MUST be a power of 2. LIB_MEM_PADDING_ALIGN_NONE
- * means no padding.
- *
- * p_err Pointer to variable that will receive the return error code from this function :
- *
- * LIB_MEM_ERR_NONE Operation was successful.
- * LIB_MEM_ERR_INVALID_SEG_SIZE Invalid segment size specified.
- * LIB_MEM_ERR_INVALID_MEM_ALIGN Invalid padding alignment.
- * LIB_MEM_ERR_NULL_PTR Error or segment data pointer NULL.
- *
- * -------------------RETURNED BY Mem_SegOverlapChkCritical()-------------------
- * LIB_MEM_ERR_INVALID_SEG_OVERLAP Segment overlaps another existing segment.
- * LIB_MEM_ERR_INVALID_SEG_EXISTS Segment already exists.
- *
- * Return(s) : None.
- *
- * Caller(s) : Application.
- *
- * Note(s) : (1) New segments are checked for overlap with existing segments. A critical section needs
- * to be maintained during the whole list search and add procedure to prevent a reentrant
- * call from creating another segment overlapping with the one being added.
- *********************************************************************************************************
- */
- void Mem_SegCreate (const CPU_CHAR *p_name,
- MEM_SEG *p_seg,
- CPU_ADDR seg_base_addr,
- CPU_SIZE_T size,
- CPU_SIZE_T padding_align,
- LIB_ERR *p_err)
- {
- CPU_SR_ALLOC();
- #if (LIB_MEM_CFG_ARG_CHK_EXT_EN == DEF_ENABLED)
- if (p_err == DEF_NULL) { /* Chk for null err ptr. */
- CPU_SW_EXCEPTION(;);
- }
- if (p_seg == DEF_NULL) { /* Chk for null seg ptr. */
- *p_err = LIB_MEM_ERR_NULL_PTR;
- return;
- }
- if (size < 1u) { /* Chk for invalid sized seg. */
- *p_err = LIB_MEM_ERR_INVALID_SEG_SIZE;
- return;
- }
- /* Chk for addr space ovf. */
- if (seg_base_addr + (size - 1u) < seg_base_addr) {
- *p_err = LIB_MEM_ERR_INVALID_SEG_SIZE;
- return;
- }
- if ((padding_align != LIB_MEM_PADDING_ALIGN_NONE) &&
- (MATH_IS_PWR2(padding_align) != DEF_YES)) {
- *p_err = LIB_MEM_ERR_INVALID_MEM_ALIGN;
- return;
- }
- #endif
- CPU_CRITICAL_ENTER();
- #if (LIB_MEM_CFG_ARG_CHK_EXT_EN == DEF_ENABLED)
- (void)Mem_SegOverlapChkCritical(seg_base_addr, /* Chk for overlap. */
- size,
- p_err);
- if (*p_err != LIB_MEM_ERR_NONE) {
- CPU_CRITICAL_EXIT();
- return;
- }
- #endif
- Mem_SegCreateCritical(p_name, /* Create seg. */
- p_seg,
- seg_base_addr,
- padding_align,
- size);
- CPU_CRITICAL_EXIT();
- *p_err = LIB_MEM_ERR_NONE;
- }
- /*
- *********************************************************************************************************
- * Mem_SegClr()
- *
- * Description : Clears a memory segment.
- *
- * Argument(s) : p_seg Pointer to segment data. Must be allocated by caller.
- *
- * p_err Pointer to variable that will receive the return error code from this function :
- *
- * LIB_MEM_ERR_NONE Operation was successful.
- * LIB_MEM_ERR_NULL_PTR Segment data pointer NULL.
- *
- * Return(s) : None.
- *
- * Caller(s) : Application.
- *
- * Note(s) : (1) This function must be used with extreme caution. It must only be called on memory
- * segments that are no longer used.
- *
- * (2) This function is disabled when debug mode is enabled to avoid heap memory leaks.
- *********************************************************************************************************
- */
- #if (LIB_MEM_CFG_DBG_INFO_EN == DEF_DISABLED)
- void Mem_SegClr (MEM_SEG *p_seg,
- LIB_ERR *p_err)
- {
- CPU_SR_ALLOC();
- #if (LIB_MEM_CFG_ARG_CHK_EXT_EN == DEF_ENABLED)
- if (p_err == DEF_NULL) { /* Chk for null err ptr. */
- CPU_SW_EXCEPTION(;);
- }
- if (p_seg == DEF_NULL) { /* Chk for null seg ptr. */
- *p_err = LIB_MEM_ERR_NULL_PTR;
- return;
- }
- #endif
- CPU_CRITICAL_ENTER();
- p_seg->AddrNext = p_seg->AddrBase;
- CPU_CRITICAL_EXIT();
- *p_err = LIB_MEM_ERR_NONE;
- }
- #endif
- /*
- *********************************************************************************************************
- * Mem_SegRemSizeGet()
- *
- * Description : Gets free space of memory segment.
- *
- * Argument(s) : p_seg Pointer to segment data.
- *
- * align Alignment in bytes to assume for calculation of free space.
- *
- * p_seg_info Pointer to structure that will receive further segment info data (used size,
- * total size, base address and next allocation address).
- *
- * p_err Pointer to variable that will receive the return error code from this function :
- *
- * LIB_MEM_ERR_NONE Operation was successful.
- * LIB_MEM_ERR_NULL_PTR Segment data pointer NULL.
- * LIB_MEM_ERR_INVALID_MEM_ALIGN Invalid memory alignment.
- *
- * Return(s) : Memory segment remaining size in bytes, if successful.
- * 0, otherwise or if memory segment empty.
- *
- * Caller(s) : Application,
- * Mem_HeapGetSizeRem(),
- * Mem_OutputUsage().
- *
- * Note(s) : None.
- *********************************************************************************************************
- */
- CPU_SIZE_T Mem_SegRemSizeGet (MEM_SEG *p_seg,
- CPU_SIZE_T align,
- MEM_SEG_INFO *p_seg_info,
- LIB_ERR *p_err)
- {
- CPU_SIZE_T rem_size;
- CPU_SIZE_T total_size;
- CPU_SIZE_T used_size;
- CPU_ADDR next_addr_align;
- CPU_SR_ALLOC();
- #if (LIB_MEM_CFG_ARG_CHK_EXT_EN == DEF_ENABLED)
- if (p_err == DEF_NULL) { /* Chk for null err ptr. */
- CPU_SW_EXCEPTION(seg_info);
- }
- if (MATH_IS_PWR2(align) != DEF_YES) { /* Chk for invalid align val. */
- *p_err = LIB_MEM_ERR_INVALID_MEM_ALIGN;
- return (0u);
- }
- #endif
- if (p_seg == DEF_NULL) { /* Dflt to heap in case p_seg is null. */
- #if (LIB_MEM_CFG_HEAP_SIZE > 0u)
- p_seg = &Mem_SegHeap;
- #else
- *p_err = LIB_MEM_ERR_NULL_PTR;
- return (0u);
- #endif
- }
- CPU_CRITICAL_ENTER(); /* Calc seg stats. */
- next_addr_align = MATH_ROUND_INC_UP_PWR2(p_seg->AddrNext, align);
- CPU_CRITICAL_EXIT();
- total_size = p_seg->AddrEnd - p_seg->AddrBase + 1u;
- used_size = next_addr_align - p_seg->AddrBase;
- rem_size = total_size - used_size;
- if (p_seg_info != DEF_NULL) {
- p_seg_info->TotalSize = total_size;
- p_seg_info->UsedSize = used_size;
- p_seg_info->AddrBase = p_seg->AddrBase;
- p_seg_info->AddrNextAlloc = next_addr_align;
- }
- *p_err = LIB_MEM_ERR_NONE;
- return (rem_size);
- }
- /*
- *********************************************************************************************************
- * Mem_SegAlloc()
- *
- * Description : Allocates memory from specified segment. Returned memory block will be aligned on a CPU
- * word boundary.
- *
- * Argument(s) : p_name Pointer to allocated object name. Used for allocations tracking. May be DEF_NULL.
- *
- * p_seg Pointer to segment from which to allocate memory. Will be allocated from
- * general-purpose heap if null.
- *
- * size Size of memory block to allocate, in bytes.
- *
- * p_err Pointer to variable that will receive the return error code from this function :
- *
- * LIB_MEM_ERR_NONE Operation was successful.
- *
- * ------------------RETURNED BY Mem_SegAllocInternal()-------------------
- * LIB_MEM_ERR_INVALID_MEM_ALIGN Invalid memory block alignment requested.
- * LIB_MEM_ERR_INVALID_MEM_SIZE Invalid memory block size specified.
- * LIB_MEM_ERR_NULL_PTR Error or segment data pointer NULL.
- * LIB_MEM_ERR_SEG_OVF Allocation would overflow memory segment.
- *
- * Return(s) : Pointer to allocated memory block, if successful.
- *
- * DEF_NULL, otherwise.
- *
- * Caller(s) : Application.
- *
- * Note(s) : (1) The memory block returned by this function will be aligned on a word boundary. In
- * order to specify a specific alignment value, use either Mem_SegAllocExt() or
- * Mem_SegAllocHW().
- *********************************************************************************************************
- */
- void *Mem_SegAlloc (const CPU_CHAR *p_name,
- MEM_SEG *p_seg,
- CPU_SIZE_T size,
- LIB_ERR *p_err)
- {
- void *p_blk;
- if (p_seg == DEF_NULL) { /* Alloc from heap if p_seg is null. */
- #if (LIB_MEM_CFG_HEAP_SIZE > 0u)
- p_seg = &Mem_SegHeap;
- #else
- *p_err = LIB_MEM_ERR_NULL_PTR;
- return (DEF_NULL);
- #endif
- }
- p_blk = Mem_SegAllocInternal(p_name,
- p_seg,
- size,
- sizeof(CPU_ALIGN),
- LIB_MEM_PADDING_ALIGN_NONE,
- DEF_NULL,
- p_err);
- return (p_blk);
- }
- /*
- *********************************************************************************************************
- * Mem_SegAllocExt()
- *
- * Description : Allocates memory from specified memory segment.
- *
- * Argument(s) : p_name Pointer to allocated object name. Used for allocations tracking. May be DEF_NULL.
- *
- * p_seg Pointer to segment from which to allocate memory. Will be allocated from
- * general-purpose heap if null.
- *
- * size Size of memory block to allocate, in bytes.
- *
- * align Required alignment of memory block, in bytes. MUST be a power of 2.
- *
- * p_bytes_reqd Pointer to variable that will receive the number of free bytes missing for
- * the allocation to succeed. Set to DEF_NULL to skip calculation.
- *
- * p_err Pointer to variable that will receive the return error code from this function :
- *
- * LIB_MEM_ERR_NONE Operation was successful.
- *
- * ------------------RETURNED BY Mem_SegAllocInternal()-------------------
- * LIB_MEM_ERR_INVALID_MEM_ALIGN Invalid memory block alignment requested.
- * LIB_MEM_ERR_INVALID_MEM_SIZE Invalid memory block size specified.
- * LIB_MEM_ERR_NULL_PTR Error or segment data pointer NULL.
- * LIB_MEM_ERR_SEG_OVF Allocation would overflow memory segment.
- *
- * Return(s) : Pointer to allocated memory block, if successful.
- *
- * DEF_NULL, otherwise.
- *
- * Caller(s) : Application.
- *
- * Note(s) : none.
- *********************************************************************************************************
- */
- void *Mem_SegAllocExt (const CPU_CHAR *p_name,
- MEM_SEG *p_seg,
- CPU_SIZE_T size,
- CPU_SIZE_T align,
- CPU_SIZE_T *p_bytes_reqd,
- LIB_ERR *p_err)
- {
- void *p_blk;
- if (p_seg == DEF_NULL) { /* Alloc from heap if p_seg is null. */
- #if (LIB_MEM_CFG_HEAP_SIZE > 0u)
- p_seg = &Mem_SegHeap;
- #else
- *p_err = LIB_MEM_ERR_NULL_PTR;
- return (DEF_NULL);
- #endif
- }
- p_blk = Mem_SegAllocInternal(p_name,
- p_seg,
- size,
- align,
- LIB_MEM_PADDING_ALIGN_NONE,
- p_bytes_reqd,
- p_err);
- return (p_blk);
- }
- /*
- *********************************************************************************************************
- * Mem_SegAllocHW()
- *
- * Description : Allocates memory from specified segment. The returned buffer will be padded in function
- * of memory segment's properties.
- *
- * Argument(s) : p_name Pointer to allocated object name. Used for allocations tracking. May be DEF_NULL.
- *
- * p_seg Pointer to segment from which to allocate memory. Will be allocated from
- * general-purpose heap if null.
- *
- * size Size of memory block to allocate, in bytes.
- *
- * align Required alignment of memory block, in bytes. MUST be a power of 2.
- *
- * p_bytes_reqd Pointer to variable that will receive the number of free bytes missing for
- * the allocation to succeed. Set to DEF_NULL to skip calculation.
- *
- * p_err Pointer to variable that will receive the return error code from this function :
- *
- * LIB_MEM_ERR_NONE Operation was successful.
- *
- * ------------------RETURNED BY Mem_SegAllocInternal()-------------------
- * LIB_MEM_ERR_INVALID_MEM_ALIGN Invalid memory block alignment requested.
- * LIB_MEM_ERR_INVALID_MEM_SIZE Invalid memory block size specified.
- * LIB_MEM_ERR_NULL_PTR Error or segment data pointer NULL.
- * LIB_MEM_ERR_SEG_OVF Allocation would overflow memory segment.
- *
- * Return(s) : Pointer to allocated memory block, if successful.
- *
- * DEF_NULL, otherwise.
- *
- * Caller(s) : Application.
- *
- * Note(s) : none.
- *********************************************************************************************************
- */
- void *Mem_SegAllocHW (const CPU_CHAR *p_name,
- MEM_SEG *p_seg,
- CPU_SIZE_T size,
- CPU_SIZE_T align,
- CPU_SIZE_T *p_bytes_reqd,
- LIB_ERR *p_err)
- {
- void *p_blk;
- if (p_seg == DEF_NULL) { /* Alloc from heap if p_seg is null. */
- #if (LIB_MEM_CFG_HEAP_SIZE > 0u)
- p_seg = &Mem_SegHeap;
- #else
- *p_err = LIB_MEM_ERR_NULL_PTR;
- return (DEF_NULL);
- #endif
- }
- p_blk = Mem_SegAllocInternal(p_name,
- p_seg,
- size,
- align,
- p_seg->PaddingAlign,
- p_bytes_reqd,
- p_err);
- return (p_blk);
- }
- /*
- *********************************************************************************************************
- * Mem_PoolCreate()
- *
- * Description : (1) Creates a memory pool :
- *
- * (a) Create memory pool from heap or dedicated memory
- * (b) Allocate memory pool memory blocks
- * (c) Configure memory pool
- *
- *
- * Argument(s) : p_pool Pointer to a memory pool structure to create (see Note #1).
- *
- * p_mem_base Memory pool segment base address :
- *
- * (a) Null address Memory pool allocated from general-purpose heap.
- * (b) Non-null address Memory pool allocated from dedicated memory
- * specified by its base address.
- *
- * mem_size Size of memory pool segment (in bytes).
- *
- * blk_nbr Number of memory pool blocks to create.
- *
- * blk_size Size of memory pool blocks to create (in bytes).
- *
- * blk_align Alignment of memory pool blocks to specific word boundary (in bytes).
- *
- * p_bytes_reqd Optional pointer to a variable to ... :
- *
- * (a) Return the number of bytes required to successfully
- * allocate the memory pool, if any error(s);
- * (b) Return 0, otherwise.
- *
- * p_err Pointer to variable that will receive the return error code from this function :
- *
- * LIB_MEM_ERR_NONE Operation was successful.
- * LIB_MEM_ERR_NULL_PTR Pointer to memory pool is null.
- * LIB_MEM_ERR_INVALID_BLK_ALIGN Invalid block alignment requested.
- * LIB_MEM_ERR_INVALID_BLK_NBR Invalid number of blocks specified.
- * LIB_MEM_ERR_INVALID_BLK_SIZE Invalid block size specified.
- * LIB_MEM_ERR_INVALID_SEG_SIZE Invalid segment size.
- * LIB_MEM_ERR_HEAP_EMPTY No more memory available on heap.
- *
- * ---------------RETURNED BY Mem_SegOverlapChkCritical()----------------
- * LIB_MEM_ERR_INVALID_SEG_EXISTS Segment already exists.
- * LIB_MEM_ERR_INVALID_SEG_OVERLAP Segment overlaps another existing segment.
- *
- * -----------------RETURNED BY Mem_SegAllocExtCritical()-----------------
- * LIB_MEM_ERR_SEG_OVF Allocation would overflow memory segment.
- *
- * ------------------RETURNED BY Mem_SegAllocInternal()-------------------
- * LIB_MEM_ERR_INVALID_MEM_ALIGN Invalid memory block alignment requested.
- * LIB_MEM_ERR_INVALID_MEM_SIZE Invalid memory block size specified.
- * LIB_MEM_ERR_NULL_PTR Error or segment data pointer NULL.
- * LIB_MEM_ERR_SEG_OVF Allocation would overflow memory segment.
- *
- * -----------------------RETURNED BY Mem_PoolClr()-----------------------
- * LIB_MEM_ERR_NULL_PTR Argument 'p_pool' passed a NULL pointer.
- *
- * Return(s) : none.
- *
- * Caller(s) : Application.
- *
- * Note(s) : (1) This function is DEPRECATED and will be removed in a future version of this product.
- * Mem_DynPoolCreate() or Mem_DynPoolCreateHW() should be used instead.
- *********************************************************************************************************
- */
- #if (LIB_MEM_CFG_HEAP_SIZE > 0u)
- void Mem_PoolCreate (MEM_POOL *p_pool,
- void *p_mem_base,
- CPU_SIZE_T mem_size,
- MEM_POOL_BLK_QTY blk_nbr,
- CPU_SIZE_T blk_size,
- CPU_SIZE_T blk_align,
- CPU_SIZE_T *p_bytes_reqd,
- LIB_ERR *p_err)
- {
- MEM_SEG *p_seg;
- void *p_pool_mem;
- CPU_SIZE_T pool_size;
- CPU_SIZE_T blk_size_align;
- CPU_ADDR pool_addr_end;
- MEM_POOL_BLK_QTY blk_ix;
- CPU_INT08U *p_blk;
- CPU_SR_ALLOC();
- #if (LIB_MEM_CFG_ARG_CHK_EXT_EN == DEF_ENABLED) /* --------------- VALIDATE RTN ERR PTR --------------- */
- if (p_err == DEF_NULL) {
- CPU_SW_EXCEPTION(;);
- }
- /* ------------- VALIDATE MEM POOL CREATE ------------- */
- if (p_pool == DEF_NULL) {
- *p_err = LIB_MEM_ERR_NULL_PTR;
- return;
- }
- if (p_mem_base != DEF_NULL) {
- if (mem_size < 1u) {
- *p_err = LIB_MEM_ERR_INVALID_SEG_SIZE;
- return;
- }
- }
- if (blk_nbr < 1u) {
- *p_err = LIB_MEM_ERR_INVALID_BLK_NBR;
- return;
- }
- if (blk_size < 1u) {
- *p_err = LIB_MEM_ERR_INVALID_BLK_SIZE;
- return;
- }
- if (MATH_IS_PWR2(blk_align) != DEF_YES) { /* Chk that req alignment is a pwr of 2. */
- *p_err = LIB_MEM_ERR_INVALID_BLK_ALIGN;
- return;
- }
- #endif
- Mem_PoolClr(p_pool, p_err); /* Init mem pool. */
- if (*p_err != LIB_MEM_ERR_NONE) {
- return;
- }
- /* -------- DETERMINE AND/OR ALLOC SEG TO USE --------- */
- if (p_mem_base == DEF_NULL) { /* Use heap seg. */
- p_seg = &Mem_SegHeap;
- } else { /* Use other seg. */
- CPU_CRITICAL_ENTER();
- p_seg = Mem_SegOverlapChkCritical((CPU_ADDR)p_mem_base,
- mem_size,
- p_err);
- switch (*p_err) {
- case LIB_MEM_ERR_INVALID_SEG_EXISTS: /* Seg already exists. */
- break;
- case LIB_MEM_ERR_NONE: /* Seg must be created. */
- p_seg = (MEM_SEG *)Mem_SegAllocExtCritical(&Mem_SegHeap,
- sizeof(MEM_SEG),
- sizeof(CPU_ALIGN),
- LIB_MEM_PADDING_ALIGN_NONE,
- p_bytes_reqd,
- p_err);
- if (*p_err != LIB_MEM_ERR_NONE) {
- CPU_CRITICAL_EXIT();
- return;
- }
- #if (LIB_MEM_CFG_DBG_INFO_EN == DEF_ENABLED) /* Track alloc if req'd. */
- Mem_SegAllocTrackCritical("Unknown segment data",
- &Mem_SegHeap,
- sizeof(MEM_SEG),
- p_err);
- if (*p_err != LIB_MEM_ERR_NONE) {
- CPU_CRITICAL_EXIT();
- return;
- }
- #endif
- Mem_SegCreateCritical( DEF_NULL,
- p_seg,
- (CPU_ADDR)p_mem_base,
- LIB_MEM_PADDING_ALIGN_NONE,
- mem_size);
- break;
- case LIB_MEM_ERR_INVALID_SEG_OVERLAP:
- default:
- CPU_CRITICAL_EXIT();
- return; /* Prevent 'break NOT reachable' compiler warning. */
- }
- CPU_CRITICAL_EXIT();
- }
- /* ---------------- ALLOC MEM FOR POOL ---------------- */
- /* Calc blk size with align. */
- blk_size_align = MATH_ROUND_INC_UP_PWR2(blk_size, blk_align);
- pool_size = blk_size_align * blk_nbr; /* Calc required size for pool. */
- /* Alloc mem for pool. */
- p_pool_mem = (void *)Mem_SegAllocInternal("Unnamed static pool",
- p_seg,
- pool_size,
- blk_align,
- LIB_MEM_PADDING_ALIGN_NONE,
- p_bytes_reqd,
- p_err);
- if (*p_err != LIB_MEM_ERR_NONE) {
- return;
- }
- /* ------------ ALLOC MEM FOR FREE BLK TBL ------------ */
- p_pool->BlkFreeTbl = (void *)Mem_SegAllocInternal("Unnamed static pool free blk tbl",
- &Mem_SegHeap,
- blk_nbr * sizeof(void *),
- sizeof(CPU_ALIGN),
- LIB_MEM_PADDING_ALIGN_NONE,
- p_bytes_reqd,
- p_err);
- if (*p_err != LIB_MEM_ERR_NONE) {
- return;
- }
- /* ------------------ INIT BLK LIST ------------------- */
- p_blk = (CPU_INT08U *)p_pool_mem;
- for (blk_ix = 0; blk_ix < blk_nbr; blk_ix++) {
- p_pool->BlkFreeTbl[blk_ix] = p_blk;
- p_blk += blk_size_align;
- }
- /* ------------------ INIT POOL DATA ------------------ */
- pool_addr_end = (CPU_ADDR)p_pool_mem + (pool_size - 1u);
- p_pool->PoolAddrStart = p_pool_mem;
- p_pool->PoolAddrEnd = (void *)pool_addr_end;
- p_pool->BlkNbr = blk_nbr;
- p_pool->BlkSize = blk_size_align;
- p_pool->BlkFreeTblIx = blk_nbr;
- }
- #endif
- /*
- *********************************************************************************************************
- * Mem_PoolClr()
- *
- * Description : Clears a memory pool (see Note #1).
- *
- * Argument(s) : p_pool Pointer to a memory pool structure to clear (see Note #2).
- *
- * p_err Pointer to variable that will receive the return error code from this function :
- *
- * LIB_MEM_ERR_NONE Operation was successful.
- * LIB_MEM_ERR_NULL_PTR Argument 'p_pool' passed a NULL pointer.
- *
- * Return(s) : none.
- *
- * Caller(s) : Application,
- * Mem_PoolCreate().
- *
- * Note(s) : (1) (a) Mem_PoolClr() ONLY clears a memory pool structure's variables & should ONLY be
- * called to initialize a memory pool structure prior to calling Mem_PoolCreate().
- *
- * (b) Mem_PoolClr() does NOT deallocate memory from the memory pool or deallocate the
- * memory pool itself & MUST NOT be called after calling Mem_PoolCreate() since
- * this will likely corrupt the memory pool management.
- *
- * (2) Assumes 'p_pool' points to a valid memory pool (if non-NULL).
- *
- * (3) This function is DEPRECATED and will be removed in a future version of this product.
- *********************************************************************************************************
- */
- #if (LIB_MEM_CFG_HEAP_SIZE > 0u)
- void Mem_PoolClr (MEM_POOL *p_pool,
- LIB_ERR *p_err)
- {
- #if (LIB_MEM_CFG_ARG_CHK_EXT_EN == DEF_ENABLED) /* -------------- VALIDATE RTN ERR PTR --------------- */
- if (p_err == DEF_NULL) {
- CPU_SW_EXCEPTION(;);
- }
- /* -------------- VALIDATE MEM POOL PTR --------------- */
- if (p_pool == DEF_NULL) {
- *p_err = LIB_MEM_ERR_NULL_PTR;
- return;
- }
- #endif
- p_pool->PoolAddrStart = DEF_NULL;
- p_pool->PoolAddrEnd = DEF_NULL;
- p_pool->BlkSize = 0u;
- p_pool->BlkNbr = 0u;
- p_pool->BlkFreeTbl = DEF_NULL;
- p_pool->BlkFreeTblIx = 0u;
- *p_err = LIB_MEM_ERR_NONE;
- }
- #endif
- /*
- *********************************************************************************************************
- * Mem_PoolBlkGet()
- *
- * Description : Gets a memory block from memory pool.
- *
- * Argument(s) : p_pool Pointer to memory pool to get memory block from.
- *
- * size Size of requested memory (in bytes).
- *
- * p_err Pointer to variable that will receive the return error code from this function :
- *
- * LIB_MEM_ERR_NONE Operation was successful.
- * LIB_MEM_ERR_INVALID_BLK_SIZE Invalid memory pool block size requested.
- * LIB_MEM_ERR_NULL_PTR Argument 'p_pool' passed a NULL pointer.
- * LIB_MEM_ERR_POOL_EMPTY NO memory blocks available in memory pool.
- *
- * Return(s) : Pointer to memory block, if NO error(s).
- *
- * Pointer to NULL, otherwise.
- *
- * Caller(s) : Application.
- *
- * Note(s) : (1) This function is DEPRECATED and will be removed in a future version of this product.
- * Mem_DynPoolBlkGet() should be used instead.
- *********************************************************************************************************
- */
- #if (LIB_MEM_CFG_HEAP_SIZE > 0u)
- void *Mem_PoolBlkGet (MEM_POOL *p_pool,
- CPU_SIZE_T size,
- LIB_ERR *p_err)
- {
- CPU_INT08U *p_blk;
- CPU_SR_ALLOC();
- (void)&size; /* Prevent possible 'variable unused' warning. */
- #if (LIB_MEM_CFG_ARG_CHK_EXT_EN == DEF_ENABLED) /* -------------- VALIDATE MEM POOL GET --------------- */
- if (p_err == DEF_NULL) { /* Validate err ptr. */
- CPU_SW_EXCEPTION(DEF_NULL);
- }
- if (p_pool == DEF_NULL) { /* Validate pool ptr. */
- *p_err = LIB_MEM_ERR_NULL_PTR;
- return (DEF_NULL);
- }
- if (size < 1u) { /* Validate req'd size as non-NULL. */
- *p_err = LIB_MEM_ERR_INVALID_BLK_SIZE;
- return (DEF_NULL);
- }
- if (size > p_pool->BlkSize) { /* Validate req'd size <= mem pool blk size. */
- *p_err = LIB_MEM_ERR_INVALID_BLK_SIZE;
- return (DEF_NULL);
- }
- #endif
- /* -------------- GET MEM BLK FROM POOL --------------- */
- p_blk = DEF_NULL;
- CPU_CRITICAL_ENTER();
- if (p_pool->BlkFreeTblIx > 0u) {
- p_pool->BlkFreeTblIx -= 1u;
- p_blk = p_pool->BlkFreeTbl[p_pool->BlkFreeTblIx];
- p_pool->BlkFreeTbl[p_pool->BlkFreeTblIx] = DEF_NULL;
- }
- CPU_CRITICAL_EXIT();
- if (p_blk == DEF_NULL) {
- *p_err = LIB_MEM_ERR_POOL_EMPTY;
- } else {
- *p_err = LIB_MEM_ERR_NONE;
- }
- return (p_blk);
- }
- #endif
- /*
- *********************************************************************************************************
- * Mem_PoolBlkFree()
- *
- * Description : Free a memory block to memory pool.
- *
- * Argument(s) : p_pool Pointer to memory pool to free memory block.
- *
- * p_blk Pointer to memory block address to free.
- *
- * p_err Pointer to variable that will receive the return error code from this function :
- *
- * LIB_MEM_ERR_NONE Operation was successful.
- * LIB_MEM_ERR_NULL_PTR Argument 'p_pool'/'p_blk' passed
- * a NULL pointer.
- * LIB_MEM_ERR_INVALID_BLK_ADDR Invalid memory block address.
- * LIB_MEM_ERR_INVALID_BLK_ADDR_IN_POOL Memory block address already
- * in memory pool.
- * LIB_MEM_ERR_POOL_FULL Pool is full.
- *
- * Return(s) : none.
- *
- * Caller(s) : Application.
- *
- * Note(s) : (1) This function is DEPRECATED and will be removed in a future version of this product.
- * Mem_DynPoolBlkFree() should be used instead.
- *********************************************************************************************************
- */
- #if (LIB_MEM_CFG_HEAP_SIZE > 0u)
- void Mem_PoolBlkFree (MEM_POOL *p_pool,
- void *p_blk,
- LIB_ERR *p_err)
- {
- #if (LIB_MEM_CFG_ARG_CHK_EXT_EN == DEF_ENABLED)
- CPU_SIZE_T tbl_ix;
- CPU_BOOLEAN addr_valid;
- #endif
- CPU_SR_ALLOC();
- #if (LIB_MEM_CFG_ARG_CHK_EXT_EN == DEF_ENABLED) /* -------------- VALIDATE MEM POOL FREE -------------- */
- if (p_err == DEF_NULL) {
- CPU_SW_EXCEPTION(;);
- }
- if (p_pool == DEF_NULL) { /* Validate mem ptrs. */
- *p_err = LIB_MEM_ERR_NULL_PTR;
- return;
- }
- if (p_blk == DEF_NULL) {
- *p_err = LIB_MEM_ERR_NULL_PTR;
- return;
- }
- addr_valid = Mem_PoolBlkIsValidAddr(p_pool, p_blk); /* Validate mem blk as valid pool blk addr. */
- if (addr_valid != DEF_OK) {
- *p_err = LIB_MEM_ERR_INVALID_BLK_ADDR;
- return;
- }
- CPU_CRITICAL_ENTER(); /* Make sure blk isn't already in free list. */
- for (tbl_ix = 0u; tbl_ix < p_pool->BlkNbr; tbl_ix++) {
- if (p_pool->BlkFreeTbl[tbl_ix] == p_blk) {
- CPU_CRITICAL_EXIT();
- *p_err = LIB_MEM_ERR_INVALID_BLK_ADDR_IN_POOL;
- return;
- }
- }
- #else /* Double-free possibility if not in critical section. */
- CPU_CRITICAL_ENTER();
- #endif
- /* --------------- FREE MEM BLK TO POOL --------------- */
- if (p_pool->BlkFreeTblIx >= p_pool->BlkNbr) {
- CPU_CRITICAL_EXIT();
- *p_err = LIB_MEM_ERR_POOL_FULL;
- return;
- }
- p_pool->BlkFreeTbl[p_pool->BlkFreeTblIx] = p_blk;
- p_pool->BlkFreeTblIx += 1u;
- CPU_CRITICAL_EXIT();
- *p_err = LIB_MEM_ERR_NONE;
- }
- #endif
- /*
- *********************************************************************************************************
- * Mem_PoolBlkGetNbrAvail()
- *
- * Description : Get memory pool's remaining number of blocks available to allocate.
- *
- * Argument(s) : p_pool Pointer to a memory pool structure.
- *
- * p_err Pointer to variable that will receive the return error code from this function :
- *
- * LIB_MEM_ERR_NONE Operation was successful.
- * LIB_MEM_ERR_NULL_PTR Argument 'p_pool' passed a NULL pointer.
- *
- * Return(s) : Remaining memory pool blocks, if NO error(s).
- *
- * 0, otherwise.
- *
- * Caller(s) : Application.
- *
- * Note(s) : (1) This function is DEPRECATED and will be removed in a future version of this product.
- * Mem_DynPoolBlkNbrAvailGet() should be used instead.
- *********************************************************************************************************
- */
- #if (LIB_MEM_CFG_HEAP_SIZE > 0u)
- MEM_POOL_BLK_QTY Mem_PoolBlkGetNbrAvail (MEM_POOL *p_pool,
- LIB_ERR *p_err)
- {
- CPU_SIZE_T nbr_avail;
- CPU_SR_ALLOC();
- #if (LIB_MEM_CFG_ARG_CHK_EXT_EN == DEF_ENABLED)
- /* --------------- VALIDATE RTN ERR PTR --------------- */
- if (p_err == DEF_NULL) {
- CPU_SW_EXCEPTION(0u);
- }
- /* ---------------- VALIDATE MEM POOL ----------------- */
- if (p_pool == DEF_NULL) { /* Validate mem ptr. */
- *p_err = LIB_MEM_ERR_NULL_PTR;
- return (0u);
- }
- #endif
- CPU_CRITICAL_ENTER();
- nbr_avail = p_pool->BlkFreeTblIx;
- CPU_CRITICAL_EXIT();
- *p_err = LIB_MEM_ERR_NONE;
- return (nbr_avail);
- }
- #endif
- /*
- *********************************************************************************************************
- * Mem_DynPoolCreate()
- *
- * Description : Creates a dynamic memory pool.
- *
- * Argument(s) : p_name Pointer to pool name.
- *
- * p_pool Pointer to pool data.
- *
- * p_seg Pointer to segment from which to allocate memory. Will be allocated from
- * general-purpose heap if null.
- *
- * blk_size Size of memory block to allocate from pool, in bytes. See Note #1.
- *
- * blk_align Required alignment of memory block, in bytes. MUST be a power of 2.
- *
- * blk_qty_init Initial number of elements to be allocated in pool.
- *
- * blk_qty_max Maximum number of elements that can be allocated from this pool. Set to
- * LIB_MEM_BLK_QTY_UNLIMITED if no limit.
- *
- * p_err Pointer to variable that will receive the return error code from this function :
- *
- * LIB_MEM_ERR_NONE Operation was successful.
- *
- * --------------------RETURNED BY Mem_DynPoolCreateInternal()-------------------
- * LIB_MEM_ERR_INVALID_BLK_ALIGN Invalid requested block alignment.
- * LIB_MEM_ERR_INVALID_BLK_SIZE Invalid requested block size.
- * LIB_MEM_ERR_INVALID_BLK_NBR Invalid requested block quantity max.
- * LIB_MEM_ERR_NULL_PTR Pool data pointer NULL.
- * LIB_MEM_ERR_INVALID_MEM_ALIGN Invalid memory block alignment requested.
- * LIB_MEM_ERR_INVALID_MEM_SIZE Invalid memory block size specified.
- * LIB_MEM_ERR_NULL_PTR Error or segment data pointer NULL.
- * LIB_MEM_ERR_SEG_OVF Allocation would overflow memory segment.
- *
- * Return(s) : None.
- *
- * Caller(s) : Application.
- *
- * Note(s) : (1) 'blk_size' must be big enough to fit a pointer since the pointer to the next free
- * block is stored in the block itself (only when free/unused).
- *********************************************************************************************************
- */
- void Mem_DynPoolCreate (const CPU_CHAR *p_name,
- MEM_DYN_POOL *p_pool,
- MEM_SEG *p_seg,
- CPU_SIZE_T blk_size,
- CPU_SIZE_T blk_align,
- CPU_SIZE_T blk_qty_init,
- CPU_SIZE_T blk_qty_max,
- LIB_ERR *p_err)
- {
- if (p_seg == DEF_NULL) { /* Alloc from heap if p_seg is null. */
- #if (LIB_MEM_CFG_HEAP_SIZE > 0u)
- p_seg = &Mem_SegHeap;
- #else
- *p_err = LIB_MEM_ERR_NULL_PTR;
- return (DEF_NULL);
- #endif
- }
- Mem_DynPoolCreateInternal(p_name,
- p_pool,
- p_seg,
- blk_size,
- blk_align,
- LIB_MEM_PADDING_ALIGN_NONE,
- blk_qty_init,
- blk_qty_max,
- p_err);
- }
- /*
- *********************************************************************************************************
- * Mem_DynPoolCreateHW()
- *
- * Description : Creates a dynamic memory pool. Memory blocks will be padded according to memory segment's
- * properties.
- *
- * Argument(s) : p_name Pointer to pool name.
- *
- * p_pool Pointer to pool data.
- *
- * p_seg Pointer to segment from which to allocate memory. Will allocate from
- * general-purpose heap if null.
- *
- * blk_size Size of memory block to allocate from pool, in bytes. See Note #1.
- *
- * blk_align Required alignment of memory block, in bytes. MUST be a power of 2.
- *
- * blk_qty_init Initial number of elements to be allocated in pool.
- *
- * blk_qty_max Maximum number of elements that can be allocated from this pool. Set to
- * LIB_MEM_BLK_QTY_UNLIMITED if no limit.
- *
- * p_err Pointer to variable that will receive the return error code from this function :
- *
- * LIB_MEM_ERR_NONE Operation was successful.
- *
- * -------------------RETURNED BY Mem_DynPoolCreateInternal()-------------------
- * LIB_MEM_ERR_INVALID_MEM_ALIGN Invalid memory block alignment requested.
- * LIB_MEM_ERR_INVALID_MEM_SIZE Invalid memory block size specified.
- * LIB_MEM_ERR_NULL_PTR Error or segment data pointer NULL.
- * LIB_MEM_ERR_SEG_OVF Allocation would overflow memory segment.
- *
- * Return(s) : None.
- *
- * Caller(s) : Application.
- *
- * Note(s) : (1) 'blk_size' must be big enough to fit a pointer since the pointer to the next free
- * block is stored in the block itself (only when free/unused).
- *********************************************************************************************************
- */
- void Mem_DynPoolCreateHW (const CPU_CHAR *p_name,
- MEM_DYN_POOL *p_pool,
- MEM_SEG *p_seg,
- CPU_SIZE_T blk_size,
- CPU_SIZE_T blk_align,
- CPU_SIZE_T blk_qty_init,
- CPU_SIZE_T blk_qty_max,
- LIB_ERR *p_err)
- {
- if (p_seg == DEF_NULL) { /* Alloc from heap if p_seg is null. */
- #if (LIB_MEM_CFG_HEAP_SIZE > 0u)
- p_seg = &Mem_SegHeap;
- #else
- *p_err = LIB_MEM_ERR_NULL_PTR;
- return (DEF_NULL);
- #endif
- }
- Mem_DynPoolCreateInternal(p_name,
- p_pool,
- p_seg,
- blk_size,
- blk_align,
- p_seg->PaddingAlign,
- blk_qty_init,
- blk_qty_max,
- p_err);
- }
- /*
- *********************************************************************************************************
- * Mem_DynPoolBlkGet()
- *
- * Description : Gets a memory block from specified pool, growing it if needed.
- *
- * Argument(s) : p_pool Pointer to pool data.
- *
- * p_err Pointer to variable that will receive the return error code from this function :
- *
- * LIB_MEM_ERR_NONE Operation was successful.
- * LIB_MEM_ERR_NULL_PTR Pool data pointer NULL.
- * LIB_MEM_ERR_POOL_EMPTY Pools is empty.
- *
- * ----------------------RETURNED BY Mem_SegAllocInternal()-----------------------
- * LIB_MEM_ERR_INVALID_MEM_ALIGN Invalid memory block alignment requested.
- * LIB_MEM_ERR_INVALID_MEM_SIZE Invalid memory block size specified.
- * LIB_MEM_ERR_NULL_PTR Error or segment data pointer NULL.
- * LIB_MEM_ERR_SEG_OVF Allocation would overflow memory segment.
- *
- * Return(s) : Pointer to memory block, if successful.
- *
- * DEF_NULL, otherwise.
- *
- * Caller(s) : Application.
- *
- * Note(s) : none.
- *********************************************************************************************************
- */
- void *Mem_DynPoolBlkGet (MEM_DYN_POOL *p_pool,
- LIB_ERR *p_err)
- {
- void *p_blk;
- const CPU_CHAR *p_pool_name;
- CPU_SR_ALLOC();
- #if (LIB_MEM_CFG_ARG_CHK_EXT_EN == DEF_ENABLED)
- if (p_err == DEF_NULL) { /* Chk for NULL err ptr. */
- CPU_SW_EXCEPTION(DEF_NULL);
- }
- if (p_pool == DEF_NULL) { /* Chk for NULL pool data ptr. */
- *p_err = LIB_MEM_ERR_NULL_PTR;
- return (DEF_NULL);
- }
- #endif
- /* Ensure pool is not empty if qty is limited. */
- if (p_pool->BlkQtyMax != LIB_MEM_BLK_QTY_UNLIMITED) {
- CPU_CRITICAL_ENTER();
- if (p_pool->BlkAllocCnt >= p_pool->BlkQtyMax) {
- CPU_CRITICAL_EXIT();
- *p_err = LIB_MEM_ERR_POOL_EMPTY;
- return (DEF_NULL);
- }
- p_pool->BlkAllocCnt++;
- CPU_CRITICAL_EXIT();
- }
- /* --------------- ALLOC FROM FREE LIST --------------- */
- CPU_CRITICAL_ENTER();
- if (p_pool->BlkFreePtr != DEF_NULL) {
- p_blk = p_pool->BlkFreePtr;
- p_pool->BlkFreePtr = *((void **)p_blk);
- CPU_CRITICAL_EXIT();
- *p_err = LIB_MEM_ERR_NONE;
- return (p_blk);
- }
- CPU_CRITICAL_EXIT();
- /* ------------------ ALLOC NEW BLK ------------------- */
- #if (LIB_MEM_CFG_DBG_INFO_EN == DEF_ENABLED)
- p_pool_name = p_pool->NamePtr;
- #else
- p_pool_name = DEF_NULL;
- #endif
- p_blk = Mem_SegAllocInternal(p_pool_name,
- p_pool->PoolSegPtr,
- p_pool->BlkSize,
- p_pool->BlkAlign,
- p_pool->BlkPaddingAlign,
- DEF_NULL,
- p_err);
- if (*p_err != LIB_MEM_ERR_NONE) {
- return (DEF_NULL);
- }
- return (p_blk);
- }
- /*
- *********************************************************************************************************
- * Mem_DynPoolBlkFree()
- *
- * Description : Frees memory block, making it available for future use.
- *
- * Argument(s) : p_pool Pointer to pool data.
- *
- * p_blk Pointer to first byte of memory block.
- *
- * p_err Pointer to variable that will receive the return error code from this function :
- *
- * LIB_MEM_ERR_NONE Operation was successful.
- * LIB_MEM_ERR_NULL_PTR 'p_pool' or 'p_blk' pointer passed is NULL.
- * LIB_MEM_ERR_POOL_FULL Pool is full.
- *
- * Return(s) : none.
- *
- * Caller(s) : Application.
- *
- * Note(s) : none.
- *********************************************************************************************************
- */
- void Mem_DynPoolBlkFree (MEM_DYN_POOL *p_pool,
- void *p_blk,
- LIB_ERR *p_err)
- {
- CPU_SR_ALLOC();
- #if (LIB_MEM_CFG_ARG_CHK_EXT_EN == DEF_ENABLED)
- if (p_err == DEF_NULL) { /* Chk for NULL err ptr. */
- CPU_SW_EXCEPTION(;);
- }
- if (p_pool == DEF_NULL) { /* Chk for NULL pool data ptr. */
- *p_err = LIB_MEM_ERR_NULL_PTR;
- return;
- }
- if (p_blk == DEF_NULL) {
- *p_err = LIB_MEM_ERR_NULL_PTR;
- return;
- }
- #endif
- if (p_pool->BlkQtyMax != LIB_MEM_BLK_QTY_UNLIMITED) { /* Ensure pool is not full. */
- CPU_CRITICAL_ENTER();
- if (p_pool->BlkAllocCnt == 0u) {
- CPU_CRITICAL_EXIT();
- *p_err = LIB_MEM_ERR_POOL_FULL;
- return;
- }
- p_pool->BlkAllocCnt--;
- CPU_CRITICAL_EXIT();
- }
- CPU_CRITICAL_ENTER();
- *((void **)p_blk) = p_pool->BlkFreePtr;
- p_pool->BlkFreePtr = p_blk;
- CPU_CRITICAL_EXIT();
- *p_err = LIB_MEM_ERR_NONE;
- }
- /*
- *********************************************************************************************************
- * Mem_DynPoolBlkNbrAvailGet()
- *
- * Description : Gets number of available blocks in dynamic memory pool. This call will fail with a
- * dynamic memory pool for which no limit was set at creation.
- *
- * Argument(s) : p_pool Pointer to pool data.
- *
- * p_err Pointer to variable that will receive the return error code from this function :
- *
- * LIB_MEM_ERR_NONE Operation was successful.
- * LIB_MEM_ERR_NULL_PTR 'p_pool' pointer passed is NULL.
- * LIB_MEM_ERR_POOL_UNLIMITED Pool has no specified limit.
- *
- * Return(s) : Number of blocks available in dynamic memory pool, if successful.
- *
- * 0, if pool is empty or if an error occurred.
- *
- * Caller(s) : Application.
- *
- * Note(s) : None.
- *********************************************************************************************************
- */
- CPU_SIZE_T Mem_DynPoolBlkNbrAvailGet (MEM_DYN_POOL *p_pool,
- LIB_ERR *p_err)
- {
- CPU_SIZE_T blk_nbr_avail;
- CPU_SR_ALLOC();
- #if (LIB_MEM_CFG_ARG_CHK_EXT_EN == DEF_ENABLED)
- if (p_err == DEF_NULL) { /* Chk for NULL err ptr. */
- CPU_SW_EXCEPTION(;);
- }
- if (p_pool == DEF_NULL) { /* Chk for NULL pool data ptr. */
- *p_err = LIB_MEM_ERR_NULL_PTR;
- return (0u);
- }
- #endif
- if (p_pool->BlkQtyMax != LIB_MEM_BLK_QTY_UNLIMITED) {
- CPU_CRITICAL_ENTER();
- blk_nbr_avail = p_pool->BlkQtyMax - p_pool->BlkAllocCnt;
- CPU_CRITICAL_EXIT();
- *p_err = LIB_MEM_ERR_NONE;
- } else {
- blk_nbr_avail = 0u;
- *p_err = LIB_MEM_ERR_POOL_UNLIMITED;
- }
- return (blk_nbr_avail);
- }
- /*
- *********************************************************************************************************
- * Mem_OutputUsage()
- *
- * Description : Outputs memory usage report through 'out_fnct'.
- *
- * Argument(s) : out_fnct Pointer to output function.
- *
- * print_details DEF_YES, if the size of each allocation should be printed.
- * DEF_NO, otherwise.
- *
- * p_err Pointer to variable that will receive the return error code from this function :
- *
- * LIB_MEM_ERR_NONE Operation was successful.
- * LIB_MEM_ERR_NULL_PTR 'out_fnct' pointer passed is NULL.
- *
- * ---------------------RETURNED BY Mem_SegRemSizeGet()--------------------
- * LIB_MEM_ERR_NULL_PTR Segment data pointer NULL.
- * LIB_MEM_ERR_INVALID_MEM_ALIGN Invalid memory alignment.
- *
- * Return(s) : None.
- *
- * Caller(s) : Application.
- *
- * Note(s) : none.
- *********************************************************************************************************
- */
- #if (LIB_MEM_CFG_DBG_INFO_EN == DEF_ENABLED)
- void Mem_OutputUsage(void (*out_fnct) (CPU_CHAR *),
- LIB_ERR *p_err)
- {
- CPU_CHAR str[DEF_INT_32U_NBR_DIG_MAX];
- MEM_SEG *p_seg;
- CPU_SR_ALLOC();
- #if (LIB_MEM_CFG_ARG_CHK_EXT_EN == DEF_ENABLED)
- if (p_err == DEF_NULL) { /* Chk for NULL err ptr. */
- CPU_SW_EXCEPTION(;);
- }
- if (out_fnct == DEF_NULL) { /* Chk for NULL out fnct ptr. */
- *p_err = LIB_MEM_ERR_NULL_PTR;
- return;
- }
- #endif
- out_fnct("---------------- Memory allocation info ----------------\r\n");
- out_fnct("| Type | Size | Free size | Name\r\n");
- out_fnct("|---------|------------|------------|-------------------\r\n");
- CPU_CRITICAL_ENTER();
- p_seg = Mem_SegHeadPtr;
- while (p_seg != DEF_NULL) {
- CPU_SIZE_T rem_size;
- MEM_SEG_INFO seg_info;
- MEM_ALLOC_INFO *p_alloc;
- rem_size = Mem_SegRemSizeGet(p_seg, 1u, &seg_info, p_err);
- if (*p_err != LIB_MEM_ERR_NONE) {
- return;
- }
- out_fnct("| Section | ");
- (void)Str_FmtNbr_Int32U(seg_info.TotalSize,
- 10u,
- DEF_NBR_BASE_DEC,
- ' ',
- DEF_NO,
- DEF_YES,
- &str[0u]);
- out_fnct(str);
- out_fnct(" | ");
- (void)Str_FmtNbr_Int32U(rem_size,
- 10u,
- DEF_NBR_BASE_DEC,
- ' ',
- DEF_NO,
- DEF_YES,
- &str[0u]);
- out_fnct(str);
- out_fnct(" | ");
- out_fnct((p_seg->NamePtr != DEF_NULL) ? (CPU_CHAR *)p_seg->NamePtr : (CPU_CHAR *)"Unknown");
- out_fnct("\r\n");
- p_alloc = p_seg->AllocInfoHeadPtr;
- while (p_alloc != DEF_NULL) {
- out_fnct("| -> Obj | ");
- (void)Str_FmtNbr_Int32U(p_alloc->Size,
- 10u,
- DEF_NBR_BASE_DEC,
- ' ',
- DEF_NO,
- DEF_YES,
- &str[0u]);
- out_fnct(str);
- out_fnct(" | | ");
- out_fnct((p_alloc->NamePtr != DEF_NULL) ? (CPU_CHAR *)p_alloc->NamePtr : (CPU_CHAR *)"Unknown");
- out_fnct("\r\n");
- p_alloc = p_alloc->NextPtr;
- }
- p_seg = p_seg->NextPtr;
- }
- CPU_CRITICAL_EXIT();
- *p_err = LIB_MEM_ERR_NONE;
- }
- #endif
- /*
- *********************************************************************************************************
- *********************************************************************************************************
- * LOCAL FUNCTIONS
- *********************************************************************************************************
- *********************************************************************************************************
- */
- /*
- *********************************************************************************************************
- * Mem_SegCreateCritical()
- *
- * Description : Creates a new memory segment to be used for runtime memory allocation or dynamic pools.
- *
- * Argument(s) : p_name Pointer to segment name.
- *
- * p_seg Pointer to segment data. Must be allocated by caller.
- * ----- Argument validated by caller.
- *
- * seg_base_addr Segment's first byte address.
- *
- * padding_align Padding alignment, in bytes, that will be added to any allocated buffer
- * from this memory segment. MUST be a power of 2.
- * LIB_MEM_PADDING_ALIGN_NONE means no padding.
- * ------------- Argument validated by caller.
- *
- * size Total size of segment, in bytes.
- * ---- Argument validated by caller.
- *
- * Return(s) : Pointer to segment data, if successful.
- *
- * DEF_NULL, otherwise.
- *
- * Caller(s) : Mem_PoolCreate(),
- * Mem_SegCreate().
- *
- * Note(s) : (1) This function MUST be called within a CRITICAL_SECTION.
- *********************************************************************************************************
- */
- static void Mem_SegCreateCritical(const CPU_CHAR *p_name,
- MEM_SEG *p_seg,
- CPU_ADDR seg_base_addr,
- CPU_SIZE_T padding_align,
- CPU_SIZE_T size)
- {
- p_seg->AddrBase = seg_base_addr;
- p_seg->AddrEnd = (seg_base_addr + (size - 1u));
- p_seg->AddrNext = seg_base_addr;
- p_seg->NextPtr = Mem_SegHeadPtr;
- p_seg->PaddingAlign = padding_align;
- #if (LIB_MEM_CFG_DBG_INFO_EN == DEF_ENABLED)
- p_seg->NamePtr = p_name;
- p_seg->AllocInfoHeadPtr = DEF_NULL;
- #else
- (void)&p_name;
- #endif
- Mem_SegHeadPtr = p_seg;
- }
- /*
- *********************************************************************************************************
- * Mem_SegOverlapChkCritical()
- *
- * Description : Checks if existing memory segment exists or overlaps with specified memory area.
- *
- * Argument(s) : seg_base_addr Address of first byte of memory area.
- *
- * size Size of memory area, in bytes.
- *
- * p_err Pointer to variable that will receive the return error code from this function :
- *
- * LIB_MEM_ERR_INVALID_SEG_OVERLAP Segment overlaps another existing segment.
- * LIB_MEM_ERR_INVALID_SEG_EXISTS Segment already exists.
- *
- * Return(s) : Pointer to memory segment that overlaps.
- *
- * DEF_NULL, otherwise.
- *
- * Caller(s) : Mem_PoolCreate(),
- * Mem_SegCreate().
- *
- * Note(s) : (1) This function MUST be called within a CRITICAL_SECTION.
- *********************************************************************************************************
- */
- static MEM_SEG *Mem_SegOverlapChkCritical (CPU_ADDR seg_base_addr,
- CPU_SIZE_T size,
- LIB_ERR *p_err)
- {
- MEM_SEG *p_seg_chk;
- CPU_ADDR seg_new_end;
- CPU_ADDR seg_chk_start;
- CPU_ADDR seg_chk_end;
- seg_new_end = seg_base_addr + (size - 1u);
- p_seg_chk = Mem_SegHeadPtr;
- while (p_seg_chk != DEF_NULL) {
- seg_chk_start = (CPU_ADDR)p_seg_chk->AddrBase;
- seg_chk_end = (CPU_ADDR)p_seg_chk->AddrEnd;
- if ((seg_base_addr == seg_chk_start) && (seg_new_end == seg_chk_end)) {
- *p_err = LIB_MEM_ERR_INVALID_SEG_EXISTS;
- return (p_seg_chk);
- } else if (((seg_base_addr >= seg_chk_start) && (seg_base_addr <= seg_chk_end)) ||
- ((seg_base_addr <= seg_chk_start) && (seg_new_end >= seg_chk_start))) {
- *p_err = LIB_MEM_ERR_INVALID_SEG_OVERLAP;
- return (p_seg_chk);
- }
- p_seg_chk = p_seg_chk->NextPtr;
- }
- *p_err = LIB_MEM_ERR_NONE;
- return (DEF_NULL);
- }
- /*
- *********************************************************************************************************
- * Mem_SegAllocInternal()
- *
- * Description : Allocates memory from specified segment.
- *
- * Argument(s) : p_name Pointer to allocated object name. Used for allocations tracking. May be DEF_NULL.
- *
- * p_seg Pointer to segment from which to allocate memory.
- * ----- Argument validated by caller.
- *
- * size Size of memory block to allocate, in bytes.
- *
- * align Required alignment of memory block, in bytes. MUST be a power of 2.
- *
- * padding_align Padding alignment, in bytes, that will be added to any allocated buffer from
- * this memory segment. MUST be a power of 2. LIB_MEM_PADDING_ALIGN_NONE
- * means no padding.
- *
- * p_bytes_reqd Pointer to variable that will receive the number of free bytes missing for
- * the allocation to succeed. Set to DEF_NULL to skip calculation.
- *
- * p_err Pointer to variable that will receive the return error code from this function :
- *
- * LIB_MEM_ERR_INVALID_MEM_ALIGN Invalid memory block alignment requested.
- * LIB_MEM_ERR_INVALID_MEM_SIZE Invalid memory block size specified.
- * LIB_MEM_ERR_NULL_PTR Error or segment data pointer NULL.
- *
- * ------------------RETURNED BY Mem_SegAllocExtCritical()------------------
- * LIB_MEM_ERR_SEG_OVF Allocation would overflow memory segment.
- *
- * Return(s) : Pointer to allocated memory block, if successful.
- *
- * DEF_NULL, otherwise.
- *
- * Caller(s) : Mem_DynPoolBlkGet(),
- * Mem_DynPoolCreateInternal(),
- * Mem_HeapAlloc(),
- * Mem_PoolCreate(),
- * Mem_SegAlloc(),
- * Mem_SegAllocExt(),
- * Mem_SegAllocHW().
- *
- * Note(s) : none.
- *********************************************************************************************************
- */
- static void *Mem_SegAllocInternal (const CPU_CHAR *p_name,
- MEM_SEG *p_seg,
- CPU_SIZE_T size,
- CPU_SIZE_T align,
- CPU_SIZE_T padding_align,
- CPU_SIZE_T *p_bytes_reqd,
- LIB_ERR *p_err)
- {
- void *p_blk;
- CPU_SR_ALLOC();
- #if (LIB_MEM_CFG_ARG_CHK_EXT_EN == DEF_ENABLED)
- if (p_err == DEF_NULL) { /* Chk for null err ptr. */
- CPU_SW_EXCEPTION(DEF_NULL);
- }
- if (size < 1u) { /* Chk for invalid sized mem req. */
- *p_err = LIB_MEM_ERR_INVALID_MEM_SIZE;
- return (DEF_NULL);
- }
- if (MATH_IS_PWR2(align) != DEF_YES) { /* Chk that align is a pwr of 2. */
- *p_err = LIB_MEM_ERR_INVALID_MEM_ALIGN;
- return (DEF_NULL);
- }
- #endif
- CPU_CRITICAL_ENTER();
- p_blk = Mem_SegAllocExtCritical(p_seg,
- size,
- align,
- padding_align,
- p_bytes_reqd,
- p_err);
- if (*p_err != LIB_MEM_ERR_NONE) {
- CPU_CRITICAL_EXIT();
- return (DEF_NULL);
- }
- #if (LIB_MEM_CFG_DBG_INFO_EN == DEF_ENABLED) /* Track alloc if req'd. */
- Mem_SegAllocTrackCritical(p_name,
- p_seg,
- size,
- p_err);
- if (*p_err != LIB_MEM_ERR_NONE) {
- CPU_CRITICAL_EXIT();
- return (DEF_NULL);
- }
- #else
- (void)&p_name;
- #endif
- CPU_CRITICAL_EXIT();
- return (p_blk);
- }
- /*
- *********************************************************************************************************
- * Mem_SegAllocExtCritical()
- *
- * Description : Allocates memory from specified segment.
- *
- * Argument(s) : p_seg Pointer to segment from which to allocate memory.
- *
- * size Size of memory block to allocate, in bytes.
- *
- * align Required alignment of memory block, in bytes. MUST be a power of 2.
- *
- * padding_align Padding alignment, in bytes, that will be added to any allocated buffer from
- * this memory segment. MUST be a power of 2. LIB_MEM_PADDING_ALIGN_NONE
- * means no padding.
- *
- * p_bytes_reqd Pointer to variable that will receive the number of free bytes missing for
- * the allocation to succeed. Set to DEF_NULL to skip calculation.
- *
- * p_err Pointer to variable that will receive the return error code from this function :
- *
- * LIB_MEM_ERR_SEG_OVF Allocation would overflow memory segment.
- *
- * Return(s) : Pointer to allocated memory block, if successful.
- *
- * DEF_NULL, otherwise.
- *
- * Caller(s) : Mem_PoolCreate(),
- * Mem_SegAllocInternal(),
- * Mem_SegAllocTrackCritical().
- *
- * Note(s) : (1) This function MUST be called within a CRITICAL_SECTION.
- *********************************************************************************************************
- */
- static void *Mem_SegAllocExtCritical (MEM_SEG *p_seg,
- CPU_SIZE_T size,
- CPU_SIZE_T align,
- CPU_SIZE_T padding_align,
- CPU_SIZE_T *p_bytes_reqd,
- LIB_ERR *p_err)
- {
- CPU_ADDR blk_addr;
- CPU_ADDR addr_next;
- CPU_SIZE_T size_rem_seg;
- CPU_SIZE_T size_tot_blk;
- CPU_SIZE_T blk_align = DEF_MAX(align, padding_align);
- blk_addr = MATH_ROUND_INC_UP_PWR2(p_seg->AddrNext, /* Compute align'ed blk addr. */
- blk_align);
- addr_next = MATH_ROUND_INC_UP_PWR2(blk_addr + size, /* Compute addr of next alloc. */
- padding_align);
- size_rem_seg = p_seg->AddrEnd - p_seg->AddrNext + 1u;
- size_tot_blk = addr_next - p_seg->AddrNext; /* Compute tot blk size including align and padding. */
- if (size_rem_seg < size_tot_blk) { /* If seg doesn't have enough space ... */
- if (p_bytes_reqd != DEF_NULL) { /* ... calc nbr of req'd bytes. */
- *p_bytes_reqd = size_tot_blk - size_rem_seg;
- }
- *p_err = LIB_MEM_ERR_SEG_OVF;
- return (DEF_NULL);
- }
- p_seg->AddrNext = addr_next;
- *p_err = LIB_MEM_ERR_NONE;
- return ((void *)blk_addr);
- }
- /*
- *********************************************************************************************************
- * Mem_SegAllocTrackCritical()
- *
- * Description : Tracks segment allocation, adding the 'size' of the allocation under the 'p_name' entry.
- *
- * Argument(s) : p_name Pointer to the name of the object. This string is not copied and its memory should
- * remain accessible at all times.
- *
- * p_seg Pointer to segment data.
- *
- * size Allocation size, in bytes.
- *
- * p_err Pointer to variable that will receive the return error code from this function :
- *
- * LIB_MEM_ERR_HEAP_EMPTY No more memory available on heap
- *
- * --------------RETURNED BY Mem_SegAllocExtCritical()---------------
- * LIB_MEM_ERR_SEG_OVF Allocation would overflow memory segment.
- *
- * Return(s) : none.
- *
- * Caller(s) : Mem_PoolCreate(),
- * Mem_SegAllocInternal().
- *
- * Note(s) : none.
- *********************************************************************************************************
- */
- #if (LIB_MEM_CFG_DBG_INFO_EN == DEF_ENABLED)
- static void Mem_SegAllocTrackCritical (const CPU_CHAR *p_name,
- MEM_SEG *p_seg,
- CPU_SIZE_T size,
- LIB_ERR *p_err)
- {
- MEM_ALLOC_INFO *p_alloc;
- /* ------- UPDATE ALLOC INFO LIST, IF POSSIBLE -------- */
- p_alloc = p_seg->AllocInfoHeadPtr;
- while (p_alloc != DEF_NULL) {
- if (p_alloc->NamePtr == p_name) {
- p_alloc->Size += size;
- *p_err = LIB_MEM_ERR_NONE;
- return;
- }
- p_alloc = p_alloc->NextPtr;
- }
- /* --------- ADD NEW ALLOC INFO ENTRY IN LIST --------- */
- p_alloc = Mem_SegAllocExtCritical(&Mem_SegHeap, /* Alloc new alloc info struct on heap. */
- sizeof(MEM_ALLOC_INFO),
- sizeof(CPU_ALIGN),
- LIB_MEM_PADDING_ALIGN_NONE,
- DEF_NULL,
- p_err);
- if (*p_err != LIB_MEM_ERR_NONE) {
- return;
- }
- p_alloc->NamePtr = p_name; /* Populate alloc info. */
- p_alloc->Size = size;
- p_alloc->NextPtr = p_seg->AllocInfoHeadPtr; /* Prepend new item in list. */
- p_seg->AllocInfoHeadPtr = p_alloc;
- }
- #endif
- /*
- *********************************************************************************************************
- * Mem_DynPoolCreateInternal()
- *
- * Description : Creates a dynamic memory pool.
- *
- * Argument(s) : p_name Pointer to pool name.
- *
- * p_pool Pointer to pool data.
- *
- * p_seg Pointer to segment from which to allocate memory.
- *
- * blk_size Size of memory block to allocate from pool, in bytes. See Note #1.
- *
- * blk_align Required alignment of memory block, in bytes. MUST be a power of 2.
- *
- * blk_padding_align Block's padding alignment, in bytes, that will be added at the end
- * of block's buffer. MUST be a power of 2. LIB_MEM_PADDING_ALIGN_NONE
- * means no padding.
- *
- * blk_qty_init Initial number of elements to be allocated in pool.
- *
- * blk_qty_max Maximum number of elements that can be allocated from this pool. Set to
- * LIB_MEM_BLK_QTY_UNLIMITED if no limit.
- *
- * p_err Pointer to variable that will receive the return error code from this function :
- *
- * LIB_MEM_ERR_INVALID_BLK_ALIGN Invalid requested block alignment.
- * LIB_MEM_ERR_INVALID_BLK_SIZE Invalid requested block size.
- * LIB_MEM_ERR_INVALID_BLK_NBR Invalid requested block quantity max.
- * LIB_MEM_ERR_NULL_PTR Pool data pointer NULL.
- *
- * ------------------RETURNED BY Mem_SegAllocInternal()-------------------
- * LIB_MEM_ERR_INVALID_MEM_ALIGN Invalid memory block alignment requested.
- * LIB_MEM_ERR_INVALID_MEM_SIZE Invalid memory block size specified.
- * LIB_MEM_ERR_NULL_PTR Error or segment data pointer NULL.
- * LIB_MEM_ERR_SEG_OVF Allocation would overflow memory segment.
- *
- * Return(s) : None.
- *
- * Caller(s) : Mem_DynPoolCreate(),
- * Mem_DynPoolCreateHW().
- *
- * Note(s) : (1) 'blk_size' must be big enough to fit a pointer since the pointer to the next free
- * block is stored in the block itself (only when free/unused).
- *********************************************************************************************************
- */
- static void Mem_DynPoolCreateInternal (const CPU_CHAR *p_name,
- MEM_DYN_POOL *p_pool,
- MEM_SEG *p_seg,
- CPU_SIZE_T blk_size,
- CPU_SIZE_T blk_align,
- CPU_SIZE_T blk_padding_align,
- CPU_SIZE_T blk_qty_init,
- CPU_SIZE_T blk_qty_max,
- LIB_ERR *p_err)
- {
- CPU_INT08U *p_blks;
- CPU_SIZE_T blk_size_align;
- CPU_SIZE_T blk_align_worst = DEF_MAX(blk_align, blk_padding_align);
- #if (LIB_MEM_CFG_ARG_CHK_EXT_EN == DEF_ENABLED)
- if (p_err == DEF_NULL) { /* Chk for NULL err ptr. */
- CPU_SW_EXCEPTION(DEF_NULL);
- }
- if (p_pool == DEF_NULL) { /* Chk for NULL pool data ptr. */
- *p_err = LIB_MEM_ERR_NULL_PTR;
- return;
- }
- if (blk_size < 1u) { /* Chk for inv blk size. */
- *p_err = LIB_MEM_ERR_INVALID_BLK_SIZE;
- return;
- }
- if ((blk_qty_max != LIB_MEM_BLK_QTY_UNLIMITED) && /* Chk for invalid blk qty. */
- (blk_qty_init > blk_qty_max)) {
- *p_err = LIB_MEM_ERR_INVALID_BLK_NBR;
- return;
- }
- if (MATH_IS_PWR2(blk_align) != DEF_YES) { /* Chk for illegal align spec. */
- *p_err = LIB_MEM_ERR_INVALID_BLK_ALIGN;
- return;
- }
- #endif
- /* Calc blk size with align. */
- if (blk_size < sizeof(void *)) { /* If size if smaller than ptr ... */
- /* ... inc size to ptr size. */
- blk_size_align = MATH_ROUND_INC_UP_PWR2(sizeof(void *), blk_align_worst);
- } else {
- blk_size_align = MATH_ROUND_INC_UP_PWR2(blk_size, blk_align_worst);
- }
- if (blk_qty_init != 0u) { /* Alloc init blks. */
- p_blks = (CPU_INT08U *)Mem_SegAllocInternal(p_name,
- p_seg,
- blk_size_align * blk_qty_init,
- blk_align_worst,
- LIB_MEM_PADDING_ALIGN_NONE,
- DEF_NULL,
- p_err);
- if (*p_err != LIB_MEM_ERR_NONE) {
- return;
- }
- }
- /* ----------------- CREATE POOL DATA ----------------- */
- p_pool->PoolSegPtr = p_seg;
- p_pool->BlkSize = blk_size;
- p_pool->BlkAlign = blk_align_worst;
- p_pool->BlkPaddingAlign = blk_padding_align;
- p_pool->BlkQtyMax = blk_qty_max;
- p_pool->BlkAllocCnt = 0u;
- if (blk_qty_init != 0u) { /* Init free list. */
- CPU_SIZE_T i;
- p_pool->BlkFreePtr = (void *)p_blks;
- for (i = 0u; i < blk_qty_init - 1u; i++) {
- *((void **)p_blks) = p_blks + blk_size_align;
- p_blks += blk_size_align;
- }
- *((void **)p_blks) = DEF_NULL;
- } else {
- p_pool->BlkFreePtr = DEF_NULL;
- }
- #if (LIB_MEM_CFG_DBG_INFO_EN == DEF_ENABLED)
- p_pool->NamePtr = p_name;
- #endif
- *p_err = LIB_MEM_ERR_NONE;
- }
- /*
- *********************************************************************************************************
- * Mem_PoolBlkIsValidAddr()
- *
- * Description : Calculates if a given memory block address is valid for the memory pool.
- *
- * Argument(s) : p_pool Pointer to memory pool structure to validate memory block address.
- * ------ Argument validated by caller.
- *
- * p_mem Pointer to memory block address to validate.
- * ----- Argument validated by caller.
- *
- * Return(s) : DEF_YES, if valid memory pool block address.
- *
- * DEF_NO, otherwise.
- *
- * Caller(s) : Mem_PoolBlkFree().
- *
- * Note(s) : none.
- *********************************************************************************************************
- */
- #if ((LIB_MEM_CFG_ARG_CHK_EXT_EN == DEF_ENABLED) && \
- (LIB_MEM_CFG_HEAP_SIZE > 0u))
- static CPU_BOOLEAN Mem_PoolBlkIsValidAddr (MEM_POOL *p_pool,
- void *p_mem)
- {
- CPU_ADDR pool_offset;
- if ((p_mem < p_pool->PoolAddrStart) ||
- (p_mem > p_pool->PoolAddrEnd)) {
- return (DEF_FALSE);
- }
- pool_offset = (CPU_ADDR)p_mem - (CPU_ADDR)p_pool->PoolAddrStart;
- if (pool_offset % p_pool->BlkSize != 0u) {
- return (DEF_FALSE);
- } else {
- return (DEF_TRUE);
- }
- }
- #endif
|