annotate prdynarray.h @ 20:f3cb11baf791

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