写一个演示分配和回收存储块前后的存储空间状态变化的至少200行c语言伙伴储存管理演示系统代码,。
时间: 2024-02-18 09:05:04 浏览: 56
以下是一个简单的示例代码,用于演示分配和回收存储块前后的存储空间状态变化:
```
#include <stdio.h>
#include <stdlib.h>
#define MAX_MEMORY_SIZE 1024 // 最大内存大小
#define MIN_BLOCK_SIZE 8 // 最小块大小
// 存储块结构体
typedef struct block_s {
int start; // 起始地址
int size; // 大小
int used; // 是否被使用
struct block_s *next; // 下一个块
} block_t;
// 空闲块表头
block_t *free_blocks = NULL;
// 占用块表头
block_t *used_blocks = NULL;
// 初始化空闲块表
void init_free_blocks() {
free_blocks = (block_t *) malloc(sizeof(block_t));
free_blocks->start = 0;
free_blocks->size = MAX_MEMORY_SIZE;
free_blocks->used = 0;
free_blocks->next = NULL;
}
// 分配存储块
void *allocate_memory(int size) {
block_t *current, *previous = NULL;
void *memory = NULL;
int block_size = size + sizeof(block_t);
// 查找空闲块
for (current = free_blocks; current != NULL; previous = current, current = current->next) {
if (current->size >= block_size) {
// 切割空闲块
if (current->size > block_size + MIN_BLOCK_SIZE) {
block_t *split_block = (block_t *) malloc(sizeof(block_t));
split_block->start = current->start + block_size;
split_block->size = current->size - block_size;
split_block->used = 0;
split_block->next = current->next;
current->next = split_block;
current->size = block_size;
}
// 更新空闲块表
if (previous == NULL) {
free_blocks = current->next;
} else {
previous->next = current->next;
}
// 更新占用块表
current->used = 1;
current->next = used_blocks;
used_blocks = current;
// 返回内存地址
memory = (void *) (current + 1);
break;
}
}
return memory;
}
// 回收存储块
void free_memory(void *memory) {
block_t *current, *previous = NULL;
block_t *block = (block_t *) memory - 1;
block->used = 0;
// 查找相邻空闲块
for (current = free_blocks; current != NULL; previous = current, current = current->next) {
if (current->start + current->size == block->start) {
current->size += block->size;
if (previous != NULL && previous->start + previous->size == current->start) {
previous->size += current->size;
previous->next = current->next;
free(current);
current = previous;
}
break;
} else if (block->start + block->size == current->start) {
block->size += current->size;
block->next = current->next;
if (previous == NULL) {
free_blocks = block;
} else {
previous->next = block;
}
free(current);
current = block;
break;
}
}
// 更新空闲块表
if (current == NULL) {
if (previous == NULL) {
free_blocks = block;
} else {
previous->next = block;
}
block->next = NULL;
}
// 更新占用块表
for (current = used_blocks, previous = NULL; current != NULL; previous = current, current = current->next) {
if (current == block) {
if (previous == NULL) {
used_blocks = block->next;
} else {
previous->next = block->next;
}
block->next = NULL;
break;
}
}
}
// 打印空闲块表
void print_free_blocks() {
printf("Free Blocks:\n");
for (block_t *current = free_blocks; current != NULL; current = current->next) {
printf("Start: %d\tSize: %d\n", current->start, current->size);
}
}
// 打印占用块表
void print_used_blocks() {
printf("Used Blocks:\n");
for (block_t *current = used_blocks; current != NULL; current = current->next) {
printf("Start: %d\tSize: %d\n", current->start, current->size);
}
}
int main() {
init_free_blocks(); // 初始化空闲块表
int n;
while (1) {
printf("Enter a positive integer (or 0 to quit): ");
scanf("%d", &n);
if (n == 0) {
break;
}
// 分配存储块
void *memory = allocate_memory(n);
if (memory != NULL) {
printf("Memory allocated at address %p.\n", memory);
print_free_blocks();
print_used_blocks();
} else {
printf("Memory allocation failed.\n");
}
// 回收存储块
printf("Enter the address of the memory to free (or 0 to skip): ");
scanf("%p", &memory);
if (memory != NULL) {
free_memory(memory);
printf("Memory freed.\n");
print_free_blocks();
print_used_blocks();
}
}
return 0;
}
```
该示例代码实现了一个简单的存储管理系统,通过不断从终端读取整数n来演示分配和回收存储块前后的存储空间状态变化。在分配存储块时,程序会查找空闲块表并切割合适的块;在回收存储块时,程序会查找相邻的空闲块并合并。同时,程序会打印出占用块表和空闲块表,以便用户了解系统状态。
阅读全文