-
Notifications
You must be signed in to change notification settings - Fork 57
/
raw_block.c
297 lines (206 loc) · 8.12 KB
/
raw_block.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
/*
raw os - Copyright (C) Lingjun Chen(jorya_txj).
This file is part of raw os.
raw os is free software; you can redistribute it it under the terms of the
GNU General Public License as published by the Free Software Foundation;
either version 3 of the License, or (at your option) any later version.
raw os is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with this program. if not, write email to jorya.txj@gmail.com
---
A special exception to the LGPL can be applied should you wish to distribute
a combined work that includes raw os, without being obliged to provide
the source code for any proprietary components. See the file exception.txt
for full details of how and when the exception can be applied.
*/
/* 2012-4 Created by jorya_txj
* xxxxxx please added here
*/
#include <raw_api.h>
#if (CONFIG_RAW_BLOCK > 0)
/*
************************************************************************************************************************
* Create a block pool for allocating memory
*
* Description: This function is called to create a block pool .
*
* Arguments: pool_ptr is the address of this pool
* --------------
* name_pr is the name of this pool
* -------------------
* block_size is the allocating size of a block
* -------------------
* pool_start is the pool start address
* ---------------------------------
* pool_size is the size of this pool
*
* Returns RAW_INVALID_ALIGN: block_size or pool_start or pool_size not be pointed aligned!!!
* RAW_BLOCK_SIZE_ERROR: Must at least have 2 blocks.
* RAW_SUCCESS: raw os return success
* Note(s) block_size and pool_start and pool_size must be pointed aligned!!!.This methods will not cause fragmention.
*
*
************************************************************************************************************************
*/
RAW_OS_ERROR raw_block_pool_create(MEM_POOL *pool_ptr, RAW_U8 *name_ptr, RAW_U32 block_size, void *pool_start, RAW_U32 pool_size)
{
RAW_U32 blocks; /* Number of blocks in pool */
RAW_U8 *block_ptr; /* Working block pointer */
RAW_U8 *next_block_ptr; /* Next block pointer */
RAW_U8 *end_of_pool; /* End of pool area */
RAW_U8 block_align_mask;
#if (RAW_BLOCK_FUNCTION_CHECK > 0)
/* Check for invalid pool size. */
if (pool_size < (block_size + block_size) ) {
return RAW_BLOCK_SIZE_ERROR;
}
if (pool_ptr == 0) {
return RAW_NULL_OBJECT;
}
if (pool_start == 0) {
return RAW_NULL_POINTER;
}
#endif
block_align_mask = sizeof(void *) - 1u;
if (((RAW_U32)pool_start & block_align_mask)){
return RAW_INVALID_ALIGN;
}
if ((pool_size & block_align_mask)) {
return RAW_INVALID_ALIGN;
}
if ((block_size & block_align_mask)) {
return RAW_INVALID_ALIGN;
}
/* Setup the basic block pool fields. */
pool_ptr->common_block_obj.name = name_ptr;
/* Calculate the end of the pool's memory area. */
end_of_pool = (RAW_U8 *)pool_start + pool_size;
/* Walk through the pool area, setting up the available block list. */
blocks = 0u;
block_ptr = (RAW_U8 *)pool_start;
next_block_ptr = block_ptr + block_size;
while (next_block_ptr <= end_of_pool) {
blocks++;
if (next_block_ptr == end_of_pool) {
break;
}
/* Setup the link to the next block. */
*((RAW_U8 * *) block_ptr) = next_block_ptr;
/* Advance to the next block. */
block_ptr = next_block_ptr;
/* Update the next block pointer. */
next_block_ptr = block_ptr + block_size;
}
/* Set the last block's forward pointer to NULL. */
*((RAW_U8 * *)block_ptr) = 0;
/* Save the remaining information in the pool control block. */
pool_ptr->raw_block_pool_available = blocks;
pool_ptr->block_size = block_size;
pool_ptr ->raw_block_pool_available_list = (RAW_U8 *)pool_start;
pool_ptr ->common_block_obj.object_type = RAW_BLOCK_OBJ_TYPE;
TRACE_BLOCK_POOL_CREATE(raw_task_active, pool_ptr);
return RAW_SUCCESS;
}
/*
************************************************************************************************************************
* Allocating block memory from pool
*
* Description: This function is called to allocate memory from pool
*
* Arguments :pool_ptr is the address of the pool
* ---------------------
* block_ptr is the address of pointer, and it will be filled the allocating block address
* ---------------------
*
* Returns RAW_NO_MEMORY: No block memory is available.
* RAW_SUCCESS: raw os return success
* Note(s) If *block_ptr is 0 which means no memory available now.This methods will not cause fragmention.
*
*
************************************************************************************************************************
*/
RAW_OS_ERROR raw_block_allocate(MEM_POOL *pool_ptr, void **block_ptr)
{
RAW_OS_ERROR status;
RAW_U8 *work_ptr;
RAW_SR_ALLOC();
#if (RAW_BLOCK_FUNCTION_CHECK > 0)
if (pool_ptr == 0) {
return RAW_NULL_OBJECT;
}
if (block_ptr == 0) {
return RAW_NULL_POINTER;
}
#endif
if (pool_ptr->common_block_obj.object_type != RAW_BLOCK_OBJ_TYPE) {
return RAW_ERROR_OBJECT_TYPE;
}
RAW_CPU_DISABLE();
/* Determine if there is an available block. */
if (pool_ptr->raw_block_pool_available) {
/* Yes, a block is available. Decrement the available count. */
pool_ptr->raw_block_pool_available--;
/* Pickup the current block pointer. */
work_ptr = pool_ptr->raw_block_pool_available_list;
/* Return the first available block to the caller. */
*((RAW_U8 **)block_ptr) = work_ptr;
/* Modify the available list to point at the next block in the pool. */
pool_ptr->raw_block_pool_available_list = *((RAW_U8 **)work_ptr);
/* Set status to success. */
status = RAW_SUCCESS;
}
/*if no block memory is available then just return*/
else {
*((RAW_U8 **)block_ptr) = 0;
status = RAW_NO_MEMORY;
TRACE_BLOCK_NO_MEMORY(raw_task_active, pool_ptr);
}
RAW_CPU_ENABLE();
return status;
}
/*
************************************************************************************************************************
* Release block memory from pool
*
* Description: This function is called to release memory from pool
*
* Arguments : block_ptr is the address want to return to memory pool.
* ---------------------
*
* Returns
* RAW_SUCCESS: raw os return success
* Note(s) This methods will not cause fragmention.
*
*
************************************************************************************************************************
*/
RAW_OS_ERROR raw_block_release(MEM_POOL *pool_ptr, void *block_ptr)
{
RAW_U8 *work_ptr;
RAW_SR_ALLOC();
#if (RAW_BLOCK_FUNCTION_CHECK > 0)
if (block_ptr == 0) {
return RAW_NULL_OBJECT;
}
if (pool_ptr == 0) {
return RAW_NULL_OBJECT;
}
#endif
if (pool_ptr->common_block_obj.object_type != RAW_BLOCK_OBJ_TYPE) {
return RAW_ERROR_OBJECT_TYPE;
}
RAW_CPU_DISABLE();
work_ptr = ((RAW_U8 *) block_ptr);
/* Put the block back in the available list. */
*((RAW_U8 **)work_ptr) = pool_ptr->raw_block_pool_available_list;
/* Adjust the head pointer. */
pool_ptr->raw_block_pool_available_list = work_ptr;
/* Increment the count of available blocks. */
pool_ptr->raw_block_pool_available++;
RAW_CPU_ENABLE();
/* Return completion status. */
return RAW_SUCCESS;
}
#endif