内存池库(转发) [英] memory pool library (repost)

查看:64
本文介绍了内存池库(转发)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我期待编写一个非常简单的内存池库,一次只存储一个数据类型
- 即提供一个连续的内存块

是调用程序分配'免费'。我是

到目前为止我已经提出了这个API,并欢迎任何反馈。

特别是,我需要帮助实现使用的链接列表

用于记录哪些块是免费的(以及对于

defragging池,当孔出现在连续块中时)


这是我到目前为止所提出的。评论/反馈欢迎。

struct mempool_t

{


unsigned char * pool_;

unsigned int block_size;


/ *变量,用于跟踪元素是否空闲* /

unsigned char * available;

};

/ *公共API * /

/ *创建大小为item_size的N个元素池。成功时返回0,1

否则* /

int mempool_create(struct mempool_t * pool,const unsigned int

num_elements,const unsigned int item_size );


/ *从池中请求mem chunk,0 ptr如果没有更多mem或rqst不能

被满足* /

unsigned char * mempool_malloc(struct mempool_t * pool,const unsigned

int num_elements);


/ *请求来自池的0-inited mem chunk,0 ptr如果没有更多的mem或

rqst无法实现* /

unsigned char * mempool_calloc(struct mempool_t * pool,const unsigned

int num_elements);


/ *从池请求mem chunk realloc,0 ptr如果没有更多mem或rqst

无法满足* /

unsigned char * mempool_realloc(struct mempool_t * pool,const unsigned

int num_elements);

/ * request mempool resize(仅扩展)0如果成功,1否则* /

int mempool_resize(stru ct mempool_t ** pool,const unsigned int

num_elements);

/ *本地(静态)函数* /

int mempool_defrag(struct mempool_t * pool);

I am looking to write a very simple memory pool library to store only
one data type at a time - i.e. to provide a contiguous block of memory
to be alloc''d free''d by the calling program. I am
I have come up with this API so far and will welcome any feedback on it.
In particular, I will need help with implementing the linked lists used
for record keeping as to which blocks were free or not (and for
"defragging" the pool when "holes" appear in the contiguous block)

This is what I''ve come up with so far. Comments/feedback welcome.
struct mempool_t
{

unsigned char * pool_ ;
unsigned int block_size ;

/* variable that keeps track of whether an element is free or not */
unsigned char * available ;
};
/* Public API */
/* Creates the pool, N elements of size item_size. Return 0 on succes, 1
otherwise */
int mempool_create(struct mempool_t *pool, const unsigned int
num_elements, const unsigned int item_size);

/* requests mem chunk from pool, 0 ptr if no more mem or rqst could not
be fulfilled */
unsigned char * mempool_malloc(struct mempool_t *pool, const unsigned
int num_elements) ;

/* requests zero-inited mem chunk from pool, 0 ptr if no more mem or
rqst could not be fulfilled */
unsigned char * mempool_calloc(struct mempool_t *pool, const unsigned
int num_elements) ;

/* requests mem chunk realloc from pool, 0 ptr if no more mem or rqst
could not be fulfilled */
unsigned char * mempool_realloc(struct mempool_t *pool, const unsigned
int num_elements) ;
/* requests mempool resize (expand only) 0 if succesful, 1 otherwise */
int mempool_resize(struct mempool_t **pool, const unsigned int
num_elements) ;
/* Local (static) functions */
int mempool_defrag(struct mempool_t * pool);

推荐答案

Gray Alien< gr ** @ andromeda.comwrites:
Grey Alien <gr**@andromeda.comwrites:

我想写一个非常简单的内存池库,一次只能存储一个数据类型
- 即提供一个连续的内存块

是调用程序分配'免费'。我是


到目前为止我已经提出了这个API,欢迎任何关于

的反馈。特别是,我需要帮助实现链接列表

用于记录哪些块是免费的(以及

defragging池时) 孔出现在连续的块中)
I am looking to write a very simple memory pool library to store only
one data type at a time - i.e. to provide a contiguous block of memory
to be alloc''d free''d by the calling program. I am
I have come up with this API so far and will welcome any feedback on
it. In particular, I will need help with implementing the linked lists
used for record keeping as to which blocks were free or not (and for
"defragging" the pool when "holes" appear in the contiguous block)



好​​的,你有API,但也许你可以这样做!可以通过使用

函数替换free来改善mallocs和frees的
成本,该函数将块放到自由链上。对于给定的大小。

分配器从自由链中获取一个项目(如果有的话)和

调用malloc否则:


void * my_alloc(size_t s)

{

void * p;

size_t size_idx;

if(s> = MIN_ALLOC_SIZE&& s< MAX_ALLOC_SIZE&&

free_chain [size_idx = s - MIN_ALLOC_SIZE]!= NULL){

p = free_chain [size_idx];

free_chain [size_idx] = *(void **)p; / * [1] * /

}

其他p = malloc(s);

返回p;

}


匹配的my_free永远不会免费调用 - 它只是将块放入

正确的链中。

当大部分程序的分配大小已知(且理想上很小)时,这种方法非常有效。


您可以使用初始块列表预先填充自由链:


for(size = MIN_ALLOC_SIZE; size< MAX_ALLOC_SIZE; size ++){

int i;

for(i = 0; i< pre_alloc_count [size - MIN_ALLOC_SIZE]; i ++){

void * p = my_alloc(size);

my_free(p,size);

}

}


显然你也可以做一个大的初始分配只是

假装它是由一点点组成,但这使得

代码变得复杂。


你也可以避免在free_chain数组中浪费空间(在某些ar的

费用算术)如果你知道常见的大小是s0,

s0 + x,s0 + 2x,s0 + 3x等等。这可能是也可能不值得

。只有当你知道有一个好处时才会使代码复杂化。


[1]这只是一个草图。可以(并且应该)移除令人讨厌的演员

但使用第一个成员为空*的结构。如果你不小心使用C99,你可以使用整理的struct hack:


struct alloc_unit {void * next; unsigned char extra []; };


MIN_ALLOC_SIZE最好是> = sizeof(void *)。


-

Ben。

OK, you have the API but maybe you could do this differently! The
costs of mallocs and frees can be ameliorated by replacing free with a
function that puts the block onto a "free chain" for a given size.
The allocator takes an item from the free chain (if there is one) and
calls malloc otherwise:

void *my_alloc(size_t s)
{
void *p;
size_t size_idx;
if (s >= MIN_ALLOC_SIZE && s < MAX_ALLOC_SIZE &&
free_chain[size_idx = s - MIN_ALLOC_SIZE] != NULL) {
p = free_chain[size_idx];
free_chain[size_idx] = *(void **)p; /* [1] */
}
else p = malloc(s);
return p;
}

A matching my_free never calls free -- it just puts the block into the
correct chain for its size.

This works very well when most of your program''s allocations are of a
few known (and ideally small) sizes.

You can pre-prime the free chains with an initial list of blocks:

for (size = MIN_ALLOC_SIZE; size < MAX_ALLOC_SIZE; size++) {
int i;
for (i = 0; i < pre_alloc_count[size - MIN_ALLOC_SIZE]; i++) {
void *p = my_alloc(size);
my_free(p, size);
}
}

Obviously you can also do one large initial allocation and just
pretend that it is made up of little bits, but that complicates the
code.

You can also avoid wasting space in the free_chain array (at the
expense of some arithmetic) if you know that the common sizes are s0,
s0 + x, s0 + 2x, s0 + 3x and so on. This may or may not be worth
while. Complicate the code only if you know there is a benefit.

[1] This is just a sketch. The yucky cast can (and should) be removed
but using a structure whose first member is a void *. If you don''t
mind using C99, you can use the tidied-up "struct hack":

struct alloc_unit { void *next; unsigned char extra[]; };

MIN_ALLOC_SIZE better be >= sizeof (void *).

--
Ben.


Ben Bacarisse< be ******** @ bsb.me.ukwrites:
Ben Bacarisse <be********@bsb.me.ukwrites:

Gray Alien< gr ** @ andromeda.comwrites:
Grey Alien <gr**@andromeda.comwrites:

>我正在寻找一个非常简单的内存池库来存储一个
一个一次数据类型 - 即提供一个连续的内存块,供调用程序免费分配。我是

到目前为止我已经提出了这个API,欢迎任何关于它的反馈。特别是,我需要帮助实现用于记录保存的链接列表,以确定哪些块是空闲的(以及当孔出现在连续中时对池进行碎片整理)块)
>I am looking to write a very simple memory pool library to store only
one data type at a time - i.e. to provide a contiguous block of memory
to be alloc''d free''d by the calling program. I am
I have come up with this API so far and will welcome any feedback on
it. In particular, I will need help with implementing the linked lists
used for record keeping as to which blocks were free or not (and for
"defragging" the pool when "holes" appear in the contiguous block)



回复自己帖子的错误表格,但我忘了说你可以

*知道*你需要defragging的所有复杂性。和内存池。

如果是这样,当然只是不理我......但我想建议一个非常简单的

API作为替代,因为你似乎建议复杂性是

让你担心。


-

Ben。

Bad form to reply to one''s own post, but I forgot to say that you may
*know* you need all the complexity of "defragging" and "memory pools".
If so of course just ignore me... but I wanted to suggest a very simple
API as an alternative as you seem to suggest the complexity is
worrying you.

--
Ben.


Ben Bacarisse写道:
Ben Bacarisse wrote:

好​​的,你有API,但也许你可以这样做!可以通过使用

函数替换free来改善mallocs和frees的
成本,该函数将块放到自由链上。对于给定的大小。
OK, you have the API but maybe you could do this differently! The
costs of mallocs and frees can be ameliorated by replacing free with a
function that puts the block onto a "free chain" for a given size.



麻烦的是,内置的malloc + free我/已经/这样做了。第二 -

猜测实施正在失去游戏。


据推测,OP有证据表明简单方法不够好吗?
< br $>
-

查询Hedgehog

Otherface:Jena RDF / Owl工具包 http://jena.sourceforge.net/

Trouble is, the built-in malloc+free my /already/ do this. Second-
guessing the implementation is losing game.

Presumably the OP has evidence that the easy way isn''t good enough?

--
Query Hedgehog
Otherface: Jena RDF/Owl toolkit http://jena.sourceforge.net/


这篇关于内存池库(转发)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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