J
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.
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.