tldm-universe/Ardent/UV/gcidir/include/uvmalloc.h
2024-09-09 17:51:08 -04:00

339 lines
11 KiB
C
Executable File

#ifndef h_malloc
#define h_malloc
/******************************************************************************
*
* UniVerse replacement for standard UNIX memory managment functions
*
* Module %M% Version %I% Date %H%
*
* (c) Copyright 1998 Ardent Software Inc. - All Rights Reserved
* This is unpublished proprietary source code of Ardent Software, Inc.
* The copyright notice above does not evidence any actual or intented
* publication of such source code.
*
*******************************************************************************
*
* Maintenence log - insert most recent change descriptions at top
*
* Date.... GTAR# WHO Description.........................................
* 10/14/98 23801 SAP Change copyrights.
* 05/06/96 18242 DTM Code Cleanup, Phase I
* 04/24/96 18242 DTM Code Cleanup, Phase I
* 03/21/96 17935 JC Use ANSI types for malloc etc with wchar's
* 11/17/94 15335 FRA Port Release 8 to RS6000 with system malloc.
* 06/25/93 11417 CSM Add MM_ODBC class
* 01/11/93 10840 RM Fixed ifdef ANSI
* 07/28/92 9412 JWT add MM_SELECT class
* 01/11/92 9075 PHH Improved Malloc Tracing & Logging
* 10/09/91 8803 MAA corrected size of MALLOC_BLOCK when TRACING=1
* 07/28/90 7324 JWT fixed malloc documentation
* 04/19/89 5960 JWT Speed up malloc
* 03/15/89 5865 TJR cleaned up typedefs
* 02/24/89 5803 PHH Improve the CORE verb
* 02/21/88 5803 PHH Make the CORE verb work
* 01/19/89 5722 PHH First cut at tracing
*
*****************************************************************************/
/* MALLOC_TRACING is a bit mapped valued which turns on different
types of runtime malloc checks at compile time. Not all
combinations of tracing modes are vaild. At this time, the
modes are:
1 = minimal trace mode: This mode is intended to assist in debugging
those nasty problems that seem to vanish when more extensive
malloc checking features are enabled. This mode, enables
the trap count, and range trap features. When used, the
global variable UVtrapcnt is incremented each time a malloc
type operation is performed. If the value in UVtrapcnt is
zero after incrementing, the function UVtrap is called.
UVtrap is also called if a malloc type operation is called
that operates on or returns an address in the range
UVmtmin to UVmtmax. UVtrapcnt, UVmtmin and UVmtmax all
have initial values of zero, but can be set in the debugger
to allow stopping of a uv process when any one of the above
conditions occurs.
2 = malloc overflow detection: This mode enables the current block
check feature. When used, an extra byte is allocated for
each malloc request, and the character '$' is placed in that
byte. Any subsequent malloc type operation that works with
that allocated chunck, will verify that the extra byte still
contains the '$'. If the '$' is not there, it is assumed that
the block has been overflowed, a message is printed, and abort
is called.
4 = malloc integrity check: This mode enables the malloc spaces
integrity checker. When used, the integrity of every block
of malloc space (in use and free) is checked for integrity.
This check is performed each time any malloc type operation is
performed, and is performed by the function UVcheckall.
If any anomalies are detected, a message is printed, and abort
is called.
8 = malloc logging: This mode enables the malloc logging facility.
This option causes a trace of all malloc activity to written
to a file for later analysis. As this feature has not been
extensively tested, and there is not a copy of the analysis
tool anywhere, use of this option is discouraged.
Some platforms cannot use our version of malloc. On those
machines, we make sure that MALLOC_TRACING is turned off.
*/
#if !OURMALLOC
# ifdef MALLOC_TRACING
# undef MALLOC_TRACING
# endif
#endif
/************************************************************************
* *
* MALLOC_BLOCK Minimum size of an individual memory block. *
* *
* MALLOC_ALIGN Alignment factor, malloc blocks will be a *
* a multiple of MALLOC_ALIGN, to insure block *
* alignment. MALLOC_ALIGN must be a power of 2. *
* MALLOC_ALIGN must be greater than the size *
* of a free block [ sizeof(FBLOCK) ]. *
* *
* MALLOC_PAGE All requests to the kernel for memory will be *
* a multiple of MALLOC_PAGE. *
* *
* MALLOC_EXTRA All requests to the kernel for more memory *
* will add in MALLOC_EXTRA before computing the *
* amount to request. *
* *
* MALLOC_UNUSED Amount of memory allowed to accumulate before *
* it is returned to the kernel. *
* *
* MALLOC_SAVE Amount of memory not returned to the kernel *
* when unused memory exceeds MALLOC_UNUSED. *
* *
* MALLOC_QnSIZE Malloc maintains up to three caches of "quick" *
* memory, which is bitmapped for being free or *
* in use. MALLOC_QnSIZE determined the number *
* blocks in each cache. SIZE should be a *
* multiple of 32. Each cache can be disabled *
* by setting SIZE to 0. *
* *
* MALLOC_QnBLOCK Specifies the blocksize for each of the three *
* caches. If Q1 is the smallest, and Q3 the *
* biggest, then when Q1 is full, further requests *
* will be filled by Q2, and so forth. *
* *
* *
* MALLOC_FORWARD This boolean value specifies that forward *
* coalese operations are to be performed. *
* *
* MALLOC_BACKWARD This boolean value specifies that backward *
* coalese operations are to be performed. *
* *
* MALLOC_GARBAGE Specifies the number of calls to malloc() *
* between each garbage collection. A value of *
* zero will disable garbage collection. *
* *
************************************************************************/
/* M_ON and M_OFF are flags used to indicate when the malloc features
described are turned on or off.
*/
#define M_ON 1 /* feature enabled */
#define M_OFF 0 /* feature disabled */
#define M_TRAPS 1 /* UVtrap checks enabled */
#define M_CHECK 2 /* overflow detection enabled */
#define M_INTEG 4 /* integrity check enabled */
#define M_LOG 8 /* malloc logging enabled */
#define M_MAP 16 /* Mapping Functions */
#define M_CORE 32 /* COre Map Dumper */
#ifndef MALLOC_TRACING
# define MALLOC_TRACING M_OFF
#endif
#ifndef MALLOC_BLOCK
# if MALLOC_TRACING & M_MAP
# define MALLOC_BLOCK 32
# else
# define MALLOC_BLOCK 16
# endif
#endif
#ifndef MALLOC_ALIGN
# define MALLOC_ALIGN 16
#endif
#ifndef MALLOC_PAGE
# define MALLOC_PAGE 4096
#endif
#ifndef MALLOC_EXTRA
# define MALLOC_EXTRA sizeof(FBLOCK)
#endif
#ifndef MALLOC_UNUSED
# define MALLOC_UNUSED ((MALLOC_PAGE * 8))
#endif
#ifndef MALLOC_SAVE
# define MALLOC_SAVE ((MALLOC_PAGE * 4) + (sizeof(FBLOCK) * 0))
#endif
#if MALLOC_TRACING & (M_CHECK | M_INTEG | M_MAP)
# undef MALLOC_Q1SIZE
# undef MALLOC_Q1BLOCK
# undef MALLOC_Q2SIZE
# undef MALLOC_Q2BLOCK
# undef MALLOC_Q3SIZE
# undef MALLOC_Q3BLOCK
# define MALLOC_Q1SIZE 0
# define MALLOC_Q1BLOCK 0
# define MALLOC_Q2SIZE 0
# define MALLOC_Q2BLOCK 0
# define MALLOC_Q3SIZE 0
# define MALLOC_Q3BLOCK 0
#else
# ifndef MALLOC_Q1SIZE
# define MALLOC_Q1SIZE 0
# endif
# ifndef MALLOC_Q1BLOCK
# define MALLOC_Q1BLOCK 1
# endif
# ifndef MALLOC_Q2SIZE
# define MALLOC_Q2SIZE 0
# endif
# ifndef MALLOC_Q2BLOCK
# define MALLOC_Q2BLOCK 2
# endif
# ifndef MALLOC_Q3SIZE
# define MALLOC_Q3SIZE 0
# endif
# ifndef MALLOC_Q3BLOCK
# define MALLOC_Q3BLOCK 4
# endif
#endif
#ifndef MALLOC_FORWARD
# define MALLOC_FORWARD M_ON
#endif
#ifndef MALLOC_BACKWARD
# define MALLOC_BACKWARD M_ON
#endif
#ifndef MALLOC_GARBAGE
# define MALLOC_GARBAGE 0
#endif
#if MALLOC_TRACING & M_CORE
# ifndef MCORE_PERIOD
# define MCORE_PERIOD 1000
# endif
# ifndef MCORE_HIGH
# define MCORE_HIGH M_ON
# endif
# ifndef MCORE_FINAL
# define MCORE_FINAL M_ON
# endif
#endif
#define Mslots 9 /* number of buddy groups for free space managment */
/* These are some useful macros to ensure correct type casting */
#define Cmalloc(x) malloc((unsign)(x))
#define Ccalloc(x,y) calloc((unsign)(x),(unsign)(y))
#define Crealloc(x,y) realloc((x),(unsign)(y))
#if ANSI == 1
#define Cfree(x) free((void*)(x))
#define Rfree(x) free((void*)(x))
#define Rrealloc(x,y) (uchar *)Crealloc((void*)(x),(y))
#else
#define Cfree(x) free((char*)(x))
#define Rfree(x) free((char*)(x))
#define Rrealloc(x,y) ((uchar*)Crealloc((char*)(x),(y)))
#endif
#define Rcalloc(x,y) ((uchar*)Ccalloc((x),(y)))
#define Rmalloc(x) ((uchar*)Cmalloc((x)))
#if ((OURMALLOC) || (ANSI == 1))
/* special location to that mallocs of zero bytes won't take up lots of space */
EXTERN char nilMblok;
#endif
#if MALLOC_TRACING & M_MAP
PRI_RO char Mmodule[] = __MODULE__;
#if ANSI
EXTERN void *UVtrace(), *UVmalloc(), *UVcalloc(), *Mtrace(), *UVrealloc();
#else
EXTERN char *UVtrace(), *UVmalloc(), *UVcalloc(), *Mtrace(), *UVrealloc();
#endif
# define malloc(x) Mtrace(UVmalloc(x), Mmodule, __LINE__)
# define calloc(x,y) Mtrace(UVcalloc(x,y), Mmodule, __LINE__)
# define realloc(x,y) Mtrace(UVrealloc(x,y), Mmodule, __LINE__)
# define MMAP(blk,x,y) if(blk && ((char*)blk != (char*)&nilMblok))\
((MBLOCK*)blk)[-1].flag = (int)x;
EXTERN STRING MM_STRING();
#else
# if ANSI || (defined(_WCHAR_T) && defined(ANSI_MALLOC_WITH_WCHAR_T))
extern void *malloc(), *realloc(), *calloc();
# else
extern char *malloc(), *realloc(), *calloc();
# endif
#define MMAP(blk,x,y)
#endif
extern void free();
/* MBLOCK is is the structure that describes the layout of the
overhead associated with a block of memory that is in use */
typedef struct MallocBlock
{ uint size; /* size of this chunk */
#if MALLOC_BACKWARD
uint bcksize; /* size of chunk that immediately
precedes this chunk */
#endif
#if MALLOC_TRACING & M_CHECK
int request; /* size actually requested */
#endif
#if MALLOC_TRACING & M_MAP
char *module; /* pointer to requestor name */
int line; /* line number from requestor */
int flag; /* malloc mapping tag code */
#endif
}MBLOCK;
/* FBLOCK is is the structure that describes the layout of the
overhead associated with a block of memory that is free */
typedef struct FreeBlock
{ int size; /* size of this chunk */
#if MALLOC_BACKWARD
uint bcksize; /* size of chunk that immediately
precedes this chunk */
#endif
struct FreeBlock *flink; /* next in free chain */
struct FreeBlock *blink; /* previous in free chain */
}FBLOCK;
#define MMAP_STRING(x,y)
#define MMAP_DBFILE(x,y)
#define MMAP_DATUM(x,y)
#define MMAP_SEQFILE(x,y)
#define MMAP_SELFILE(x,y)
#if MALLOC_TRACING & M_LOG
# include "MLOG.h"
#endif
/* codes used by malloc map facility of CORE verb to tag MBLOCKs */
#define MM_OVERHEAD 1
#define MM_HISTORY 2
#define MM_ATVARIABLE 3
#define MM_PRINTER 4
#define MM_COMMAND 5
#define MM_QUERY 6
#define MM_SORTER 7
#define MM_FORMAT 8
#define MM_SCRATCH 9
#define MM_DISKIO 10
#define MM_NAMCOM 11
#define MM_SELECT 12
#define MM_ODBC 13
#endif /* end of uvmalloc.h */