Message-Id: Comments: Authenticated sender is From: "Salvador Eduardo Tropea (SET)" Organization: INTI To: djgpp AT delorie DOT com Date: Wed, 19 Mar 1997 15:34:06 +0000 MIME-Version: 1.0 Content-type: Multipart/Mixed; boundary=Message-Boundary-4697 Subject: An experimental and enhanced malloc. --Message-Boundary-4697 Content-type: text/plain; charset=US-ASCII Content-transfer-encoding: 7BIT Content-description: Mail message body "A.Appleyard" wrote: >Re my messages about malloc(), here is a version of malloc() free() realloc() >which I made by altering djgpp\src\libc\ansi\stdlib\malloc.c; I have tested it >quickly and it seems to work. See the comments `/*******' herinunder. I taked a look to your code and to the code in libc, and: 1) Seems to be right. 2) Seems to be better. You ask in your code: > /****** If size>=pagesize, the standard form gets (size/pagesize + 1) */ > /****** pages here. What is the extra unused page for? */ The extra page is for realloc, see the following lines taked from the libc version of realloc: onb = 1 << (i + 3); if (onb < pagesz) onb -= sizeof (*op) + RSLOP; else onb += pagesz - sizeof (*op) - RSLOP; So here if size>=pagesize realloc asumes that we have an extra page in the block. It helps realloc but eats a lot of memory if you are allocating blocks of size 4K or similar. And now another thing, I don't know what a hell the ANSI says but the realloc implementation isn't very good for my code, here is the relevant code (I taked it from the A. APPLEYARD code because is much more clear): if (N+OW<=oldsize) if (N+OW>oldsize/2) return Old; free(Old); if (!(New=malloc(N))) return 0; From my point of view the free must be moved AFTER the call to malloc because in this way if malloc fails the old block isn't moved to the free pool of memory. We don't gain anything freeing the block first because we checked that malloc won't reuse this block. And finally another one: The algorithm used by malloc is fast but is easy to get the memory fragmented, for example if we allocate a lot of blocks of 100Kb and then we free these blocks and we try to allocate one of 200Kb ... malloc will fail even when all the memory is free just because the free memory is in the 128Kb bucket and not in the 256Kb one. I don't know if that was discused some days ago or was another topic about malloc but I guess that we need some kind of optimizer to be called under these situations to reorder the heap and liberate the unused memory so then morecore can allocate the requested block. I attached to this mail a modified version of the A. Appleyard malloc to reuse buckets when there is no more memory. The code is unoptimized and was tested only with the file included in the zip. I included 2 examples: 1) Just allocates as many 1Mb blocks as posible and then frees all the blocks, after that just tries to allocate 2 Mb, imposible with the actual malloc, but this malloc joins 2 1Mb buckets to create one of 2 Mb. 2) The program allocate blocks as large as posible, then liberate the blocks and finally tries to allocate a block of size = 1/2 of the smaller previously allocated. This time malloc will split a big bucket into smaller ones. Sorry if I'm talking about allready solved things but I discovered this problem testing my editor and was very annoying see how 24 Mb of memory wasn't enough to handle a 9 Mb clipboard that grown incrementally. SET --------------- 0 -------------------------------- Salvador Eduardo Tropea (SET). Address: Curapaligue 2124, Caseros, 3 de Febrero Buenos Aires, (1678), ARGENTINA TE: +(541) 759 0013 --Message-Boundary-4697 Content-type: text/plain; charset=US-ASCII Content-description: Information about this message. This message contains a file prepared for transmission using the MIME BASE64 transfer encoding scheme. If you are using Pegasus Mail or another MIME-compliant system, you should be able to extract it from within your mailer. If you cannot, please ask your system administrator for help. ---- File information ----------- File: malloc.zip Date: 18 Mar 1997, 20:50 Size: 3239 bytes. Type: ZIP-archive --Message-Boundary-4697 Content-type: Application/ZIP; name=malloc.zip Content-transfer-encoding: BASE64 UEsDBBQAAoAIAMykciKTQdu4wQIAAC0GAAADAAAATS5DfVTpT9swFP9eqf/Dg2pb0oMeArSt h8SkMaHBmIB9YihKGqfxSOzMcagQ4n/fe7bbpoWRtontvuN3vLbfbjaArpuUl1AouVBhDriM WckXgsWgJZSpXEIpcwYl11WouRQlLFOmGOiUQcajOeRhlsm5KZaEPCuBPTBBQYJiMBJrMiGr RQo5y6V6PHCNz7m4B65hyXVqyuUy5gnHzrYkAdCs1EAgKE6q+3KV/Ku0EFbAqYisNDxwhUAz 12qv2Wj3m40WF/OsihlMSh0j6IN0Vj+Mi5ybo2aj32/FLOGCwc3X65vg9PIq+HL2DYN3T68v Ts7PKYMLjXi58B4kj/1m4wkB0hLaxe3o6PhujPsgoBZBohgLEFjARSIJIX1H+aLLx1RrFbhg eh2MLEy8yo383ns88ylRTHF1oKUOs0BUecRUIBObVoQLVs5mHymsUNgh8fZ/0tlneBeD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