• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2009-2022 Huawei Technologies Co., Ltd. All rights reserved.
3  *
4  * UniProton is licensed under Mulan PSL v2.
5  * You can use this software according to the terms and conditions of the Mulan PSL v2.
6  * You may obtain a copy of Mulan PSL v2 at:
7  *          http://license.coscl.org.cn/MulanPSL2
8  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
9  * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
10  * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
11  * See the Mulan PSL v2 for more details.
12  * Create: 2009-01-20
13  * Description: 内存基本功能的内部头文件。
14  */
15 #ifndef PRT_MEM_EXTERNAL_H
16 #define PRT_MEM_EXTERNAL_H
17 
18 #include "prt_mem.h"
19 #include "prt_err_external.h"
20 #include "prt_hook_external.h"
21 
22 #define OS_MEM_HEAD_MAGICWORD       0xABAB /* 内存块的块控制头魔术字,确保为奇数 */
23 
24 #define OS_MAX_PT_NUM               253
25 
26 #define OS_MEM_ALIGN_CHECK(value) ((value) & 0x3UL)
27 
28 #define OS_FSC_MEM_MAGIC_USED                               (struct TagFscMemCtrl *)0x5a5aa5a5
29 #define OS_FSC_MEM_LAST_IDX                                 16
30 #define OS_FSC_MEM_USED_HEAD_SIZE                           (sizeof(struct TagFscMemCtrl))
31 #define OS_FSC_MEM_TAIL_SIZE                                (sizeof(U32))
32 
33 #define OS_FSC_MEM_SZGET(currBlk)                           ((U32)(currBlk->size))
34 #define OS_FSC_MEM_MAXVAL                                   ((1U << OS_FSC_MEM_LAST_IDX) - OS_FSC_MEM_SIZE_ALIGN)
35 
36 #define OS_FSC_MEM_TAIL_MAGIC                               0xABCDDCBA
37 
38 /* FSC算法块控制头结构,注意各成员顺序是和其他算法保持一致偏移的,不能随便改动,保持ptNo和其他算法偏移一致 */
39 struct TagFscMemCtrl {
40     struct TagFscMemCtrl *next;
41     // 块大小
42     uintptr_t size;
43     // 若前面相邻的物理块空闲,则此字段记录前面空闲块大小,否则为OS_FSC_MEM_PREV_USED
44     uintptr_t prevSize;
45     // 空闲时为上一个控制块地址
46     struct TagFscMemCtrl *prev;
47 };
48 
49 extern void *OsMemAlloc(enum MoudleId mid, U8 ptNo, U32 size);
50 extern void *OsMemAllocAlign(U32 mid, U8 ptNo, U32 size, enum MemAlign alignPow);
51 
52 /* 对齐之后,返回地址不一定紧跟在内存头后面,需要设置返回地址与内存头之间的差值 */
OsMemSetHeadAddr(uintptr_t usrAddr,uintptr_t ctrlAddr)53 OS_SEC_ALW_INLINE INLINE void OsMemSetHeadAddr(uintptr_t usrAddr, uintptr_t ctrlAddr)
54 {
55     U16 *headAddr = (U16 *)(usrAddr) - 1;
56 
57     *headAddr  = (U16)(usrAddr - ctrlAddr);
58     return;
59 }
OsMemGetHeadAddr(uintptr_t usrAddr)60 OS_SEC_ALW_INLINE INLINE void *OsMemGetHeadAddr(uintptr_t usrAddr)
61 {
62     U16 headOffset = *((U16 *)usrAddr - 1);
63 
64     return (void *)(uintptr_t)((usrAddr - (uintptr_t)headOffset) - sizeof(struct TagFscMemCtrl));
65 }
OsMemSetHookAddr(uintptr_t addrress)66 OS_SEC_ALW_INLINE INLINE uintptr_t OsMemSetHookAddr(uintptr_t addrress)
67 {
68     return LOG_ADDR_DBG(addrress);
69 }
70 
71 #endif /* PRT_MEM_EXTERNAL_H */
72