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