📄 lib_mem.lst
字号:
608 CPU_SW_EXCEPTION((void *)0);
609 }
610 #endif
611
612 /* ------------ VALIDATE RTN OCTETS PTR ----------- */
613 if (poctets_reqd == (CPU_SIZE_T *) 0) { /* If NOT avail, ... */
614 poctets_reqd = (CPU_SIZE_T *)&octets_reqd_unused; /* ... re-cfg NULL rtn ptr to unused local var. */
615 (void)&octets_reqd_unused; /* Prevent possible 'variable unused' warning. */
616 }
617 *poctets_reqd = 0u; /* Init octets req'd for err (see Note #1). */
618
619
620 #if (LIB_MEM_CFG_ARG_CHK_EXT_EN == DEF_ENABLED) /* ------------ VALIDATE HEAP MEM ALLOC ----------- */
621 if (size < 1) {
622 *perr = LIB_MEM_ERR_INVALID_MEM_SIZE;
623 return ((void *)0);
624 }
625
626 if (align < 1) {
627 *perr = LIB_MEM_ERR_INVALID_MEM_ALIGN;
628 return ((void *)0);
629 }
630 #endif
631
632 /* -------------- ALLOC HEAP MEM BLK -------------- */
633 pmem_pool_heap = &Mem_PoolHeap;
634
635 CPU_CRITICAL_ENTER();
636
637 pmem_addr = pmem_pool_heap->SegAddrNextAvail;
638 size_rem = pmem_pool_heap->SegSizeRem;
639 size_req = Mem_PoolSegCalcTotSize(pmem_addr,
640 1u, /* Calc alloc for single mem blk from heap. */
641 size,
642 align);
643 #if (LIB_MEM_CFG_ARG_CHK_EXT_EN == DEF_ENABLED)
644 if (size_req < 1) { /* If req'd size ovf, ... */
645 CPU_CRITICAL_EXIT();
646 *poctets_reqd = size; /* ... rtn add'l heap size needed. */
647 *perr = LIB_MEM_ERR_HEAP_OVF;
648 return ((void *)0);
649 }
650 #endif
651
652 if (size_req > size_rem) { /* If req'd size > rem heap size, ... */
653 CPU_CRITICAL_EXIT();
654 *poctets_reqd = size_req - size_rem; /* ... rtn add'l heap size needed. */
655 *perr = LIB_MEM_ERR_HEAP_EMPTY;
656 return ((void *)0);
657 }
658
659 pmem_blk = Mem_PoolSegAlloc(pmem_pool_heap, size, align);
660 if (pmem_blk == (void *)0) { /* If mem blk NOT avail from heap, ... */
661 CPU_CRITICAL_EXIT();
662 *poctets_reqd = size_req; /* ... rtn add'l heap size needed. */
663 *perr = LIB_MEM_ERR_HEAP_EMPTY;
664 return ((void *)0);
665 }
666
667 CPU_CRITICAL_EXIT();
668
669 *perr = LIB_MEM_ERR_NONE;
670
671 return (pmem_blk);
672 }
673 #endif
674
675
676 /*$PAGE*/
677 /*
678 *********************************************************************************************************
679 * Mem_PoolClr()
680 *
681 * Description : Clear a memory pool (see Note #1).
682 *
683 * Argument(s) : pmem_pool Pointer to a memory pool structure to clear (see Note #2).
684 *
685 * perr Pointer to variable that will receive the return error code from this function :
686 *
687 * LIB_MEM_ERR_NONE Memory pool successfully cleared.
688 * LIB_MEM_ERR_NULL_PTR Argument 'pmem_pool' passed a NULL pointer.
689 *
690 * Return(s) : none.
691 *
692 * Caller(s) : Application,
693 * Mem_PoolCreate().
694 *
695 * Note(s) : (1) (a) Mem_PoolClr() ONLY clears a memory pool structure's variables & should ONLY be
696 * called to initialize a memory pool structure prior to calling Mem_PoolCreate().
697 *
698 * (b) Mem_PoolClr() does NOT deallocate memory from the memory pool or deallocate the
699 * memory pool itself & MUST NOT be called after calling Mem_PoolCreate() since
700 * this will likely corrupt the memory pool management.
701 *
702 * (2) Assumes 'pmem_pool' points to a valid memory pool (if non-NULL).
703 *********************************************************************************************************
704 */
705
706 #if (LIB_MEM_CFG_ALLOC_EN == DEF_ENABLED)
707 void Mem_PoolClr (MEM_POOL *pmem_pool,
708 LIB_ERR *perr)
709 {
710
711 #if (LIB_MEM_CFG_ARG_CHK_EXT_EN == DEF_ENABLED) /* -------------- VALIDATE RTN ERR PTR --------------- */
712 if (perr == (LIB_ERR *)0) {
713 CPU_SW_EXCEPTION(;);
714 }
715 #endif
716
717 /* -------------- VALIDATE MEM POOL PTR --------------- */
718 if (pmem_pool == (MEM_POOL *)0) {
719 *perr = LIB_MEM_ERR_NULL_PTR;
720 return;
721 }
722
723
724 pmem_pool->Type = (LIB_MEM_TYPE)LIB_MEM_TYPE_NONE;
725 pmem_pool->SegPrevPtr = (MEM_POOL *)0;
726 pmem_pool->SegNextPtr = (MEM_POOL *)0;
727 pmem_pool->PoolPrevPtr = (MEM_POOL *)0;
728 pmem_pool->PoolNextPtr = (MEM_POOL *)0;
729 pmem_pool->PoolAddrStart = (void *)0;
730 pmem_pool->PoolAddrEnd = (void *)0;
731 pmem_pool->PoolPtrs = (void **)0;
732 pmem_pool->PoolSize = (CPU_SIZE_T )0u;
733 pmem_pool->BlkAlign = (CPU_SIZE_T )0u;
734 pmem_pool->BlkSize = (CPU_SIZE_T )0u;
735 pmem_pool->BlkNbr = (CPU_SIZE_T )0u;
736 pmem_pool->BlkIx = (MEM_POOL_IX )0u;
737 pmem_pool->SegAddr = (void *)0;
738 pmem_pool->SegAddrNextAvail = (void *)0;
739 pmem_pool->SegSizeTot = (CPU_SIZE_T )0u;
740 pmem_pool->SegSizeRem = (CPU_SIZE_T )0u;
741
742
743 *perr = LIB_MEM_ERR_NONE;
744 }
745 #endif
746
747
748 /*$PAGE*/
749 /*
750 *********************************************************************************************************
751 * Mem_PoolCreate()
752 *
753 * Description : (1) Create a memory pool :
754 *
755 * (a) Create memory pool from heap or dedicated memory
756 * (b) Allocate memory pool memory blocks
757 * (c) Update memory pool table
758 * (d) Configure memory pool
759 *
760 *
761 * (2) Memory pools are indexed by the Memory Segments they use.
762 *
763 * (a) The memory pool table is composed by a two-dimensional list :
764 *
765 * (1) Memory segments manage the following memory segment/pool information :
766 *
767 * (A) Memory segment base address
768 * (B) Memory segment next available address
769 * (C) Memory segment total size
770 * (D) Memory segment remaining size
771 *
772 * (2) Memory pools share memory from memory segments but do NOT manage any memory
773 * segment information. To access the memory segment information, the head
774 * memory segment must be accessed.
775 *
776 * (b) In the diagram below, memory pools in vertical columns represent they share the same
777 * memory segment for the memory blocks they have. The heads of the memory pool are
778 * linked horizontally to form a memory pool table.
779 *
780 * (1) 'Mem_PoolTbl' points to the head of the Memory Pool table.
781 *
782 * (2) Memory Pools' 'SegPrevPtr' & 'SegNextPtr' doubly-link each memory segment to
783 * form the list of memory segments.
784 *
785 * (3) Memory Pools' 'PoolPrevPtr' & 'PoolNextPtr' doubly-link the memory pools of
786 * each memory segment.
787 *
788 * (c) New memory pools, which do not share a memory segment, are inserted in the Memory
789 * Segments Primary List. The point of insertion is such to keep ascended order by
790 * memory segment base address.
791 *
792 * (d) Memory pool pointers to memory blocks 'PoolPtrs' must be allocated for each created
793 * memory pool. These pointers are stored in the memory pool heap segment 'Mem_PoolHeap'.
794 *
795 * (1) A memory pool can also have its memory blocks allocated from the memory pool heap.
796 * 'pmem_base_addr' must be set to NULL & 'mem_size' must be set to (0) to create the
797 * memory pool.
798 *
799 *
800 * | |
801 * |<----------------------- Memory Segments ----------------------->|
802 * | (see Note #2a1) |
803 *
804 * Lowest Memory Segment Highest Memory Segment
805 * Base Address Base Address
806 * (see Note #2c) (see Note #2c)
807 *
808 * | SegNextPtr Heap Memory Pool |
809 * | (see Note #2b2) (see Note #2d) |
810 * | | |
811 * v | | v
812 * | v
813 * --- Head of Memory ------- ------- v ------- ------- -------
814 * ^ Pool Table --->| |------->| |------->| |------->| |------->| |
815 * | (see Note #2b1) | | | | | | | H | | |
816 * |
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -