/*static char *SCCSID = "@(#)smlocal.h  6.3 92/03/21";*/
/*
 *       IBM/Microsoft Confidential
 *
 *       Copyright (c) IBM Corporation 1987, 1989
 *       Copyright (c) Microsoft Corporation 1987, 1989
 *
 *       All Rights Reserved
 */

typedef unsigned long swapid_t;

/***ET SwapData_s - used to save page data when the swapper
 *      is performing group swap outs.
 */
typedef struct SwapData_s {
        struct pf_s         *pPF;      // pointer to page frame
        struct vp_s         *pVP;      // pointer to virtual page structure
        ulong_t            laddr;      // linear transfer address
} SwapData_t;

/***ET SMPageCmdList_s - data area to save per page swapper data
 *     Structure used to hold per page data when group swap outs are
 *     performed.
 */
typedef struct SMPageCmdList_s {
        struct PageCmdHeader_s  Hdr;
        struct PageCmd          Cmd[MAXSWAPGROUPSIZE];//array of PageCmd strucs
        struct SwapData_s       Swp[MAXSWAPGROUPSIZE];//array of GRPSwap strucs
} SMPageCmd_t;

/***LP  RoundUpPages - round page count up to swap file granularity
 *
 *      RoundUpPages rounds a page count up to the next multiple
 *      of FileSizeQuantum.  Used to determine how many pages will
 *      be in a newly expanded swap file.
 *
 *      ENTRY   pagecnt - a count of pages
 *      EXIT    returns pagecnt rounded up to the next multiple of
 *              the swap file size growth quantum
 */
#define RoundUpPages(pagecnt) (((pagecnt) + PFQMASK) & ~(PFQMASK))



/***LP  SwapIdToByte  - swap id to file offset conversion
 *
 *      SwapIdToByte converts a swap id into the byte offset of
 *      that swap frame in the swap file.
 *
 *      ENTRY   SwapId  - a swap id
 *      EXIT    returns the byte offset in the swap file where the
 *              page with swap id SwapId is located
 */
// #define SwapIdToByte(swapid) ((swapid) << PAGESHIFT)
#define SwapIdToByte(swapid) ((SMcInMemFile == 0) ?          \
           ((swapid) << PAGESHIFT) :  (((swapid)-SMcInMemFile) << PAGESHIFT))

/***LP  ByteToSwapId  - file offset to swap id conversion
 *
 *      SwapIdToByte converts a byte offset in the swap file into
 *      a swap id of a swap frame
 *
 *      ENTRY   foffs - offset into swap file (FileOffset)
 *      EXIT    returns the swap id for that file offset in the swap file
 */
// #define ByteToSwapId(foffs) ((foffs) >> PAGESHIFT)
#define ByteToSwapId(foffs)  ((SMcInMemFile == 0) ?          \
           ((foffs) >> PAGESHIFT) : (((foffs) >> PAGESHIFT) - SMcInMemFile))


/***LP  PagesToBytes - page count to byte count conversion
 *
 *      PagesToBytes converts a number of pages into the number of
 *      bytes contained in them.
 *
 *      ENTRY   pagecnt - a count of pages
 *      EXIT    returns the number of bytes contained in pagecnt pages
 */
#define PagesToBytes(pagecnt) ((pagecnt) << PAGESHIFT)


/*
 * Declarations of local functions
 */
extern unsigned smSIOInit(char *pszSwapPath, swapid_t *pInitSize,
                ulong_t *pFlags);
extern unsigned smSIOSwapIn(swapid_t, ulong_t, ulong_t);
extern unsigned smSIOResize(swapid_t);
extern unsigned smSIOSpaceQuery(swapid_t *);
extern unsigned smAllocSFT(int*);
extern void     smFreeSFT(int);
extern ulong_t  smAllocDF(int);
extern int      smSFTalloc(int *);
extern void     smSFTfree(int);
extern unsigned smIncreaseSpace(swapid_t);
extern unsigned smDecreaseSpace(swapid_t);
extern unsigned smPerformShrink(void);
extern void     smSetSwapFlags(uchar_t);
extern void     smClearSwapFlags(uchar_t);
extern int      smGRPAllocDF(ulong_t, struct SMPageCmdList_s *);
extern int      smGrpSwpErrHandler( struct SMPageCmdList_s *, int );
extern int      smRetrySwapOut(struct SMPageCmdList_s *, int);
extern void     smSetCmd1(struct SMPageCmdList_s *);

extern int      SMSetSwapSpace2(void);
extern void     smHardError(void);

/*
 * external declarations of local data
 */
extern swapid_t smFileSize;
extern unsigned smMinFree;
extern ulong_t  smSFTmap;
extern hSFN     *smSFNarr;
extern SMPageCmd_t *smPCarr[MAXSIMULSWAP];
extern ulong_t  smDFMac;
extern char     *smbmDF;
extern unsigned smcBrokenDF;
extern char     smPendingShrink;
extern char     smInitialized;


/***LK+ swap file sizing constants
 *
 *      BYTEFILEQUANTUM is the incremental growth unit of the swap file,
 *      in bytes.
 *
 *      BM_xxx are used to compute DF bitmap overheap.
 *
 *      The remaining constants are used in deciding when to shrink
 *      the swap file.
 */
#define BYTEFILEQUANTUM (1024*_1K)
#define PAGEFILEQUANTUM (BYTEFILEQUANTUM/PAGESIZE) /* 0x100 */
#define PFQMASK         (PAGEFILEQUANTUM-1)        /* 0xff */
#define SWAP_MAX_SIZE   (2*_1K*(_1MEG / PAGESIZE))

#define SM_MIN_SWAPINIT_SIZE  0x100
#define SM_MIN_MINFREE_SIZE   0x80

extern uchar_t *smpHeap;
extern uchar_t *smpHeapEnd;
extern VMHOB smHobheap;     // hob of smheap object
extern uchar_t SMCommit;    // should swapper back allocations

/*
 * Define assertion checking macros for use in the swap manager.
 */
#if     defined(SMSTRICT)
#define smAssert(x)     _assert(x)
#else
#define smAssert(x)
#endif
