• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* ----------------------------------------------------------------------------
2  * Copyright (c) Huawei Technologies Co., Ltd. 2013-2015. All rights reserved.
3  * Description: LiteOS memory Module Implementation
4  * Author: Huawei LiteOS Team
5  * Create: 2013-05-12
6  * Redistribution and use in source and binary forms, with or without modification,
7  * are permitted provided that the following conditions are met:
8  * 1. Redistributions of source code must retain the above copyright notice, this list of
9  * conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11  * of conditions and the following disclaimer in the documentation and/or other materials
12  * provided with the distribution.
13  * 3. Neither the name of the copyright holder nor the names of its contributors may be used
14  * to endorse or promote products derived from this software without specific prior written
15  * permission.
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
18  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
20  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
21  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
22  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
23  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
24  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
25  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
26  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  * --------------------------------------------------------------------------- */
28 
29 /**
30  * @defgroup los_slab Slab
31  * @ingroup kernel
32  */
33 
34 #ifndef _LOS_SLAB_PRI_H
35 #define _LOS_SLAB_PRI_H
36 
37 #include "los_slab.h"
38 #include "los_base.h"
39 
40 #ifdef __cplusplus
41 extern "C" {
42 #endif /* __cplusplus */
43 
44 #ifdef LOSCFG_KERNEL_MEM_SLAB_EXTENTION
45 
46 /* number of slab class */
47 #define SLAB_MEM_COUNT              4
48 
49 /* step size of each class */
50 #define SLAB_MEM_CLASS_STEP_SIZE    0x10U
51 
52 /* max slab block size */
53 #define SLAB_MEM_MAX_SIZE           (SLAB_MEM_CLASS_STEP_SIZE << (SLAB_MEM_COUNT - 1))
54 
55 typedef struct tagLosSlabStatus {
56     UINT32 totalSize;
57     UINT32 usedSize;
58     UINT32 freeSize;
59     UINT32 allocCount;
60     UINT32 freeCount;
61 } LosSlabStatus;
62 
63 typedef struct tagOsSlabBlockNode {
64     UINT16 magic;
65     UINT8  blkSz;
66     UINT8  recordId;
67 } OsSlabBlockNode;
68 
69 struct AtomicBitset {
70     UINT32 numBits;
71     UINT32 words[0];
72 };
73 
74 typedef struct tagOsSlabAllocator {
75     UINT32 itemSz;
76     UINT8 *dataChunks;
77     struct AtomicBitset *bitset;
78 } OsSlabAllocator;
79 
80 #ifdef LOSCFG_KERNEL_MEM_SLAB_AUTO_EXPANSION_MODE
81 typedef struct tagOsSlabMemAllocator {
82     struct tagOsSlabMemAllocator *next;
83     OsSlabAllocator *slabAlloc;
84 } OsSlabMemAllocator;
85 #endif
86 
87 typedef struct tagOsSlabMem {
88     UINT32 blkSz;
89     UINT32 blkCnt;
90     UINT32 blkUsedCnt;
91 #ifdef LOSCFG_KERNEL_MEM_SLAB_AUTO_EXPANSION_MODE
92     UINT32 allocatorCnt;
93     OsSlabMemAllocator *bucket;
94 #else
95     OsSlabAllocator *alloc;
96 #endif
97 } OsSlabMem;
98 
99 struct LosSlabControlHeader {
100     BOOL enabled;
101 #ifdef LOSCFG_KERNEL_MEM_SLAB_AUTO_EXPANSION_MODE
102     OsSlabAllocator *allocatorBucket;
103 #endif
104     OsSlabMem slabClass[SLAB_MEM_COUNT];
105 };
106 
107 #ifdef LOSCFG_KERNEL_MEM_SLAB_AUTO_EXPANSION_MODE
108 #define SLAB_MEM_DEFAULT_BUCKET_CNT                1
109 #endif
110 
111 #define OS_SLAB_MAGIC                              0xdede
112 #define OS_SLAB_BLOCK_HEAD_GET(ptr)                ((OsSlabBlockNode *)(VOID *)((UINT8 *)(ptr) - \
113                                                       sizeof(OsSlabBlockNode)))
114 #define OS_SLAB_BLOCK_MAGIC_SET(slabNode)          (((OsSlabBlockNode *)(slabNode))->magic = (UINT16)OS_SLAB_MAGIC)
115 #define OS_SLAB_BLOCK_MAGIC_GET(slabNode)          (((OsSlabBlockNode *)(slabNode))->magic)
116 #define OS_SLAB_BLOCK_SIZE_SET(slabNode, size)     (((OsSlabBlockNode *)(slabNode))->blkSz = (UINT8)(size))
117 #define OS_SLAB_BLOCK_SIZE_GET(slabNode)           (((OsSlabBlockNode *)(slabNode))->blkSz)
118 #define OS_SLAB_BLOCK_ID_SET(slabNode, id)         (((OsSlabBlockNode *)(slabNode))->recordId = (id))
119 #define OS_SLAB_BLOCK_ID_GET(slabNode)             (((OsSlabBlockNode *)(slabNode))->recordId)
120 #define OS_ALLOC_FROM_SLAB_CHECK(slabNode)         (((OsSlabBlockNode *)(slabNode))->magic == (UINT16)OS_SLAB_MAGIC)
121 #define OS_SLAB_LOG2(value)                        ((UINT32)(32 - CLZ(value) - 1)) /* get highest bit one position */
122 #define OS_SLAB_CLASS_LEVEL_GET(size) \
123         (OS_SLAB_LOG2((size - 1) >> (OS_SLAB_LOG2(SLAB_MEM_CLASS_STEP_SIZE - 1))))
124 
125 extern OsSlabAllocator *OsSlabAllocatorNew(VOID *pool, UINT32 itemSz, UINT32 itemAlign, UINT32 numItems);
126 extern VOID OsSlabAllocatorDestroy(VOID *pool, OsSlabAllocator *allocator);
127 extern VOID *OsSlabAllocatorAlloc(OsSlabAllocator *allocator);
128 extern BOOL OsSlabAllocatorFree(OsSlabAllocator *allocator, VOID* ptr);
129 extern BOOL OsSlabAllocatorEmpty(const OsSlabAllocator *allocator);
130 extern VOID OsSlabAllocatorGetSlabInfo(const OsSlabAllocator *allocator, UINT32 *itemSize,
131     UINT32 *itemCnt, UINT32 *curUsage);
132 extern BOOL OsSlabAllocatorCheck(const OsSlabAllocator *allocator, const VOID *ptr);
133 extern VOID OsSlabMemInit(VOID *pool, UINT32 size);
134 extern VOID OsSlabMemDeinit(VOID *pool);
135 extern VOID *OsSlabMemAlloc(VOID *pool, UINT32 sz);
136 extern BOOL OsSlabMemFree(VOID *pool, VOID *ptr);
137 extern UINT32 OsSlabMemCheck(const VOID *pool, const VOID *ptr);
138 extern UINT32 OsSlabStatisticsGet(const VOID *pool, LosSlabStatus *status);
139 extern UINT32 OsSlabGetMaxFreeBlkSize(const VOID *pool);
140 extern VOID *OsSlabCtrlHdrGet(const VOID *pool);
141 extern VOID *OsSlabAllocatorGetIdxP(const OsSlabAllocator *allocator, UINT32 idx);
142 
OsSlabMemProcInitFlag(VOID * pool,UINT32 size,BOOL slabEnable)143 STATIC INLINE VOID OsSlabMemProcInitFlag(VOID *pool, UINT32 size, BOOL slabEnable)
144 {
145     struct LosSlabControlHeader *header = (struct LosSlabControlHeader *)OsSlabCtrlHdrGet(pool);
146 
147     if (slabEnable) {
148         OsSlabMemInit(pool, size);
149     }
150 
151     /* flag Must be set after OsSlabMemInit */
152     header->enabled = slabEnable;
153 }
154 
155 #else /* !LOSCFG_KERNEL_MEM_SLAB_EXTENTION */
156 
157 STATIC INLINE VOID OsSlabMemProcInitFlag(VOID *pool, UINT32 size, BOOL slabEnable)
158 {
159     (VOID)pool;
160     (VOID)size;
161     (VOID)slabEnable;
162 }
163 
164 STATIC INLINE VOID *OsSlabMemAlloc(VOID *pool, UINT32 size)
165 {
166     (VOID)pool;
167     (VOID)size;
168     return NULL;
169 }
170 
171 STATIC INLINE BOOL OsSlabMemFree(VOID *pool, VOID *ptr)
172 {
173     (VOID)pool;
174     (VOID)ptr;
175     return FALSE;
176 }
177 
178 STATIC INLINE UINT32 OsSlabMemCheck(const VOID *pool, const VOID *ptr)
179 {
180     (VOID)pool;
181     (VOID)ptr;
182     return (UINT32)-1;
183 }
184 
185 #endif /* LOSCFG_KERNEL_MEM_SLAB_EXTENTION */
186 
187 #ifdef __cplusplus
188 }
189 #endif /* __cplusplus */
190 
191 #endif /* _LOS_SLAB_PRI_H */
192