diff 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
line diff
     1.1 --- a/prdynarray.h	Tue Mar 04 12:45:08 2014 -0800
     1.2 +++ b/prdynarray.h	Fri May 09 11:19:53 2014 +0200
     1.3 @@ -1,91 +1,91 @@
     1.4 -/* 
     1.5 - * File:   Vector.h
     1.6 - * Author: Me
     1.7 - *
     1.8 - * Created on May 14, 2010, 3:08 PM
     1.9 - */
    1.10 -
    1.11 -#ifndef _DYNARRAY_H
    1.12 -#define	_DYNARRAY_H
    1.13 -
    1.14 -#include <PR__include/PR__primitive_data_types.h>
    1.15 -
    1.16 -
    1.17 -
    1.18 -/*WARNING: Passing a DynArray as a param is dangerous if add to the DynArray
    1.19 - * inside the function called!   After adding or other operation that might
    1.20 - * change the size, must re-read the addr of the chunk of memory that is the
    1.21 - * array, via the DynArrayInfo.
    1.22 - *Here's why: An array variable is a location, either on the stack
    1.23 - * or in a field of a struct, whose contents is an addr.  That addr is of the
    1.24 - * first location of a chunk of locations.  The DynArray works by changing
    1.25 - * the chunk of locations, then modifying the contents of the original 
    1.26 - * array variable.  It overwrites the addr of the old chunk of locations
    1.27 - * with the addr of the new chunk.
    1.28 - *But when the array variable is passed as a parameter, such as 
    1.29 - * in this: "foo( myDynArray )", then there are now two locations that hold
    1.30 - * the addr of the same chunk of locations.  So when a call is made that
    1.31 - * adds to the DynArray, and inside the DynArray expands, it only updates
    1.32 - * the original location with  the new addr.  Hence, the function will begin
    1.33 - * overwriting memory past the end of the old chunk, because it still has 
    1.34 - * the pointer to the old chunk of locations.
    1.35 - *
    1.36 - *A dynamic array is accessed same as any other array.  However, must use
    1.37 - * dyn array calls, defined in here, in order to add or increase the size.
    1.38 - * Must re-read the original array variable after any size-changing calls.
    1.39 - *To pass a DynArray as a parameter to a function, can only pass the 
    1.40 - * DynArrayInfo, then inside the function, to read the addr of the first 
    1.41 - * location in the chunk of locations that is the array, do this:
    1.42 - * "localArrayCopy = *(myDynArrayInfo->addrOfPtrToArray).  After that, can 
    1.43 - * treat localArrayCopy as a normal array, as long as don't make any calls
    1.44 - * that add or otherwise could increase the size of the array.  If do make
    1.45 - * such a call, then re-copy the array via the above.  Can then use the
    1.46 - * copy up until another add to the array.
    1.47 - * 
    1.48 - */
    1.49 -typedef struct
    1.50 - {
    1.51 -   void ***addrOfPtrToArray; //addr of var that is array of ptrs == triple *
    1.52 -   int32   numInArray;  //num entries added
    1.53 -   int32   sizeOfArray; //num elems alloc'd
    1.54 -   int32   sizeOfElem;  //num bytes in one elem of array -- used in 2nd version
    1.55 - }
    1.56 -PrivDynArrayInfo;
    1.57 -
    1.58 -PrivDynArrayInfo *
    1.59 -makePrivDynArrayInfoFrom( void ***addrOfPtrToArray, int32 sizeOfArray );
    1.60 -
    1.61 -PrivDynArrayInfo *
    1.62 -makePrivDynArrayOfSize( void ***addrOfPtrToArray, int32 sizeOfArray );
    1.63 -
    1.64 -PrivDynArrayInfo *
    1.65 -makePrivDynArrayOfSize_Ext( void ***addrOfPtrToArray, int32 sizeOfArray );
    1.66 -
    1.67 -int32
    1.68 -addToDynArray( void *value, PrivDynArrayInfo *info );
    1.69 -
    1.70 -void
    1.71 -makeHighestDynArrayIndexBe( PrivDynArrayInfo *info, int32 highestIndex );
    1.72 -
    1.73 -void
    1.74 -makeHighestDynArrayIndexBeAtLeast(PrivDynArrayInfo *info,int32 highestIndex);
    1.75 -
    1.76 -void
    1.77 -increaseSizeOfDynArrayTo( PrivDynArrayInfo *info, int32 newSize );
    1.78 -
    1.79 -typedef void  (*FreeFnPtr)  ( void * ); //fn has to cast void * to whatever
    1.80 -
    1.81 -void
    1.82 -freeDynArrayDeep( PrivDynArrayInfo *info, FreeFnPtr freeFnPtr );
    1.83 -
    1.84 -void
    1.85 -freeDynArrayFlat( PrivDynArrayInfo *info );
    1.86 -
    1.87 -
    1.88 -typedef void  (*DynArrayFnPtr)  ( void * );  //fn has to cast void *
    1.89 -
    1.90 -void
    1.91 -forAllInDynArrayDo( PrivDynArrayInfo *info, DynArrayFnPtr fnPtr );
    1.92 -
    1.93 -#endif	/* _DYNARRAY_H */
    1.94 -
    1.95 +/* 
    1.96 + * File:   Vector.h
    1.97 + * Author: Me
    1.98 + *
    1.99 + * Created on May 14, 2010, 3:08 PM
   1.100 + */
   1.101 +
   1.102 +#ifndef _DYNARRAY_H
   1.103 +#define	_DYNARRAY_H
   1.104 +
   1.105 +#include <PR__include/PR__primitive_data_types.h>
   1.106 +
   1.107 +
   1.108 +
   1.109 +/*WARNING: Passing a DynArray as a param is dangerous if add to the DynArray
   1.110 + * inside the function called!   After adding or other operation that might
   1.111 + * change the size, must re-read the addr of the chunk of memory that is the
   1.112 + * array, via the DynArrayInfo.
   1.113 + *Here's why: An array variable is a location, either on the stack
   1.114 + * or in a field of a struct, whose contents is an addr.  That addr is of the
   1.115 + * first location of a chunk of locations.  The DynArray works by changing
   1.116 + * the chunk of locations, then modifying the contents of the original 
   1.117 + * array variable.  It overwrites the addr of the old chunk of locations
   1.118 + * with the addr of the new chunk.
   1.119 + *But when the array variable is passed as a parameter, such as 
   1.120 + * in this: "foo( myDynArray )", then there are now two locations that hold
   1.121 + * the addr of the same chunk of locations.  So when a call is made that
   1.122 + * adds to the DynArray, and inside the DynArray expands, it only updates
   1.123 + * the original location with  the new addr.  Hence, the function will begin
   1.124 + * overwriting memory past the end of the old chunk, because it still has 
   1.125 + * the pointer to the old chunk of locations.
   1.126 + *
   1.127 + *A dynamic array is accessed same as any other array.  However, must use
   1.128 + * dyn array calls, defined in here, in order to add or increase the size.
   1.129 + * Must re-read the original array variable after any size-changing calls.
   1.130 + *To pass a DynArray as a parameter to a function, can only pass the 
   1.131 + * DynArrayInfo, then inside the function, to read the addr of the first 
   1.132 + * location in the chunk of locations that is the array, do this:
   1.133 + * "localArrayCopy = *(myDynArrayInfo->addrOfPtrToArray).  After that, can 
   1.134 + * treat localArrayCopy as a normal array, as long as don't make any calls
   1.135 + * that add or otherwise could increase the size of the array.  If do make
   1.136 + * such a call, then re-copy the array via the above.  Can then use the
   1.137 + * copy up until another add to the array.
   1.138 + * 
   1.139 + */
   1.140 +typedef struct
   1.141 + {
   1.142 +   void ***addrOfPtrToArray; //addr of var that is array of ptrs == triple *
   1.143 +   int32   numInArray;  //num entries added
   1.144 +   int32   sizeOfArray; //num elems alloc'd
   1.145 +   int32   sizeOfElem;  //num bytes in one elem of array -- used in 2nd version
   1.146 + }
   1.147 +PrivDynArrayInfo;
   1.148 +
   1.149 +PrivDynArrayInfo *
   1.150 +makePrivDynArrayInfoFrom( void ***addrOfPtrToArray, int32 sizeOfArray );
   1.151 +
   1.152 +PrivDynArrayInfo *
   1.153 +makePrivDynArrayOfSize( void ***addrOfPtrToArray, int32 sizeOfArray );
   1.154 +
   1.155 +PrivDynArrayInfo *
   1.156 +makePrivDynArrayOfSize_Ext( void ***addrOfPtrToArray, int32 sizeOfArray );
   1.157 +
   1.158 +int32
   1.159 +addToDynArray( void *value, PrivDynArrayInfo *info );
   1.160 +
   1.161 +void
   1.162 +makeHighestDynArrayIndexBe( PrivDynArrayInfo *info, int32 highestIndex );
   1.163 +
   1.164 +void
   1.165 +makeHighestDynArrayIndexBeAtLeast(PrivDynArrayInfo *info,int32 highestIndex);
   1.166 +
   1.167 +void
   1.168 +increaseSizeOfDynArrayTo( PrivDynArrayInfo *info, int32 newSize );
   1.169 +
   1.170 +typedef void  (*FreeFnPtr)  ( void * ); //fn has to cast void * to whatever
   1.171 +
   1.172 +void
   1.173 +freeDynArrayDeep( PrivDynArrayInfo *info, FreeFnPtr freeFnPtr );
   1.174 +
   1.175 +void
   1.176 +freeDynArrayFlat( PrivDynArrayInfo *info );
   1.177 +
   1.178 +
   1.179 +typedef void  (*DynArrayFnPtr)  ( void * );  //fn has to cast void *
   1.180 +
   1.181 +void
   1.182 +forAllInDynArrayDo( PrivDynArrayInfo *info, DynArrayFnPtr fnPtr );
   1.183 +
   1.184 +#endif	/* _DYNARRAY_H */
   1.185 +