Changeset 2ec725f in mainline for kernel/generic/src/mm/buddy.c
- Timestamp:
- 2008-07-06T18:20:02Z (16 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 5f7a0ef
- Parents:
- 000350f8
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
kernel/generic/src/mm/buddy.c
r000350f8 r2ec725f 36 36 * 37 37 * 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. 40 39 */ 41 40 … … 47 46 #include <macros.h> 48 47 49 /** Return size needed for the buddy configuration data */48 /** Return size needed for the buddy configuration data. */ 50 49 size_t buddy_conf_size(int max_order) 51 50 { … … 54 53 55 54 56 /** Create buddy system 55 /** Create buddy system. 57 56 * 58 57 * Allocate memory for and initialize new buddy system. 59 58 * 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 */ 66 void 67 buddy_system_create(buddy_system_t *b, uint8_t max_order, 68 buddy_system_operations_t *op, void *data) 71 69 { 72 70 int i; … … 82 80 83 81 /* 84 * Use memory after our own structure 82 * Use memory after our own structure. 85 83 */ 86 84 b->order = (link_t *) (&b[1]); … … 94 92 } 95 93 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 */ 101 bool buddy_system_can_alloc(buddy_system_t *b, uint8_t i) 102 { 104 103 uint8_t k; 105 104 … … 108 107 * we know immediatly that we cannot satisfy the request. 109 108 */ 110 if (i > b->max_order) return false; 109 if (i > b->max_order) 110 return false; 111 111 112 112 /* 113 113 * Check if any bigger or equal order has free elements 114 114 */ 115 for (k =i; k <= b->max_order; k++) {115 for (k = i; k <= b->max_order; k++) { 116 116 if (!list_empty(&b->order[k])) { 117 117 return true; … … 120 120 121 121 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. 128 127 */ 129 128 link_t *buddy_system_alloc_block(buddy_system_t *b, link_t *block) … … 136 135 list_remove(left); 137 136 while (1) { 138 if (! b->op->get_order(b,left)) {137 if (!b->op->get_order(b, left)) { 139 138 b->op->mark_busy(b, left); 140 139 return left; … … 144 143 145 144 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); 148 147 149 148 tmp = b->op->find_block(b, block, BUDDY_SYSTEM_INNER_BLOCK); … … 162 161 /** Allocate block from buddy system. 163 162 * 164 * @param b 165 * @param i 166 * 167 * @return 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. 168 167 */ 169 168 link_t *buddy_system_alloc(buddy_system_t *b, uint8_t i) … … 219 218 220 219 return res; 221 222 220 } 223 221 224 222 /** Return block to buddy system. 225 223 * 226 * @param b 227 * @param block 224 * @param b Buddy system pointer. 225 * @param block Block to return. 228 226 */ 229 227 void buddy_system_free(buddy_system_t *b, link_t *block) … … 269 267 270 268 /* 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. 272 271 */ 273 272 buddy_system_free(b, hlp); … … 280 279 */ 281 280 list_append(block, &b->order[i]); 282 283 }284 285 /** Prints out structure of buddy system286 *287 * @param b Pointer to buddy system288 * @param elem_size Element size289 */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);321 281 } 322 282
Note:
See TracChangeset
for help on using the changeset viewer.