-
Notifications
You must be signed in to change notification settings - Fork 57
/
raw_byte.c
472 lines (326 loc) · 13.3 KB
/
raw_byte.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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
/*
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-7 Created by jorya_txj
* xxxxxx please added here
*/
#include <raw_api.h>
#if (CONFIG_RAW_BYTE > 0)
/*
************************************************************************************************************************
* Create a byte pool for allocating memory
*
* Description: This function is called to create a byte pool .
*
* Arguments :pool_ptr is the address of this pool
* --------------
* name_ptr is the name of this pool
* -------------------
* pool_start is the pool start address
* -------------------
* pool_size is the allocating size of a block
* -------------------
*
* Returns RAW_INVALID_ALIGN:pool_start or pool_size not be pointed aligned!!!
* RAW_SUCCESS: raw os return success
* Note(s) This methods will cause memory fragmention and not suggested to be used by time critical condition.
*
*
************************************************************************************************************************
*/
RAW_OS_ERROR raw_byte_pool_create(RAW_BYTE_POOL_STRUCT *pool_ptr, RAW_U8 *name_ptr, void *pool_start, RAW_U32 pool_size)
{
RAW_U8 *block_ptr; /* Working block pointer */
RAW_U8 byte_align_mask;
#if (RAW_BYTE_FUNCTION_CHECK > 0)
if (pool_ptr == 0) {
return RAW_NULL_POINTER;
}
if (pool_start == 0) {
return RAW_NULL_POINTER;
}
/* Check for invalid pool size. */
if (pool_size < RAW_BYTE_POOL_MIN) {
return RAW_BYTE_SIZE_ERROR;
}
#endif
byte_align_mask = sizeof(void *) - 1u;
/*pool_start needs 4 bytes aligned*/
if (((RAW_U32)pool_start & byte_align_mask)){
return RAW_INVALID_ALIGN;
}
/*pool_size needs 4 bytes aligned*/
if ((pool_size & byte_align_mask)) {
return RAW_INVALID_ALIGN;
}
/* Setup the basic byte pool fields. */
pool_ptr->common_block_obj.name = name_ptr;
pool_ptr->raw_byte_pool_search = (RAW_U8 *)pool_start;
pool_ptr->raw_byte_pool_owner = 0;
/*Initially, the pool will have two blocks. One large block at the
beginning that is available and a small allocated block at the end
of the pool that is there just for the algorithm. Be sure to count
the available block's header in the available bytes count. */
pool_ptr->raw_byte_pool_available = pool_size - sizeof(RAW_U8 *) - sizeof(RAW_U32);
pool_ptr->raw_byte_pool_fragments = 2u;
/* Calculate the end of the pool's memory area. */
block_ptr = ((RAW_U8 *)pool_start) + (RAW_U32) pool_size;
/* Backup the end of the pool pointer and build the pre-allocated block. */
block_ptr = block_ptr - sizeof(RAW_U32);
*((RAW_U32 *)block_ptr) = RAW_BYTE_BLOCK_ALLOC;
block_ptr = block_ptr - sizeof(RAW_U8 *);
*((RAW_U8 * *)block_ptr) = pool_start;
/* Now setup the large available block in the pool. */
*((RAW_U8 * *) pool_start) = block_ptr;
block_ptr = (RAW_U8 *)pool_start;
block_ptr = block_ptr + sizeof(RAW_U8 *);
*((RAW_U32 *) block_ptr) = RAW_BYTE_BLOCK_FREE;
pool_ptr->common_block_obj.object_type = RAW_BYTE_OBJ_TYPE;
return RAW_SUCCESS;
}
static void *raw_byte_pool_search(RAW_BYTE_POOL_STRUCT *pool_ptr, RAW_U32 memory_size)
{
RAW_U8 *current_ptr; /* Current block pointer */
RAW_U8 *next_ptr; /* Next block pointer */
RAW_U32 available_bytes; /* Calculate bytes available */
RAW_U32 examine_blocks; /* Blocks to be examined */
RAW_SR_ALLOC();
/* Disable interrupts. */
RAW_CPU_DISABLE();
/* First, determine if there are enough bytes in the pool. */
if (memory_size >= pool_ptr->raw_byte_pool_available) {
/* Restore interrupts. */
RAW_CPU_ENABLE();
/* Not enough memory, return a NULL pointer. */
return 0;
}
/* Walk through the memory pool in search for a large enough block. */
current_ptr = pool_ptr->raw_byte_pool_search;
examine_blocks = pool_ptr->raw_byte_pool_fragments + 1u;
available_bytes = 0u;
do {
/* Check to see if this block is free. */
if (*((RAW_U32 *)(current_ptr + sizeof(RAW_U8 *))) == RAW_BYTE_BLOCK_FREE) {
/* Block is free, see if it is large enough. */
/* Pickup the next block's pointer. */
next_ptr = *((RAW_U8 * *) current_ptr);
/* Calculate the number of byte available in this block. */
available_bytes = next_ptr - current_ptr - sizeof(RAW_U8 *) - sizeof(RAW_U32);
/* If this is large enough, we are done because our first-fit algorithm
has been satisfied! */
if (available_bytes >= memory_size) {
/* Find the suitable position */
break;
}
else {
/* Clear the available bytes variable. */
available_bytes = 0u;
/* Not enough memory, check to see if the neighbor is
free and can be merged. */
if (*((RAW_U32 *)(next_ptr + sizeof(RAW_U8 *))) == RAW_BYTE_BLOCK_FREE) {
/* Yes, neighbor block can be merged! This is quickly accomplished
by updating the current block with the next blocks pointer. */
*((RAW_U8 * *)current_ptr) = *((RAW_U8 * *)next_ptr);
/* Reduce the fragment number, and does not need to increase available bytes since
they are already there*/
pool_ptr->raw_byte_pool_fragments--;
}
else {
/* Neighbor is not free so get to the next search point*/
current_ptr = *((RAW_U8 * *)next_ptr);
/* Reduse the examined block since we have skiped one search */
if (examine_blocks) {
examine_blocks--;
}
}
}
}
else {
/* Block is not free, move to next block. */
current_ptr = *((RAW_U8 * *)current_ptr);
}
/* finish one block search*/
if (examine_blocks) {
examine_blocks--;
}
/* Restore interrupts temporarily. */
RAW_CPU_ENABLE();
/* Disable interrupts. */
RAW_CPU_DISABLE();
/* Determine if anything has changed in terms of pool ownership. */
if (pool_ptr ->raw_byte_pool_owner != raw_task_active) {
/* Pool changed ownership during interrupts.so we reset the search*/
current_ptr = pool_ptr ->raw_byte_pool_search;
examine_blocks = pool_ptr ->raw_byte_pool_fragments + 1u;
/* Setup our ownership again. */
pool_ptr ->raw_byte_pool_owner = raw_task_active;
}
} while (examine_blocks);
/* Determine if a block was found. If so, determine if it needs to be
split. */
if (available_bytes) {
/* Do we need to split this block if this is big enough.*/
if ((available_bytes - memory_size) >= ((RAW_U32) RAW_BYTE_BLOCK_MIN)) {
/* Split the block. */
next_ptr = current_ptr + memory_size + sizeof(RAW_U8 *) + sizeof(RAW_U32);
/* Setup the new free block. */
*((RAW_U8 * *) next_ptr) = *((RAW_U8 * *) current_ptr);
*((RAW_U32 *) (next_ptr + sizeof(RAW_U8 *))) = RAW_BYTE_BLOCK_FREE;
/* Increase the total fragment counter. */
pool_ptr->raw_byte_pool_fragments++;
/* Update the current pointer to point at the newly created block. */
*((RAW_U8 * *)current_ptr) = next_ptr;
/* Set available equal to memory size for subsequent calculation. */
available_bytes = memory_size;
}
/* In any case, mark the current block as allocated. */
*((RAW_U32 *)(current_ptr + sizeof(RAW_U8 *))) = RAW_BYTE_BLOCK_ALLOC;
/* Reduce the number of available bytes in the pool. */
pool_ptr->raw_byte_pool_available = pool_ptr ->raw_byte_pool_available - available_bytes
- sizeof(RAW_U8 *) - sizeof(RAW_U32);
/* Adjust the pointer for the application. */
current_ptr = current_ptr + sizeof(RAW_U8 *) + sizeof(RAW_U32);
}
else {
/* Set current pointer to NULL to indicate nothing was found. */
current_ptr = 0;
}
/* Restore interrupts temporarily. */
RAW_CPU_ENABLE();
/* Return the searched result*/
return current_ptr;
}
/*
************************************************************************************************************************
* Allocating byte memory from pool
*
* Description: This function is called to allocate memory from pool
*
* Arguments :pool_ptr is the address of the pool
* ---------------------
* memory_ptr is the address of pointer, and it will be filled the allocating block address
* ---------------------
* memory_size is the size you want to allocate
* ---------------------
*
* Returns RAW_NO_MEMORY: No block memory available.
RAW_SUCCESS: raw os return success
* Note(s) If *memory_ptr is 0 which means no memory available now.This methods will cause fragmention.
*
*
************************************************************************************************************************
*/
RAW_OS_ERROR raw_byte_allocate(RAW_BYTE_POOL_STRUCT *pool_ptr, void **memory_ptr, RAW_U32 memory_size)
{
RAW_OS_ERROR status; /* Return status */
RAW_U8 *work_ptr; /* Working byte pointer */
RAW_TASK_OBJ *current_work_task;
RAW_U8 byte_align_mask;
RAW_SR_ALLOC();
#if (RAW_BYTE_FUNCTION_CHECK > 0)
if (pool_ptr == 0) {
return RAW_NULL_POINTER;
}
if (memory_ptr == 0) {
return RAW_NULL_POINTER;
}
#endif
if (pool_ptr->common_block_obj.object_type != RAW_BYTE_OBJ_TYPE) {
return RAW_ERROR_OBJECT_TYPE;
}
byte_align_mask = sizeof(void *) - 1u;
/* align the memory size to pointer align*/
memory_size = ((memory_size & (~byte_align_mask)) + sizeof(void *));
current_work_task = raw_task_active;
/* Disable interrupts. */
RAW_CPU_DISABLE();
/* Loop to handle cases where the owner of the pool changed. */
do {
/* Indicate that this thread is the current owner. */
pool_ptr->raw_byte_pool_owner = current_work_task;
/* Restore interrupts. */
RAW_CPU_ENABLE();
/*Search for free memory*/
work_ptr = raw_byte_pool_search(pool_ptr, memory_size);
/* Disable interrupts. */
RAW_CPU_DISABLE();
/*if raw_byte_pool_owner changed and we have not got memory yet, continue tom do search*/
} while ((!work_ptr) && (pool_ptr ->raw_byte_pool_owner != current_work_task));
/* Determine if memory was found. */
if (work_ptr) {
/* Copy the pointer into the return destination. */
*memory_ptr = (RAW_U8 *) work_ptr;
/* Set the status to success. */
status = RAW_SUCCESS;
}
else {
*memory_ptr = 0;
/* Set the status to RAW_NO_MEMORY. */
status = RAW_NO_MEMORY;
TRACE_BYTE_NO_MEMORY(raw_task_active, pool_ptr);
}
RAW_CPU_ENABLE();
return status;
}
/*
************************************************************************************************************************
* Release byte memory from pool
*
* Description: This function is called to allocate 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_byte_release(RAW_BYTE_POOL_STRUCT *pool_ptr, void *memory_ptr)
{
RAW_U8 *work_ptr; /* Working block pointer */
RAW_SR_ALLOC();
#if (RAW_BYTE_FUNCTION_CHECK > 0)
if (pool_ptr == 0) {
return RAW_NULL_POINTER;
}
if (memory_ptr == 0) {
return RAW_NULL_POINTER;
}
#endif
if (pool_ptr ->common_block_obj.object_type != RAW_BYTE_OBJ_TYPE) {
return RAW_ERROR_OBJECT_TYPE;
}
/* Back off the memory pointer to pickup its header. */
work_ptr = (RAW_U8 *)memory_ptr - sizeof(RAW_U8 *) - sizeof(RAW_U32);
/* Disable interrupts. */
RAW_CPU_DISABLE();
/* Indicate that this thread is the current owner. */
pool_ptr->raw_byte_pool_owner = raw_task_active;
/* Release the memory.*/
*((RAW_U32 *)(work_ptr + sizeof(RAW_U8 *))) = RAW_BYTE_BLOCK_FREE;
/* Update the number of available bytes in the pool. */
pool_ptr->raw_byte_pool_available =
pool_ptr->raw_byte_pool_available + (*((RAW_U8 * *)(work_ptr)) - work_ptr);
/* Set the pool search value appropriately. */
pool_ptr->raw_byte_pool_search = work_ptr;
RAW_CPU_ENABLE();
return RAW_SUCCESS;
}
#endif