Chinaunix
标题:
这个 clearpool 怎么写 ?
[打印本页]
作者:
mjus
时间:
2010-08-20 08:02
标题:
这个 clearpool 怎么写 ?
#include<stdio.h>
#include<stdlib.h>
typedef struct _anyT {
int val;
} anyT;
#define UNITS 10
typedef union _data_block {
union _data_block *next;
anyT the_type;
} data_block;
static data_block *free_list = NULL;
static void moremem(void) {
int i;
data_block *more = calloc(sizeof(data_block),UNITS);
for(i = 0; i < UNITS; i++) {
more[i].next = free_list;
free_list = more + i;
}
}
anyT *allocanyT(void) {
data_block *current;
if(free_list == NULL) {
moremem();
return allocanyT();
}
current = free_list;
free_list = free_list->next;
return &(current->the_type);
}
void freeanyT(anyT *x)
{
((data_block *)x)->next = free_list;
free_list = (data_block *)x;
}
void clearpool() {
data_block *head;
anyT* cur;
for(head=free_list;head;head=free_list) {
free_list=free_list->next;
cur=(anyT*)&head->the_type;
free(cur);
}
}
#define MAX 100
int main() {
anyT* arr[MAX];
int i;
for(i=0; i<MAX; i++)
arr[i]=allocanyT();
clearpool();
}
如上所见, function clearppool 没有真正清除 pool 里的 objects, 怎样做才对呢?
作者:
yihai-0494
时间:
2010-08-20 13:46
在moremem中: 调用 malloc UNITS 次然后连接起来试试
作者:
mjus
时间:
2010-08-20 21:39
static void moremem(void) {
int i;
data_block *more = calloc(sizeof(data_block),UNITS);
for(i = 0; i < UNITS; i++) {
more[i].next = free_list; //<<<
free_list = more + i; //<<<
}
}
连接起来了阿!不是吗?
欢迎光临 Chinaunix (http://bbs.chinaunix.net/)
Powered by Discuz! X3.2