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