home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 4 / FreshFish_May-June1994.bin / bbs / may94 / util / edit / jade.lha / Jade / src / stringmem.h < prev    next >
C/C++ Source or Header  |  1994-04-16  |  4KB  |  104 lines

  1. /* stringmem.h -- Declarations for stringmem.c
  2.    Copyright (C) 1993, 1994 John Harper <jsh@ukc.ac.uk>
  3.  
  4. This file is part of Jade.
  5.  
  6. Jade is free software; you can redistribute it and/or modify it
  7. under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. Jade is distributed in the hope that it will be useful, but
  12. WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with Jade; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #ifndef _STRINGMEM_H
  21. #define _STRINGMEM_H
  22.  
  23. /* #define STRMEM_STATS */
  24.  
  25. typedef struct MEMCHUNK {
  26.     /* This next union is a bit of a kludge. The problem is that I need some
  27.        way of tracking MEMCHUNKs allocated straight from malloc() (so I can
  28.        do garbage collection, etc) without wasting a lot of memory. For
  29.        my own reasons you can't call sm_free() on one of these blocks. But
  30.        since I only use this technique on Lisp memory (which is never freed
  31.        except by the GC which is weird) everything is ok.  */
  32.     union mc_header {
  33.     struct MEMCHUNK *next;
  34.     int         blktype;
  35.     }            mc_Header;
  36. #define mc_BlkType mc_Header.blktype
  37.     union {
  38.     struct MEMCHUNK *nextfree;
  39.     u_char         mem[0];
  40.     }            mc_Mem;
  41. } MEMCHUNK;
  42. #define MBT_FREE   -2
  43. #define MBT_MALLOC -1
  44. #define MCHNK_SIZE(chunksiz) \
  45.     ((chunksiz) + (sizeof(union mc_header)))
  46.  
  47. typedef struct MEMBLOCK {
  48.     struct MinNode  mbl_Node;
  49.     MEMCHUNK        mbl_Chunks[0];
  50. } MEMBLOCK;
  51. #define MBLK_SIZE(chunksiz, numchunks) \
  52.     ((MCHNK_SIZE(chunksiz) * (numchunks)) + sizeof(MEMBLOCK))
  53.  
  54. typedef struct MEMBUCKET {
  55.     struct MinList  mbu_MemBlocks;
  56.     MEMCHUNK       *mbu_FreeList;
  57.     /* Number of free operations since last scan for free blocks.  */
  58.     int            mbu_FreeCount;
  59. } MEMBUCKET;
  60.  
  61. /* difference in size between each bucket */
  62. #define GRAIN          8
  63.  
  64. /* allocations > this go to malloc() */
  65. #define MAXBUCKETSIZE 128
  66.  
  67. #define NUMBUCKETS    (MAXBUCKETSIZE / GRAIN)
  68.  
  69. /* Each MEMBLOCK should be around 2K */
  70. #define MBLOCKSIZE    (2044 - sizeof(MEMBLOCK))
  71.  
  72. typedef struct STRMEM {
  73.     MEMBUCKET        sm_MemBuckets[NUMBUCKETS];
  74.     int            sm_ChunksPerBlock[NUMBUCKETS];
  75.     /* This next number defines the number of sm_frees() which have to
  76.        be called on memory from a particular bucket before that bucket is
  77.        scanned for totally free blocks (any found are released to system).
  78.        the actual number is (FREESBEFOREFLUSH * ChunksPerBlock[bucket]).  */
  79.     char        sm_FreesBeforeFlush;
  80.     /* Whether or not to use sm_MallocChain to chain all malloc() MEMCHUNKs
  81.        together. If using this don't call sm_free().  */
  82.     char        sm_UseMallocChain;
  83.     MEMCHUNK       *sm_MallocChain;
  84. #ifdef STRMEM_STATS
  85.     int            sm_AllocCount[NUMBUCKETS + 1];
  86.     int            sm_FreeCount[NUMBUCKETS + 1];
  87. #endif
  88. } STRMEM;
  89.  
  90. extern int     sm_init(STRMEM *);
  91. extern void    sm_kill(STRMEM *);
  92. extern void   *sm_alloc(STRMEM *, int);
  93. extern u_char *sm_strdupn(STRMEM *, const u_char *, int);
  94. extern u_char *sm_strdup(STRMEM *, const u_char *);
  95. extern void    sm_free(STRMEM *, void *);
  96. extern void    sm_flush(STRMEM *);
  97.  
  98. #define mystralloc(n)  sm_alloc(&MainStrMem, n)
  99. #define mystrdupn(s,n) sm_strdupn(&MainStrMem, s, n)
  100. #define mystrdup(s)    sm_strdup(&MainStrMem, s)
  101. #define mystrfree(s)   sm_free(&MainStrMem, s)
  102.  
  103. #endif
  104.