home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / comp / sys / amiga / programm / 18200 < prev    next >
Encoding:
Internet Message Format  |  1993-01-06  |  1.9 KB

  1. Path: sparky!uunet!wupost!csus.edu!netcom.com!netcomsv!terapin!paulk
  2. From: paulk@terapin.com (Paul Kienitz)
  3. Newsgroups: comp.sys.amiga.programmer
  4. Subject: Re: How to allocate memory nicely?
  5. References: <1993Jan5.094153@rbg.informatik.th-darmstadt.de>
  6. Message-ID: <paulk.33x8@terapin.com>
  7. Date: 5 Jan 93 20:43:07 PST
  8. Organization: BBS
  9. Lines: 55
  10.  
  11. > > How should I allocate memory for short strings of, say, 15
  12. > > characters or so without unnecessarily fragmenting the memory?
  13.  
  14. > I would just use malloc() or calloc(). On most systems this works
  15. > just as you suggested, that is, if you request 15 bytes, it
  16. > actually gets a larger page of memory from the OS and saves the
  17. > rest for further calls of malloc().
  18.  
  19. Not with Aztec.  If using Aztec, malloc() is not a good idea for
  20. large numbers of small allocations.
  21.  
  22. I happen to have here a home-grown version of an allocator designed
  23. for lots of little odd-sized pieces, not to be freed until exit time:
  24.  
  25. static char *slablist = null;           /* see Shal() */
  26. #define SLABSIZE 4000
  27. static unsigned short slabused = slabsize;
  28.  
  29.  
  30. /* This here is an allocation efficiencizer which assumes that no
  31. space is freed between allocations.  The size is in bytes.  The piece of
  32. memory it returns is zeroed. */
  33.  
  34. void *Shal(unsigned short size)
  35. {
  36.     void *t;
  37.     if (size > SLABSIZE - slabused) {
  38.         if (!(t = AllocMem(SLABSIZE, MEMF_CLEAR)))
  39.             die(20, "no memory eh");
  40.         *((char **) t) = slablist;   /* chain old onto new */
  41.         slabused = 4;
  42.         slablist = t;
  43.     }
  44.     t = slablist + slabused;
  45.     slabused += size;
  46.     return (t);
  47. }
  48.  
  49.  
  50.  
  51. /* makes the next Shal shortword aligned */
  52. void Shalign(void)
  53. {
  54.     if (slabused & 1) slabused++;
  55. }
  56.  
  57.  
  58. /* then, at exit time, do this: */
  59.     ...
  60.     while (slablist) {
  61.         t = slablist;
  62.         slablist = *((char **) slablist);
  63.         FreeMem(t, (long) SLABSIZE);
  64.     }
  65.     ...
  66.