annotate DynArray.h @ 35:59eaf81a9cc7

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