home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / dbmalloc.zip / mallocin.h < prev    next >
C/C++ Source or Header  |  1993-01-04  |  10KB  |  353 lines

  1. /*
  2.  * (c) Copyright 1990, 1991, 1992 Conor P. Cahill (cpcahil@virtech.vti.com)
  3.  *
  4.  * This software may be distributed freely as long as the following conditions
  5.  * are met:
  6.  *         * the distribution, or any derivative thereof, may not be
  7.  *          included as part of a commercial product
  8.  *        * full source code is provided including this copyright
  9.  *        * there is no charge for the software itself (there may be
  10.  *          a minimal charge for the copying or distribution effort)
  11.  *        * this copyright notice is not modified or removed from any
  12.  *          source file
  13.  */
  14. /*
  15.  * $Id: mallocin.h,v 1.27 1992/09/03 22:24:33 cpcahil Exp $
  16.  */
  17. #ifndef _MALLOCIN_H
  18. #define _MALLOCIN_H 1
  19.  
  20. /*
  21.  * this file contains macros that are internal to the malloc library
  22.  * and therefore, are not needed in malloc.h.
  23.  */
  24.  
  25. #if POSIX_HEADERS
  26. #include <unistd.h>
  27. #endif
  28. #if ANSI_HEADERS
  29. #include <stdlib.h>
  30. #endif
  31.  
  32. #ifndef SYS_TYPES_H_INCLUDED
  33. #include <sys/types.h>
  34. #endif
  35.  
  36. #define IN_MALLOC_CODE    1
  37. #include "sysdefs.h"
  38. #include "malloc.h"
  39.  
  40. #define IDTYPE unsigned long
  41. #define ULONG  unsigned long
  42.  
  43. /*
  44.  * Data structures used by stack mechanism
  45.  */
  46. struct stack
  47. {
  48.     struct stack    * beside;
  49.     struct stack    * below;
  50.     struct stack     * above;
  51.     CONST char    * func;
  52.     CONST char    * file;
  53.     int          line;
  54. };
  55.  
  56. /*
  57.  * minimum round up to get to a doubleword boundry, assuming it is the
  58.  * strictest alignment requirement on the system.  If not, the union s
  59.  * in struct mlist will also have to be changed.  Note that this must be
  60.  * a power of two because of how it is used throughout the code.
  61.  */
  62. #define M_RND        0x08
  63. #define M_YOFFSET       (sizeof(SIZETYPE))
  64.  
  65. #define LONGFILL 2
  66.  
  67. struct mlist
  68. {
  69.     struct mlist    * next;            /* next entry in chain    */
  70.     struct mlist    * prev;            /* prev entry in chain    */
  71.     struct mlist    * freenext;        /* next ent in free chn */
  72.     struct mlist    * freeprev;        /* prev ent in free chn */
  73.     struct stack    * stack;        /* current stack level    */
  74.     struct stack    * freestack;        /* free stack level    */
  75.     long           flag;            /* inuse flag        */
  76.     CONST char    * file;            /* file where called fm    */
  77.     int          line;            /* line where called fm    */
  78.     CONST char    * ffile;        /* file where freed     */
  79.     int          fline;        /* line where freed     */
  80.     IDTYPE          fid;            /* free call number    */
  81.     IDTYPE          hist_id;        /* historical id    */
  82.     IDTYPE          id;            /* malloc call number    */
  83.     SIZETYPE      r_size;        /* requested size    */
  84.     union
  85.     {
  86.         SIZETYPE      size;        /* actual size        */
  87.         SIZETYPE      filler[LONGFILL];
  88.         double          unused_just_for_alignment;
  89.     } s;
  90.     char          data[M_RND];
  91. };
  92.  
  93. /*
  94.  * pre-filler size
  95.  */
  96. #define FILLSIZE    (sizeof(mlist.s) - sizeof(mlist.s.filler[0]))
  97.  
  98. /*
  99.  * kludge to get offsetof the data element for the mlist structure
  100.  */
  101. #define M_SIZE        ((SIZETYPE)(char *)((struct mlist *)0)->data)
  102. #define M_FLAGOFF    ((SIZETYPE)(char *)&((struct mlist *)0)->flag)
  103.  
  104. #define M_INUSE     0x01
  105. #define M_FILLED     0x02
  106. #define M_MARKED    0x04
  107. #define M_DFILLED    0x08
  108. #define M_MAGIC     0x31561000
  109. #define M_MAGIC_BITS    0xfffff000
  110.  
  111. #define M_BLOCKSIZE    (1024*8)
  112.  
  113. #define EOS            '\0'
  114. #define M_DFLT_FILL        '\01'
  115. #define M_DFLT_FREE_FILL    '\02'
  116. #define M_DFLT_BSIZE        1
  117. #define M_FILL            ((char)malloc_fillbyte)
  118. #define M_FREE_FILL        ((char)malloc_freebyte)
  119. #define M_BSIZE            (malloc_boundsize)
  120.  
  121. #define MALLOC_INIT()    if( malloc_data_start == (DATATYPE *)0 ) malloc_init()
  122.  
  123. /* 
  124.  * malloc internal options
  125.  */
  126. #define MOPT_MFILL    0x0001        /* fill malloc'd segments    */
  127. #define MOPT_FFILL    0x0002        /* fill free'd segments        */
  128. #define MOPT_REUSE    0x0004        /* reuse free segments?        */
  129. #define MOPT_LOWFRAG    0x0008        /* low fragmentation        */
  130. #define MOPT_CKCHAIN    0x0010        /* enable chain checking    */
  131. #define MOPT_CKDATA    0x0020        /* enable data checking        */
  132. #define MOPT_DFILL    0x0040        /* fill over/underflow areas    */
  133. #define MOPT_SLINKS     0x0080        /* turn off/on adjacent link disp */
  134. #define MOPT_DETAIL     0x0100        /* turn off/on detailed output  */
  135. #define MOPT_FREEMARK   0x0200        /* warning when freeing marked    */
  136. #define MOPT_ZERO       0x0400        /* warning about zero length allocs */
  137.  
  138. /*
  139.  * malloc_freeseg() operation arguments
  140.  */
  141. #define M_FREE_REMOVE    1
  142. #define M_FREE_ADD    2
  143. /*
  144.  * Malloc types
  145.  */
  146. #define M_T_MALLOC    0x010
  147. #define M_T_REALLOC    0x020
  148. #define M_T_CALLOC    0x030
  149. #define M_T_SPLIT    0x040
  150. #define M_T_STACK    0x050
  151. #define M_T_XTMALLOC    0x060
  152. #define M_T_XTREALLOC    0x070
  153. #define M_T_XTCALLOC    0x080
  154. #define M_T_ALIGNED    0x090
  155. #define M_T_BITS    0x0F0
  156.  
  157. #define GETTYPE(_ptr)        (_ptr->flag & M_T_BITS)
  158. #define SETTYPE(_ptr,_type)     (_ptr->flag = ((_ptr->flag & ~M_T_BITS)|_type))
  159.  
  160. #define DATATOMLIST(_ptr) ((struct mlist *) (((char *)_ptr) - M_SIZE))
  161.  
  162. /*
  163.  * Free types
  164.  */
  165. #define F_T_FREE    0x100
  166. #define F_T_CFREE    0x200
  167. #define F_T_XTFREE    0x300
  168. #define F_T_REALLOC    0x400
  169. #define F_T_BITS    0xF00
  170.  
  171. #define GETFTYPE(_ptr)        (_ptr->flag & F_T_BITS)
  172. #define SETFTYPE(_ptr,_type)     (_ptr->flag = ((_ptr->flag & ~F_T_BITS)|_type))
  173.  
  174. /*
  175.  * Fill types
  176.  */
  177. #define FILL_MALLOC    1
  178. #define FILL_REALLOC    2
  179. #define FILL_CALLOC    3
  180. #define FILL_FREE    4
  181. #define FILL_SPLIT    5
  182. #define FILL_JOIN    6
  183.  
  184. #define FILLCHECK(a,b,c,d,e) ( malloc_fill ? FillCheck(a,b,c,d,e) : 0 )
  185. #define FILLDATA(a,b,c,d)     if( malloc_fill ) FillData(a,b,c,d)
  186.  
  187. /*
  188.  * malloc_chain_check flags
  189.  */
  190. #define SHOWERRORS    1
  191. #define DONTSHOWERRS    0
  192.  
  193. /*
  194.  * DBFmalloc fill flags
  195.  */
  196. #define DOFILL        1
  197. #define DONTFILL    0
  198.  
  199. /*
  200.  * malloc join flags
  201.  */
  202. #define NOTINUSE    0    /* don't join inuse segments        */
  203. #define INUSEOK        1    /* ok to join if current seg is in use    */
  204. #define NEXTPTR_INUSEOK    2
  205. #define ANY_INUSEOK    3
  206.  
  207. /*
  208.  * number of longwords to search for magic number before we give up and 
  209.  * search the entire malloc list.
  210.  */
  211. #define MAX_KLUDGE_CHECKS    100
  212. /*
  213.  * Misc stuff..
  214.  */
  215. #define M_ROUNDUP(size)    {\
  216.                 if( size & (M_RND-1) ) \
  217.                 { \
  218.                     size += (M_RND-1); \
  219.                     size &= ~(M_RND-1); \
  220.                 } \
  221.             }
  222.  
  223. EXITTYPE      exit __STDCARGS((int));
  224. char        * getenv __STDCARGS((CONST char *));
  225. DATATYPE    * sbrk __STDCARGS((int));
  226.  
  227. /*
  228.  * stuff for X compatibility routines (needed here so that the prototypes 
  229.  * don't fail
  230.  */
  231. typedef struct {
  232.     char*       start;
  233.     char*       current;
  234.     int         bytes_remaining;
  235. } Heap;
  236.  
  237. #ifndef _XtIntrinsic_h
  238. typedef unsigned int Cardinal;
  239. typedef char * String;
  240. #endif
  241.  
  242. #include "prototypes.h"
  243.  
  244. /*
  245.  * global variables
  246.  */
  247. extern int          in_malloc_code;
  248. extern SIZETYPE          malloc_align;
  249. extern int          malloc_boundsize;
  250. extern DATATYPE        * malloc_data_start;
  251. extern DATATYPE        * malloc_data_end;
  252. extern struct mlist     * malloc_end;
  253. extern int          malloc_errfd;
  254. extern int          malloc_errno;
  255. extern CONST char    * malloc_err_strings[];
  256. extern int          malloc_fatal_level;
  257. extern int          malloc_fill;
  258. extern int          malloc_fillbyte;
  259. extern int          malloc_freebyte;
  260. extern struct mlist    * malloc_freelist;
  261. extern long          malloc_hist_id;
  262. extern int          malloc_opts;
  263. extern int          malloc_round;
  264. extern struct mlist      malloc_start;
  265. extern int          malloc_warn_level;
  266.  
  267. #endif /* _MALLOCIN_H */
  268.  
  269. /*
  270.  * $Log: mallocin.h,v $
  271.  * Revision 1.27  1992/09/03  22:24:33  cpcahil
  272.  * final changes for PL14
  273.  *
  274.  * Revision 1.26  1992/08/22  16:27:13  cpcahil
  275.  * final changes for pl14
  276.  *
  277.  * Revision 1.25  1992/07/12  15:30:58  cpcahil
  278.  * Merged in Jonathan I Kamens' changes
  279.  *
  280.  * Revision 1.24  1992/07/03  00:03:25  cpcahil
  281.  * more fixes for pl13, several suggestons from Rich Salz.
  282.  *
  283.  * Revision 1.23  1992/07/02  13:49:54  cpcahil
  284.  * added support for new malloc_size function and additional tests to testerr
  285.  *
  286.  * Revision 1.22  1992/06/30  13:06:39  cpcahil
  287.  * added support for aligned allocations
  288.  *
  289.  * Revision 1.21  1992/06/22  23:40:10  cpcahil
  290.  * many fixes for working on small int systems
  291.  *
  292.  * Revision 1.20  1992/05/09  00:16:16  cpcahil
  293.  * port to hpux and lots of fixes
  294.  *
  295.  * Revision 1.19  1992/05/08  02:30:35  cpcahil
  296.  * minor cleanups from minix/atari port
  297.  *
  298.  * Revision 1.18  1992/05/08  01:44:11  cpcahil
  299.  * more performance enhancements
  300.  *
  301.  * Revision 1.17  1992/05/06  05:37:44  cpcahil
  302.  * added overriding of fill characters and boundary size
  303.  *
  304.  * Revision 1.16  1992/05/06  04:53:29  cpcahil
  305.  * performance enhancments
  306.  *
  307.  * Revision 1.15  1992/04/24  12:09:13  cpcahil
  308.  * (hopefully) final cleanup for patch 10
  309.  *
  310.  * Revision 1.14  1992/04/24  11:18:52  cpcahil
  311.  * Fixes from Denny Page and Better integration of Xt alloc hooks
  312.  *
  313.  * Revision 1.13  1992/04/22  18:17:32  cpcahil
  314.  * added support for Xt Alloc functions, linted code
  315.  *
  316.  * Revision 1.12  1992/04/20  22:29:14  cpcahil
  317.  * changes to fix problems introduced by insertion of size_t
  318.  *
  319.  * Revision 1.11  1992/04/13  18:26:30  cpcahil
  320.  * changed sbrk arg to int which it should be on all systems.
  321.  *
  322.  * Revision 1.10  1992/04/13  14:16:11  cpcahil
  323.  * ansi changes corresponding to changes made in stack.c
  324.  *
  325.  * Revision 1.9  1992/04/13  03:06:33  cpcahil
  326.  * Added Stack support, marking of non-leaks, auto-config, auto-testing
  327.  *
  328.  * Revision 1.8  1992/03/01  12:42:38  cpcahil
  329.  * added support for managing freed areas and fixed doublword bndr problems
  330.  *
  331.  * Revision 1.7  1991/12/31  21:31:26  cpcahil
  332.  * changes for patch 6.  See CHANGES file for more info
  333.  *
  334.  * Revision 1.6  1991/12/06  08:54:18  cpcahil
  335.  * cleanup of __STDC__ usage and addition of CHANGES file
  336.  *
  337.  * Revision 1.5  91/12/04  18:01:22  cpcahil
  338.  * cleand up some aditional warnings from gcc -Wall
  339.  * 
  340.  * Revision 1.4  91/12/04  09:23:42  cpcahil
  341.  * several performance enhancements including addition of free list
  342.  * 
  343.  * Revision 1.3  91/12/02  19:10:12  cpcahil
  344.  * changes for patch release 5
  345.  * 
  346.  * Revision 1.2  91/11/25  14:42:02  cpcahil
  347.  * Final changes in preparation for patch 4 release
  348.  * 
  349.  * Revision 1.1  91/11/24  00:49:30  cpcahil
  350.  * first cut at patch 4
  351.  * 
  352.  */
  353.