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