Changeset d851f597 in mainline for uspace/lib/libc/generic/malloc.c


Ignore:
Timestamp:
2009-07-02T14:51:09Z (15 years ago)
Author:
Martin Decky <martin@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
271283b
Parents:
9c40f88
Message:

small allocator optimization
add some comments

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/libc/generic/malloc.c

    r9c40f88 rd851f597  
    277277                                result = addr;
    278278                        } else {
    279                                 /* Block start has to be aligned, thus
    280                                    the previous block needs to be enlarged */
     279                                /* Block start has to be aligned */
    281280                                size_t excess = (size_t) (aligned - addr);
    282281                               
    283282                                if (cur->size >= real_size + excess) {
     283                                        /* The current block is large enought to fit
     284                                           data in including alignment */
    284285                                        if ((void *) cur > heap_start) {
    285                                                 // TODO: This can be optimized further in case
    286                                                 // of expanding a previous allocated block if the
    287                                                 // excess is large enought to put another free
    288                                                 // block in between
     286                                                /* There is a block before the current block.
     287                                                   This previous block can be enlarged to compensate
     288                                                   for the alignment excess */
    289289                                                heap_block_foot_t *prev_foot =
    290290                                                    ((void *) cur) - sizeof(heap_block_foot_t);
     
    296296                                               
    297297                                                size_t reduced_size = cur->size - excess;
    298                                                 cur = ((void *) cur) + excess;
    299                                                
    300                                                 block_init(prev_head, prev_head->size + excess, prev_head->free);
    301                                                 block_init(cur, reduced_size, true);
    302                                                 split_mark(cur, real_size);
     298                                                heap_block_head_t *next_head = ((void *) cur) + excess;
     299                                               
     300                                                if ((!prev_head->free) && (excess >= STRUCT_OVERHEAD)) {
     301                                                        /* The previous block is not free and there is enought
     302                                                           space to fill in a new free block between the previous
     303                                                           and current block */
     304                                                        block_init(cur, excess, true);
     305                                                } else {
     306                                                        /* The previous block is free (thus there is no need to
     307                                                           induce additional fragmentation to the heap) or the
     308                                                           excess is small, thus just enlarge the previous block */
     309                                                        block_init(prev_head, prev_head->size + excess, prev_head->free);
     310                                                }
     311                                               
     312                                                block_init(next_head, reduced_size, true);
     313                                                split_mark(next_head, real_size);
    303314                                                result = aligned;
     315                                                cur = next_head;
    304316                                        } else {
     317                                                /* The current block is the first block on the heap.
     318                                                   We have to make sure that the alignment excess
     319                                                   is large enought to fit a new free block just
     320                                                   before the current block */
    305321                                                while (excess < STRUCT_OVERHEAD) {
    306322                                                        aligned += falign;
     
    308324                                                }
    309325                                               
     326                                                /* Check for current block size again */
    310327                                                if (cur->size >= real_size + excess) {
    311328                                                        size_t reduced_size = cur->size - excess;
Note: See TracChangeset for help on using the changeset viewer.