Re: Manage an unknown number of objects without malloc

Discussion in 'C Programming' started by Ian Collins, Aug 27, 2011.

  1. Ian Collins

    Ian Collins Guest

    On 08/27/11 07:31 PM, pozz wrote:
    > I'm writing a software on an embedded platform, with a C compiler that
    > hasn't malloc/free facilities, so the allocation can't be dynamic.
    >
    > I'd like to mimic the standard I/O functions in one of my API to let the
    > user developer manage several objects. I, as the library developer,
    > don't know how many objects will be used by the user developer.
    >
    > Consider the fopen:
    >
    > FILE * fopen (const char *filename, const char *opentype)
    >
    > The mechanism is simple: the I/O library doesn't know in advance how
    > many files the user developer will want to manage at the same time, so
    > fopen() dynamically allocates the FILE object and returns it if the
    > operation is successful.


    My view is if the implementation can't use dynamic allocation, it should
    have fixed limits of all resources that would be dynamically allocated.
    Otherwise just implement malloc/free.

    The normal reason for using a "static" design is the ability guarantee
    the system will not run out of memory. That guarantee should also apply
    to other resources.

    --
    Ian Collins
     
    Ian Collins, Aug 27, 2011
    #1
    1. Advertising

  2. Ian Collins

    Ian Collins Guest

    On 08/29/11 03:26 AM, pozz wrote:
    > Il 27/08/2011 22:25, Ian Collins ha scritto:
    >> On 08/27/11 07:31 PM, pozz wrote:
    >>> I'm writing a software on an embedded platform, with a C compiler that
    >>> hasn't malloc/free facilities, so the allocation can't be dynamic.
    >>>
    >>> I'd like to mimic the standard I/O functions in one of my API to let the
    >>> user developer manage several objects. I, as the library developer,
    >>> don't know how many objects will be used by the user developer.
    >>>
    >>> Consider the fopen:
    >>>
    >>> FILE * fopen (const char *filename, const char *opentype)
    >>>
    >>> The mechanism is simple: the I/O library doesn't know in advance how
    >>> many files the user developer will want to manage at the same time, so
    >>> fopen() dynamically allocates the FILE object and returns it if the
    >>> operation is successful.

    >>
    >> My view is if the implementation can't use dynamic allocation, it should
    >> have fixed limits of all resources that would be dynamically allocated.
    >> Otherwise just implement malloc/free.

    >
    > It's a good point... do you have any suggestion about implementing my
    > own malloc/free functions? I think it'll be a very hard job.


    All of the memory managers I have written were targeted at specific
    applications.

    Start with something simple, say some fixed sized arrays of fixed sized
    (2^n) blocks. That scheme keeps the housekeeping simple (you can use
    the address passed to free to work out which array the block came from).

    Include statistics gathering so you can build a profile of the actual
    requests. This will help you tune the block and pool sizes.

    --
    Ian Collins
     
    Ian Collins, Aug 28, 2011
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Robert Scarborough

    Manage State of COM objects

    Robert Scarborough, Jul 17, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    314
    Robert Scarborough
    Jul 17, 2003
  2. Martin Maercker

    JNI - how to manage C++ objects from Java?

    Martin Maercker, Jan 16, 2005, in forum: Java
    Replies:
    2
    Views:
    3,519
    Chris Uppal
    Jan 18, 2005
  3. Rajshekhar
    Replies:
    5
    Views:
    2,161
    Jonathan Bartlett
    Mar 29, 2005
  4. HENRY Eshbaugh

    Re: Manage an unknown number of objects without malloc

    HENRY Eshbaugh, Aug 30, 2011, in forum: C Programming
    Replies:
    8
    Views:
    318
    Malcolm McLean
    Sep 27, 2011
  5. Vincent Arnoux
    Replies:
    1
    Views:
    252
    Arnaud Bergeron
    Aug 11, 2006
Loading...

Share This Page