提示:文章
文章目录
- 前言
- 一、背景
- 二、
- 2.1
- Ace代码
- 三、
- 3.1
- 总结
前言
前期疑问:
本文目标:
一、背景
最近
二、
2.1
Ace代码
Aced代码形式如下
#include <stdbool.h>
#include <stdio.h>
#include <malloc.h>
#include "securec.h"typedef struct {int start;int end;
} MemoryUint;typedef struct {bool status;int index;
} HashMap;typedef struct {MemoryUint memoryUint[101];HashMap hashMap[101];int hashCount;int unitCount;int memLeftSize;int totalMemorySize;
} MemSystem;static int compare(const void* a, const void* b)
{return ((HashMap*) a)->index - ((HashMap*) b)->index;
}// 注意:该函数为类构造函数,返回的对象指针将作为其他待实现函数的入参;框架代码在调用该函数后,会输出 null(而非指针)
static MemSystem* MemSystemCreate(int space)
{MemSystem* sys = (MemSystem*) malloc(sizeof(MemSystem));if (sys == NULL) {return NULL;}memset_s(sys->memoryUint, sizeof(sys->memoryUint), 0, sizeof(sys->memoryUint));memset_s(sys->hashMap, sizeof(sys->hashMap), 0, sizeof(sys->hashMap));sys->totalMemorySize = space;sys->memLeftSize = space;sys->hashCount = 0;sys->unitCount = 0;return sys;
}static int MemSystemRequest(MemSystem* sys, int size)
{if(sys->totalMemorySize < size){return -1;}if (size == 0) {return -1;}if (sys->hashCount == 0) {sys->memoryUint[0].start = 0;sys->memoryUint[0].end = sys->memoryUint[0].end + size;sys->hashMap[0].status = true;sys->hashMap[0].index = sys->memoryUint[0].start;sys->hashCount += 1;return sys->memoryUint[0].start;}for (int i = 0; i < sys->hashCount; i++) {if (sys->hashMap[i].status == true) {int spareMemSize = 0;if (sys->hashCount == 1) {if (sys->memoryUint[sys->hashMap[0].index].start >= size) {sys->memoryUint[0].start = 0;sys->memoryUint[0].end = sys->memoryUint[0].start + size;sys->hashMap[sys->hashCount].status = true;sys->hashMap[sys->hashCount].index = sys->memoryUint[0].start;sys->hashCount += 1;qsort(sys->hashMap, sys->hashCount, sizeof(HashMap), compare);return sys->memoryUint[0].start;}spareMemSize = sys->totalMemorySize - sys->memoryUint[sys->hashMap[0].index].end;} else {if (sys->memoryUint[sys->hashMap[0].index].start >= size) {sys->memoryUint[0].start = 0;sys->memoryUint[0].end = sys->memoryUint[0].start + size;sys->hashMap[sys->hashCount].status = true;sys->hashMap[sys->hashCount].index = sys->memoryUint[0].start;sys->hashCount += 1;qsort(sys->hashMap, sys->hashCount, sizeof(HashMap), compare);return sys->memoryUint[0].start;}int lastMemInfo = 0;if (sys->hashMap[i + 1].status == false) {lastMemInfo = sys->totalMemorySize;} else {lastMemInfo = sys->hashMap[i + 1].index;}spareMemSize = lastMemInfo - sys->memoryUint[sys->hashMap[i].index].end;}if (spareMemSize >= size) {sys->memoryUint[sys->memoryUint[sys->hashMap[i].index].end].start = sys->memoryUint[sys->hashMap[i].index].end;sys->memoryUint[sys->memoryUint[sys->hashMap[i].index].end].end =sys->memoryUint[sys->memoryUint[sys->hashMap[i].index].end].start + size;if (sys->hashMap[i + 1].status == false) {sys->hashMap[i + 1].status = true;sys->hashMap[i + 1].index = sys->memoryUint[sys->memoryUint[sys->hashMap[i].index].end].start;} else {// 在中间申请内存,hash表中间插入一个key值for (int j = sys->hashCount - 1; j > i; j--) {bool status = sys->hashMap[j].status;int index = sys->hashMap[j].index;sys->hashMap[j + 1].status = status;sys->hashMap[j + 1].index = index;}sys->hashMap[i + 1].status = true;sys->hashMap[i + 1].index = sys->memoryUint[sys->memoryUint[sys->hashMap[i].index].end].start;}sys->hashCount += 1;return sys->memoryUint[sys->memoryUint[sys->hashMap[i].index].end].start;}}}return -1;
}static bool MemSystemRelease(MemSystem* sys, int addr)
{for (int i = 0; i < sys->hashCount; i++) {if (sys->hashMap[i].status == true) {if (sys->memoryUint[sys->hashMap[i].index].start == addr) {sys->memoryUint[sys->hashMap[i].index].start = 0;sys->memoryUint[sys->hashMap[i].index].end = 0;sys->hashMap[i].status = false;sys->hashMap[i].index = 0;for (int j = i; j < sys->hashCount; j++) {bool status = sys->hashMap[j + 1].status;int index = sys->hashMap[j + 1].index;sys->hashMap[j].status = status;sys->hashMap[j].index = index;}sys->hashMap[sys->hashCount].status = false;sys->hashMap[sys->hashCount].index = 0;sys->hashCount -= 1;return true;}}}return false;
}static void MemSystemFree(MemSystem* sys)
{free(sys);
}
降低圈复杂度和嵌套深度,优化代码如下
#include <stdbool.h>
#include <stdio.h>
#include <malloc.h>
#include "securec.h"typedef struct {int start;int end;
} MemoryUint;typedef struct {bool status;int index;
} HashMap;typedef struct {MemoryUint memoryUint[101];HashMap hashMap[101];int hashCount;int unitCount;int memLeftSize;int totalMemorySize;
} MemSystem;static int compare(const void* a, const void* b)
{return ((HashMap*) a)->index - ((HashMap*) b)->index;
}static bool GetStartWhileStartAddrLargerThenRequestSize(MemSystem* sys, int size, int* start)
{if (sys->memoryUint[sys->hashMap[0].index].start >= size) {sys->memoryUint[0].start = 0;sys->memoryUint[0].end = sys->memoryUint[0].start + size;sys->hashMap[sys->hashCount].status = true;sys->hashMap[sys->hashCount].index = sys->memoryUint[0].start;sys->hashCount += 1;qsort(sys->hashMap, sys->hashCount, sizeof(HashMap), compare);*start = sys->memoryUint[0].start;return true;}return false;
}static int GetStartWhileMemoryIsAvailable(MemSystem* sys, int size, int i)
{sys->memoryUint[sys->memoryUint[sys->hashMap[i].index].end].start = sys->memoryUint[sys->hashMap[i].index].end;sys->memoryUint[sys->memoryUint[sys->hashMap[i].index].end].end =sys->memoryUint[sys->memoryUint[sys->hashMap[i].index].end].start + size;if (sys->hashMap[i + 1].status == false) {sys->hashMap[i + 1].status = true;sys->hashMap[i + 1].index = sys->memoryUint[sys->memoryUint[sys->hashMap[i].index].end].start;} else {// 在中间申请内存,hash表中间插入一个key值for (int j = sys->hashCount - 1; j > i; j--) {bool status = sys->hashMap[j].status;int index = sys->hashMap[j].index;sys->hashMap[j + 1].status = status;sys->hashMap[j + 1].index = index;}sys->hashMap[i + 1].status = true;sys->hashMap[i + 1].index = sys->memoryUint[sys->memoryUint[sys->hashMap[i].index].end].start;}sys->hashCount += 1;return sys->memoryUint[sys->memoryUint[sys->hashMap[i].index].end].start;
}// 注意:该函数为类构造函数,返回的对象指针将作为其他待实现函数的入参;框架代码在调用该函数后,会输出 null(而非指针)
static MemSystem* MemSystemCreate(int space)
{MemSystem* sys = (MemSystem*) malloc(sizeof(MemSystem));if (sys == NULL) {return NULL;}memset_s(sys->memoryUint, sizeof(sys->memoryUint), 0, sizeof(sys->memoryUint));memset_s(sys->hashMap, sizeof(sys->hashMap), 0, sizeof(sys->hashMap));sys->totalMemorySize = space;sys->memLeftSize = space;sys->hashCount = 0;sys->unitCount = 0;return sys;
}static int MemSystemRequest(MemSystem* sys, int size)
{if (sys->totalMemorySize < size) {return -1;}if (size == 0) {return -1;}if (sys->hashCount == 0) {sys->memoryUint[0].start = 0;sys->memoryUint[0].end = sys->memoryUint[0].end + size;sys->hashMap[0].status = true;sys->hashMap[0].index = sys->memoryUint[0].start;sys->hashCount += 1;return sys->memoryUint[0].start;}for (int i = 0; i < sys->hashCount; i++) {if (sys->hashMap[i].status == true) {int spareMemSize = 0;if (sys->hashCount == 1) {int start = 0;if(GetStartWhileStartAddrLargerThenRequestSize(sys, size, &start)){return start;}spareMemSize = sys->totalMemorySize - sys->memoryUint[sys->hashMap[0].index].end;} else {int start = 0;if(GetStartWhileStartAddrLargerThenRequestSize(sys, size, &start)){return start;}int lastMemInfo = 0;if (sys->hashMap[i + 1].status == false) {lastMemInfo = sys->totalMemorySize;} else {lastMemInfo = sys->hashMap[i + 1].index;}spareMemSize = lastMemInfo - sys->memoryUint[sys->hashMap[i].index].end;}if (spareMemSize >= size) {int start = 0;start = GetStartWhileMemoryIsAvailable(sys, size, i);return start;}}}return -1;
}static bool MemSystemRelease(MemSystem* sys, int addr)
{for (int i = 0; i < sys->hashCount; i++) {if (sys->hashMap[i].status == true) {if (sys->memoryUint[sys->hashMap[i].index].start == addr) {sys->memoryUint[sys->hashMap[i].index].start = 0;sys->memoryUint[sys->hashMap[i].index].end = 0;sys->hashMap[i].status = false;sys->hashMap[i].index = 0;for (int j = i; j < sys->hashCount; j++) {bool status = sys->hashMap[j + 1].status;int index = sys->hashMap[j + 1].index;sys->hashMap[j].status = status;sys->hashMap[j].index = index;}sys->hashMap[sys->hashCount].status = false;sys->hashMap[sys->hashCount].index = 0;sys->hashCount -= 1;return true;}}}return false;
}static void MemSystemFree(MemSystem* sys)
{free(sys);
}
三、
3.1
总结
未完待续