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