/* $Id: alloc.h,v 1.4 1996/11/06 11:37:44 roitzsch Exp $ */

#ifndef ALLOC_H
#define ALLOC_H

#include <iostream.h>

#include "stack.h"

//-------------------------------------------------------------------------

// the class StaticAllocator is designed only for use as a static object,
// thus its destructor takes no action

// for the derived class Allocator, the objects must supply a reset-function
// (as the constructor is not called if an object is returned from the stack)

//-------------------------------------------------------------------------


template<class T>  

class StaticAllocator
{
  protected:

    struct MemoryBlock { T* space; MemoryBlock* next; };

    MemoryBlock* lastBlock;
    Stack<T*>    returnedElements;

    int 	maxElementsInBlock;
    int 	noOfElementsInBlock;
    int		noOfBlocksSinceIncrement;

    int 	noOfBlocks, memSpace; 
    T*		freeElement;

    T*   getFromBlock();
    void allocationError();

  public:

    StaticAllocator(int maxElementsInBlock);
    virtual ~StaticAllocator() { }

    virtual T* Get();
    void Return(T* elem) { if (elem) returnedElements.push(elem); }

    void Info()	      const;
    int  MemSpace()   const;
    int  NoOfBlocks() const { return noOfBlocks; }
};
//-------------------------------------------------------------------------
//-------------------------------------------------------------------------


template<class T>  


class Allocator : public StaticAllocator<T>
{
  public:

    Allocator(int maxElementsInBlock0) 
    				: StaticAllocator<T>(maxElementsInBlock0) {}
    virtual ~Allocator();

//    virtual T* Allocator<T>:: Get();
    virtual T* Get();
};
//-------------------------------------------------------------------------

#if INCLUDE_TEMPLATE_DEFS
#include "alloc.c"
#endif

#endif
