/*static char *SCCSID = "@(#)meminit.c	6.2 91/01/14";*/
#define SCCSID "@(#)meminit.c	6.2 91/01/14"
/*
 *	IBM/Microsoft Confidential
 *
 *	Copyright (c) IBM Corporation 1987, 1989
 *	Copyright (c) Microsoft Corporation 1987, 1989
 *
 *	All Rights Reserved
 *
 */

/*
 *  MEMINIT.C
 *
 *    DESCRIPTION
 *    
 *      Perform the initial memory allocation and set up linked lists
 *      data structures.
 *
 *
 *    FUNCTIONS DEFINED
 *
 *      InitMemory ()   -   Determine if memory initialization has occurred.
 *
 * 
 *  REVISION HISTORY (in reverse chronological order)
 *  -----------------------------------------------------------------------
 *  4/6/90      Brian Andrew    Removed 'FreeSegments'.
 *
 *  4/4/90      Brian Andrew    Moved 'InitSegments' to its own file.
 *                              Only change init done flag if 'InitSegments'
 *                              has no errors.
 *
 *  11/16/89    Brian Andrew    Initialized pgFfrst_Params
 *  11/16/89    Brian Andrew    Initialized pgMount_Params
 *  11/15/89    Brian Andrew    Initialized pgFind_Buf_Loc
 *  11/15/89    Brian Andrew    Added global for RAW_PATH_ISO to reduce
 *                                  stack usage in RecoverPathTable.                        
 *  9/13/89     Brian Andrew    Added InitMemory, called from FS_ entries.
 *  8/10/89     Brian Andrew    Initial functions.
 *  8/4/89      Brian Andrew    Initial Blank Template.
 *
 *
 *  TO DO
 *  -------------------------------------------------------------------------
 */

#define INCL_DOSERRORS
#define INCL_NOPMAPI

#include <os2.h>
#include <fsd.h>
#include <fsh.h>

#include "cdfs.h"
#include "sem.h"
#include "vol.h"
#include "path.h"
#include "file.h"
#include "buffer.h"
#include "mem.h"
#include "heap.h"
#include "message.h"

#ifdef STATS_H
#include "stats.h"
#endif

#include "fsdata.h"
#include "pr_memin.h"
#include "pr_fsini.h"
#include "pr_heap.h"

#ifdef DEBUG
#include "pr_debug.h"
#endif


/***    InitMemory ()   -   Determine if memory initialization has occurred.
 *      
 *  DESCRIPTION
 *
 *      This function is called from the FS_ entry points whenever the
 *      memory structures managed by this FSD have not been initialized.
 *
 *      There is a global variable 'cInitDone' which is used as a
 *      semaphore to prevent simultaneous access to other global variables.
 *      We wait on the semaphore at the start of this function for access
 *      to the globals.  After passing the semaphore, if the initialization
 *      has not been done, it is done now by calling 'InitSegments'.
 *
 *      
 *  ENTRY
 *
 *      None.
 *
 *
 *  LOCAL
 *
 *      usRet   =   Return value from this function.
 *
 *
 *  EXIT
 *
 *      Return value from 'InitSegments' if it is called.
 *      NO_ERROR otherwise.
 */

USHORT InitMemory ()
{
	USHORT usRet;

#ifdef DEBUG
	dprintf ("\tInitMemory: Entered\n");
#endif

	/* Assume that there will be no errors */
	usRet = NO_ERROR;

	SEM_REQ (cInitDone);

	/* Make sure initialization didn't take place while waiting */
	if (fNoMemInit)
	{
		/* If allocation was successful clear init flag */
		if (!(usRet = InitSegments ()))
		{
			fNoMemInit = FALSE;

		}
	}

	SEM_CLEAR (cInitDone);

#ifdef DEBUG
	dprintf ("\tInitMemory: Exit = %d\n", usRet);
#endif

	return usRet;

} /* end InitMemory */
