From mboxrd@z Thu Jan 1 00:00:00 1970 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fee84,de8f33acc05f1b39 X-Google-Attributes: gidfee84,public X-Google-ArrivalTime: 1994-02-23 15:17:00 PST Path: gmd.de!newsserver.jvnc.net!netnews.upenn.edu!msuinfo!agate!howland.reston.ans.net!vixen.cso.uiuc.edu!newsrelay.iastate.edu!news.iastate.edu!john From: john@iastate.edu (John Hascall) Newsgroups: comp.unix.osf.osf1 Subject: Re: strange malloc -- fragmentation ? Date: 23 Feb 1994 23:17:00 GMT Organization: Iowa State University, Ames, Iowa (USA) Distribution: world Message-ID: <2kgo5c$cke@news.iastate.edu> References: <2kelld$snu@gap.cco.caltech.edu> <2kfmau$ot@quabbin.crl.dec.com> NNTP-Posting-Host: pvcons.cc.iastate.edu Jim Gettys wrote: }sancho@quixote.gps.caltech.edu (Ignotum per ignotius) writes: }> DEC ALPHA 3000/400 OSF1.3(a) [...malloc grows program RSS more than expected...] }> What's happening, is it an alignment/fragmentation question or something }> else. Is there a simple fix ? This is not a time-critical element of my }> code. }The malloc in DEC OSF 1.2 and 1.3 is a Berkeley power-of-two allocator (as I }remember), and if you happen to allocate near a power of two number of bytes, }rounds up to the next power of two, causing a near factor of two of memory }usage for allocations of this size. This algorithm is very fast. As a matter of fact, if it is like the Ultrix malloc, the very worst size to malloc is any power of 2 less or equal to 4096 (the page size). The best (least wasteful) size is (2^n - 4). This malloc always grabs core in pagesize chunks. For sizes < pagesize, the chunk is divided into equal parts. For example, when the "512 byte" bucket needs more entries, 4096 bytes are grabbed with sbrk and divided into 8 512 bytes blocks. Blocks in this bucket are used to satisfy requests from 256(253) to 508 bytes, as 4 bytes at the front are used for housekeeping. For buckets 4K and bigger, the lossage is upto 1 page (so the returned chunk is always page-aligned). John -- I present the following for your horror and amazement (it saved 7MB core) -- #ifdef BLASPHEMOUS_MALLOC /* * The size of these hash tables is always a power of 2, * so guess which class of sizes are most wastefully allocated * by Ultrix's malloc() -- good guess... * * We do some blasphemous malloc twiddling here... */ union overhead { union overhead * ov_next; /* when free */ struct { unsigned char ovu_magic; /* magic number */ unsigned char ovu_index; /* bucket # */ } ovu; }; #define ov_magic ovu.ovu_magic #define ov_index ovu.ovu_index #define MAGIC 0xef /* magic # on accounting info */ HashEntry ** hashAllocTable(size) unsigned int size; { register unsigned int bytes = size * sizeof(caddr_t); union overhead * new; if (bytes <= sizeof(union overhead)) return (NULL); new = (union overhead *)malloc(bytes - sizeof(union overhead)); if (new == NULL) return (NULL); return ((HashEntry **)new - 1); } hashFreeTable(table, size) HashEntry ** table; unsigned int size; { register int bytes = size * sizeof(caddr_t); register int bucket; union overhead * old; if (table == NULL) return; bucket = -3; while ((bytes >>= 1) > 0) ++bucket; if (bucket < 0) return; if (bucket == 10) bucket = 9; /* correct anomaly */ old = (union overhead *)table; old->ov_magic = MAGIC; old->ov_index = bucket; free(old + 1); } #else /* ... normal way ... */ #endif ---------------------------------------------------------------------------- -- John Hascall ``An ill-chosen word is the fool's messenger.'' Systems Software Engineer Project Vincent Iowa State University Computation Center + Ames, IA 50011 + 515/294-9551