Changeset 2ec725f in mainline for kernel/generic/src/mm/buddy.c


Ignore:
Timestamp:
2008-07-06T18:20:02Z (16 years ago)
Author:
Jakub Jermar <jakub@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
5f7a0ef
Parents:
000350f8
Message:

Avoid deadlock during the 'zone n' kconsole command. Buddy allocator detail is
no longer printed because the effort to avoid the deadlock was simply not worth
it.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • kernel/generic/src/mm/buddy.c

    r000350f8 r2ec725f  
    3636 *
    3737 * This file contains buddy system allocator framework.
    38  * Specialized functions are needed for this abstract framework
    39  * to be useful.
     38 * Specialized functions are needed for this abstract framework to be useful.
    4039 */
    4140
     
    4746#include <macros.h>
    4847
    49 /** Return size needed for the buddy configuration data */
     48/** Return size needed for the buddy configuration data. */
    5049size_t buddy_conf_size(int max_order)
    5150{
     
    5453
    5554
    56 /** Create buddy system
     55/** Create buddy system.
    5756 *
    5857 * Allocate memory for and initialize new buddy system.
    5958 *
    60  * @param b Preallocated buddy system control data.
    61  * @param max_order The biggest allocable size will be 2^max_order.
    62  * @param op Operations for new buddy system.
    63  * @param data Pointer to be used by implementation.
    64  *
    65  * @return New buddy system.
    66  */
    67 void buddy_system_create(buddy_system_t *b,
    68                          uint8_t max_order,
    69                          buddy_system_operations_t *op,
    70                          void *data)
     59 * @param b             Preallocated buddy system control data.
     60 * @param max_order     The biggest allocable size will be 2^max_order.
     61 * @param op            Operations for new buddy system.
     62 * @param data          Pointer to be used by implementation.
     63 *
     64 * @return              New buddy system.
     65 */
     66void
     67buddy_system_create(buddy_system_t *b, uint8_t max_order,
     68    buddy_system_operations_t *op, void *data)
    7169{
    7270        int i;
     
    8280
    8381        /*
    84          * Use memory after our own structure
     82         * Use memory after our own structure.
    8583         */
    8684        b->order = (link_t *) (&b[1]);
     
    9492}
    9593
    96 /** Check if buddy system can allocate block
    97  *
    98  * @param b Buddy system pointer
    99  * @param i Size of the block (2^i)
    100  *
    101  * @return True if block can be allocated
    102  */
    103 bool buddy_system_can_alloc(buddy_system_t *b, uint8_t i) {
     94/** Check if buddy system can allocate block.
     95 *
     96 * @param b             Buddy system pointer.
     97 * @param i             Size of the block (2^i).
     98 *
     99 * @return              True if block can be allocated.
     100 */
     101bool buddy_system_can_alloc(buddy_system_t *b, uint8_t i)
     102{
    104103        uint8_t k;
    105104       
     
    108107         * we know immediatly that we cannot satisfy the request.
    109108         */
    110         if (i > b->max_order) return false;
     109        if (i > b->max_order)
     110                return false;
    111111
    112112        /*
    113113         * Check if any bigger or equal order has free elements
    114114         */
    115         for (k=i; k <= b->max_order; k++) {
     115        for (k = i; k <= b->max_order; k++) {
    116116                if (!list_empty(&b->order[k])) {
    117117                        return true;
     
    120120       
    121121        return false;
    122        
    123 }
    124 
    125 /** Allocate PARTICULAR block from buddy system
    126  *
    127  * @ return Block of data or NULL if no such block was found
     122}
     123
     124/** Allocate PARTICULAR block from buddy system.
     125 *
     126 * @return              Block of data or NULL if no such block was found.
    128127 */
    129128link_t *buddy_system_alloc_block(buddy_system_t *b, link_t *block)
     
    136135        list_remove(left);
    137136        while (1) {
    138                 if (! b->op->get_order(b,left)) {
     137                if (!b->op->get_order(b, left)) {
    139138                        b->op->mark_busy(b, left);
    140139                        return left;
     
    144143
    145144                right = b->op->bisect(b, left);
    146                 b->op->set_order(b, left, order-1);
    147                 b->op->set_order(b, right, order-1);
     145                b->op->set_order(b, left, order - 1);
     146                b->op->set_order(b, right, order - 1);
    148147
    149148                tmp = b->op->find_block(b, block, BUDDY_SYSTEM_INNER_BLOCK);
     
    162161/** Allocate block from buddy system.
    163162 *
    164  * @param b Buddy system pointer.
    165  * @param i Returned block will be 2^i big.
    166  *
    167  * @return Block of data represented by link_t.
     163 * @param b             Buddy system pointer.
     164 * @param i             Returned block will be 2^i big.
     165 *
     166 * @return              Block of data represented by link_t.
    168167 */
    169168link_t *buddy_system_alloc(buddy_system_t *b, uint8_t i)
     
    219218       
    220219        return res;
    221        
    222220}
    223221
    224222/** Return block to buddy system.
    225223 *
    226  * @param b Buddy system pointer.
    227  * @param block Block to return.
     224 * @param b             Buddy system pointer.
     225 * @param block         Block to return.
    228226 */
    229227void buddy_system_free(buddy_system_t *b, link_t *block)
     
    269267
    270268                        /*
    271                          * Recursively add the coalesced block to the list of order i + 1.
     269                         * Recursively add the coalesced block to the list of
     270                         * order i + 1.
    272271                         */
    273272                        buddy_system_free(b, hlp);
     
    280279         */
    281280        list_append(block, &b->order[i]);
    282 
    283 }
    284 
    285 /** Prints out structure of buddy system
    286  *
    287  * @param b Pointer to buddy system
    288  * @param elem_size Element size
    289  */
    290 void buddy_system_structure_print(buddy_system_t *b, size_t elem_size) {
    291         index_t i;
    292         count_t cnt, elem_count = 0, block_count = 0;
    293         link_t *cur;
    294        
    295 
    296         printf("Order\tBlocks\tSize    \tBlock size\tElems per block\n");
    297         printf("-----\t------\t--------\t----------\t---------------\n");
    298        
    299         for (i = 0;i <= b->max_order; i++) {
    300                 cnt = 0;
    301                 if (!list_empty(&b->order[i])) {
    302                         for (cur = b->order[i].next; cur != &b->order[i]; cur = cur->next)
    303                                 cnt++;
    304                 }
    305        
    306                 printf("#%" PRIi "\t%5" PRIc "\t%7" PRIc "K\t%8" PRIi "K\t%6u\t",
    307                         i, cnt, SIZE2KB(cnt * (1 << i) * elem_size), SIZE2KB((1 << i) * elem_size), 1 << i);
    308                 if (!list_empty(&b->order[i])) {
    309                         for (cur = b->order[i].next; cur != &b->order[i]; cur = cur->next) {
    310                                 b->op->print_id(b, cur);
    311                                 printf(" ");
    312                         }
    313                 }
    314                 printf("\n");
    315                        
    316                 block_count += cnt;
    317                 elem_count += (1 << i) * cnt;
    318         }
    319         printf("-----\t------\t--------\t----------\t---------------\n");
    320         printf("Buddy system contains %" PRIc " free elements (%" PRIc " blocks)\n" , elem_count, block_count);
    321281}
    322282
Note: See TracChangeset for help on using the changeset viewer.