有选择地限制递归 [英] Limiting Recursion Selectively

查看:69
本文介绍了有选择地限制递归的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是来自分配器的C的片段。此分配器允许用户

指定回调以在必要时回收内存。如果内存不能找到
,则输入'if(reclaim)''块并调用回调。


但是,我想实现某种障碍阻止了

回调重复调用suba_alloc。一个递归调用就可以了,

但是如果第二次没有找到内存,那么很有可能会出现无限循环

循环。

那我怎么能限制这个递归调用呢?下面我设计了一个方法,在第一次调用时保存对象的地址。

次要调用不能重用相同的地址,但这是一个很差的

解决方案,因为很难将reclaim_barrier重置为NULL。


是否有其他任何方法都有一个简单的解决方案来限制这个递归调用

我已经描述过了吗?


void *

suba_alloc(struct allocator * suba,size_t size,int zero)

{

int reclaim = 0;




if(reclaim){

if(suba> reclaim_barrier == NULL){

suba> reclaim_barrier =& reclaim;

}

if(suba) - > reclaim == NULL || suba> reclaim_barrier!=& reclaim ||

suba> reclaim(suba,suba> reclaim_arg,reclaim)== 0){

errno = ENOMEM;

返回NULL;

}

}


if(找不到记忆){

reclaim ++;
再次转到;

}


返回内存;

}


谢谢,

Mike

Here''s fragment of C from an allocator. This allocator permits the user
to specify a callback to reclaim memory if necessary. If memory cannot be
found the ''if (reclaim)'' block is entered and the callback is called.

However, I would like to implement some kind of barrier that prevents the
callback from calling suba_alloc recusively. One recursive call is ok,
but if memory isn''t found a second time, chances are good an infinate
loop will occur.

So how can I limit this recursive call? Below I have devised a method of
saving the address of an object on the first call reasoning that
subsiquent calls cannot reuse the same address but this is a poor
solution as it is difficult to reset the reclaim_barrier to NULL.

Does any else have a simple solution to limit this recursive call in the
way I''ve described?

void *
suba_alloc(struct allocator *suba, size_t size, int zero)
{
int reclaim = 0;

again:
if (reclaim) {
if (suba->reclaim_barrier == NULL) {
suba->reclaim_barrier = &reclaim;
}
if (suba->reclaim == NULL || suba->reclaim_barrier != &reclaim ||
suba->reclaim(suba, suba->reclaim_arg, reclaim) == 0) {
errno = ENOMEM;
return NULL;
}
}

if (can''t find memory) {
reclaim++;
goto again;
}

return the memory;
}

Thanks,
Mike

推荐答案

Michael B Allen写道:
Michael B Allen wrote:

这是来自分配器的C的片段。此分配器允许用户指定回调以在必要时回收内存。如果无法找到内存,则输入'if(reclaim)''块并调用回调。

然而,我想实现某种阻止<回复调用suba_alloc回调。一个递归调用是可以的,
但是如果第二次没有找到内存,很可能会出现无限循环。

那么如何限制这个递归呼叫?下面我已经设计了一种方法,在第一次调用时保存对象的地址,因为
子项调用不能重用相同的地址,但由于难以重置,这是一个很差的解决方案reclaim_barrier为NULL。

有没有其他方法有一个简单的解决方案来限制我已经描述过的这种递归调用?

void *
suba_alloc(struct allocator * suba,size_t size,int zero)
{
int reclaim = 0;

再次:
if(reclaim){
if(suba> reclaim_barrier == NULL){
suba> reclaim_barrier =& reclaim;
}
if(suba> reclaim == NULL || suba - > reclaim_barrier!=& reclaim ||
suba> reclaim(suba,suba> reclaim_arg,reclaim)== 0){
errno = ENOMEM;
返回NULL ;
}

如果(找不到记忆){
回收++;
再次转到;


返回内存;
}

Here''s fragment of C from an allocator. This allocator permits the user
to specify a callback to reclaim memory if necessary. If memory cannot be
found the ''if (reclaim)'' block is entered and the callback is called.

However, I would like to implement some kind of barrier that prevents the
callback from calling suba_alloc recusively. One recursive call is ok,
but if memory isn''t found a second time, chances are good an infinate
loop will occur.

So how can I limit this recursive call? Below I have devised a method of
saving the address of an object on the first call reasoning that
subsiquent calls cannot reuse the same address but this is a poor
solution as it is difficult to reset the reclaim_barrier to NULL.

Does any else have a simple solution to limit this recursive call in the
way I''ve described?

void *
suba_alloc(struct allocator *suba, size_t size, int zero)
{
int reclaim = 0;

again:
if (reclaim) {
if (suba->reclaim_barrier == NULL) {
suba->reclaim_barrier = &reclaim;
}
if (suba->reclaim == NULL || suba->reclaim_barrier != &reclaim ||
suba->reclaim(suba, suba->reclaim_arg, reclaim) == 0) {
errno = ENOMEM;
return NULL;
}
}

if (can''t find memory) {
reclaim++;
goto again;
}

return the memory;
}



void *

suba_alloc(struct allocator * suba,size_t size,int zero)

{

int reclaim = 0;


do {

if(suba> reclaim_barrier == NULL){

suba> reclaim_barrier =& reclaim;

}

if(suba> reclaim == NULL || suba> reclaim_barrier!=& reclaim

||

suba> reclaim(suba,suba> reclaim_arg,reclaim)== 0){

errno = ENOMEM;

返回NULL;

}

} while((找不到记忆) && 2> ++ reclaim);


返回内存;

}


- -

pete


void *
suba_alloc(struct allocator *suba, size_t size, int zero)
{
int reclaim = 0;

do {
if (suba->reclaim_barrier == NULL) {
suba->reclaim_barrier = &reclaim;
}
if (suba->reclaim == NULL || suba->reclaim_barrier != &reclaim
||
suba->reclaim(suba, suba->reclaim_arg, reclaim) == 0) {
errno = ENOMEM;
return NULL;
}
} while ((can''t find memory) && 2 > ++reclaim);

return the memory;
}

--
pete


pete写道:

Michael B Allen写道:

Michael B Allen wrote:

这里是来自分配器的C的片段。
那我怎么能限制这个递归调用呢?
有没有其他方法有一个简单的解决方案来限制这个递归调用,就像我描述的那样?

Here''s fragment of C from an allocator. So how can I limit this recursive call? Does any else have a simple solution to
limit this recursive call in the way I''ve described?


} while((找不到记忆)& & 2> ++ reclaim);

} while ((can''t find memory) && 2 > ++reclaim);




很抱歉。

我认为问题更复杂,

比我想的要好。


-

pete



Sorry about that.
I think the problem is more complicated,
than what I was thinking about.

--
pete


Michael B Allen写道:
Michael B Allen wrote:

这里是来自分配器的C的片段。此分配器允许用户指定回调以在必要时回收内存。如果无法找到内存,则输入'if(reclaim)''块并调用回调。

然而,我想实现某种阻止<回复调用suba_alloc回调。一个递归调用是可以的,
但是如果第二次没有找到内存,很可能会出现无限循环。

那么如何限制这个递归呼叫?下面我已经设计了一种方法,在第一次调用时保存对象的地址,因为
子项调用不能重用相同的地址,但由于难以重置,这是一个很差的解决方案reclaim_barrier为NULL。

有没有其他方法有一个简单的解决方案来限制我已经描述过的这种递归调用?

void *
suba_alloc(struct allocator * suba,size_t size,int zero)
{
int reclaim = 0;

再次:
if(reclaim){
if(suba> reclaim_barrier == NULL){
suba> reclaim_barrier =& reclaim;
}
if(suba> reclaim == NULL || suba - > reclaim_barrier!=& reclaim ||
suba> reclaim(suba,suba> reclaim_arg,reclaim)== 0){
errno = ENOMEM;
返回NULL ;
}

如果(找不到记忆){
回收++;
再次转到;


返回内存;
}

Here''s fragment of C from an allocator. This allocator permits the user
to specify a callback to reclaim memory if necessary. If memory cannot be
found the ''if (reclaim)'' block is entered and the callback is called.

However, I would like to implement some kind of barrier that prevents the
callback from calling suba_alloc recusively. One recursive call is ok,
but if memory isn''t found a second time, chances are good an infinate
loop will occur.

So how can I limit this recursive call? Below I have devised a method of
saving the address of an object on the first call reasoning that
subsiquent calls cannot reuse the same address but this is a poor
solution as it is difficult to reset the reclaim_barrier to NULL.

Does any else have a simple solution to limit this recursive call in the
way I''ve described?

void *
suba_alloc(struct allocator *suba, size_t size, int zero)
{
int reclaim = 0;

again:
if (reclaim) {
if (suba->reclaim_barrier == NULL) {
suba->reclaim_barrier = &reclaim;
}
if (suba->reclaim == NULL || suba->reclaim_barrier != &reclaim ||
suba->reclaim(suba, suba->reclaim_arg, reclaim) == 0) {
errno = ENOMEM;
return NULL;
}
}

if (can''t find memory) {
reclaim++;
goto again;
}

return the memory;
}




你可以让'reclaim_barrier'成为一个简单的深度

计数器而不是地址? (你能不能考虑

摆脱'goto''?)


void *

suba_alloc(struct allocator * suba,size_t size,int zero)

{

void * new;

int rc;


while((new = get_some_memory(...))== NULL){

if(suba> reclaim_depth> = RECLAIM_MAX_DEPTH)

break;

++ suba-> reclaim_depth;

rc = suba_reclaim(...);

--suba-> reclaim_depth;

if(rc == 0)

break;

}

if(new == NULL)

errno = ENOMEM;

返回新的;

}


-
Er ********* @ sun.com


这篇关于有选择地限制递归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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