在C中重新定义空闲内存函数
我正在用 C 重新定义内存函数,我想知道这个想法是否可以作为 free() 函数的实现:
typedef struct _mem_dictionary
{
void *addr;
size_t size;
} mem_dictionary;
mem_dictionary *dictionary = NULL; //array of memory dictionaries
int dictionary_ct = 0; //dictionary struct counter
void *malloc(size_t size)
{
void *return_ptr = (void *) sbrk(size);
if (dictionary == NULL)
dictionary = (void *) sbrk(1024 * sizeof(mem_dictionary));
dictionary[dictionary_ct].addr = return_ptr;
dictionary[dictionary_ct].size = size;
dictionary_ct++;
printf("malloc(): %p assigned memory\n",return_ptr);
return return_ptr;
}
void free(void *ptr)
{
size_t i;
int flag = 0;
for(i = 0; i < dictionary_ct ; i++){
if(dictionary[i].addr == ptr){
dictionary[i].addr=NULL;
dictionary[i].size = 0;
flag = 1;
break;
}
}
if(!flag){
printf("Remember to free!\n");
}
}
提前致谢!
I'm redefining memory functions in C and I wonder if this idea could work as implementation for the free() function:
typedef struct _mem_dictionary
{
void *addr;
size_t size;
} mem_dictionary;
mem_dictionary *dictionary = NULL; //array of memory dictionaries
int dictionary_ct = 0; //dictionary struct counter
void *malloc(size_t size)
{
void *return_ptr = (void *) sbrk(size);
if (dictionary == NULL)
dictionary = (void *) sbrk(1024 * sizeof(mem_dictionary));
dictionary[dictionary_ct].addr = return_ptr;
dictionary[dictionary_ct].size = size;
dictionary_ct++;
printf("malloc(): %p assigned memory\n",return_ptr);
return return_ptr;
}
void free(void *ptr)
{
size_t i;
int flag = 0;
for(i = 0; i < dictionary_ct ; i++){
if(dictionary[i].addr == ptr){
dictionary[i].addr=NULL;
dictionary[i].size = 0;
flag = 1;
break;
}
}
if(!flag){
printf("Remember to free!\n");
}
}
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不,不会的。在这样的调用之后,您“释放”的地址实际上会丢失。您如何知道特定的内存块再次可用于分配?
这方面有很多研究,这里有一些概述 - 快速内存分配在多布斯博士那里。
编辑0:
您对
sbrk 的看法是错误的(2)
- 它不是“更好的 malloc”,您不能这样使用它。该系统调用修改进程数据段的结尾。No, it will not. The address you are "freeing" is effectively lost after such a call. How would you ever know that the particular chunk of memory is again available for allocation?
There has been a lot of research in this area, here is some overview - Fast Memory Allocation in Dr. Dobbs.
Edit 0:
You are wrong about
sbrk(2)
- it's not a "better malloc" and you cannot use it as such. That system call modifies end of process data segment.几件事:
字典
分配内存?dictionary->addr
指向的内存?如果没有malloc
的代码,就看不到您的free
是否可以工作。malloc
函数中,您要遍历进程可用的每个内存地址来检查它是否未被您的字典
使用,而仅仅是赋值Dictionary[i].addr=NULL
不会“释放”内存,并且绝对不会保留它以供重用。顺便说一句,当用户在据称未分配的指针上调用 free 时,您的
free
版本中的printf
函数将打印Remember to free!
, 正确的?那为什么要“记得免费”呢?编辑:
因此,使用
malloc
函数,不,您的free
不会释放内存。首先,您将丢失内存的地址,因此每次调用此malloc
时,您实际上都会进一步推动进程中断,并且永远不会重用已释放的内存位置。解决此问题的一种方法是以某种方式跟踪您已“释放”的位置,以便下次调用 malloc 时,您可以检查是否已分配给进程足够的可用内存,并且然后重用这些位置。另外,请记住,sbrk
是brk
的包装器,这是一个昂贵的系统调用,您应该优化您的malloc
,以便占用大量内存使用sbrk
从操作系统请求,然后跟踪您正在使用哪个部分以及哪个部分可用。Few things:
dictionary
?dictionary->addr
is pointing at? Without having the code for yourmalloc
it is not visible if yourfree
would work.malloc
function you're going through each and every memory address available to the process to check if it is not used by yourdictionary
, merely the assignmentdictionary[i].addr=NULL
would not "free" the memory, and definitely not keep it for reuse.BTW, the
printf
function in your version offree
would printRemember to free!
when the user calls free on a pointer that is supposedly not allocated, right? Then why "remember to free"?Edit:
So with that
malloc
function, no, yourfree
does not free the memory. First of all, you're losing the address of the memory, so every time you call thismalloc
you're actually pushing the process break a little further, and never reuse freed memory locations. One way to solve this is to somehow keep track of locations that you have "freed" so that next time thatmalloc
is called, you can check if you have enough available memory already allocated to the process, and then reuse those locations. Also, remember thatsbrk
is a wrapper aroundbrk
which is an expensive system call, you should optimize yourmalloc
so that a big chunk of memory is requested from OS usingsbrk
and then just keep track of which part you're using, and which part is available.