apr-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Damir Dezeljin <program...@nib.si>
Subject apr_allocator_* PRIOR: Memory management question
Date Tue, 29 Oct 2002 14:41:22 GMT

I think about the strategy of constructing my queue. I found
apr_allocator_* functions in APR that seams good for my purpose.

Because I don't realy undestand the documentation (BTW: Is there a more
detailed documentation?) I have some new questions:
- If I want to use a memmory debugger with APR, will Njamd work
corectly with it? I used it for the example below and I think that it
works fine, but ... (see below).

- I write a demo program and don't exctly know what it is doing (I also
make some references in text to dicuse them later):
#include <stdlib.h>
#include <stdio.h>
#include "apr.h"
#include "apr_errno.h"
#include "apr_want.h"
#include "apr_pools.h"
#include "apr_thread_mutex.h"

#define LEN     10000

int main(void) {
    apr_allocator_t *al=NULL;
    int *tmp[LEN];
    int i;


    apr_allocator_max_free_set(al, 1);                         /* 1. */

    for (i=0; i<LEN; i++) {
        tmp[i] = (int *) apr_allocator_alloc(al, sizeof(int)); /* 2. */
                                                               /* 3. */

    for (i=0; i<LEN; i++) {                                    /* 4. */
        apr_allocator_free(al, (apr_memnode_t *) tmp[i]);      /* 5. */

    apr_allocator_destroy(al);                                 /* 6. */

    return 0;

1. - If I understod the docs correctly, the number means how many bytes
should be free in allocator so that some memmory is freed - so I put 1 to
asure that the memory is freed as soon as posible (0 means never free?).
2. I alocate some memory
5. I free the allocated memory
6. I destroy the allocator

If I run this program under Njamd all works fine. The memmory is realy
freed after the loop 4. . What is very strange to me is that if I put in
loop 4. the condition (LEN-1) so that I want free one less value than was
allocated, nothing is freed. Why???

The other problem I noticed is that if I put on line pointed by 3. for ex.
*tmp[i] = i;
the free fails. Why I can use the allocated memory (I asigned a value to
it, use it to print it to the stdout, ...) but when I try to free it I
get segmentation failure.

- This allocator seams what I need, but I don't know how to use it.
I also check what Sander sugest me (to look at bucket allocator), but
unfortunaly the code is dificult to understand so I'm now considering to
really construct my queue so that each leaf  (node / element) will be
generated in its own subpool of the root pool. Then when I want to free
the element I will destory the subpool.

Please if anyone have any better sugestion, send me a e-mail (hehe ... try
to explain it so that I know what you are speak about :) ). Thanking you
in advance for all the help you provide me.


View raw message