diff options
Diffstat (limited to 'apps/codecs/lib/tlsf/src/tlsf.c')
-rw-r--r-- | apps/codecs/lib/tlsf/src/tlsf.c | 160 |
1 files changed, 80 insertions, 80 deletions
diff --git a/apps/codecs/lib/tlsf/src/tlsf.c b/apps/codecs/lib/tlsf/src/tlsf.c index 830184761a..6d15c3a3f6 100644 --- a/apps/codecs/lib/tlsf/src/tlsf.c +++ b/apps/codecs/lib/tlsf/src/tlsf.c | |||
@@ -56,19 +56,19 @@ | |||
56 | #include <string.h> | 56 | #include <string.h> |
57 | 57 | ||
58 | #ifndef TLSF_USE_LOCKS | 58 | #ifndef TLSF_USE_LOCKS |
59 | #define TLSF_USE_LOCKS (0) | 59 | #define TLSF_USE_LOCKS (0) |
60 | #endif | 60 | #endif |
61 | 61 | ||
62 | #ifndef TLSF_STATISTIC | 62 | #ifndef TLSF_STATISTIC |
63 | #define TLSF_STATISTIC (0) | 63 | #define TLSF_STATISTIC (0) |
64 | #endif | 64 | #endif |
65 | 65 | ||
66 | #ifndef USE_MMAP | 66 | #ifndef USE_MMAP |
67 | #define USE_MMAP (0) | 67 | #define USE_MMAP (0) |
68 | #endif | 68 | #endif |
69 | 69 | ||
70 | #ifndef USE_SBRK | 70 | #ifndef USE_SBRK |
71 | #define USE_SBRK (0) | 71 | #define USE_SBRK (0) |
72 | #endif | 72 | #endif |
73 | 73 | ||
74 | 74 | ||
@@ -82,18 +82,18 @@ | |||
82 | #endif | 82 | #endif |
83 | 83 | ||
84 | #if TLSF_STATISTIC | 84 | #if TLSF_STATISTIC |
85 | #define TLSF_ADD_SIZE(tlsf, b) do { \ | 85 | #define TLSF_ADD_SIZE(tlsf, b) do { \ |
86 | tlsf->used_size += (b->size & BLOCK_SIZE) + BHDR_OVERHEAD; \ | 86 | tlsf->used_size += (b->size & BLOCK_SIZE) + BHDR_OVERHEAD; \ |
87 | if (tlsf->used_size > tlsf->max_size) \ | 87 | if (tlsf->used_size > tlsf->max_size) \ |
88 | tlsf->max_size = tlsf->used_size; \ | 88 | tlsf->max_size = tlsf->used_size; \ |
89 | } while(0) | 89 | } while(0) |
90 | 90 | ||
91 | #define TLSF_REMOVE_SIZE(tlsf, b) do { \ | 91 | #define TLSF_REMOVE_SIZE(tlsf, b) do { \ |
92 | tlsf->used_size -= (b->size & BLOCK_SIZE) + BHDR_OVERHEAD; \ | 92 | tlsf->used_size -= (b->size & BLOCK_SIZE) + BHDR_OVERHEAD; \ |
93 | } while(0) | 93 | } while(0) |
94 | #else | 94 | #else |
95 | #define TLSF_ADD_SIZE(tlsf, b) do{}while(0) | 95 | #define TLSF_ADD_SIZE(tlsf, b) do{}while(0) |
96 | #define TLSF_REMOVE_SIZE(tlsf, b) do{}while(0) | 96 | #define TLSF_REMOVE_SIZE(tlsf, b) do{}while(0) |
97 | #endif | 97 | #endif |
98 | 98 | ||
99 | #if USE_MMAP || USE_SBRK | 99 | #if USE_MMAP || USE_SBRK |
@@ -125,37 +125,37 @@ | |||
125 | /* Unlike the preview TLSF versions, now they are statics */ | 125 | /* Unlike the preview TLSF versions, now they are statics */ |
126 | #define BLOCK_ALIGN (sizeof(void *) * 2) | 126 | #define BLOCK_ALIGN (sizeof(void *) * 2) |
127 | 127 | ||
128 | #define MAX_FLI (30) | 128 | #define MAX_FLI (30) |
129 | #define MAX_LOG2_SLI (5) | 129 | #define MAX_LOG2_SLI (5) |
130 | #define MAX_SLI (1 << MAX_LOG2_SLI) /* MAX_SLI = 2^MAX_LOG2_SLI */ | 130 | #define MAX_SLI (1 << MAX_LOG2_SLI) /* MAX_SLI = 2^MAX_LOG2_SLI */ |
131 | 131 | ||
132 | #define FLI_OFFSET (6) /* tlsf structure just will manage blocks bigger */ | 132 | #define FLI_OFFSET (6) /* tlsf structure just will manage blocks bigger */ |
133 | /* than 128 bytes */ | 133 | /* than 128 bytes */ |
134 | #define SMALL_BLOCK (128) | 134 | #define SMALL_BLOCK (128) |
135 | #define REAL_FLI (MAX_FLI - FLI_OFFSET) | 135 | #define REAL_FLI (MAX_FLI - FLI_OFFSET) |
136 | #define MIN_BLOCK_SIZE (sizeof (free_ptr_t)) | 136 | #define MIN_BLOCK_SIZE (sizeof (free_ptr_t)) |
137 | #define BHDR_OVERHEAD (sizeof (bhdr_t) - MIN_BLOCK_SIZE) | 137 | #define BHDR_OVERHEAD (sizeof (bhdr_t) - MIN_BLOCK_SIZE) |
138 | #define TLSF_SIGNATURE (0x2A59FA59) | 138 | #define TLSF_SIGNATURE (0x2A59FA59) |
139 | 139 | ||
140 | #define PTR_MASK (sizeof(void *) - 1) | 140 | #define PTR_MASK (sizeof(void *) - 1) |
141 | #define BLOCK_SIZE (0xFFFFFFFF - PTR_MASK) | 141 | #define BLOCK_SIZE (0xFFFFFFFF - PTR_MASK) |
142 | 142 | ||
143 | #define GET_NEXT_BLOCK(_addr, _r) ((bhdr_t *) ((char *) (_addr) + (_r))) | 143 | #define GET_NEXT_BLOCK(_addr, _r) ((bhdr_t *) ((char *) (_addr) + (_r))) |
144 | #define MEM_ALIGN ((BLOCK_ALIGN) - 1) | 144 | #define MEM_ALIGN ((BLOCK_ALIGN) - 1) |
145 | #define ROUNDUP_SIZE(_r) (((_r) + MEM_ALIGN) & ~MEM_ALIGN) | 145 | #define ROUNDUP_SIZE(_r) (((_r) + MEM_ALIGN) & ~MEM_ALIGN) |
146 | #define ROUNDDOWN_SIZE(_r) ((_r) & ~MEM_ALIGN) | 146 | #define ROUNDDOWN_SIZE(_r) ((_r) & ~MEM_ALIGN) |
147 | #define ROUNDUP(_x, _v) ((((~(_x)) + 1) & ((_v)-1)) + (_x)) | 147 | #define ROUNDUP(_x, _v) ((((~(_x)) + 1) & ((_v)-1)) + (_x)) |
148 | 148 | ||
149 | #define BLOCK_STATE (0x1) | 149 | #define BLOCK_STATE (0x1) |
150 | #define PREV_STATE (0x2) | 150 | #define PREV_STATE (0x2) |
151 | 151 | ||
152 | /* bit 0 of the block size */ | 152 | /* bit 0 of the block size */ |
153 | #define FREE_BLOCK (0x1) | 153 | #define FREE_BLOCK (0x1) |
154 | #define USED_BLOCK (0x0) | 154 | #define USED_BLOCK (0x0) |
155 | 155 | ||
156 | /* bit 1 of the block size */ | 156 | /* bit 1 of the block size */ |
157 | #define PREV_FREE (0x2) | 157 | #define PREV_FREE (0x2) |
158 | #define PREV_USED (0x0) | 158 | #define PREV_USED (0x0) |
159 | 159 | ||
160 | 160 | ||
161 | #define DEFAULT_AREA_SIZE (1024*10) | 161 | #define DEFAULT_AREA_SIZE (1024*10) |
@@ -352,46 +352,46 @@ static __inline__ bhdr_t *FIND_SUITABLE_BLOCK(tlsf_t * _tlsf, int *_fl, int *_sl | |||
352 | } | 352 | } |
353 | 353 | ||
354 | 354 | ||
355 | #define EXTRACT_BLOCK_HDR(_b, _tlsf, _fl, _sl) do { \ | 355 | #define EXTRACT_BLOCK_HDR(_b, _tlsf, _fl, _sl) do { \ |
356 | _tlsf -> matrix [_fl] [_sl] = _b -> ptr.free_ptr.next; \ | 356 | _tlsf -> matrix [_fl] [_sl] = _b -> ptr.free_ptr.next; \ |
357 | if (_tlsf -> matrix[_fl][_sl]) \ | 357 | if (_tlsf -> matrix[_fl][_sl]) \ |
358 | _tlsf -> matrix[_fl][_sl] -> ptr.free_ptr.prev = NULL; \ | 358 | _tlsf -> matrix[_fl][_sl] -> ptr.free_ptr.prev = NULL; \ |
359 | else { \ | 359 | else { \ |
360 | clear_bit (_sl, &_tlsf -> sl_bitmap [_fl]); \ | 360 | clear_bit (_sl, &_tlsf -> sl_bitmap [_fl]); \ |
361 | if (!_tlsf -> sl_bitmap [_fl]) \ | 361 | if (!_tlsf -> sl_bitmap [_fl]) \ |
362 | clear_bit (_fl, &_tlsf -> fl_bitmap); \ | 362 | clear_bit (_fl, &_tlsf -> fl_bitmap); \ |
363 | } \ | 363 | } \ |
364 | _b -> ptr.free_ptr.prev = NULL; \ | 364 | _b -> ptr.free_ptr.prev = NULL; \ |
365 | _b -> ptr.free_ptr.next = NULL; \ | 365 | _b -> ptr.free_ptr.next = NULL; \ |
366 | }while(0) | 366 | }while(0) |
367 | 367 | ||
368 | 368 | ||
369 | #define EXTRACT_BLOCK(_b, _tlsf, _fl, _sl) do { \ | 369 | #define EXTRACT_BLOCK(_b, _tlsf, _fl, _sl) do { \ |
370 | if (_b -> ptr.free_ptr.next) \ | 370 | if (_b -> ptr.free_ptr.next) \ |
371 | _b -> ptr.free_ptr.next -> ptr.free_ptr.prev = _b -> ptr.free_ptr.prev; \ | 371 | _b -> ptr.free_ptr.next -> ptr.free_ptr.prev = _b -> ptr.free_ptr.prev; \ |
372 | if (_b -> ptr.free_ptr.prev) \ | 372 | if (_b -> ptr.free_ptr.prev) \ |
373 | _b -> ptr.free_ptr.prev -> ptr.free_ptr.next = _b -> ptr.free_ptr.next; \ | 373 | _b -> ptr.free_ptr.prev -> ptr.free_ptr.next = _b -> ptr.free_ptr.next; \ |
374 | if (_tlsf -> matrix [_fl][_sl] == _b) { \ | 374 | if (_tlsf -> matrix [_fl][_sl] == _b) { \ |
375 | _tlsf -> matrix [_fl][_sl] = _b -> ptr.free_ptr.next; \ | 375 | _tlsf -> matrix [_fl][_sl] = _b -> ptr.free_ptr.next; \ |
376 | if (!_tlsf -> matrix [_fl][_sl]) { \ | 376 | if (!_tlsf -> matrix [_fl][_sl]) { \ |
377 | clear_bit (_sl, &_tlsf -> sl_bitmap[_fl]); \ | 377 | clear_bit (_sl, &_tlsf -> sl_bitmap[_fl]); \ |
378 | if (!_tlsf -> sl_bitmap [_fl]) \ | 378 | if (!_tlsf -> sl_bitmap [_fl]) \ |
379 | clear_bit (_fl, &_tlsf -> fl_bitmap); \ | 379 | clear_bit (_fl, &_tlsf -> fl_bitmap); \ |
380 | } \ | 380 | } \ |
381 | } \ | 381 | } \ |
382 | _b -> ptr.free_ptr.prev = NULL; \ | 382 | _b -> ptr.free_ptr.prev = NULL; \ |
383 | _b -> ptr.free_ptr.next = NULL; \ | 383 | _b -> ptr.free_ptr.next = NULL; \ |
384 | } while(0) | 384 | } while(0) |
385 | 385 | ||
386 | #define INSERT_BLOCK(_b, _tlsf, _fl, _sl) do { \ | 386 | #define INSERT_BLOCK(_b, _tlsf, _fl, _sl) do { \ |
387 | _b -> ptr.free_ptr.prev = NULL; \ | 387 | _b -> ptr.free_ptr.prev = NULL; \ |
388 | _b -> ptr.free_ptr.next = _tlsf -> matrix [_fl][_sl]; \ | 388 | _b -> ptr.free_ptr.next = _tlsf -> matrix [_fl][_sl]; \ |
389 | if (_tlsf -> matrix [_fl][_sl]) \ | 389 | if (_tlsf -> matrix [_fl][_sl]) \ |
390 | _tlsf -> matrix [_fl][_sl] -> ptr.free_ptr.prev = _b; \ | 390 | _tlsf -> matrix [_fl][_sl] -> ptr.free_ptr.prev = _b; \ |
391 | _tlsf -> matrix [_fl][_sl] = _b; \ | 391 | _tlsf -> matrix [_fl][_sl] = _b; \ |
392 | set_bit (_sl, &_tlsf -> sl_bitmap [_fl]); \ | 392 | set_bit (_sl, &_tlsf -> sl_bitmap [_fl]); \ |
393 | set_bit (_fl, &_tlsf -> fl_bitmap); \ | 393 | set_bit (_fl, &_tlsf -> fl_bitmap); \ |
394 | } while(0) | 394 | } while(0) |
395 | 395 | ||
396 | #if USE_SBRK || USE_MMAP | 396 | #if USE_SBRK || USE_MMAP |
397 | static __inline__ void *get_new_area(size_t * size) | 397 | static __inline__ void *get_new_area(size_t * size) |
@@ -656,9 +656,9 @@ void *tlsf_realloc(void *ptr, size_t size) | |||
656 | void *ret; | 656 | void *ret; |
657 | 657 | ||
658 | #if USE_MMAP || USE_SBRK | 658 | #if USE_MMAP || USE_SBRK |
659 | if (!mp) { | 659 | if (!mp) { |
660 | return tlsf_malloc(size); | 660 | return tlsf_malloc(size); |
661 | } | 661 | } |
662 | #endif | 662 | #endif |
663 | 663 | ||
664 | TLSF_ACQUIRE_LOCK(&((tlsf_t *)mp)->lock); | 664 | TLSF_ACQUIRE_LOCK(&((tlsf_t *)mp)->lock); |
@@ -813,7 +813,7 @@ void *realloc_ex(void *ptr, size_t new_size, void *mem_pool) | |||
813 | new_size = (new_size < MIN_BLOCK_SIZE) ? MIN_BLOCK_SIZE : ROUNDUP_SIZE(new_size); | 813 | new_size = (new_size < MIN_BLOCK_SIZE) ? MIN_BLOCK_SIZE : ROUNDUP_SIZE(new_size); |
814 | tmp_size = (b->size & BLOCK_SIZE); | 814 | tmp_size = (b->size & BLOCK_SIZE); |
815 | if (new_size <= tmp_size) { | 815 | if (new_size <= tmp_size) { |
816 | TLSF_REMOVE_SIZE(tlsf, b); | 816 | TLSF_REMOVE_SIZE(tlsf, b); |
817 | if (next_b->size & FREE_BLOCK) { | 817 | if (next_b->size & FREE_BLOCK) { |
818 | MAPPING_INSERT(next_b->size & BLOCK_SIZE, &fl, &sl); | 818 | MAPPING_INSERT(next_b->size & BLOCK_SIZE, &fl, &sl); |
819 | EXTRACT_BLOCK(next_b, tlsf, fl, sl); | 819 | EXTRACT_BLOCK(next_b, tlsf, fl, sl); |
@@ -833,12 +833,12 @@ void *realloc_ex(void *ptr, size_t new_size, void *mem_pool) | |||
833 | INSERT_BLOCK(tmp_b, tlsf, fl, sl); | 833 | INSERT_BLOCK(tmp_b, tlsf, fl, sl); |
834 | b->size = new_size | (b->size & PREV_STATE); | 834 | b->size = new_size | (b->size & PREV_STATE); |
835 | } | 835 | } |
836 | TLSF_ADD_SIZE(tlsf, b); | 836 | TLSF_ADD_SIZE(tlsf, b); |
837 | return (void *) b->ptr.buffer; | 837 | return (void *) b->ptr.buffer; |
838 | } | 838 | } |
839 | if ((next_b->size & FREE_BLOCK)) { | 839 | if ((next_b->size & FREE_BLOCK)) { |
840 | if (new_size <= (tmp_size + (next_b->size & BLOCK_SIZE))) { | 840 | if (new_size <= (tmp_size + (next_b->size & BLOCK_SIZE))) { |
841 | TLSF_REMOVE_SIZE(tlsf, b); | 841 | TLSF_REMOVE_SIZE(tlsf, b); |
842 | MAPPING_INSERT(next_b->size & BLOCK_SIZE, &fl, &sl); | 842 | MAPPING_INSERT(next_b->size & BLOCK_SIZE, &fl, &sl); |
843 | EXTRACT_BLOCK(next_b, tlsf, fl, sl); | 843 | EXTRACT_BLOCK(next_b, tlsf, fl, sl); |
844 | b->size += (next_b->size & BLOCK_SIZE) + BHDR_OVERHEAD; | 844 | b->size += (next_b->size & BLOCK_SIZE) + BHDR_OVERHEAD; |
@@ -856,7 +856,7 @@ void *realloc_ex(void *ptr, size_t new_size, void *mem_pool) | |||
856 | INSERT_BLOCK(tmp_b, tlsf, fl, sl); | 856 | INSERT_BLOCK(tmp_b, tlsf, fl, sl); |
857 | b->size = new_size | (b->size & PREV_STATE); | 857 | b->size = new_size | (b->size & PREV_STATE); |
858 | } | 858 | } |
859 | TLSF_ADD_SIZE(tlsf, b); | 859 | TLSF_ADD_SIZE(tlsf, b); |
860 | return (void *) b->ptr.buffer; | 860 | return (void *) b->ptr.buffer; |
861 | } | 861 | } |
862 | } | 862 | } |