/*static char *SCCSID = "@(#)mem.c	6.3 91/10/29";*/
/***	MEM.C - Handle dynamic memory
 *
 *	Format
 *	Michael Walma
 *	Copyright March 1988, Microsoft Corp.
 *	March 28 1988
 *
 *	Modification History:
 *
 */

#include "ldefs.h"
#include "ltypes.h"
#include <pr_fmt.h>


/***	fmt_get_bit_map - allocate a bit map
 *
 *	fmt_get_bit_map returns a huge pointer to a bit map allocated via halloc().
 *
 *	fmt_get_bit_map (num_bit_maps)
 *
 *	ENTRY		num_bit_maps - number of 2K blocks to allocate
 *
 *	EXIT		Returns a huge pointer to a bit map
 *
 *	CALLS		halloc
 *			fmt_exit_error
 *
 *	WARNINGS	May terminate program prematurely if insufficient mem.
 *
 *	EFFECTS		Allocates memory
 */
UCHAR huge *fmt_get_bit_map (num_bit_maps)
  USHORT num_bit_maps;
{
  UCHAR huge *p;

  if (!(p=halloc ((ULONG)num_bit_maps, (ULONG)SPB * SECSIZE)))
    fmt_exit_error (INSUFFICENT_MEM);

  return (p);
}

/***	fmt_get_bad_list - allocate a bad block list
 *
 *	This function is called to allocate a bad block list.  It is passed
 *	a pointer to a list to expand.  If that is NULL, an initial 2K
 *	block is allocated using calloc().  Otherwise, realloc() is called
 *	to expand the block to the requested size.
 *
 *	fmt_get_bad_list (ptr, num_bad_blocks)
 *
 *	ENTRY		ptr	       - pointer to list to expand
 *			num_bad_blocks - number of 2K blocks needed
 *
 *	EXIT		Returns a pointer to the new block
 *
 *	CALLS		calloc
 *			realloc
 *			fmt_exit_error
 *
 *	WARNINGS	May terminate program prematurely if insufficient mem.
 *
 *	EFFECTS		Allocates memory
 */
struct bad_list *fmt_get_bad_list (ptr, num_bad_blocks)
  struct bad_list *ptr;
  USHORT num_bad_blocks;
{
  struct bad_list *p;

  if (!ptr)
    p = (struct bad_list *)calloc (num_bad_blocks, sizeof (struct bad_list));
  else
    p = (struct bad_list *)realloc (ptr, num_bad_blocks *
					 sizeof (struct bad_list));

  if (!p)
    fmt_exit_error (INSUFFICENT_MEM);

  return (p);
}


/***	fmt_get_indirect_block - allocate a bitmap indirect block
 *
 * This function is called to allocate the bitmap indirect blocks.
 * It allocates a pointer to a run blocks big enough to hold the
 * bitmap indirect table.
 *
 *	fmt_get_indirect_block (USHORT cMaps)
 *
 *	ENTRY		cblocks - number of SPB blocks it will take
 *			to hold the entire bitmap indirect table
 *
 *	EXIT		Returns a pointer to an indirect block
 *
 *	CALLS		malloc
 *			fmt_exit_error
 *
 *	WARNINGS	May terminate program prematurely if insufficient mem.
 *
 *	EFFECTS		Allocates memory
 */
ULONG *fmt_get_indirect_block (USHORT cblocks)		 /* BIGDISK */
{
  ULONG *p;


  if (!(p=(ULONG *)malloc (cblocks * SPB * SECSIZE)))	 /* BIGDISK */
    fmt_exit_error (INSUFFICENT_MEM);

  memset ((UCHAR *)p, '\0', (cblocks * SPB * SECSIZE));	 /* BIGDISK */

  return (p);
}
