annotate prdynarray.h @ 4:10986666560d

Changes on copy on D:
author Sean Halle <seanhalle@yahoo.com>
date Sun, 15 Sep 2013 07:18:46 -0700
parents
children 14241f07f742
rev   line source
seanhalle@0 1 /*
seanhalle@0 2 * File: Vector.h
seanhalle@0 3 * Author: Me
seanhalle@0 4 *
seanhalle@0 5 * Created on May 14, 2010, 3:08 PM
seanhalle@0 6 */
seanhalle@0 7
seanhalle@0 8 #ifndef _DYNARRAY_H
seanhalle@0 9 #define _DYNARRAY_H
seanhalle@0 10
seanhalle@0 11 #include <PR__include/PR__primitive_data_types.h>
seanhalle@0 12
seanhalle@0 13
seanhalle@0 14
seanhalle@0 15 /*WARNING: Passing a DynArray as a param is dangerous if add to the DynArray
seanhalle@0 16 * inside the function called! After adding or other operation that might
seanhalle@0 17 * change the size, must re-read the addr of the chunk of memory that is the
seanhalle@0 18 * array, via the DynArrayInfo.
seanhalle@0 19 *Here's why: An array variable is a location, either on the stack
seanhalle@0 20 * or in a field of a struct, whose contents is an addr. That addr is of the
seanhalle@0 21 * first location of a chunk of locations. The DynArray works by changing
seanhalle@0 22 * the chunk of locations, then modifying the contents of the original
seanhalle@0 23 * array variable. It overwrites the addr of the old chunk of locations
seanhalle@0 24 * with the addr of the new chunk.
seanhalle@0 25 *But when the array variable is passed as a parameter, such as
seanhalle@0 26 * in this: "foo( myDynArray )", then there are now two locations that hold
seanhalle@0 27 * the addr of the same chunk of locations. So when a call is made that
seanhalle@0 28 * adds to the DynArray, and inside the DynArray expands, it only updates
seanhalle@0 29 * the original location with the new addr. Hence, the function will begin
seanhalle@0 30 * overwriting memory past the end of the old chunk, because it still has
seanhalle@0 31 * the pointer to the old chunk of locations.
seanhalle@0 32 *
seanhalle@0 33 *A dynamic array is accessed same as any other array. However, must use
seanhalle@0 34 * dyn array calls, defined in here, in order to add or increase the size.
seanhalle@0 35 * Must re-read the original array variable after any size-changing calls.
seanhalle@0 36 *To pass a DynArray as a parameter to a function, can only pass the
seanhalle@0 37 * DynArrayInfo, then inside the function, to read the addr of the first
seanhalle@0 38 * location in the chunk of locations that is the array, do this:
seanhalle@0 39 * "localArrayCopy = *(myDynArrayInfo->addrOfPtrToArray). After that, can
seanhalle@0 40 * treat localArrayCopy as a normal array, as long as don't make any calls
seanhalle@0 41 * that add or otherwise could increase the size of the array. If do make
seanhalle@0 42 * such a call, then re-copy the array via the above. Can then use the
seanhalle@0 43 * copy up until another add to the array.
seanhalle@0 44 *
seanhalle@0 45 */
seanhalle@0 46 typedef struct
seanhalle@0 47 {
seanhalle@0 48 void ***addrOfPtrToArray; //addr of var that is array of ptrs == triple *
seanhalle@0 49 int32 numInArray; //num entries added
seanhalle@0 50 int32 sizeOfArray; //num elems alloc'd
seanhalle@0 51 int32 sizeOfElem; //num bytes in one elem of array -- used in 2nd version
seanhalle@0 52 }
seanhalle@0 53 PrivDynArrayInfo;
seanhalle@0 54
seanhalle@0 55 PrivDynArrayInfo *
seanhalle@0 56 makePrivDynArrayInfoFrom( void ***addrOfPtrToArray, int32 sizeOfArray );
seanhalle@0 57
seanhalle@0 58 PrivDynArrayInfo *
seanhalle@0 59 makePrivDynArrayOfSize( void ***addrOfPtrToArray, int32 sizeOfArray );
seanhalle@0 60
seanhalle@0 61 PrivDynArrayInfo *
seanhalle@0 62 makePrivDynArrayOfSize_Ext( void ***addrOfPtrToArray, int32 sizeOfArray );
seanhalle@0 63
seanhalle@0 64 int32
seanhalle@0 65 addToDynArray( void *value, PrivDynArrayInfo *info );
seanhalle@0 66
seanhalle@0 67 void
seanhalle@0 68 makeHighestDynArrayIndexBe( PrivDynArrayInfo *info, int32 highestIndex );
seanhalle@0 69
seanhalle@0 70 void
seanhalle@0 71 makeHighestDynArrayIndexBeAtLeast(PrivDynArrayInfo *info,int32 highestIndex);
seanhalle@0 72
seanhalle@0 73 void
seanhalle@0 74 increaseSizeOfDynArrayTo( PrivDynArrayInfo *info, int32 newSize );
seanhalle@0 75
seanhalle@0 76 typedef void (*FreeFnPtr) ( void * ); //fn has to cast void * to whatever
seanhalle@0 77
seanhalle@0 78 void
seanhalle@0 79 freeDynArrayDeep( PrivDynArrayInfo *info, FreeFnPtr freeFnPtr );
seanhalle@0 80
seanhalle@0 81 void
seanhalle@0 82 freeDynArrayFlat( PrivDynArrayInfo *info );
seanhalle@0 83
seanhalle@0 84
seanhalle@0 85 typedef void (*DynArrayFnPtr) ( void * ); //fn has to cast void *
seanhalle@0 86
seanhalle@0 87 void
seanhalle@0 88 forAllInDynArrayDo( PrivDynArrayInfo *info, DynArrayFnPtr fnPtr );
seanhalle@0 89
seanhalle@0 90 #endif /* _DYNARRAY_H */
seanhalle@0 91