annotate prdynarray.h @ 1:14241f07f742

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