154568cb3Sopenharmony_ci/*
254568cb3Sopenharmony_ci * Copyright (c) 2009-2022 Huawei Technologies Co., Ltd. All rights reserved.
354568cb3Sopenharmony_ci *
454568cb3Sopenharmony_ci * UniProton is licensed under Mulan PSL v2.
554568cb3Sopenharmony_ci * You can use this software according to the terms and conditions of the Mulan PSL v2.
654568cb3Sopenharmony_ci * You may obtain a copy of Mulan PSL v2 at:
754568cb3Sopenharmony_ci *          http://license.coscl.org.cn/MulanPSL2
854568cb3Sopenharmony_ci * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
954568cb3Sopenharmony_ci * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
1054568cb3Sopenharmony_ci * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
1154568cb3Sopenharmony_ci * See the Mulan PSL v2 for more details.
1254568cb3Sopenharmony_ci * Create: 2009-01-20
1354568cb3Sopenharmony_ci * Description: 内存基本功能的内部头文件。
1454568cb3Sopenharmony_ci */
1554568cb3Sopenharmony_ci#ifndef PRT_MEM_EXTERNAL_H
1654568cb3Sopenharmony_ci#define PRT_MEM_EXTERNAL_H
1754568cb3Sopenharmony_ci
1854568cb3Sopenharmony_ci#include "prt_mem.h"
1954568cb3Sopenharmony_ci#include "prt_err_external.h"
2054568cb3Sopenharmony_ci#include "prt_hook_external.h"
2154568cb3Sopenharmony_ci
2254568cb3Sopenharmony_ci#define OS_MEM_HEAD_MAGICWORD       0xABAB /* 内存块的块控制头魔术字,确保为奇数 */
2354568cb3Sopenharmony_ci
2454568cb3Sopenharmony_ci#define OS_MAX_PT_NUM               253
2554568cb3Sopenharmony_ci
2654568cb3Sopenharmony_ci#define OS_MEM_ALIGN_CHECK(value) ((value) & 0x3UL)
2754568cb3Sopenharmony_ci
2854568cb3Sopenharmony_ci#define OS_FSC_MEM_MAGIC_USED                               (struct TagFscMemCtrl *)0x5a5aa5a5
2954568cb3Sopenharmony_ci#define OS_FSC_MEM_LAST_IDX                                 16
3054568cb3Sopenharmony_ci#define OS_FSC_MEM_USED_HEAD_SIZE                           (sizeof(struct TagFscMemCtrl))
3154568cb3Sopenharmony_ci#define OS_FSC_MEM_TAIL_SIZE                                (sizeof(U32))
3254568cb3Sopenharmony_ci
3354568cb3Sopenharmony_ci#define OS_FSC_MEM_SZGET(currBlk)                           ((U32)(currBlk->size))
3454568cb3Sopenharmony_ci#define OS_FSC_MEM_MAXVAL                                   ((1U << OS_FSC_MEM_LAST_IDX) - OS_FSC_MEM_SIZE_ALIGN)
3554568cb3Sopenharmony_ci
3654568cb3Sopenharmony_ci#define OS_FSC_MEM_TAIL_MAGIC                               0xABCDDCBA
3754568cb3Sopenharmony_ci
3854568cb3Sopenharmony_ci/* FSC算法块控制头结构,注意各成员顺序是和其他算法保持一致偏移的,不能随便改动,保持ptNo和其他算法偏移一致 */
3954568cb3Sopenharmony_cistruct TagFscMemCtrl {
4054568cb3Sopenharmony_ci    struct TagFscMemCtrl *next;
4154568cb3Sopenharmony_ci    // 块大小
4254568cb3Sopenharmony_ci    uintptr_t size;
4354568cb3Sopenharmony_ci    // 若前面相邻的物理块空闲,则此字段记录前面空闲块大小,否则为OS_FSC_MEM_PREV_USED
4454568cb3Sopenharmony_ci    uintptr_t prevSize;
4554568cb3Sopenharmony_ci    // 空闲时为上一个控制块地址
4654568cb3Sopenharmony_ci    struct TagFscMemCtrl *prev;
4754568cb3Sopenharmony_ci};
4854568cb3Sopenharmony_ci
4954568cb3Sopenharmony_ciextern void *OsMemAlloc(enum MoudleId mid, U8 ptNo, U32 size);
5054568cb3Sopenharmony_ciextern void *OsMemAllocAlign(U32 mid, U8 ptNo, U32 size, enum MemAlign alignPow);
5154568cb3Sopenharmony_ci
5254568cb3Sopenharmony_ci/* 对齐之后,返回地址不一定紧跟在内存头后面,需要设置返回地址与内存头之间的差值 */
5354568cb3Sopenharmony_ciOS_SEC_ALW_INLINE INLINE void OsMemSetHeadAddr(uintptr_t usrAddr, uintptr_t ctrlAddr)
5454568cb3Sopenharmony_ci{
5554568cb3Sopenharmony_ci    U16 *headAddr = (U16 *)(usrAddr) - 1;
5654568cb3Sopenharmony_ci
5754568cb3Sopenharmony_ci    *headAddr  = (U16)(usrAddr - ctrlAddr);
5854568cb3Sopenharmony_ci    return;
5954568cb3Sopenharmony_ci}
6054568cb3Sopenharmony_ciOS_SEC_ALW_INLINE INLINE void *OsMemGetHeadAddr(uintptr_t usrAddr)
6154568cb3Sopenharmony_ci{
6254568cb3Sopenharmony_ci    U16 headOffset = *((U16 *)usrAddr - 1);
6354568cb3Sopenharmony_ci
6454568cb3Sopenharmony_ci    return (void *)(uintptr_t)((usrAddr - (uintptr_t)headOffset) - sizeof(struct TagFscMemCtrl));
6554568cb3Sopenharmony_ci}
6654568cb3Sopenharmony_ciOS_SEC_ALW_INLINE INLINE uintptr_t OsMemSetHookAddr(uintptr_t addrress)
6754568cb3Sopenharmony_ci{
6854568cb3Sopenharmony_ci    return LOG_ADDR_DBG(addrress);
6954568cb3Sopenharmony_ci}
7054568cb3Sopenharmony_ci
7154568cb3Sopenharmony_ci#endif /* PRT_MEM_EXTERNAL_H */
72