home *** CD-ROM | disk | FTP | other *** search
/ Crawly Crypt Collection 1 / crawlyvol1.bin / program / compiler / nasm20b / nasm_src / nmalloc.h < prev    next >
C/C++ Source or Header  |  1993-01-19  |  16KB  |  290 lines

  1. /* ---------------------------------------------------------------------- */
  2. /*                   Copyright (C) 1991 by Natürlich!                     */
  3. /*                      This file is copyrighted!                         */
  4. /*                Refer to the documentation for details.                 */
  5. /* ---------------------------------------------------------------------- */
  6. #define TEST 0                   /* may fail on big MACROS    */
  7. /* these aren't actually _MAXIMAL_ values,but 'malloc' values */
  8. /* if your malloc only allocates 64K and you don't want to go */
  9. /* for farmalloc (as in MesS-DOS), figure values that keep    */
  10. /* sizeof( struct) *  _MAXIMAL_ + 256 to < 0xFFFF             */
  11. /* The values look very suggestively, like the higher the     */
  12. /* better but actually 1/2 1/4 or even 1/8 won't reduce the   */
  13. /* speed dramatically                                         */
  14. /* too small values may actually CRASH your system            */
  15. #if ! VERSION && TEST
  16. # define LABMAX    10L           /* #global labels            */
  17. # define LLABMAX   10L           /* #local labels             */
  18. # define STRMAX    0x100L        /* #bytes for strings        */
  19. # define EXPMAX    0x40L         /* #expressions              */
  20. # define FIXMAX    0x10L         /* #forward ref fixes        */
  21. # define LEXPMAX   0x10L         /* #lexpression              */
  22. # define REFMAX    0x80L         /* #forward refs             */
  23. # define MACTOKMAX 0x100L        /* #token in macro buffer    */
  24. # define NBUFSIZ   0x1000L       /* #bytes file buffer space  */
  25. # define SEGMAX    10L           /* #code segments            */
  26. # define IMMMAX    50L           /* #immediate patch pointers */
  27. #else
  28. # define LABMAX    1000L         /* #global labels            */
  29. # define LLABMAX   100L          /* #local labels             */
  30. # define STRMAX    0x2000L       /* #bytes for strings        */
  31. # define EXPMAX    0x400L        /* #expressions              */
  32. # define FIXMAX    0x40L         /* #forward ref fixes        */
  33. # define LEXPMAX   0x200L        /* #lexpression              */
  34. # define REFMAX    0x80L         /* #forward refs             */
  35. # define MACTOKMAX 0x1000L       /* #token in macro buffer    */
  36. # define NBUFSIZ   0x2000L       /* #bytes file buffer space  */
  37. # define SEGMAX    50L           /* #code segments            */
  38. # define IMMMAX    100L          /* #immediate patch pointers */
  39. #endif
  40. /* ---------------------------------------------------------- */
  41. #define MAXMODULE 0xFE00L        /* 64K address space on 6502 */
  42. #if OS == UNIX
  43. # define LIBMAX   0x80000L       /* Library maximum size 512K */
  44. #endif
  45. #if OS == MSDOS
  46. # define LIBMAX   0x40000L
  47. #endif
  48.  
  49. #define MAXFLOATS 32             /* #floats in one line       */
  50. #define MACSPACE  (MACTOKMAX * (sizeof( int) + sizeof( lword)))
  51.  
  52. #if __NSTDC__ && __TURBOC__
  53. void huge   *nmalloc( lword),
  54.             nfree( void huge *);
  55. #else
  56. void huge   *nmalloc();
  57. void        nfree();
  58. #endif
  59. /* ---------------------------------------------------------- */
  60. /*          Copyright (c) 1990 by Natürlich!                  */
  61. /* ---------------------------------------------------------- */
  62. /* This is a generic function so to speak (it is of course    */
  63. /* just a simple macro) that builds a custom mallocer for a   */
  64. /* structure. The benefits are:                               */
  65. /*       Speed                                                */
  66. /*       Garbage collectability                               */
  67. /*       Fragmentation                                        */
  68. /* The detriments (?) are:                                    */
  69. /*       No "free()"                                          */
  70. /*         Not all that portable if it isn't ANSI-C.          */
  71. /*       <space>##<space> used to stand here, but some        */
  72. /*      Compilers can't take it.                              */
  73. /* ---------------------------------------------------------- */
  74. #if ! STATISTICS
  75.  
  76. #if CANCONCAT
  77.  
  78. #define make_mallocer( type, max, fun_name)                       \
  79. typedef struct _##type##_m                                        \
  80. {                                                                 \
  81.    long            free;                                          \
  82.    type huge       *tab;                                          \
  83.    struct _##type##_m huge  *before;                              \
  84. } type##_m;                                                       \
  85.                                                                   \
  86. type##_m huge *##type##_h = 0;                                    \
  87.                                                                   \
  88. type *fun_name()                                                  \
  89. {                                                                 \
  90.    register type##_m huge *p;                                     \
  91.                                                                   \
  92.    if( (p = type##_h) && p->free--)                               \
  93.       return( p->tab++);                                          \
  94.    p         = nmalloc( (lword) (sizeof(type##_m) +               \
  95.                                        max * sizeof( type)));     \
  96.    p->free   = max - 1;                                           \
  97.    p->tab    = (type *) ((char huge *) p + sizeof( type##_m));    \
  98.    p->before = (type##_m *) type##_h;                             \
  99.    type##_h  = p;                                                 \
  100.    return( p->tab++);                                             \
  101. }
  102. #else
  103. #define make_mallocer( type, max, fun_name)                       \
  104. typedef struct _/**/type/**/_m                                    \
  105. {                                                                 \
  106.    long            free;                                          \
  107.    type huge       *tab;                                          \
  108.    struct _/**/type/**/_m huge *before;                           \
  109. } type/**/_m;                                                     \
  110.                                                                   \
  111. type/**/_m huge */**/type/**/_h = 0;                              \
  112.                                                                   \
  113. type *fun_name()                                                  \
  114. {                                                                 \
  115.    register type/**/_m huge *p;                                   \
  116.                                                                   \
  117.    if( (p = type/**/_h) && p->free--)                             \
  118.       return( p->tab++);                                          \
  119.    p         = nmalloc((lword) (sizeof(type/**/_m)+               \
  120.                                        max * sizeof( type)));     \
  121.    p->free   = max - 1;                                           \
  122.    p->tab    = (type *) ((char huge *) p + sizeof( type/**/_m));  \
  123.    p->before = (type/**/_m *) type/**/_h;                         \
  124.    type/**/_h = p;                                                \
  125.    return( p->tab++);                                             \
  126. }
  127. #endif
  128.  
  129. #else
  130.  
  131. #if CANCONCAT
  132. #define make_mallocer( type, max, fun_name)                       \
  133. typedef struct _##type##_m                                        \
  134. {                                                                 \
  135.    long            free;                                          \
  136.    type huge       *tab;                                          \
  137.    struct _##type##_m huge *before;                               \
  138. } type##_m;                                                       \
  139.                                                                   \
  140. type##_m huge *##type##_h = 0;                                    \
  141.                                                                   \
  142. type *fun_name()                                                  \
  143. {                                                                 \
  144.    register type##_m huge *p;                                     \
  145.    extern   word        _a_##type,                                \
  146.                         _m_##type;                                \
  147.    extern   lword       _s_##type;                                \
  148.                                                                   \
  149.    _a_##type++;                                                   \
  150.    if( (p = type##_h) && p->free--)                               \
  151.       return( p->tab++);                                          \
  152.    _m_##type++;                                                   \
  153.    _s_##type = sizeof(type##_m) + max * sizeof( type);            \
  154.    p         = nmalloc( (lword) (sizeof(type##_m) +               \
  155.                                        max * sizeof( type)));     \
  156.    p->free   = max - 1;                                           \
  157.    p->tab    = (type *) ((char huge *) p + sizeof( type##_m));    \
  158.    p->before = (type##_m *) type##_h;                             \
  159.    type##_h  = p;                                                 \
  160.    return( p->tab++);                                             \
  161. }
  162. #else
  163. #define make_mallocer( type, max, fun_name)                       \
  164. typedef struct _/**/type/**/_m                                    \
  165. {                                                                 \
  166.    long            free;                                          \
  167.    type huge       *tab;                                          \
  168.    struct _/**/type/**/_m huge *before;                           \
  169. } type/**/_m;                                                     \
  170.                                                                   \
  171. type/**/_m huge */**/type/**/_h = 0;                              \
  172.                                                                   \
  173. type *fun_name()                                                  \
  174. {                                                                 \
  175.    register type/**/_m huge  *p;                                  \
  176.    extern   word        _a_/**/type,                              \
  177.                         _m_/**/type;                              \
  178.    extern   lword       _s_/**/type;                              \
  179.                                                                   \
  180.    _a_/**/type++;                                                 \
  181.    if( (p = type/**/_h) && p->free--)                             \
  182.       return( p->tab++);                                          \
  183.    _m_/**/type++;                                                 \
  184.    _s_/**/type = sizeof(type/**/_m) + max * sizeof( type);        \
  185.    p         = nmalloc((lword) sizeof(type/**/_m)+                \
  186.                                        max * sizeof( type)));     \
  187.    p->free   = max - 1;                                           \
  188.    p->tab    = (type *) ((char huge *) p + sizeof( type/**/_m));  \
  189.    p->before = (type/**/_m *) type/**/_h;                         \
  190.    type/**/_h = p;                                                \
  191.    return( p->tab++);                                             \
  192. }
  193. #endif
  194. #endif
  195.  
  196. #if ! STATISTICS
  197.  
  198. #if CANCONCAT
  199.  
  200. #define dup_mallocer( type, max, fun_name, id)                    \
  201. type##_m huge   * id##type##_h = 0;                               \
  202.                                                                   \
  203. type *fun_name()                                                  \
  204. {                                                                 \
  205.    register type##_m huge *p;                                     \
  206.                                                                   \
  207.    if( (p = id##type##_h) && p->free--)                           \
  208.       return( p->tab++);                                          \
  209.    p         = nmalloc((lword) (sizeof(type##_m) +                \
  210.                                        max * sizeof( type)));     \
  211.    p->free   = max - 1;                                           \
  212.    p->tab    = (type *) ((char huge *) p + sizeof( type##_m));    \
  213.    p->before = (type##_m *) id##type##_h;                         \
  214.    id##type##_h = p;                                              \
  215.    return( p->tab++);                                             \
  216. }
  217. #else
  218. #define dup_mallocer( type, max, fun_name, id)                    \
  219.                                                                   \
  220. type/**/_m huge  *id/**/type/**/_h = 0;                           \
  221.                                                                   \
  222. type *fun_name()                                                  \
  223. {                                                                 \
  224.    register type/**/_m huge *p;                                   \
  225.                                                                   \
  226.    if( (p = id/**/type/**/_h) && p->free--)                       \
  227.       return( p->tab++);                                          \
  228.    p         = nmalloc((lword) (sizeof(type/**/_m)+               \
  229.                                        max * sizeof( type)));     \
  230.    p->free   = max - 1;                                           \
  231.    p->tab    = (type *) ((char huge *) p + sizeof( type/**/_m));  \
  232.    p->before = (type/**/_m *) id/**/type/**/_h;                   \
  233.    id/**/type/**/_h = p;                                          \
  234.    return( p->tab++);                                             \
  235. }
  236. #endif
  237.  
  238. #else
  239.  
  240. #if CANCONCAT
  241. #define dup_mallocer( type, max, fun_name, id)                    \
  242. type##_m huge  * id##type##_h = 0;                                \
  243.                                                                   \
  244. type *fun_name()                                                  \
  245. {                                                                 \
  246.    register type##_m huge *p;                                     \
  247.    extern   word        _a_##id##type,                            \
  248.                         _m_##id##type;                            \
  249.    extern   lword       _s_##id##type;                            \
  250.                                                                   \
  251.    _a_##id##type++;                                               \
  252.    if( (p = id##type##_h) && p->free--)                           \
  253.       return( p->tab++);                                          \
  254.    _m_##id##type++;                                               \
  255.    _s_##id##type = sizeof(type##_m) + max * sizeof( type);        \
  256.    p         = nmalloc( (lword) (sizeof(type##_m) +               \
  257.                                        max * sizeof( type)));     \
  258.    p->free   = max - 1;                                           \
  259.    p->tab    = (type *) ((char huge *) p + sizeof( type##_m));    \
  260.    p->before = (type##_m *) id##type##_h;                         \
  261.    id##type##_h = p;                                              \
  262.    return( p->tab++);                                             \
  263. }
  264. #else
  265. #define dup_mallocer( type, max, fun_name, id)                    \
  266. type/**/_m huge  *id/**/type/**/_h = 0;                           \
  267.                                                                   \
  268. type *fun_name()                                                  \
  269. {                                                                 \
  270.    register type/**/_m huge *p;                                   \
  271.    extern   word        _a_/**/id/**/type,                        \
  272.                         _m_/**/id/**/type;                        \
  273.    extern   lword       _s_/**/id/**/type;                        \
  274.                                                                   \
  275.    _a_/**/id/**/type++;                                           \
  276.    if( (p = id/**/type/**/_h) && p->free--)                       \
  277.       return( p->tab++);                                          \
  278.    _m_/**/id/**/type++;                                           \
  279.    _s_/**/id/**/type = sizeof(type/**/_m) + max * sizeof( type);  \
  280.    p         = nmalloc((lword) (sizeof(type/**/_m)+               \
  281.                                        max * sizeof( type)));     \
  282.    p->free   = max - 1;                                           \
  283.    p->tab    = (type *) ((char huge *) p + sizeof( type/**/_m));  \
  284.    p->before = (type/**/_m *) id/**/type/**/_h;                   \
  285.    id/**/type/**/_h = p;                                          \
  286.    return( p->tab++);                                             \
  287. }
  288. #endif
  289. #endif
  290.