Mercurial > cgi-bin > hgwebdir.cgi > VMS > C_Libraries > DynArray
diff DynArray3.h @ 33:3ed337b6a04f
creating version of dyn array that keeps info in prolog instead of 2nd var
| author | Sean Halle <seanhalle@yahoo.com> |
|---|---|
| date | Fri, 19 Jul 2013 12:27:43 -0700 |
| parents | |
| children |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/DynArray3.h Fri Jul 19 12:27:43 2013 -0700 1.3 @@ -0,0 +1,97 @@ 1.4 +/* 1.5 + * File: Vector.h 1.6 + * Author: Me 1.7 + * 1.8 + * Created on May 14, 2010, 3:08 PM 1.9 + */ 1.10 + 1.11 +#ifndef _DYNARRAY_H 1.12 +#define _DYNARRAY_H 1.13 + 1.14 +#include "PR_impl/PR_primitive_data_types.h" 1.15 +#include "PR_impl/Services_Offered_by_PR/Memory_Handling/vmalloc.h" 1.16 + 1.17 + 1.18 +/*WARNING: Passing a DynArray as a param is dangerous if add to the DynArray 1.19 + * inside the function called! After adding or other operation that might 1.20 + * change the size, must re-read the addr of the chunk of memory that is the 1.21 + * array, via the DynArrayInfo. 1.22 + *Here's why: An array variable is a location, either on the stack 1.23 + * or in a field of a struct, whose contents is an addr. That addr is of the 1.24 + * first location of a chunk of locations. The DynArray works by changing 1.25 + * the chunk of locations, then modifying the contents of the original 1.26 + * array variable. It overwrites the addr of the old chunk of locations 1.27 + * with the addr of the new chunk. 1.28 + *But when the array variable is passed as a parameter, such as 1.29 + * in this: "foo( myDynArray )", then there are now two locations that hold 1.30 + * the addr of the same chunk of locations. So when a call is made that 1.31 + * adds to the DynArray, and inside the DynArray expands, it only updates 1.32 + * the original location with the new addr. Hence, the function will begin 1.33 + * overwriting memory past the end of the old chunk, because it still has 1.34 + * the pointer to the old chunk of locations. 1.35 + * 1.36 + *A dynamic array is accessed same as any other array. However, must use 1.37 + * dyn array calls, defined in here, in order to add or increase the size. 1.38 + * Must re-read the original array variable after any size-changing calls. 1.39 + *To pass a DynArray as a parameter to a function, can only pass the 1.40 + * DynArrayInfo, then inside the function, to read the addr of the first 1.41 + * location in the chunk of locations that is the array, do this: 1.42 + * "localArrayCopy = *(myDynArrayInfo->addrOfPtrToArray). After that, can 1.43 + * treat localArrayCopy as a normal array, as long as don't make any calls 1.44 + * that add or otherwise could increase the size of the array. If do make 1.45 + * such a call, then re-copy the array via the above. Can then use the 1.46 + * copy up until another add to the array. 1.47 + * 1.48 + */ 1.49 +typedef struct 1.50 + { 1.51 + void ***addrOfPtrToArray; //addr of array of ptrs == triple * 1.52 + int32 numInArray; 1.53 + int32 sizeOfArray; 1.54 + } 1.55 +PrivDynArrayInfo; 1.56 + 1.57 + 1.58 +typedef struct 1.59 + { 1.60 + void **ptrToLocations; //must be in first position 1.61 + int32 numInArray; 1.62 + int32 sizeOfArray; 1.63 + } 1.64 +PrivDynArray; 1.65 + 1.66 + 1.67 +PrivDynArrayInfo * 1.68 +makePrivDynArrayOfSize( int32 sizeOfArray ); 1.69 + 1.70 +PrivDynArrayInfo * 1.71 +makePrivDynArrayOfSize_Ext( int32 sizeOfArray ); 1.72 + 1.73 +int32 1.74 +addToDynArray( void *value, void *array ); 1.75 + 1.76 +void 1.77 +makeHighestDynArrayIndexBe( void *array, int32 highestIndex ); 1.78 + 1.79 +void 1.80 +makeHighestDynArrayIndexBeAtLeast( void *array, int32 highestIndex); 1.81 + 1.82 +void 1.83 +increaseSizeOfDynArrayTo( void *array, int32 newSize ); 1.84 + 1.85 +typedef void (*FreeFnPtr) ( void * ); //fn has to cast void * to whatever 1.86 + 1.87 +void 1.88 +freeDynArrayDeep( void *array, FreeFnPtr freeFnPtr ); 1.89 + 1.90 +void 1.91 +freeDynArrayFlat( void *array ); 1.92 + 1.93 + 1.94 +typedef void (*DynArrayFnPtr) ( void * ); //fn has to cast void * 1.95 + 1.96 +void 1.97 +forAllInDynArrayDo( void *array, DynArrayFnPtr fnPtr ); 1.98 + 1.99 +#endif /* _DYNARRAY_H */ 1.100 +
