

#ifndef  __CHUNK_STUFF__
#define  __CHUNK_STUFF__

#include "chunk.h"
#include "list.h"


#define  declareChunk(type)   extern Chunk type ## Chunk;          			 \
                              extern type *alloc ## type ## Chunk(); 			 \
                              extern void free ## type ## Chunk(type *ptr);		 \

#define  defineChunk(type)								 \
											 \
Chunk type ## Chunk = (Chunk) 0;							 \
											 \
type *alloc ## type ## Chunk()								 \
{											 \
  if (type ## Chunk == NULLCHUNK) {							 \
    type ## Chunk = chNew(sizeof(type));						 \
    if (type ## Chunk == NULLCHUNK) {							 \
      return (type *) NULL;								 \
    }											 \
  }											 \
  return (type *) chAlloc(type ## Chunk);						 \
}											 \
											 \
void free ## type ## Chunk(type *ptr)							 \
{											 \
  chFree(type ## Chunk, (void *) ptr);							 \
}

typedef void (*ChunkFreeFnType)(void *);

#define  allocChunk(type)           alloc ## type ## Chunk()
#define  freeChunk(type, ptr)       free ## type ## Chunk((ptr))
#define  freeChunkList(type, list)							 \
                    __free_Chunk_List__((list), (ChunkFreeFnType) free ## type ## Chunk)

extern void __free_Chunk_List__(List l, ChunkFreeFnType ff);


#endif   
