/*  Author: Mark Moraes <moraes@csri.toronto.edu> */

#include <server_memory.h>

/*
 *  All globals are names starting with _malloc, which should not clash
 *  with anything else.
 */
/*
 *  Remember to initialize the variable in globals.c if you want, and
 *  provide an alternative short name in globrename.h
 */
/* PH: NOT REQUIRED FOR WINEX #include "globrename.h" */
/* PH: NOT REQUIRED FOR WINEX #include "version.c" */

/*
 *  _malloc_minchunk is the minimum number of units that a block can be
 *  cut down to.  If the difference between the required block size, and
 *  the first available block is greater than _malloc_minchunk, the
 *  block is chopped into two pieces, else the whole block is returned.
 *  The larger this is, the less fragmentation there will be, but the
 *  greater the waste. The actual minimum size of a block is therefore
 *  _malloc_minchunk*sizeof(Word) This consists of one word each for the
 *  boundary tags, one for the next and one for the prev pointers in a
 *  free block.
 */
const size_t _malloc_minchunk = FIXEDOVERHEAD;

/*
 * _malloc_rovers is the array of pointers into that each point 'someplace'
 * into a list of free blocks smaller than a specified size. We start our
 * search for a block from _malloc_rovers, thus starting the search at a
 * different place everytime, rather than at the start of the list.  This
 * improves performance considerably, sez Knuth
 */
/* See below: 
Word *_malloc_rovers[MAXBINS+1] = {NULL};
*/
const short _malloc_binmax[MAXBINS] = {
    8, 16, 32, 64, 128, 256, 512, 1024
};
/* See below 
int _malloc_firstbin = 0;
int _malloc_lastbin = 0;
Word *_malloc_hiword = NULL;
Word *_malloc_loword = NULL;
*/

/* 
 *  _malloc_sbrkunits is the multiple of sizeof(Word) to actually use in
 *  sbrk() calls - _malloc_sbrkunits should be large enough that sbrk
 *  isn't called too often, but small enough that any program that
 *  mallocs a few bytes doesn't end up being very large. I've set it to
 *  1K resulting in a sbrk'ed size of 8K. This is (coincidentally!) the
 *  pagesize on Suns.  I think that this seems a reasonable number for
 *  modern programs that malloc heavily. For small programs, you may
 *  want to set it to a lower number.
 */
const size_t _malloc_sbrkunits = DEF_SBRKUNITS;

/* See below
Word *_malloc_mem = NULL;
*/

/*
 *  Do not call any output routine other than fputs() - use sprintf() if
 *  you want to format something before printing. We don't want stdio
 *  calling malloc() if we can help it
 */
const int _malloc_tracing = 0;	/* No tracing */
/* Doesn't compile so done at runtime: FILE *_malloc_statsfile = stderr; */
FILE *_malloc_statsfile = NULL; /* PH: Per process */
char _malloc_statsbuf[128]; /* PH: Per process is fine */

const int _malloc_leaktrace = 0;

#ifdef PROFILESIZES
/* See below
int _malloc_scount[MAXPROFILESIZE];
*/
#endif /* PROFILESIZES */

#ifdef DEBUG
/*
 *  0 or 1 means checking all pointers before using them. Reasonably
 *  thorough.  2 means check the entire heap on every call to
 *  malloc/free/realloc/memalign. (the rest call these)
 */
const int _malloc_debugging = 3;
#endif /* DEBUG */

/* PH: Moved to bilge.c univptr_t (* _malloc_memfunc) proto((size_t)) = _mal_sbrk; */

START_FILE_STATIC_VARIABLES( mem_global )
  Word *_malloc_rovers[MAXBINS+1];
  
  int _malloc_firstbin;
  int _malloc_lastbin;
  Word *_malloc_hiword;
  Word *_malloc_loword;
  
  Word *_malloc_mem;
  
#ifdef PROFILESIZES
  int _malloc_scount[MAXPROFILESIZE];
#endif /* PROFILESIZES */

END_FILE_STATIC_VARIABLES_NO_AUTO_INIT( mem_global )

size_t shm_allocator_size(void)
{
  return sizeof( __server_static_memory_area_struct_for_file );
}

void shm_setup_memory( void* ptr )
{
  server_memory_block->__server_static_holder_for_mem_global = ptr;
  memset( server_memory_block->__server_static_holder_for_mem_global, 0, sizeof( __server_static_memory_area_struct_for_file ) );
}


