对我的mallocator的建议 [英] Suggestions for my mallocator

查看:55
本文介绍了对我的mallocator的建议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


这是我在这个论坛上发表的第一篇文章!我希望它会成为很多人中的第一个,因为我努力成为一名C大师:)


我们有自己的写作存储分配器,我的评分很差,评价为绝望天真。现在我可以看到

的主要问题,就是有些调用myfree()实际上不会释放内存,但这很难实现,并且

其余部分似乎对我来说很好。


如果这里的任何人都能做出任何建设性的评论,那就太好了

:)

#include< stdio.h>

#include< string.h>

#include< stddef.h>


#define SYSTEM_RAM(1<<< 30)

#define MAX_TO_USE(((SYSTEM_RAM)/ 3)* 2)


静态字符堆[MAX_TO_USE];

static char * p = heap;

static char * oblivion = heap + MAX_TO_USE;

静态字符*最后一个;


void * mymalloc(长字节)

{

if( p + bytes< oblivion){

last = p;

p + = bytes;

return(void *)last;

}

返回NULL;

}


void myfree(void * q)

{

if (q == NULL)

返回;

if(q == last){

p = last;

last = NULL;

返回;

}

/ *需要更复杂的东西 - 我们没有它,所以

*让'只是泄漏内存:) * /

返回;

}


int main()

{

char * t = mymalloc(14);

strcpy(t,Hello,world! );

printf("%s \ n",t);

myfree(t);

返回0;

}

Hi all,

This is my first posting on this forum! I hope it will be the first of
many as I work my way up to be a C guru :)

We had an assigment to write our own storage allocator, and mine was
graded very poorly with comment "Hopelessly naive". Now I can see the
main problem, which is that some calls to myfree() won''t actually
release the memory, but that would be pretty difficult to achieve, and
the rest of it seems to work fine to me.

If anyone here can make any constructive comments, that would be great
:)
#include <stdio.h>
#include <string.h>
#include <stddef.h>

#define SYSTEM_RAM ( 1<<30 )
#define MAX_TO_USE ( ((SYSTEM_RAM) / 3) * 2 )

static char heap[MAX_TO_USE];
static char *p=heap;
static char *oblivion=heap+MAX_TO_USE;
static char *last;

void *mymalloc(long bytes)
{
if(p+bytes<oblivion) {
last=p;
p+=bytes;
return (void *) last;
}
return NULL;
}

void myfree(void *q)
{
if(q==NULL)
return;
if(q==last) {
p=last;
last=NULL;
return;
}
/* need something more sophisticated - we don''t have it, so
* let''s just leak the memory :) */
return;
}

int main()
{
char *t=mymalloc(14);
strcpy(t,"Hello, world!");
printf("%s\n", t);
myfree(t);
return 0;
}

推荐答案

nf ********* @ mailinator.com 说:

大家好,


这是我在这个论坛上发表的第一篇文章!我希望它会成为很多人中的第一个,因为我努力成为一名C大师:)


我们有自己的写作存储分配器,我的评分很差,评价为绝望天真。现在我可以看到

的主要问题,就是有些调用myfree()实际上不会释放内存,但这很难实现,和

其余部分似乎对我很好。
Hi all,

This is my first posting on this forum! I hope it will be the first of
many as I work my way up to be a C guru :)

We had an assigment to write our own storage allocator, and mine was
graded very poorly with comment "Hopelessly naive". Now I can see the
main problem, which is that some calls to myfree() won''t actually
release the memory, but that would be pretty difficult to achieve, and
the rest of it seems to work fine to me.



解除分配器的工作是使存储可以通过程序重新使用
。参见K& R2的pp185ff作为例子。


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上述域名中, - www。

The job of the deallocator is to make the storage available for re-use by
the program. See pp185ff of K&R2 for an example.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.


我的确如此,但诚然在某些情况下: 〜


唯一的问题是如果mymalloc和myfree调用交织在一起

而不是像括号那样嵌套。毕竟这只是一个

练习 - 它不像任何生产代码都会使用我的功能 -

我猜你最终需要内核要求严格执行malloc(在这个地方保留大型阵列不能实现理想:?)


无论如何,回到我的代码,似乎麻烦就是要保持跟踪对mymalloc()的任意数量的调用,我的分配器将

本身需要任意大的存储空间,它只能从

malloc()中得到这个,这会破坏对象。 :???:

Richard Heathfield写道:
Mine does do that, but admittedly not in certain cases :~

The only problem occurs if mymalloc and myfree calls are intertwined
rather than nested like parentheses. And after all this was just an
exercise - it''s not like any production code will use my functions -
and I''d guess that ultimately you need kernel calls for a serious
implementation of malloc (keeping large arrays around the place can''t
really be ideal :?)

Anyway, back to my code, it seems that the trouble is that to keep
track of an arbitrary number of calls to mymalloc(), my allocator would
itself need arbitrarily large storage, and it could only get this from
malloc(), which sort of defeats the object. :???:
Richard Heathfield wrote:
nf ********* @ mailinator.com 说:

大家好,


这是我在这个论坛上发表的第一篇文章!我希望它会成为很多人中的第一个,因为我努力成为一名C大师:)


我们有自己的写作存储分配器,我的评分很差,评价为绝望天真。现在我可以看到

的主要问题,就是有些调用myfree()实际上不会释放内存,但这很难实现,和

其余部分似乎对我很好。
Hi all,

This is my first posting on this forum! I hope it will be the first of
many as I work my way up to be a C guru :)

We had an assigment to write our own storage allocator, and mine was
graded very poorly with comment "Hopelessly naive". Now I can see the
main problem, which is that some calls to myfree() won''t actually
release the memory, but that would be pretty difficult to achieve, and
the rest of it seems to work fine to me.



解除分配器的工作是使存储可以重新使用

程序。参见K& R2的pp185ff作为例子。


-

Richard Heathfield

Usenet是一个奇怪的地方 - dmr 29/7/1999
http://www.cpax.org.uk

电子邮件:rjh在上述域名, - www。


The job of the deallocator is to make the storage available for re-use by
the program. See pp185ff of K&R2 for an example.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.




< nf ********* @ mailinator.comwrote in message

news:11 ********************* @ i12g2000cwa.googlegro ups.com ...

<nf*********@mailinator.comwrote in message
news:11*********************@i12g2000cwa.googlegro ups.com...

我的确如此,但在某些情况下肯定不是这样:〜


唯一的问题是如果mymalloc和myfree调用交织在一起

而不是嵌套像括号一样。毕竟这只是一个

练习 - 它不像任何生产代码都会使用我的功能 -

我猜你最终需要内核要求严格执行malloc(在这个地方保留大型阵列不能实现理想:?)


无论如何,回到我的代码,似乎麻烦就是要保持跟踪对mymalloc()的任意数量的调用,我的分配器将

本身需要任意大的存储空间,它只能从

malloc()中得到这个,这会破坏对象。 :???:
Mine does do that, but admittedly not in certain cases :~

The only problem occurs if mymalloc and myfree calls are intertwined
rather than nested like parentheses. And after all this was just an
exercise - it''s not like any production code will use my functions -
and I''d guess that ultimately you need kernel calls for a serious
implementation of malloc (keeping large arrays around the place can''t
really be ideal :?)

Anyway, back to my code, it seems that the trouble is that to keep
track of an arbitrary number of calls to mymalloc(), my allocator would
itself need arbitrarily large storage, and it could only get this from
malloc(), which sort of defeats the object. :???:



否。只允许有限但有大量的
分配是合理的。如果你有一个兆字节的可用内存,并且只允许1024

分配,那么除非平均分配低于1K,否则用户将永远不会注意到b $ b。大量的商业分配器将填补小额分配

到1K。


重要的是调用malloc()和free()可以交错

任何顺序,只要free()跟随其匹配的malloc(),当

内存被释放时,它是可用的重新分配,以及你的块/ br / >
分配不重叠。实现这一点并非易事。


对于奖励积分,您希望有效地使用存储区域 - 避免

碎片 - 并且您想要malloc()和free()在

短时间内执行。


我已经为生产系统手动编码mallocs。这些是嵌入式程序。

分配器不必在特殊的内核模式下运行。它只是一个访问全局数组的正常C函数。

-
www.personal.leeds.ac.uk/~bgy1mm

免费软件游戏下载。

No. It is quite legitimate to allow only a finite, though large number of
allocations. If you have a megabyte of memory available, and allow only 1024
allocations, then unless the average allocation is under 1K the user will
never notice. plenty of commercial allocators will pad small allocations up
to 1K.

The important thing is that calls to malloc() and free() can be interleaved
in any order, as long as the free() follows its matching malloc(), when
memory is freed it is avialable for reallocation, and the blocks you
allocate do not overlap. This is not trivial to achieve.

For bonus points you want to use your storage area efficiently - avoid
fragmentation - and you want the malloc() and the free() to execute in a
short time.

I''ve hand-code mallocs for production systems. These were embedded programs.
The allocator didn''t have to run in special kernel mode. It was simply a
normal C function that accessed a global array.
--
www.personal.leeds.ac.uk/~bgy1mm
freeware games to download.


这篇关于对我的mallocator的建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆