/* $Id: varalloc.h,v 1.1.1.1 1996/10/02 10:35:55 roitzsch Exp $ */

#ifndef VARALLOC_H
#define VARALLOC_H

#include "stack.h"

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

// allocator for objects of variable size, thus no return function is supplied;
// it may NOT be used for objects of classes with virtual functions

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

 
class VarSizeAllocator
{
  private:

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

    MemoryBlock* lastBlock;

    int 	blockSize;
    char*	topOfBlock;
    char*	freeSpace;

    int		noOfBlocksSinceIncrement;
    int 	noOfBlocks, memSpace; 

    static int 	elemBaseSize;			// used for address alignment

    void allocationError();

  public:

    VarSizeAllocator(int blockSize);
    ~VarSizeAllocator();

    void* Get(int size);

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

// allocator for objects of fixed size;
// it may NOT be used for objects of classes with virtual functions

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

 
class FixedSizeAllocator
{
  private:

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

    MemoryBlock* lastBlock;

    int 	maxElementsInBlock;
    int 	noOfElementsInBlock;
    int		noOfBlocksSinceIncrement;
    
    int 	elementSize, noOfBlocks, memSpace; 
    char*	freeElement;

    Stack<void*> returnedElements;

    void*   getFromBlock();
    void allocationError();

  public:

    FixedSizeAllocator(int elementSize, int maxElementsInBlock);
    ~FixedSizeAllocator();

    void* Get();
    void Return(void* data) { if (data) returnedElements.push(data); }

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

#endif
