view prdynarray.h @ 22:d8f96a3bbea4

Updated with newer PRMalloc header and some changes to (...)types.h
author Philipe Louchtch - de Raadt
date Sat, 12 Jul 2014 20:09:42 +0200
parents c3829f630c2f
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__include/PR__primitive_data_types.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 var that is array of ptrs == triple *
49 int32 numInArray; //num entries added
50 int32 sizeOfArray; //num elems alloc'd
51 int32 sizeOfElem; //num bytes in one elem of array -- used in 2nd version
52 }
53 PrivDynArrayInfo;
55 PrivDynArrayInfo *
56 makePrivDynArrayInfoFrom( void ***addrOfPtrToArray, int32 sizeOfArray );
58 PrivDynArrayInfo *
59 makePrivDynArrayOfSize( void ***addrOfPtrToArray, int32 sizeOfArray );
61 PrivDynArrayInfo *
62 makePrivDynArrayOfSize_Ext( void ***addrOfPtrToArray, int32 sizeOfArray );
64 int32
65 addToDynArray( void *value, PrivDynArrayInfo *info );
67 void
68 makeHighestDynArrayIndexBe( PrivDynArrayInfo *info, int32 highestIndex );
70 void
71 makeHighestDynArrayIndexBeAtLeast(PrivDynArrayInfo *info,int32 highestIndex);
73 void
74 increaseSizeOfDynArrayTo( PrivDynArrayInfo *info, int32 newSize );
76 typedef void (*FreeFnPtr) ( void * ); //fn has to cast void * to whatever
78 void
79 freeDynArrayDeep( PrivDynArrayInfo *info, FreeFnPtr freeFnPtr );
81 void
82 freeDynArrayFlat( PrivDynArrayInfo *info );
85 typedef void (*DynArrayFnPtr) ( void * ); //fn has to cast void *
87 void
88 forAllInDynArrayDo( PrivDynArrayInfo *info, DynArrayFnPtr fnPtr );
90 #endif /* _DYNARRAY_H */