#include <linux/types.h>
#include <stdlib.h>
#include <stdio.h>
Include dependency graph for rtl_malloc.h:
This graph shows which files directly or indirectly include this file:
Go to the source code of this file.
Defines | |
#define | WITH_RTL_PREFIX |
#define | MAX_FL_INDEX 24 |
#define | __u8 unsigned char |
#define | __u16 unsigned short |
#define | __u32 unsigned int |
#define | __s8 char |
#define | __s16 short |
#define | __s32 int |
#define | PRINT_MSG printf |
#define | PRINT_DBG_C(message) printf(message) |
#define | PRINT_DBG_D(message) printf("%i", message); |
#define | PRINT_DBG_F(message) printf("%f", message); |
#define | PRINT_DBG_H(message) printf("%x", (unsigned int) message); |
#define | associate_buffer(ptr) main_buffer = (char *) ptr; |
#define | rtl_malloc(size) rtl_malloc_ex (size, main_buffer) |
#define | rtl_realloc(p, new_len) rtl_realloc_ex (p, new_len, main_buffer) |
#define | rtl_calloc(nelem, elem_size) rtl_calloc_ex (nelem, elem_size, main_buffer) |
#define | rtl_free(ptr) rtl_free_ex (ptr, main_buffer) |
Functions | |
int | init_memory_pool (int max_size, int max_sl_log2_index, char *block_ptr) |
void | destroy_memory_pool (char *block_ptr) |
void * | rtl_malloc_ex (size_t size, char *block_ptr) |
void * | rtl_realloc_ex (void *p, size_t new_len, char *block_ptr) |
void * | rtl_calloc_ex (size_t nelem, size_t elem_size, char *block_ptr) |
void | rtl_free_ex (void *ptr, char *block_ptr) |
void | free_blocks_status (char *block_ptr) |
void | structure_status (char *block_ptr) |
Variables | |
char * | main_buffer |
|
Definition at line 53 of file rtl_malloc.h. |
|
Definition at line 54 of file rtl_malloc.h. Referenced by init_memory_pool(), log2size(), and mapping_function(). |
|
Definition at line 52 of file rtl_malloc.h. |
|
Definition at line 50 of file rtl_malloc.h. |
|
Definition at line 51 of file rtl_malloc.h. Referenced by init_memory_pool(). |
|
Definition at line 49 of file rtl_malloc.h. Referenced by rtl_calloc_ex(), rtl_free_ex(), rtl_malloc_ex(), rtl_realloc_ex(), and structure_status(). |
|
Definition at line 91 of file rtl_malloc.h. Referenced by main(). |
|
Definition at line 39 of file rtl_malloc.h. Referenced by init_memory_pool(). |
|
Definition at line 75 of file rtl_malloc.h. Referenced by dump_memory_region(), free_blocks_status(), print_block(), and structure_status(). |
|
Definition at line 76 of file rtl_malloc.h. Referenced by free_blocks_status(), print_block(), and structure_status(). |
|
Definition at line 77 of file rtl_malloc.h. |
|
Definition at line 78 of file rtl_malloc.h. Referenced by dump_memory_region(), free_blocks_status(), print_block(), and structure_status(). |
|
Definition at line 74 of file rtl_malloc.h. Referenced by free_blocks_status(), init_memory_pool(), rtl_free_ex(), rtl_malloc_ex(), and structure_status(). |
|
Definition at line 136 of file rtl_malloc.h. |
|
Definition at line 153 of file rtl_malloc.h. Referenced by FIFO_dealloc_rx_buffer(), free_all_resources(), main(), sys_free(), vortex_close(), and vortex_open(). |
|
Definition at line 118 of file rtl_malloc.h. Referenced by FIFO_initialize_rx_buffer(), main(), sys_malloc(), and vortex_open(). |
|
Definition at line 127 of file rtl_malloc.h. |
|
Definition at line 24 of file rtl_malloc.h. |
|
Definition at line 385 of file rtl_malloc.c. References TLSF_t. Referenced by main().
|
|
Definition at line 860 of file rtl_malloc.c. References block_header_t, block_header_struct::free_ptr, MAGIC_NUMBER, MIN_LOG2_SIZE, free_ptr_struct::next, NULL, print_block(), PRINT_DBG_C, PRINT_DBG_D, PRINT_DBG_H, PRINT_MSG, TLSF_t, and TLSF_WORDS2BYTES.
00860 { 00861 int i, j; 00862 block_header_t *b; 00863 00864 TLSF_t *ptr_TLSF; 00865 00866 ptr_TLSF = (TLSF_t *) block_ptr; 00867 if (ptr_TLSF == NULL || ptr_TLSF -> magic_number != MAGIC_NUMBER) { 00868 PRINT_MSG ("FATAL ERROR: TLSF structure not initialized\n"); 00869 return; 00870 } 00871 00872 PRINT_DBG_C ("\nTLSF structure address 0x"); 00873 PRINT_DBG_H (ptr_TLSF); 00874 PRINT_DBG_C ("\nFREE BLOCKS\n\n"); 00875 for (i = ptr_TLSF -> max_fl_index - 1 - MIN_LOG2_SIZE; i >= 0; i--) { 00876 if (ptr_TLSF -> fl_array [i].bitmapSL > 0) 00877 for (j = ptr_TLSF -> max_sl_index - 1; j >= 0; j--) { 00878 if (ptr_TLSF -> fl_array [i].sl_array[j] != NULL) { 00879 b = ptr_TLSF -> fl_array [i].sl_array [j]; 00880 PRINT_DBG_C ("["); 00881 PRINT_DBG_D (i + MIN_LOG2_SIZE); 00882 PRINT_DBG_C ("] "); 00883 PRINT_DBG_D (TLSF_WORDS2BYTES(1 << (i + MIN_LOG2_SIZE))); 00884 PRINT_DBG_C (" bytes -> Free blocks: 0x"); 00885 PRINT_DBG_H (ptr_TLSF -> fl_array [i].bitmapSL); 00886 PRINT_DBG_C ("\n"); 00887 00888 while (b != NULL) { 00889 PRINT_DBG_C (">>>> First_Level ["); 00890 PRINT_DBG_D (i + MIN_LOG2_SIZE); 00891 PRINT_DBG_C ("] Second Level ["); 00892 PRINT_DBG_D (j); 00893 PRINT_DBG_C ("] -> "); 00894 PRINT_DBG_D (TLSF_WORDS2BYTES((1 << (i + MIN_LOG2_SIZE)) + 00895 ( ((1 << (i + MIN_LOG2_SIZE)) / 00896 ptr_TLSF -> max_sl_index) * j))); 00897 00898 PRINT_DBG_C (" bytes\n"); 00899 print_block (b); 00900 b = b -> ptr.free_ptr.next; 00901 } 00902 } 00903 } 00904 } 00905 } |
Here is the call graph for this function:
|
Definition at line 270 of file rtl_malloc.c. References __s32, __u32, beg_header_overhead, block_header_t, BYTES2TLSF_WORDS, fl_array_t, GET_BLOCK_SIZE, INIT_THREAD_MUTEX, LOG2_TLSF_WORD_SIZE, MAGIC_NUMBER, mapping_function(), MAX_FL_INDEX, MIN_LOG2_SIZE, MIN_SIZE, NULL, PRINT_MSG, SET_LAST_BLOCK, TLSF__set_bit, TLSF_t, and TLSF_WORDS2BYTES. Referenced by main().
00271 { 00272 __s32 n, free_mem = 0, total_size; 00273 block_header_t *initial_block_ptr; 00274 __s32 size_fl_sl_array, i, fl, sl; 00275 TLSF_t *ptr_TLSF; 00276 00277 if (!(max_size > 0)) { 00278 PRINT_MSG ("ERROR: size must be > 0\n"); 00279 return -1; 00280 } 00281 00282 if (max_sl_log2_index > 5 || max_sl_log2_index < 1) { 00283 PRINT_MSG ("ERROR max_sl_log2_index must be >=1 or <= 5\n"); 00284 return -1; 00285 } 00286 00287 if (max_sl_log2_index <= 0) { 00288 PRINT_MSG ("ERROR: max_sl_log2_index (%d) must be >= 0\n", 00289 max_sl_log2_index); 00290 return -1; 00291 } 00292 00293 if ((((__u32) block_ptr >> LOG2_TLSF_WORD_SIZE) << LOG2_TLSF_WORD_SIZE) 00294 != (__u32) block_ptr) { 00295 PRINT_MSG ("ERROR block_ptr must be aligned\n"); 00296 return -1; 00297 } 00298 00299 memset ((char *) block_ptr, 0x00, max_size * 1024); 00300 00301 INIT_THREAD_MUTEX(); 00302 ptr_TLSF = (TLSF_t *) block_ptr; 00303 00304 ptr_TLSF -> magic_number = MAGIC_NUMBER; 00305 00306 /* Total size of the block, TLSF_struct + free_memory */ 00307 00308 total_size = BYTES2TLSF_WORDS(max_size * 1024); 00309 00310 ptr_TLSF -> max_sl_log2_index = max_sl_log2_index; 00311 ptr_TLSF -> max_sl_index = (1 << ptr_TLSF -> max_sl_log2_index); 00312 00313 size_fl_sl_array = BYTES2TLSF_WORDS((__s32) sizeof (fl_array_t) + 00314 ((__s32) sizeof (block_header_t *) * (__s32) ptr_TLSF -> max_sl_index)); 00315 00316 free_mem = total_size - BYTES2TLSF_WORDS(sizeof (TLSF_t)) - size_fl_sl_array; 00317 00318 n = MIN_LOG2_SIZE + 1; 00319 00320 while ((int) TLSF_WORDS2BYTES(free_mem) > 00321 (1 << (n + LOG2_TLSF_WORD_SIZE)) && n < MAX_FL_INDEX) { 00322 n ++; 00323 free_mem -= size_fl_sl_array; 00324 } 00325 00326 if (free_mem < 0) return -1; 00327 00328 // max_fl_index never will be greater than 32 (4 Gbytes) 00329 00330 ptr_TLSF -> max_fl_index = n; 00331 ptr_TLSF -> max_fl_pow2_index = (1 << ptr_TLSF -> max_fl_index); 00332 00333 n -= MIN_LOG2_SIZE; 00334 00335 /* max_fl_index will never be greater than MAX_FL_INDEX */ 00336 if (ptr_TLSF -> max_fl_index < 0 || MAX_FL_INDEX < 0) return -1; 00337 00338 ptr_TLSF -> fl_array = ( fl_array_t *) 00339 ((__u32) &(ptr_TLSF -> fl_array) 00340 + (__u32) sizeof (ptr_TLSF -> fl_array)); 00341 00342 for (i = 0 ; i < n; i ++) 00343 ptr_TLSF -> fl_array [i] .sl_array = (block_header_t **) 00344 (((__s32) ptr_TLSF -> fl_array + ((__s32) sizeof (fl_array_t) * n)) + 00345 ((__s32) sizeof (block_header_t *) * 00346 (__s32) ptr_TLSF -> max_sl_index * i)); 00347 00348 initial_block_ptr = (block_header_t *) 00349 ((__u32) ptr_TLSF -> fl_array + 00350 (TLSF_WORDS2BYTES(size_fl_sl_array) * n)); 00351 00352 ptr_TLSF -> first_bh = initial_block_ptr; 00353 00354 beg_header_overhead = BYTES2TLSF_WORDS((int) initial_block_ptr -> ptr.buffer 00355 - (int) initial_block_ptr); 00356 00357 ptr_TLSF -> bitmapFL = 0; 00358 00359 00360 initial_block_ptr -> size = 00361 free_mem - beg_header_overhead; 00362 00363 SET_LAST_BLOCK (initial_block_ptr); 00364 00365 initial_block_ptr -> ptr.free_ptr.prev = NULL; 00366 initial_block_ptr -> ptr.free_ptr.next = NULL; 00367 initial_block_ptr -> prev_phys_block = NULL; 00368 00369 if (TLSF_WORDS2BYTES(GET_BLOCK_SIZE(initial_block_ptr)) 00370 <= MIN_SIZE) { 00371 return -1; 00372 } else { 00373 mapping_function (GET_BLOCK_SIZE(initial_block_ptr), &fl, &sl, 00374 ptr_TLSF); 00375 fl -= MIN_LOG2_SIZE; 00376 } 00377 00378 ptr_TLSF -> fl_array [fl].sl_array [sl] = initial_block_ptr; 00379 TLSF__set_bit (sl, ptr_TLSF -> fl_array[fl].bitmapSL); 00380 TLSF__set_bit (fl, ptr_TLSF -> bitmapFL); 00381 00382 return TLSF_WORDS2BYTES(GET_BLOCK_SIZE(initial_block_ptr)); 00383 } |
Here is the call graph for this function:
|
Definition at line 764 of file rtl_malloc.c. References __u8, NULL, and rtl_malloc_ex().
00764 { 00765 #else 00766 void *calloc_ex (size_t nelem, size_t elem_size, char *block_ptr) { 00767 #endif 00768 00769 __u8 *p; 00770 00771 if (nelem <= 0 || elem_size <= 0) return NULL; 00772 00773 #ifdef WITH_RTL_PREFIX 00774 if ((p = (__u8 *) rtl_malloc_ex (nelem * elem_size, block_ptr)) == NULL) 00775 return NULL; 00776 #else 00777 if ((p = (__u8 *) malloc_ex (nelem * elem_size, block_ptr)) == NULL ) 00778 return NULL; 00779 #endif 00780 00781 memset (p, 0, nelem * elem_size); 00782 00783 return ((void *) p); 00784 } |
Here is the call graph for this function:
|
Definition at line 582 of file rtl_malloc.c. References __u8, beg_header_overhead, block_header_t, block_header_struct::buffer, free_ptr_struct::first_index, block_header_struct::free_ptr, GET_BLOCK_SIZE, IS_LAST_BLOCK, IS_USED_BLOCK, MAGIC_NUMBER, mapping_function(), MIN_LOG2_SIZE, free_ptr_struct::next, NULL, free_ptr_struct::prev, PRINT_MSG, free_ptr_struct::second_index, SET_FREE_BLOCK, SET_LAST_BLOCK, THREAD_LOCK, THREAD_UNLOCK, TLSF__clear_bit, TLSF__set_bit, TLSF_t, and TLSF_WORDS2BYTES. Referenced by rtl_realloc_ex().
00582 { 00583 #else 00584 void free_ex (void *ptr, char *block_ptr) { 00585 #endif 00586 int fl, sl; 00587 block_header_t *bh = NULL, *bh2 = NULL, *bh3; 00588 00589 TLSF_t *ptr_TLSF; 00590 00591 ptr_TLSF = (TLSF_t *) block_ptr; 00592 00593 if (ptr_TLSF == NULL || ptr_TLSF -> magic_number != MAGIC_NUMBER) { 00594 PRINT_MSG ("FATAL ERROR: TLSF structure not initialized\n"); 00595 return; 00596 } 00597 00598 bh = (block_header_t *) ((__u8 *) ptr - 00599 TLSF_WORDS2BYTES(beg_header_overhead)); 00600 00601 THREAD_LOCK(); 00602 /* now bh is a free block */ 00603 00604 SET_FREE_BLOCK (bh); 00605 bh -> ptr.free_ptr.prev = NULL; 00606 bh -> ptr.free_ptr.next = NULL; 00607 00608 /* 00609 * first of all, we will try to merge bh with the 00610 * physically contiguos free block and 00611 * after we will inserte bh into TLSF structure 00612 */ 00613 00614 /* Now we will try if we can merge bh with the next phys. contiguos block */ 00615 if (!IS_LAST_BLOCK (bh)) { 00616 /* is it the next block free? */ 00617 /* The next block is easy to found */ 00618 00619 bh2 = (block_header_t *) (__u8 *) (bh -> ptr.buffer + 00620 TLSF_WORDS2BYTES 00621 (GET_BLOCK_SIZE(bh))); 00622 if (!IS_USED_BLOCK (bh2)) { 00623 /* we are lucky, we can merge bh with the following one */ 00624 if (bh2 -> ptr.free_ptr.next != NULL) 00625 bh2 -> ptr.free_ptr.next -> ptr.free_ptr.prev = 00626 bh2 -> ptr.free_ptr.prev; 00627 00628 if (bh2 -> ptr.free_ptr.prev != NULL) 00629 bh2 -> ptr.free_ptr.prev -> ptr.free_ptr.next = 00630 bh2 -> ptr.free_ptr.next; 00631 00632 fl = bh2 -> ptr.free_ptr.first_index; 00633 sl = bh2 -> ptr.free_ptr.second_index; 00634 00635 /* bh2 must be deleted from fl_array */ 00636 if (ptr_TLSF -> fl_array [fl].sl_array [sl] == bh2) 00637 ptr_TLSF -> fl_array [fl].sl_array [sl] = bh2 -> ptr.free_ptr.next; 00638 00639 if (ptr_TLSF -> fl_array [fl].sl_array [sl] == NULL){ 00640 TLSF__clear_bit (sl, ptr_TLSF -> fl_array[fl].bitmapSL); 00641 if (!ptr_TLSF -> fl_array[fl].bitmapSL) 00642 TLSF__clear_bit (fl, ptr_TLSF -> bitmapFL); 00643 00644 } 00645 00646 bh -> size += bh2 -> size + beg_header_overhead; 00647 if (!IS_LAST_BLOCK (bh2)) { 00648 bh3 = (block_header_t *) (__u8 *) (bh2 -> ptr.buffer + 00649 TLSF_WORDS2BYTES 00650 (GET_BLOCK_SIZE(bh2))); 00651 bh3 -> prev_phys_block = bh; 00652 } 00653 00654 if (IS_LAST_BLOCK (bh2)) SET_LAST_BLOCK (bh); 00655 } 00656 } 00657 00658 /* is it free the previous physical block? */ 00659 if (bh -> prev_phys_block != NULL) { // This block is not the first block 00660 00661 bh2 = bh -> prev_phys_block; 00662 00663 if (!IS_USED_BLOCK (bh2)) { 00664 00665 if (bh2 -> ptr.free_ptr.next != NULL) 00666 bh2 -> ptr.free_ptr.next -> ptr.free_ptr.prev = 00667 bh2 -> ptr.free_ptr.prev; 00668 00669 if (bh2 -> ptr.free_ptr.prev != NULL) 00670 bh2 -> ptr.free_ptr.prev -> ptr.free_ptr.next = 00671 bh2 -> ptr.free_ptr.next; 00672 00673 fl = bh2 -> ptr.free_ptr.first_index; 00674 sl = bh2 -> ptr.free_ptr.second_index; 00675 00676 if (ptr_TLSF -> fl_array [fl].sl_array [sl] == bh2) 00677 ptr_TLSF -> fl_array [fl].sl_array [sl] = bh2 -> ptr.free_ptr.next; 00678 00679 if (ptr_TLSF -> fl_array[fl].sl_array[sl] == NULL){ 00680 00681 TLSF__clear_bit (sl, ptr_TLSF -> fl_array[fl].bitmapSL); 00682 if (!ptr_TLSF -> fl_array[fl].bitmapSL) 00683 TLSF__clear_bit (fl, ptr_TLSF -> bitmapFL); 00684 00685 } 00686 bh2 -> size += bh -> size + beg_header_overhead; 00687 00688 bh = bh2; 00689 if (!IS_LAST_BLOCK (bh)) { 00690 bh3 = (block_header_t *) (__u8 *) (bh -> ptr.buffer + 00691 TLSF_WORDS2BYTES 00692 (GET_BLOCK_SIZE(bh))); 00693 bh3 -> prev_phys_block = bh; 00694 } 00695 00696 } 00697 00698 } 00699 00700 /* 00701 * and now we can merge the free block with the initial memory 00702 */ 00703 mapping_function (GET_BLOCK_SIZE (bh), &fl, &sl, ptr_TLSF); 00704 fl -= MIN_LOG2_SIZE; 00705 bh -> ptr.free_ptr.first_index = fl; 00706 bh -> ptr.free_ptr.second_index = sl; 00707 bh -> ptr.free_ptr.next = ptr_TLSF -> fl_array [fl].sl_array [sl]; 00708 bh -> ptr.free_ptr.prev = NULL; 00709 00710 if (ptr_TLSF -> fl_array [fl].sl_array [sl] != NULL) 00711 ptr_TLSF -> fl_array [fl].sl_array [sl] -> ptr.free_ptr.prev = bh; 00712 ptr_TLSF -> fl_array [fl].sl_array [sl] = bh; 00713 00714 TLSF__set_bit (sl, ptr_TLSF -> fl_array[fl].bitmapSL); 00715 TLSF__set_bit (fl, ptr_TLSF -> bitmapFL); 00716 THREAD_UNLOCK(); 00717 } |
Here is the call graph for this function:
|
Definition at line 406 of file rtl_malloc.c. References __u8, beg_header_overhead, block_header_t, block_header_struct::buffer, BYTES2TLSF_WORDS, free_ptr_struct::first_index, TLSF_struct::fl_array, block_header_struct::free_ptr, GET_BLOCK_SIZE, IS_LAST_BLOCK, MAGIC_NUMBER, mapping_function(), MIN_LOG2_SIZE, MIN_SIZE, free_ptr_struct::next, NULL, free_ptr_struct::prev, PRINT_MSG, free_ptr_struct::second_index, SET_LAST_BLOCK, SET_USED_BLOCK, fl_array_struct::sl_array, THREAD_LOCK, THREAD_UNLOCK, TLSF__clear_bit, TLSF__set_bit, TLSF_fls(), TLSF_t, and TLSF_WORDS2BYTES. Referenced by rtl_calloc_ex(), and rtl_realloc_ex().
00406 { 00407 #else 00408 void *malloc_ex (size_t size, char *block_ptr) { 00409 #endif 00410 TLSF_t *ptr_TLSF; 00411 int fl, sl, n, found = 0, old_size = BYTES2TLSF_WORDS(size), last_block; 00412 block_header_t *bh = NULL, *bh2 = NULL, *bh3 = NULL; 00413 00414 ptr_TLSF = (TLSF_t *) block_ptr; 00415 00416 if (ptr_TLSF == NULL || ptr_TLSF -> magic_number != MAGIC_NUMBER) { 00417 PRINT_MSG ("FATAL ERROR: TLSF structure not initialized\n"); 00418 return NULL; 00419 } 00420 00421 if (!(size > 0)) { 00422 PRINT_MSG ("ERROR: Memory pool exhausted!!!\n"); 00423 return NULL; 00424 } 00425 00426 if (old_size < MIN_SIZE) { 00427 size = MIN_SIZE; 00428 fl = 0; 00429 sl = 0; 00430 } else { 00431 mapping_function (old_size, &fl, &sl, ptr_TLSF); 00432 00433 if (++sl == ptr_TLSF -> max_sl_index) { 00434 fl ++; 00435 sl = 0; 00436 } 00437 00438 /* 00439 * This is the reason of the internal fragmentation 00440 * The block given is greater that the size demanded 00441 */ 00442 00443 /* size can be smaller that maximum SLI, in this case the mapping function 00444 has problems calculating fl and sl values */ 00445 00446 if (old_size >= 00447 BYTES2TLSF_WORDS(ptr_TLSF -> max_sl_index << 2)) { 00448 size = (1 << fl); 00449 size += (sl << (fl - ptr_TLSF -> max_sl_log2_index)); 00450 } else { 00451 size = old_size + 1; 00452 sl = old_size - MIN_SIZE; 00453 } 00454 00455 fl -= MIN_LOG2_SIZE; 00456 } 00457 00458 00459 /*----------------------------------------*/ 00460 /* The search for a free block begins now */ 00461 /*----------------------------------------*/ 00462 00463 /* 00464 * Our first try, we take the first free block 00465 * from fl_array or its buddy 00466 */ 00467 00468 THREAD_LOCK(); 00469 sl = ptr_TLSF -> fl_array[fl].bitmapSL & ((~0) << sl); 00470 if (sl != 0) { 00471 sl = TLSF_fls(sl); 00472 bh = ptr_TLSF -> fl_array [fl].sl_array [sl]; 00473 ptr_TLSF -> fl_array [fl].sl_array [sl] = bh -> ptr.free_ptr.next; 00474 if (ptr_TLSF -> fl_array [fl].sl_array [sl] != NULL) 00475 ptr_TLSF ->fl_array [fl].sl_array [sl] -> ptr.free_ptr.prev = NULL; 00476 else { 00477 TLSF__clear_bit (sl, ptr_TLSF -> fl_array[fl].bitmapSL); 00478 if (!ptr_TLSF -> fl_array[fl].bitmapSL) 00479 TLSF__clear_bit (fl, ptr_TLSF -> bitmapFL); 00480 } 00481 found = 1; 00482 goto out; 00483 } 00484 00485 /* 00486 * On the last case a free block is searched using a bitmap 00487 */ 00488 00489 fl = TLSF_fls(ptr_TLSF -> bitmapFL & ((~0) << (fl + 1))); 00490 00491 if (fl > 0) { 00492 sl = TLSF_fls(ptr_TLSF -> fl_array[fl].bitmapSL); 00493 bh = ptr_TLSF -> fl_array [fl].sl_array [sl]; 00494 ptr_TLSF -> fl_array [fl].sl_array [sl] = bh -> ptr.free_ptr.next; 00495 if (ptr_TLSF -> fl_array [fl].sl_array [sl] != NULL){ 00496 ptr_TLSF -> fl_array [fl].sl_array [sl] 00497 -> ptr.free_ptr.prev = NULL; 00498 } else { 00499 TLSF__clear_bit (sl, ptr_TLSF -> fl_array[fl].bitmapSL); 00500 if (!ptr_TLSF -> fl_array[fl].bitmapSL) 00501 TLSF__clear_bit (fl, ptr_TLSF -> bitmapFL); 00502 } 00503 found = 1; 00504 goto out; 00505 } 00506 /* end of the search */ 00507 /*------------------------------------------------------------*/ 00508 00509 out: 00510 00511 /* 00512 * HUGGGG, NOT ENOUGHT MEMORY 00513 * I think that we have done all that we have been able, I'm sorry 00514 */ 00515 00516 if (!found) { 00517 THREAD_UNLOCK(); 00518 PRINT_MSG ("ERROR: Memory pool exhausted!!!\n"); 00519 return NULL; 00520 } 00521 00522 /* 00523 * we can say: YESSSSSSSSSSS, we have enought memory!!!! 00524 */ 00525 00526 /* can bh be splitted? */ 00527 00528 n = (int)(GET_BLOCK_SIZE(bh) - size - beg_header_overhead); 00529 if (n >= (int) MIN_SIZE) { 00530 /* 00531 * Yes, bh will be splitted 00532 */ 00533 00534 /* The new block will begin at the end of the current block */ 00535 last_block = IS_LAST_BLOCK(bh)?1:0; 00536 bh -> size = size; 00537 SET_USED_BLOCK(bh); 00538 00539 bh2 = (block_header_t *) (__u8 *) (bh -> ptr.buffer + 00540 TLSF_WORDS2BYTES 00541 (GET_BLOCK_SIZE(bh))); 00542 bh2 -> prev_phys_block = bh; 00543 bh2 -> size = n; 00544 if (last_block) SET_LAST_BLOCK (bh2); 00545 mapping_function (GET_BLOCK_SIZE(bh2), &fl, &sl, ptr_TLSF); 00546 fl -= MIN_LOG2_SIZE; 00547 bh2 -> ptr.free_ptr.first_index = fl; 00548 bh2 -> ptr.free_ptr.second_index = sl; 00549 bh2 -> ptr.free_ptr.prev = NULL; 00550 bh2 -> ptr.free_ptr.next = ptr_TLSF -> fl_array [fl].sl_array [sl]; 00551 00552 if (!IS_LAST_BLOCK (bh2)) { 00553 bh3 = (block_header_t *) (__u8 *) (bh2 -> ptr.buffer + 00554 TLSF_WORDS2BYTES 00555 (GET_BLOCK_SIZE(bh2))); 00556 bh3 -> prev_phys_block = bh2; 00557 } 00558 00559 00560 if (ptr_TLSF -> fl_array [fl].sl_array [sl] != NULL) 00561 ptr_TLSF -> fl_array [fl].sl_array [sl] -> ptr.free_ptr.prev = bh2; 00562 ptr_TLSF -> fl_array [fl].sl_array [sl] = bh2; 00563 TLSF__set_bit (sl, ptr_TLSF -> fl_array[fl].bitmapSL); 00564 TLSF__set_bit (fl, ptr_TLSF -> bitmapFL); 00565 } 00566 00567 SET_USED_BLOCK(bh); 00568 00569 THREAD_UNLOCK(); 00570 00571 return (void *) bh -> ptr.buffer; 00572 } |
Here is the call graph for this function:
|
Definition at line 721 of file rtl_malloc.c. References __u8, beg_header_overhead, block_header_t, NULL, rtl_free_ex(), rtl_malloc_ex(), and TLSF_WORDS2BYTES.
00721 { 00722 #else 00723 void *realloc_ex (void *p, size_t new_len, char *block_ptr) { 00724 #endif 00725 __u8 *ptr_aux; 00726 block_header_t *b; 00727 00728 if (p == NULL) 00729 #ifdef WITH_RTL_PREFIX 00730 return (void *) rtl_malloc_ex (new_len, block_ptr); 00731 #else 00732 return (void *) malloc_ex (new_len, block_ptr); 00733 #endif 00734 else if (new_len == 0) { 00735 #ifdef WITH_RTL_PREFIX 00736 rtl_free_ex (p, block_ptr); 00737 #else 00738 free_ex (p, block_ptr); 00739 #endif 00740 return NULL; 00741 } 00742 00743 #ifdef WITH_RTL_PREFIX 00744 ptr_aux = (__u8 *) rtl_malloc_ex (new_len * sizeof (__u8), block_ptr); 00745 #else 00746 ptr_aux = (__u8 *) malloc_ex (new_len * sizeof (__u8), block_ptr); 00747 #endif 00748 00749 b = (block_header_t *) (((__u8 *) p) - 00750 TLSF_WORDS2BYTES(beg_header_overhead)); 00751 00752 memcpy ((__u8 *) ptr_aux, (__u8 *) b, b -> size); 00753 #ifdef WITH_RTL_PREFIX 00754 rtl_free_ex (p, block_ptr); 00755 #else 00756 free_ex (p, block_ptr); 00757 #endif 00758 00759 return ((void *) ptr_aux); 00760 } |
Here is the call graph for this function:
|
Definition at line 825 of file rtl_malloc.c. References __u8, block_header_t, block_header_struct::buffer, GET_BLOCK_SIZE, IS_LAST_BLOCK, MAGIC_NUMBER, NULL, print_block(), PRINT_DBG_C, PRINT_DBG_D, PRINT_DBG_H, PRINT_MSG, TLSF_t, and TLSF_WORDS2BYTES.
00825 { 00826 block_header_t *b; 00827 TLSF_t *ptr_TLSF; 00828 int end = 0; 00829 00830 ptr_TLSF = (TLSF_t *) block_ptr; 00831 if (ptr_TLSF == NULL || ptr_TLSF -> magic_number != MAGIC_NUMBER) { 00832 PRINT_MSG ("FATAL ERROR: TLSF structure not initialized\n"); 00833 return; 00834 } 00835 00836 b = ptr_TLSF -> first_bh; 00837 PRINT_DBG_C ("\nTLSF structure address 0x"); 00838 PRINT_DBG_H (ptr_TLSF); 00839 PRINT_DBG_C ("\nMax. first level index: "); 00840 PRINT_DBG_D (ptr_TLSF -> max_fl_index); 00841 PRINT_DBG_C ("\nMax. second level index: "); 00842 PRINT_DBG_D (ptr_TLSF -> max_sl_log2_index); 00843 PRINT_DBG_C ("\n\nALL BLOCKS\n"); 00844 while (!end) { 00845 print_block (b); 00846 if (IS_LAST_BLOCK(b)) 00847 end = 1; 00848 else 00849 b = (block_header_t *) (__u8 *) (b -> ptr.buffer + 00850 TLSF_WORDS2BYTES 00851 (GET_BLOCK_SIZE(b))); 00852 } 00853 00854 } |
Here is the call graph for this function:
|
Definition at line 82 of file rtl_malloc.h. |