/*
 * getblocks.c
 * the guts of the allocation scheme.
 * [eichin:19880330.1727EST]
 */

#include <msimdecl.h>

#define BLOCKS 4

ropeptr getblock()
{
  ropeptr retptr;
  
  if (!nextfree)
    {
      ropeptr brk_and_format_block();
      nextfree = brk_and_format_block();
    }
  retptr = nextfree;
  nextfree = nextfree->data.NEXT;

  retptr->tag.CTL = BEGIN;
  retptr->data.NEXT = retptr+1;

  return(retptr);
}

ropeptr brk_and_format_block()
{
  ropeptr malloc(), newnext, scan, chainptr = NULL;
  
  newnext = malloc(sizeof(rope_elem)*BLOCKS*ROPE_SIZE);
  
  for(scan = newnext; scan < newnext+BLOCKS*ROPE_SIZE; scan += ROPE_SIZE)
    {
      ropeptr iter = scan;

      iter->tag.CTL = FREELIST;
      iter->data.NEXT = chainptr;
      chainptr = iter++;

      for(; iter < scan+ROPE_SIZE; iter++)
	{
	  iter->tag.CTL = END;
	  iter->data.CTL = notend;
	}
      (--iter)->data.CTL = end;
    }
  return(chainptr);
}
