扩展/收缩堆? [英] extend/shrink heap ?

查看:62
本文介绍了扩展/收缩堆?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从内存管理的角度来看,如何在c中创建一个可以扩展和缩小的托管





谢谢提前帮忙。


青竹

From memory management perspective, how to code in c to create a managed
heap
that can be extended and shrunk ?
Thanks for help in advance.

Qingzhu

推荐答案

刘青珠[CAR:2N84:EXCH]写道:
Liu, Qingzhu [CAR:2N84:EXCH] wrote:
从内存管理的角度来看,如何在c中编写代码来创建一个可以扩展和缩小的托管
堆?
提前感谢您的帮助。
From memory management perspective, how to code in c to create a managed
heap
that can be extended and shrunk ?
Thanks for help in advance.



考虑到这样做的事实 - 如果你可以的话 - 是特定的平台

,将你的问题提交给特定于你的新闻组。 >
平台是正确的方法。在这里,你不在话题。


HTH,

- g


-

Artie Gold - 德克萨斯州奥斯汀


是的。这是一个都市传奇。但它是一个很棒的城市传奇!


Considering the fact that doing so -- if you can at all -- is platform
specific, addressing your question to a newsgroup specific to your
platform is the right way to go. Here, you''re off topic.

HTH,
--ag

--
Artie Gold -- Austin, Texas

"Yeah. It''s an urban legend. But it''s a *great* urban legend!"


Liu,Qingzhu [CAR:2N84:EXCH]写道:
Liu, Qingzhu [CAR:2N84:EXCH] wrote:
来自记忆管理方面,如何在c中编写代码来创建一个可以扩展和缩小的托管堆?
提前感谢您的帮助。

青竹
From memory management perspective, how to code in c to create a managed
heap
that can be extended and shrunk ?
Thanks for help in advance.

Qingzhu




首先,C语言不需要实现

来堆。有自动内存和动态内存。


如果你想自己管理动态内存,

然后使用malloc()并分配一个巨大的块。


就缩小或扩展程序的动态内存而言,

取决于操作系统并在
中进行了最佳讨论
新闻组致力于您的操作系统。


-

Thomas Matthews


C ++新闻组欢迎消息:
http://www.slack.net/~ shiva / welcome.txt

C ++常见问题: http://www.parashift.com/c++-faq-lite

C常见问题: http://www.eskimo.com/~scs/c-faq/top.html

alt.comp.lang.learn.c-c ++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html

其他网站:
http://www.josuttis.com - C ++ STL图书馆书籍/> http://www.sgi.com/tech/stl - - 标准模板库



First off, the C language does not require an implementation
to have a heap. There is automatic memory and dynamic memory.

If you would like to manage the dynamic memory yourself,
then use malloc() and allocate a huge chunk.

As far as shrinking or expanding a program''s dynamic memory,
that is up to the operating system and best discussed in a
newsgroup devoted to your operating system.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library




Liu,Qingzhu [CAR:2N84:EXCH]" < ql ** @ americasm01.nt.com>

"Liu, Qingzhu [CAR:2N84:EXCH]" <ql**@americasm01.nt.com>
从内存管理的角度来看,如何在c中编写代码来创建>管理
堆可以扩展和缩小?
From memory management perspective, how to code in c to create a > managed heap that can be extended and shrunk ?



实现meory分配方案的基本方法是声明一个竞技场


静态BLOCK竞技场[100000];


然后你分配包含


typedef struct block的块

{

struct block * next;

size_t size;

} BLOCK;


免费list包含一系列块的链表。最初

整个竞技场是一个很大的免费区块。分配时,走免费的

列表,直到找到足够大的块。然后你溢出它并且

返回控制块后面的指针。

当你自由时,走自由列表,直到你找到ponter
大于前面的并且小于下面的块,那么你需要将块添加到空闲列表并尽可能合并。

诀窍是你总是返回控制块+1,所以大小是

总是存储在你分配的内存之前。


如果你知道编译时的竞技场大小,只需硬编码即可。如果你需要查询操作系统以了解允许多少内存

使用,你就离开了comp.lang.c的领域,你将不得不在

平台特定组中询问。


The basic way to implement a meory allocation scheme is to declare an arena

static BLOCK arena[100000];

You then allocate blocks which consist of

typedef struct block
{
struct block *next;
size_t size;
}BLOCK;

The free list consists of a linked list of a a series of blocks. Initially
the whole arena is one big free block. When you allocate, walk the free
list until you find a block that is large enough. Then you spilt it and
return the pointer that is immediately after the control block.
When you free, walk the free list until you find the place where the ponter
is greater than the preceding and less than the following block, then you
have to add the block to the free list and consolidate if possible.
The trick is that you always return the control block +1, so the size is
always stored immediately before the memory you allocate.

If you know the arena size at compile time, simply hard code it in. If you
have to query the operating system to know how much memory you are allowed
to use, you leave the realms of comp.lang.c, and you will have to ask in a
platform-specific group.


这篇关于扩展/收缩堆?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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