C???????????????(????????)
?????SegmentFault ???????[ 2017/3/27 11:28:17 ] ????????C ??????????? ???
???????????????ЩC?л?????????????????Fist-fit?? Best-fit????????????С??????????????????С??????漰????????malloc??free????????????????????????????????????????????Ч?????????????????????????????????????????(?????????????????????????????????)???????????????????????
??????????????μ?????????????????
????struct T {
????T prev;
????char *avail;
????char *limit;
????};
????prev???洢?????? avail???????????????limit???洢????β????????д?avail??limit?????????????????????????????????????????????????????????????????????????????
????????????????????????????????????????????????????????е????????????????????????????????????????о???????????????????????????????????????????????????????????洢???Щ?????????????鶼???????????棬 ???????alloc?????????list?????????????飬?????????????з???
T Arena_new() {
T arena = malloc(sizeof(*arena));
if (arena == NULL) {
fprintf(stderr?? "ALLOC MEM FAILED
");
return NULL;
}
arena->prev = NULL;
arena->limit = arena->avail = NULL;
return arena;
}
??????????header?????????????????????????鰱???????й?????????滹??????????
void *Arena_alloc(T arena?? long nbytes??const char *file??
int line){
assert(arena);
assert(nbytes > 0);
// for memory align
size_t ldsize = sizeof(long double);
nbytes = (((nbytes + ldsize - 1) / ldsize) * ldsize);
while(nbytes > arena->limit - arena->avail) {
T ptr;
char *limit;
if ((ptr = freechunks) != NULL) {
freechunks = freechunks->prev;
nfree--;
limit = ptr->limit;
} else {
long m = sizeof(struct T) + nbytes + 10 * 1024;
ptr = (T)malloc(m);
if (ptr == NULL) {
fprintf(stderr?? "ALLOC MEM FAILED %s:%d
"?? file?? line);
}
limit = (char *)ptr + m;
}
*ptr = *arena;
arena->avail= (char *)((struct T *)ptr + 1);
arena->limit = limit;
arena->prev = ptr;
}
arena->avail += nbytes;
return arena->avail - nbytes;
}
???????????????????????10k+???С??????????????????????header->prev????????????????棬while???????????л???????е??????header->prev??????????????????????????????????????????????(??????while????)??????????????????????????????free?????????????????????????黹?????????????????????????freechuncks?б??????????????μ??????????????????freechunks??????????????????????????????check?????????????request???????????????????????????????freechunks????????????????????????????freechunks????????linklist?????????????????????malloc???????????????????????????????????????????freechunks????????????????????????????arena????linkList?С?
???????????????????????free??????????????freechunks???????????free???????????????????????????????????arena?????freechunks???СС??threshold??????????????freechunks??????????????free??????
void Arena_free(T arena) {
assert(arena);
while(arena->prev) {
struct T tmp = *arena->prev;
if (nfree < THRESHOLD) {
arena->prev->prev = freechunks;
freechunks = arena->prev;
nfree++;
freechunks->limit = arena->limit;
} else
free(arena->prev);
*arena = tmp;
}
??????????????к???????????6.1 ???????????arena?????????飬???????????????????μ????飬?????????????????????????????????????????freechuncks?????????Щ???飬??Щ??????С????10k+???????10k+?????μ??????????????????????????10k???????????????????????????????
??????????????????????????
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11