home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!wupost!csus.edu!netcom.com!netcomsv!terapin!paulk
- From: paulk@terapin.com (Paul Kienitz)
- Newsgroups: comp.sys.amiga.programmer
- Subject: Re: How to allocate memory nicely?
- References: <1993Jan5.094153@rbg.informatik.th-darmstadt.de>
- Message-ID: <paulk.33x8@terapin.com>
- Date: 5 Jan 93 20:43:07 PST
- Organization: BBS
- Lines: 55
-
- > > How should I allocate memory for short strings of, say, 15
- > > characters or so without unnecessarily fragmenting the memory?
-
- > I would just use malloc() or calloc(). On most systems this works
- > just as you suggested, that is, if you request 15 bytes, it
- > actually gets a larger page of memory from the OS and saves the
- > rest for further calls of malloc().
-
- Not with Aztec. If using Aztec, malloc() is not a good idea for
- large numbers of small allocations.
-
- I happen to have here a home-grown version of an allocator designed
- for lots of little odd-sized pieces, not to be freed until exit time:
-
- static char *slablist = null; /* see Shal() */
- #define SLABSIZE 4000
- static unsigned short slabused = slabsize;
-
-
- /* This here is an allocation efficiencizer which assumes that no
- space is freed between allocations. The size is in bytes. The piece of
- memory it returns is zeroed. */
-
- void *Shal(unsigned short size)
- {
- void *t;
- if (size > SLABSIZE - slabused) {
- if (!(t = AllocMem(SLABSIZE, MEMF_CLEAR)))
- die(20, "no memory eh");
- *((char **) t) = slablist; /* chain old onto new */
- slabused = 4;
- slablist = t;
- }
- t = slablist + slabused;
- slabused += size;
- return (t);
- }
-
-
-
- /* makes the next Shal shortword aligned */
- void Shalign(void)
- {
- if (slabused & 1) slabused++;
- }
-
-
- /* then, at exit time, do this: */
- ...
- while (slablist) {
- t = slablist;
- slablist = *((char **) slablist);
- FreeMem(t, (long) SLABSIZE);
- }
- ...
-