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