这个 clearpool 怎么写 ?
时间:2010-08-20
来源:互联网
#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, 怎样做才对呢?
#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, 怎样做才对呢?
作者: mjus 发布时间: 2010-08-20
在moremem中: 调用 malloc UNITS 次然后连接起来试试
作者: yihai-0494 发布时间: 2010-08-20
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28