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