/* 
 * File:   ListOfArrays.h
 * Author: Nina Engelhardt
 *
 * Created on December 16, 2011, 2:06 PM
 */

#ifndef LISTOFARRAYS_H
#define	LISTOFARRAYS_H

typedef struct {
    ArrayFragment* next;
    void* data;
} ArrayFragment;

typedef struct {
    ArrayFragment* first;
    ArrayFragment* last;
    size_t entry_size;
    int num_entries_per_fragment;
    int next_free_index;
} ListOfArrays;

#define addToListOfArrays(type,value,list) do { \
    int offset_in_fragment = list->next_free_index % list->num_entries_per_fragment; \
    if(offset_in_fragment == 0){ \
        ArrayFragment* newBlock = (ArrayFragment*) VMS__malloc(sizeof(ArrayFragment*) + list->entry_size * list->num_entries_per_fragment); \
        newBlock->next == NULL; \
        if(list->first == NULL) {\
            list->first = newBlock; \
        } \
        if(list->last != NULL) { \
            list->last->next = newBlock; \
        } \
        list->last = newBlock; \
    } \
    (type*) typedFragment = (type*) list->last->data; \
    typedFragment[offset_in_fragment] = value; \
} while (0)

typedef void  (*ListOfArraysFnPtr)  ( void * );  //fn has to cast void *

#endif	/* LISTOFARRAYS_H */

