Usenet comp.unix.osf.osf1 (1992-2004)
 help / color / mirror / Atom feed
From: john@iastate.edu (John Hascall)
Subject: Re: strange malloc -- fragmentation ?
Date: 23 Feb 1994 23:17:00 GMT	[thread overview]
Message-ID: <2kgo5c$cke@news.iastate.edu> (raw)
In-Reply-To: 2kfmau$ot@quabbin.crl.dec.com

Jim Gettys <jg@crl.dec.com> 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


      reply	other threads:[~1994-02-23 23:17 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1994-02-23  4:22 strange malloc -- fragmentation ? Ignotum per ignotius
1994-02-23 13:39 ` Jim Gettys
1994-02-23 23:17   ` John Hascall [this message]
read-only archive

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox