避免malloc [英] Avoiding malloc

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

问题描述






对于资源有限的嵌入式系统,我想避免使用malloc。


目前我有各种各样的图书馆。隐藏数据的模块

opaque指针。因为我读到这是件好事。


例如在界面foo.h中


typedef struct foo_t * foo_t;

foo_t fooNew(void);


然后在实现中foo.c


struct foo_t

{

int private_field;

...

};

启动时(仅限)应用程序使用foo模块提供的

函数创建foo对象:


foo_t bob = fooNew();


fooNew目前通过调用malloc分配存储,它知道如何分配多少空间,因为它可以访问私有定义

of foo_t。


这一切都运行良好,我认为是相当标准的东西,但我现在想要在一个较小的系统中使用这些模块。

提供的malloc在代码和数据中都占用了太多空间。另外

提前很难知道应用程序使用了多少存储空间

应用程序。


我想要什么喜欢做的是在

应用程序中静态分配存储空间。级别,而不是动态来自库内部

模块。例如


静态无符号字符bob_private_data [sizeof * foo_t];


foo_t bob = fooInit(bob_private_data);


我不介意重写模块来支持这个。但是我看不到

如何在不暴露foo_t表示的情况下完成它。


我想我只是要放私人结构

接口标题中的定义,但这与我b / b
读过的所有内容相比。


我猜它是不可能,也许这就是为什么C ++ [咳嗽]必须把它的私人放在一边。标题中的定义也是如此。


-


John Devereux


Hi,

For a resource constrained embedded system I want to avoid malloc.

Currently I have various "library" modules that hide their data behind
opaque pointers. Because I read that was a good thing.

For example in the interface foo.h

typedef struct foo_t *foo_t;
foo_t fooNew(void);

Then in the implementation foo.c

struct foo_t
{
int private_field;
...
};
At startup (only) the application creates foo objects using the
function provided by the foo module:

foo_t bob = fooNew();

fooNew currently allocates storage by calling malloc, it knows how
much space to allocate since it has access to the private definition
of foo_t.

This all works well and is pretty standard stuff I think, but I now
would like to use some of these modules in a smaller system. The
provided malloc takes up too much space, both in code and data. Also
it is difficult to know ahead of time how much storage is being used
by the application.

What I would like to do is statically allocate storage at the
"application" level, instead of dynamically from inside the library
module. E.g.

static unsigned char bob_private_data[sizeof *foo_t];

foo_t bob = fooInit(bob_private_data);

I don''t mind rewriting the modules to support this. But I can''t see
how to do it at all without exposing the representation of foo_t.

I think I am just going to have to put the "private" structure
definition in the interface header, but this goes against everything I
have read about.

I am guessing it is impossible, perhaps that is why C++[cough] has to
put its "private" definitions in the headers too.

--

John Devereux

推荐答案

John Devereux写道:
John Devereux wrote:


对于资源受限的嵌入式系统,我想避免使用malloc。

目前我有各种库将数据隐藏在不透明指针之后的模块。因为我读到这是一件好事。

在启动时(仅),应用程序使用foo模块提供的
函数创建foo对象:

foo_t bob = fooNew();

fooNew目前通过调用malloc来分配存储,它知道如何分配多少空间,因为它可以访问foo_t的私有定义。

这一切都运行良好,我认为是相当标准的东西,但我现在想要在较小的系统中使用其中一些模块。
提供的malloc在代码和数据中占用了太多空间。另外
很难提前知道应用程序使用了多少存储空间。

我想要做的是静态分配存储在
应用程序级别,而不是从库内部动态模块。例如

静态unsigned char bob_private_data [sizeof * foo_t];

foo_t bob = fooInit(bob_private_data);

我不介意改写支持这个的模块。但是我没有看到如何在不暴露foo_t表示的情况下完成它。
Hi,

For a resource constrained embedded system I want to avoid malloc.

Currently I have various "library" modules that hide their data behind
opaque pointers. Because I read that was a good thing.

At startup (only) the application creates foo objects using the
function provided by the foo module:

foo_t bob = fooNew();

fooNew currently allocates storage by calling malloc, it knows how
much space to allocate since it has access to the private definition
of foo_t.

This all works well and is pretty standard stuff I think, but I now
would like to use some of these modules in a smaller system. The
provided malloc takes up too much space, both in code and data. Also
it is difficult to know ahead of time how much storage is being used
by the application.

What I would like to do is statically allocate storage at the
"application" level, instead of dynamically from inside the library
module. E.g.

static unsigned char bob_private_data[sizeof *foo_t];

foo_t bob = fooInit(bob_private_data);

I don''t mind rewriting the modules to support this. But I can''t see
how to do it at all without exposing the representation of foo_t.



如果您知道要使用多少个对象,为什么不呢?创建一个

数组(池)并返回指向

中数组元素的指针,同样malloc将返回指向已分配内存的指针?


-

Ian Collins。


If you know how many objects you are going to use, why not create an
array (pool) of them and return pointers to the array elements in the
same way malloc would return a pointer to allocated memory?

--
Ian Collins.


" John Devereux" < JD ****** @ THISdevereux.me.uk>在消息中写道

news:87 ************ @ cordelia.devereux.me.uk ...

:资源有限嵌入式系统我想避免使用malloc。



:目前我有各种各样的库。隐藏数据的模块

:不透明指针。因为我读到这是一件好事。


这种方法提供了很好的封装 - 这个确实很好,特别是如果你分发一个库以二进制形式,
或需要能够替换库而无需重新编译所有应用程序(例如Linux内核或操作系统级库)。


但是对于某些应用程序来说它也有缺点(例如,确实会产生
开销,需要内存分配,这可能是不必要的)。

所以这个解决方案不适合所有应用程序。


:这一切都运行良好,我认为是相当标准的东西,但我现在

:想要使用一些这些模块在较小的系统中。

:提供的malloc在代码和数据中占用了太多空间。另外

:很难提前知道使用了多少存储空间

:应用程序。


嗯,你可以提供你自己的malloc,减少存储空间 -

如果所有的分配都是在

启动时完成的,或者只是简单地完成,这可以很简单像堆栈一样的时尚...


但是说真的,如果你的目标平台资源非常紧张

(例如一台带有4Kb RAM的微控制器,就像我一样一直在使用),

没有必要通过箍来提供高水平的封装




:我想我只需要把私人放在一边。结构

:界面标题中的定义,但是这与我所有的一切相反。
:已阅读过。


Hominem unius libri Timeo酒店。得到一些其他的书;)

....并且不要太担心它。


:我猜这是不可能的,也许这就是为什么C ++ [咳嗽]必须

:将其私有放入标题中的定义也是如此。


如果你想要基于堆栈的对象,那么对象大小至少必须暴露并且被用户代码知道。这确实限制了封装。

另一种方法是接受使用堆分配的

对象的开销 - 这是C ++支持的解决方案,以及其他成语。

亲切问候,

伊万

-
http://ivan.vecerina.com/contact/?subject=NG_POST < - 电子邮件联系表格
"John Devereux" <jd******@THISdevereux.me.uk> wrote in message
news:87************@cordelia.devereux.me.uk...
: For a resource constrained embedded system I want to avoid malloc.
:
: Currently I have various "library" modules that hide their data behind
: opaque pointers. Because I read that was a good thing.

This approach is a means of providing storong encapsulation - this
is good indeed, especially if you distribute a library in binary form,
or need to be able to replace the library without recompiling
all applications (e.g. for the Linux kernel or an OS-level library).

But it has drawbacks too for some applications (e.g. does create
overhead, requiring memory allocations that could be unnecessary).
So this solution is not fit for all applications.

: This all works well and is pretty standard stuff I think, but I now
: would like to use some of these modules in a smaller system. The
: provided malloc takes up too much space, both in code and data. Also
: it is difficult to know ahead of time how much storage is being used
: by the application.

Well, you could provide your own malloc with less storage overhead -
this can be simple enough if all your allocations were done at
start-up, or are done in a simple stack-like fashion ...

But seriously, if your target platform is really tight on resources
(e.g. a microcontroller with 4Kb of RAM, like I have been using),
there is no point in going through hoops to provide a high level
of encapsulation.

: I think I am just going to have to put the "private" structure
: definition in the interface header, but this goes against everything I
: have read about.

Hominem unius libri timeo. Get some other books ;)
.... and don''t worry about it too much.

: I am guessing it is impossible, perhaps that is why C++[cough] has to
: put its "private" definitions in the headers too.

If you want stack-based objects, the object size at least has to
be exposed and known by user code. This does limit encapsulation.
The alternative is to accept the overhead of using heap-allocated
objects -- a solution that C++ supports well, among other idioms.
Kind regards,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form


John Devereux写道:
John Devereux wrote:


对于资源受限的嵌入式系统,我想避免使用malloc。

目前我有各种库将数据隐藏在不透明指针之后的模块。因为我读到这是件好事。

例如在界面foo.h中

typedef struct foo_t * foo_t;
foo_t fooNew(void);


< snip>

启动时(仅),应用程序使用foo模块提供的
函数创建foo对象:

foo_t bob = fooNew();

fooNew目前通过调用malloc来分配存储空间,它知道如何


< snip>

我想要做的是在
应用程序中静态分配存储空间。级别,而不是从库内部动态模块。例如

静态unsigned char bob_private_data [sizeof * foo_t];

foo_t bob = fooInit(bob_private_data);

我不介意改写支持这个的模块。但是我没有看到如何在不暴露foo_t表示的情况下完成它。
Hi,

For a resource constrained embedded system I want to avoid malloc.

Currently I have various "library" modules that hide their data behind
opaque pointers. Because I read that was a good thing.

For example in the interface foo.h

typedef struct foo_t *foo_t;
foo_t fooNew(void);
<snip>
At startup (only) the application creates foo objects using the
function provided by the foo module:

foo_t bob = fooNew();

fooNew currently allocates storage by calling malloc, it knows how
<snip>
What I would like to do is statically allocate storage at the
"application" level, instead of dynamically from inside the library
module. E.g.

static unsigned char bob_private_data[sizeof *foo_t];

foo_t bob = fooInit(bob_private_data);

I don''t mind rewriting the modules to support this. But I can''t see
how to do it at all without exposing the representation of foo_t.




< snip>


可能有的作用是:


/ * foo.c * /

#include" app_config.h"

#include" foo.h"


/ * define type foo_t * /


static foo_t foo_private_data [APP_FOOS_REQUIRED ];

....


/ * app_config.h * /

#define APP_FOOS_REQUIRED 10

....


然后foo_t仍然可以是应用程序的opaque类型。缺点

是必须针对新的

app_config.h为每个应用程序重建库。生活充满了妥协。

-

Flash Gordon,生活在有趣的时代。

网站 - http://home.flash-gordon.me.uk/

comp.lang.c发布指南和介绍:
http:// clc- wiki.net/wiki/Intro_to_clc


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

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