seanhalle@33: /* seanhalle@33: * File: Vector.h seanhalle@33: * Author: Me seanhalle@33: * seanhalle@33: * Created on May 14, 2010, 3:08 PM seanhalle@33: */ seanhalle@33: seanhalle@33: #ifndef _DYNARRAY_H seanhalle@33: #define _DYNARRAY_H seanhalle@33: seanhalle@33: #include "PR_impl/PR_primitive_data_types.h" seanhalle@33: #include "PR_impl/Services_Offered_by_PR/Memory_Handling/vmalloc.h" seanhalle@33: seanhalle@33: seanhalle@33: /*WARNING: Passing a DynArray as a param is dangerous if add to the DynArray seanhalle@33: * inside the function called! After adding or other operation that might seanhalle@33: * change the size, must re-read the addr of the chunk of memory that is the seanhalle@33: * array, via the DynArrayInfo. seanhalle@33: *Here's why: An array variable is a location, either on the stack seanhalle@33: * or in a field of a struct, whose contents is an addr. That addr is of the seanhalle@33: * first location of a chunk of locations. The DynArray works by changing seanhalle@33: * the chunk of locations, then modifying the contents of the original seanhalle@33: * array variable. It overwrites the addr of the old chunk of locations seanhalle@33: * with the addr of the new chunk. seanhalle@33: *But when the array variable is passed as a parameter, such as seanhalle@33: * in this: "foo( myDynArray )", then there are now two locations that hold seanhalle@33: * the addr of the same chunk of locations. So when a call is made that seanhalle@33: * adds to the DynArray, and inside the DynArray expands, it only updates seanhalle@33: * the original location with the new addr. Hence, the function will begin seanhalle@33: * overwriting memory past the end of the old chunk, because it still has seanhalle@33: * the pointer to the old chunk of locations. seanhalle@33: * seanhalle@33: *A dynamic array is accessed same as any other array. However, must use seanhalle@33: * dyn array calls, defined in here, in order to add or increase the size. seanhalle@33: * Must re-read the original array variable after any size-changing calls. seanhalle@33: *To pass a DynArray as a parameter to a function, can only pass the seanhalle@33: * DynArrayInfo, then inside the function, to read the addr of the first seanhalle@33: * location in the chunk of locations that is the array, do this: seanhalle@33: * "localArrayCopy = *(myDynArrayInfo->addrOfPtrToArray). After that, can seanhalle@33: * treat localArrayCopy as a normal array, as long as don't make any calls seanhalle@33: * that add or otherwise could increase the size of the array. If do make seanhalle@33: * such a call, then re-copy the array via the above. Can then use the seanhalle@33: * copy up until another add to the array. seanhalle@33: * seanhalle@33: */ seanhalle@33: typedef struct seanhalle@33: { seanhalle@33: void ***addrOfPtrToArray; //addr of array of ptrs == triple * seanhalle@33: int32 numInArray; seanhalle@33: int32 sizeOfArray; seanhalle@33: } seanhalle@33: PrivDynArrayInfo; seanhalle@33: seanhalle@33: seanhalle@33: typedef struct seanhalle@33: { seanhalle@33: void **ptrToLocations; //must be in first position seanhalle@33: int32 numInArray; seanhalle@33: int32 sizeOfArray; seanhalle@33: } seanhalle@33: PrivDynArray; seanhalle@33: seanhalle@33: seanhalle@33: PrivDynArrayInfo * seanhalle@33: makePrivDynArrayOfSize( int32 sizeOfArray ); seanhalle@33: seanhalle@33: PrivDynArrayInfo * seanhalle@33: makePrivDynArrayOfSize_Ext( int32 sizeOfArray ); seanhalle@33: seanhalle@33: int32 seanhalle@33: addToDynArray( void *value, void *array ); seanhalle@33: seanhalle@33: void seanhalle@33: makeHighestDynArrayIndexBe( void *array, int32 highestIndex ); seanhalle@33: seanhalle@33: void seanhalle@33: makeHighestDynArrayIndexBeAtLeast( void *array, int32 highestIndex); seanhalle@33: seanhalle@33: void seanhalle@33: increaseSizeOfDynArrayTo( void *array, int32 newSize ); seanhalle@33: seanhalle@33: typedef void (*FreeFnPtr) ( void * ); //fn has to cast void * to whatever seanhalle@33: seanhalle@33: void seanhalle@33: freeDynArrayDeep( void *array, FreeFnPtr freeFnPtr ); seanhalle@33: seanhalle@33: void seanhalle@33: freeDynArrayFlat( void *array ); seanhalle@33: seanhalle@33: seanhalle@33: typedef void (*DynArrayFnPtr) ( void * ); //fn has to cast void * seanhalle@33: seanhalle@33: void seanhalle@33: forAllInDynArrayDo( void *array, DynArrayFnPtr fnPtr ); seanhalle@33: seanhalle@33: #endif /* _DYNARRAY_H */ seanhalle@33: