1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- #include "cache.h"
- size_t lub_heap__get_block_overhead(lub_heap_t * this, const void *block)
- {
- size_t overhead;
- lub_heap_cache_bucket_t *bucket = 0;
- if (this->cache) {
- bucket =
- lub_heap_cache_find_bucket_from_address(this->cache, block);
- }
- if (bucket) {
- overhead =
- lub_heap_cache_bucket__get_block_overhead(bucket, block);
- } else {
- overhead = sizeof(lub_heap_alloc_block_t);
- }
- return overhead;
- }
- size_t lub_heap__get_block_size(lub_heap_t * this, const void *block)
- {
- size_t bytes = 0;
- lub_heap_cache_bucket_t *bucket = 0;
- if (this->cache) {
- bucket =
- lub_heap_cache_find_bucket_from_address(this->cache, block);
- }
- if (bucket) {
- bytes = lub_heap_cache_bucket__get_block_size(bucket, block);
- } else {
- const lub_heap_tag_t *header = block;
- --header;
- bytes = (header->words << 2);
- bytes -= sizeof(lub_heap_alloc_block_t);
- }
- return bytes;
- }
- void *lub_heap_static_alloc(lub_heap_t * this, size_t requested_size)
- {
- void *result = NULL;
- lub_heap_free_block_t *free_block;
-
- words_t words;
- lub_heap_key_t key;
- size_t size = requested_size;
- size = (size + (LUB_HEAP_ALIGN_NATIVE - 1)) / LUB_HEAP_ALIGN_NATIVE;
- size *= LUB_HEAP_ALIGN_NATIVE;
- words = (size >> 2);
-
- key.words = words;
- key.block = 0;
-
- while ((free_block = lub_bintree_findnext(&this->free_tree, &key))) {
- lub_heap_tag_t *tail =
- lub_heap_block__get_tail((lub_heap_block_t *) free_block);
-
- if (1 == tail->segment) {
-
- break;
- }
-
- lub_heap_block_getkey(free_block, (lub_bintree_key_t *) & key);
- }
- if (NULL != free_block) {
-
- lub_bintree_remove(&this->free_tree, free_block);
-
- result = lub_heap_slice_from_top(this,
- &free_block,
- &words, BOOL_TRUE);
- if (NULL != free_block) {
-
- lub_bintree_insert(&this->free_tree, free_block);
- }
- if (NULL != result) {
- size_t allocated_size = (words << 2);
-
- ++this->stats.static_blocks;
- this->stats.static_bytes += size;
- this->stats.static_overhead += (allocated_size - size);
- }
- }
- return result;
- }
|