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