1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298
| #include<stdio.h> #include<malloc.h> #include<algorithm>
struct buddy { unsigned int size; unsigned int* longest; unsigned int* offset; bool* is_exist; bool* is_alloc; bool* is_cut; bool* is_run; };
inline bool is_power_of_2(unsigned x) { return !(x & (x - 1)); }
unsigned fixsize(unsigned size) { size |= size >> 1; size |= size >> 2; size |= size >> 4; size |= size >> 8; size |= size >> 16; return size + 1; }
inline unsigned left_leaf(unsigned index) { return index * 2 + 1; }
inline unsigned right_leaf(unsigned index) { return index * 2 + 2; }
inline unsigned parent(unsigned index) { return (index + 1) / 2 - 1; }
struct buddy* buddy_new(int size) { struct buddy* self; unsigned node_size; int i;
if (size < 1) { size = 1; }
if (is_power_of_2(size) == false) { size = fixsize(size); }
self = (struct buddy*)malloc(2 * size * sizeof(size_t)); self->size = size; node_size = size * 2;
self->longest = (unsigned int*)malloc((size * 2 - 1) * sizeof(unsigned int)); self->offset = (unsigned int*)malloc((size * 2 - 1) * sizeof(unsigned int)); self->is_exist = (bool*)malloc((size * 2 - 1) * sizeof(bool)); self->is_alloc = (bool*)malloc((size * 2 - 1) * sizeof(bool)); self->is_cut = (bool*)malloc((size * 2 - 1) * sizeof(bool)); self->is_run = (bool*)malloc((size * 2 - 1) * sizeof(bool));
for (i = 0; i < size * 2 - 1; ++i) { if (is_power_of_2(i + 1)) { node_size /= 2; } self->longest[i] = node_size; self->offset[i] = 0; self->is_exist[i] = false; self->is_alloc[i] = false; self->is_cut[i] = false; self->is_run[i] = true; } self->is_exist[0] = true; return self; }
void buddy_print(buddy* bd) { int i, j; int layers; int y = 2; int size = bd->size; int size_tmp = size;
for (layers = 1; size_tmp != 1; layers++) { size_tmp /= 2; }
printf("\nbuddy size: %d (%d layers)\n", size, layers); for (i = 1; i <= 2 * size - 1; i++) {
for (j = 0; j < layers; j++) { printf(" "); }
if (bd->is_exist[i - 1] == false) { printf("%d", bd->longest[i - 1]); } else if (bd->is_cut[i - 1] == true) { printf("(%d)", bd->longest[i - 1]); } else if (bd->is_alloc[i - 1] == true) { printf("<%d>", bd->longest[i - 1]); } else if (bd->is_alloc[i - 1] == false) { printf("[%d]", bd->longest[i - 1]); }
if (i == y - 1) { printf("\n"); y = y * 2; layers--; } } }
void buddy_alloc(struct buddy* bp, int size) { unsigned index = 0; unsigned node_size; unsigned offset = 0; int i; struct buddy* self = bp;
if (size <= 0 || size > self->longest[1]) { printf("\nSize wrong\n"); } else if (!is_power_of_2(size)) { size = fixsize(size); }
for (i = 0; i < self->size * 2 - 1; ++i) { self->is_run[i] = true; }
for (node_size = self->size; node_size != size; node_size /= 2) {
if (self->longest[left_leaf(index)] >= size && self->is_alloc[left_leaf(index)] == false && self->is_run[left_leaf(index)] == true) { if (self->is_exist[left_leaf(index)] == false) { self->is_cut[index] = true; self->is_exist[left_leaf(index)] = true; self->is_exist[right_leaf(index)] = true; } if (self->longest[left_leaf(index)] == size && self->is_cut[left_leaf(index)] == true) { self->is_run[left_leaf(index)] = false; node_size *= 2; continue; } index = left_leaf(index); } else if (self->longest[right_leaf(index)] >= size && self->is_alloc[right_leaf(index)] == false && self->is_run[right_leaf(index)] == true) { index = right_leaf(index); } else { self->is_run[index] = false; if (index == 0) { break; } index = parent(index); node_size *= 4; } offset = (index + 1) * node_size - self->size; }
if (index == 0) { printf("\nbuffer is full\n"); return; }
self->is_alloc[index] = true; self->offset[index] = offset; printf("your index:%d\n", index); printf("your offset:%d\n", offset); }
void buddy_free(struct buddy* self, int bdindex) { unsigned node_size; unsigned index = bdindex;
if (self->is_exist[index] == false) { printf("\nIndex wrong\n"); return; }
if (self->is_cut[index] == true) { printf("\nIts children are still being used\n"); return; }
if (self->is_alloc[index] == false) { printf("\nDouble free\n"); return; } printf("\ntarget buffer index: %d\n", index); printf(">> size: %d\n", self->longest[index]); printf(">> offset: %d\n", self->offset[index]);
for (; index != 0;) { if (self->is_alloc[index] = true) { self->is_alloc[index] = false; } index = parent(index); if (self->is_alloc[left_leaf(index)] == false && self->is_alloc[right_leaf(index)] == false && self->is_cut[left_leaf(index)] == false && self->is_cut[right_leaf(index)] == false) { self->is_exist[left_leaf(index)] = false; self->is_exist[right_leaf(index)] = false; self->is_cut[index] = false; } } }
char Choose() { char choice[2]; printf("----------------\n"); printf("|Buddy System |\n"); printf("| |\n"); printf("|your choice: |\n"); printf("|1. new |\n"); printf("|2. alloc |\n"); printf("|3. free |\n"); printf("|4. show |\n"); printf("|5. exit |\n"); printf("----------------\n"); printf(">> "); scanf("%s", choice); choice[1] = 0; return choice[0]; }
int main() { buddy* bd = NULL; int size; char choice;
while (true) { choice = Choose(); switch (choice) { case 0x31: printf("input size:\n"); printf(">> "); scanf("%d", &size); bd = buddy_new(size); buddy_print(bd); break; case 0x32: if (bd == NULL) { printf("\nplease create a Buddy System first\n\n"); continue; } printf("input size\n"); printf(">> "); scanf("%d", &size); buddy_alloc(bd, size); buddy_print(bd); break; case 0x33: if (bd == NULL) { printf("\nplease create a Buddy System first\n\n"); continue; } printf("input offset\n"); printf(">> "); scanf("%d", &size); buddy_free(bd, size); buddy_print(bd); break; case 0x34: if (bd == NULL) { printf("\nplease create a Buddy System first\n\n"); continue; } buddy_print(bd); break; case 0x35: printf("\nending\n"); exit(0); default: printf("Choice wrong\n"); break; } }
return 0; }
|