/*****************************************************************************/
/*
                                 Vm.h
*/
/*****************************************************************************/

#ifndef VM_H_LOADED
#define VM_H_LOADED 1

#include "wasd.h"

/*~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
#define VM_BACKPORT_FOR_DTAG 0
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~*/

/**********/
/* macros */
/**********/

/* more intended for development and troubleshooting than production */
#define VM_DECC 0            /* intercept DECC calls */
#define VM_EXPAT 1           /* use this module to manage EXPAT memory */
#define VM_OPENSSL 1         /* use this module to manage OpenSSL memory */
#define VM_STATS_ONLY 0      /* do not create zones just collect stats */

#ifndef VM_DECC
#  define VM_DECC 0
#endif

#ifndef VM_EXPAT
#  define VM_EXPAT 0
#endif

#ifndef VM_OPENSSL
#  define VM_OPENSSL 0
#endif

#ifndef VM_STATS_ONLY
#  define VM_STATS_ONLY 0
#endif

#ifdef __VAX
   /* unsuitable for 32 bit VAX */
#  undef VM_DECC
#  define VM_DECC 0
#  undef VM_DECC_LIB_VM 0
#  define VM_DECC_LIB_VM 0
#  undef VM_EXPAT
#  define VM_EXPAT 0
#  undef VM_OPENSSL
#  define VM_OPENSSL 0
#  undef VM_STATS_ONLY
#  define VM_STATS_ONLY 0
#endif

#define VM_MAGIC_CACHE      0xBADCAFE1   /* cache */
#define VM_MAGIC_DECC       0xBADCAFE2   /* DECC interception */
#define VM_MAGIC_EXPAT      0xBADCAFE3   /* expat */
#define VM_MAGIC_GENERAL    0xBADCAFE4   /* general */
#define VM_MAGIC_HTTP2      0xBADCAFE5   /* HTTP/2 */
#define VM_MAGIC_OPENSSL    0xBADCAFE6   /* OpenSSL */
#define VM_MAGIC_PERMCACHE  0xBADCAFE7   /* permanent cache */
#define VM_MAGIC_REQUEST    0xBADCAFE8   /* request */
#define VM_GUARD            0x2C0FFEE5   /* integrity checking */

/* page file can grow to this percentage then the server spits */
#define VM_DEFAULT_PGFL_LIMIT 95

/**************/
/* structures */
/**************/

#pragma member_alignment __save
#pragma nomember_alignment

typedef struct VmStruct VM_STRUCT;

/* 24 bytes overhead in total */
struct VmStruct
{
   ulong  size,
          magic;

   /* fill to quadword */
   uchar  fill [7];

   /* inbuilt HTTP/2 header fields (also see HTTP2.H) */
   union vm_header2
   {
      uchar h2header [9];
      struct
      { 
         uchar  h2length [3];
         uchar  h2type   [1];
         uchar  h2flags  [1];
         uchar  h2ident  [4];
      };
   };
   uchar  h2payload [];
};

#pragma member_alignment __restore

typedef struct vm_zone_struct VM_ZONE;

struct vm_zone_struct
{
   ulong  CallocCount,
          ExtendPages,
          FreeCount,
          InitialPages,
          MagicValue,
          MallocCount,
          MaxPages,
          MinPages,
          ReallocCount,
          ZoneId;
              
#ifndef __VAX
   __int64  AllocBytes,
            DeallocBytes,
            FreeBeforeBytes,
            InUseBytes,
            InUseMaxBytes,
            LastLookBytes,
            LastLookTime64;
#else
   /*
      These data are never used with VAX and so safely can be demoted
      to 32 bits to allow the structure to be compiled.  Maintain the
      64 bit size with fillers.
   */
   ulong    AllocBytes,
            AllocBytes2,
            DeallocBytes,
            DeallocBytes2,
            FreeBeforeBytes,
            FreeBeforeBytes2,
            InUseBytes,
            InUseBytes2,
            InUseMaxBytes,
            InUseMaxBytes2,
            LastLookBytes,
            LastLookBytes2,
            LastLookTime64[2];
#endif

   char  *LogicalName,
         *ReportTitle;
};

/***********************/
/* function prototypes */
/***********************/

/* general memory */

void VmlInit ();
void VmGeneralInit ();
void VmCheckPgFlLimit ();
uchar* VmGet (ulong);
void VmFree (uchar*, char*, int);
uchar* VmRealloc (uchar*, ulong, char*, int);
ulong VmZoneCurrentPages (ulong);
int VmZoneCurrentSize (struct dsc$descriptor*, ulong*);
void VmSetPages (struct vm_zone_struct*);

void* VmGenericCalloc (struct vm_zone_struct*, int, int, char*, int);
void* VmGenericRealloc (struct vm_zone_struct*, uchar*, int, char*, int);
void VmGenericFree (struct vm_zone_struct*, uchar*, char*, int);

#if VM_DECC
void* VmDeccCalloc (int, int);
void* DECC$CALLOC (int, int);
void VmDeccInit ();
void* VmDeccMalloc (int);
void* DECC$MALLOC (int);
void* VmDeccRealloc (uchar*, int);
void* DECC$REALLOC (uchar*, int);
void VmDeccFree (uchar*);
void DECC$FREE (uchar*);
#endif VM_DECC

/* these are not conditional to allow SESOLA.C build for either case */
void VmOpenSslFree (uchar*);
BOOL VmOpenSslInit ();
void* VmOpenSslMalloc (int);
void* VmOpenSslRealloc (uchar*, int);

/* these are not conditional to allow DAVXML.C build for either case */
void VmExpatFree (uchar*);
BOOL VmExpatInit ();
void* VmExpatMalloc (int);
void* VmExpatRealloc (uchar*, int);

/* HTTP/2 memory */

void VmHttp2Init ();
HTTP2_STRUCT* VmHttp2Get (int);
void VmHttp2Free (HTTP2_STRUCT*, char*, int);

void VmFree2Heap (HTTP2_STRUCT*, char*, int);
void VmFreeFrom2Heap (HTTP2_STRUCT*, uchar*, char*, int);
uchar* VmRealloc2Heap (HTTP2_STRUCT*, uchar*, ulong, char*, int);

/* request memory */

void VmRequestInit ();
REQUEST_STRUCT* VmGetRequest (int);
void VmFreeRequest (REQUEST_STRUCT*, char*, int);

uchar* VmGetHeap (REQUEST_STRUCT*, ulong);
void VmFreeHeap (REQUEST_STRUCT*, char*, int);
void VmFreeFromHeap (REQUEST_STRUCT*, uchar*, char*, int);
uchar* VmReallocHeap (REQUEST_STRUCT*, uchar*, ulong, char*, int);

/* cache memory */

void VmCacheInit (int);
uchar* VmGetCache (ulong);
void VmFreeCache (uchar*, char*, int);

void VmPermCacheInit (int);
uchar* VmGetPermCache (ulong);
void VmFreePermCache (uchar*, char*, int);

/* memory report */

#if VM_BACKPORT_FOR_DTAG
void VmReport (REQUEST_STRUCT*, REQUEST_AST);
#else
void VmReport (REQUEST_STRUCT*);
#endif
void VmReportZone (REQUEST_STRUCT*, struct vm_zone_struct*);
int VmWrite (struct dsc$descriptor*, REQUEST_STRUCT*);

/* inline debug */
void VmDebugOpenSslZone (char *module, int lnumber);

#endif /* VM_H_LOADED */

/*****************************************************************************/

