annotate prdynarray.h @ 36:82b36ebdc648

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