- 论坛徽章:
- 0
|
#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, 怎样做才对呢? |
|