Return-Path: Delivered-To: apmail-apr-dev-archive@apr.apache.org Received: (qmail 99756 invoked by uid 500); 29 Oct 2002 15:19:48 -0000 Mailing-List: contact dev-help@apr.apache.org; run by ezmlm Precedence: bulk List-Post: List-Help: List-Unsubscribe: List-Subscribe: Delivered-To: mailing list dev@apr.apache.org Received: (qmail 99691 invoked from network); 29 Oct 2002 15:19:47 -0000 From: "Sander Striker" To: "Damir Dezeljin" Cc: "APR" Subject: RE: apr_allocator_* PRIOR: Memory management question Date: Tue, 29 Oct 2002 16:32:55 +0100 Message-ID: MIME-Version: 1.0 Content-Type: text/plain; charset="US-ASCII" Content-Transfer-Encoding: 7bit X-Priority: 3 (Normal) X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook IMO, Build 9.0.2416 (9.0.2911.0) X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106 In-Reply-To: Importance: Normal X-Spam-Rating: daedalus.apache.org 1.6.2 0/1000/N > From: Damir Dezeljin [mailto:programing@nib.si] > Sent: 29 October 2002 15:41 > Hi. > > 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?) Ahum, no. I have it on my list of things to do... > 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). If you are going to try and find memory allocation issues, I can recommend turning on pool debugging [--enable-pool-debug] and using valgrind to track things down. Ofcourse other tools might work just as well for you. Just remember to turn on the pool debugging. > - 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 > #include > #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_create(&al); > > apr_allocator_max_free_set(al, 1); /* 1. */ > > for (i=0; i tmp[i] = (int *) apr_allocator_alloc(al, sizeof(int)); /* 2. */ > /* 3. */ > } > > for (i=0; i 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?). Correct. > 2. I alocate some memory Some memory being a minimum of 8k per allocation... > 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??? Good question. > 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. Sure. You can't just cast away the apr_memnode_t * and just start writing to the memory holding information about the node. You need to look at apr_memnode_t's fields. first_avail and endp are the ones that probably interest you most. > - 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), Have you looked at apr-util/buckets/apr_buckets_alloc.c? > 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. Ouch! That means each leaf will at least consume 8k. > 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. Basically you want something like the bucket allocator. If your allocation function you allocate from the apr_allocator and devide up the memory in the apr_memnode_t nodes into smaller chunks. Put these chunks in some kind of freelist. Return the first chunk in the freelist. In your free function simply put the block on the freelist. For a general idea look at this old code: http://cvs.apache.org/viewcvs.cgi/*checkout*/apr/memory/unix/Attic/apr_sms_blocks.c?rev=1.11 Sander