changeset 22:ffdffedd579e pure_C

made pure C brch
author Me@portablequad
date Mon, 13 Feb 2012 10:29:40 -0800
parents b6d3365013c6
children ddda19b59374
files DynArray.c DynArray.h __brch__DEPRECATED_README __brch__pure_C
diffstat 4 files changed, 281 insertions(+), 8 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/DynArray.c	Mon Feb 13 10:29:40 2012 -0800
     1.3 @@ -0,0 +1,214 @@
     1.4 +/*
     1.5 + * Copyright 2010  OpenSourceCodeStewardshipFoundation
     1.6 + *
     1.7 + * Licensed under BSD
     1.8 + */
     1.9 +
    1.10 +
    1.11 +
    1.12 +#include <stdio.h>
    1.13 +
    1.14 +#include "DynArray.h"
    1.15 +#include "../vmalloc.h"
    1.16 +
    1.17 +//== declarations
    1.18 +void
    1.19 +increaseSizeOfDynArrayTo_Ext( PrivDynArrayInfo *info, int32 newSize );
    1.20 +//==
    1.21 +
    1.22 +PrivDynArrayInfo *
    1.23 +makePrivDynArrayInfoFrom( void ***addrOfPtrToArray, int32 sizeOfArray )
    1.24 + { PrivDynArrayInfo *info;
    1.25 +
    1.26 +   info = malloc( sizeof(PrivDynArrayInfo) );
    1.27 +
    1.28 +   info->addrOfPtrToArray = addrOfPtrToArray;
    1.29 +   info->sizeOfArray      = sizeOfArray;
    1.30 +   info->numInArray       = 0;
    1.31 +   return info;
    1.32 + }
    1.33 +
    1.34 +PrivDynArrayInfo *
    1.35 +makePrivDynArrayOfSize( void ***addrOfPtrToArray, int32 sizeOfArray )
    1.36 + { PrivDynArrayInfo *info;
    1.37 +
    1.38 +   info = malloc( sizeof(PrivDynArrayInfo) );
    1.39 +
    1.40 +   info->addrOfPtrToArray = addrOfPtrToArray;
    1.41 +
    1.42 +   *(addrOfPtrToArray)    = malloc( sizeOfArray * sizeof(void *) );
    1.43 +   info->sizeOfArray      = sizeOfArray;
    1.44 +   info->numInArray       = 0;
    1.45 +   return info;
    1.46 + }
    1.47 +
    1.48 +PrivDynArrayInfo *
    1.49 +makePrivDynArrayOfSize_Ext( void ***addrOfPtrToArray, int32 sizeOfArray )
    1.50 + { PrivDynArrayInfo *info;
    1.51 +
    1.52 +   info = malloc( sizeof(PrivDynArrayInfo) );
    1.53 +
    1.54 +   info->addrOfPtrToArray = addrOfPtrToArray;
    1.55 +
    1.56 +   *(addrOfPtrToArray)    = malloc( sizeOfArray * sizeof(void *) );
    1.57 +   info->sizeOfArray      = sizeOfArray;
    1.58 +   info->numInArray       = 0;
    1.59 + }
    1.60 +
    1.61 +
    1.62 +/*A dynamic array is same as any other array, but add a DynArrayInfo next
    1.63 + * to it.  Accesses and updates of array indexes are done normally, it's
    1.64 + * only when add a new element into array that use the extra info.
    1.65 + * An add can cause the pointer to the normal array to change..  so must
    1.66 + * be protected to single VP at a time.
    1.67 + *
    1.68 + *Only need to use this Fn when need a new index, higher than any previous
    1.69 + */
    1.70 +int32
    1.71 +addToDynArray( void *value, PrivDynArrayInfo *info )
    1.72 + { int32 numInArray, sizeOfArray;
    1.73 +   void **array;
    1.74 +
    1.75 +   numInArray = info->numInArray;
    1.76 +   sizeOfArray    = info->sizeOfArray;
    1.77 +
    1.78 +   if( numInArray >= sizeOfArray )
    1.79 +    {
    1.80 +      increaseSizeOfDynArrayTo( info, sizeOfArray * 2 );
    1.81 +    }
    1.82 +
    1.83 +   array = *(info->addrOfPtrToArray);
    1.84 +   array[ numInArray ] = value;
    1.85 +   info->numInArray++;
    1.86 +
    1.87 +   return numInArray; //pre-incr value is the index put value into
    1.88 + }
    1.89 +int32
    1.90 +addToDynArray_Ext( void *value, PrivDynArrayInfo *info )
    1.91 + { int32 numInArray, sizeOfArray;
    1.92 +   void **array;
    1.93 +
    1.94 +   numInArray = info->numInArray;
    1.95 +   sizeOfArray    = info->sizeOfArray;
    1.96 +
    1.97 +   if( numInArray >= sizeOfArray )
    1.98 +    {
    1.99 +      increaseSizeOfDynArrayTo_Ext( info, sizeOfArray * 2 );
   1.100 +    }
   1.101 +
   1.102 +   array = *(info->addrOfPtrToArray);
   1.103 +   array[ numInArray ] = value;
   1.104 +   info->numInArray++;
   1.105 +
   1.106 +   return numInArray; //pre-incr value is the index put value into
   1.107 + }
   1.108 +
   1.109 +
   1.110 +/*Use this when know how many things going to add in -- then can do this
   1.111 + * once and use as normal array afterwards.  If later add another chunk,
   1.112 + * do this again.  Note, this makes new size be just big enough to hold
   1.113 + * highest index, so will do a linear number of copies if use only this.
   1.114 + *To cut down on number of copies, can use the increaseSizeTo Fn to
   1.115 + * exponentially increase size..
   1.116 + */
   1.117 +void
   1.118 +makeHighestDynArrayIndexBe( PrivDynArrayInfo *info, int32 highestIndex )
   1.119 + {
   1.120 +   if( info->sizeOfArray <= highestIndex )
   1.121 +    {
   1.122 +      increaseSizeOfDynArrayTo( info, highestIndex + 1 );
   1.123 +    }
   1.124 +   info->numInArray = highestIndex + 1;
   1.125 + }
   1.126 +
   1.127 +void
   1.128 +makeHighestDynArrayIndexBeAtLeast(PrivDynArrayInfo *info, int32 index)
   1.129 + {
   1.130 +   if( index < info->numInArray ) return;
   1.131 +   else makeHighestDynArrayIndexBe( info, index );
   1.132 + }
   1.133 +
   1.134 +
   1.135 +/*Only use this if certain new size is bigger than current size
   1.136 + */
   1.137 +void
   1.138 +increaseSizeOfDynArrayTo( PrivDynArrayInfo *info, int32 newSize )
   1.139 + { int32 oldSizeOfArray, i;
   1.140 +   void **newArray, **oldArray;
   1.141 +
   1.142 +   oldSizeOfArray   = info->sizeOfArray;
   1.143 +   if( newSize <= oldSizeOfArray ) return;
   1.144 +
   1.145 +   oldArray         = *(info->addrOfPtrToArray);
   1.146 +   newArray         = malloc( newSize * sizeof(void *) );
   1.147 +
   1.148 +   for( i = 0; i < oldSizeOfArray; i++ )
   1.149 +    {
   1.150 +      newArray[i] = oldArray[i];
   1.151 +    }
   1.152 +   *(info->addrOfPtrToArray) = newArray; //change location of array-ptr
   1.153 +   info->sizeOfArray = newSize;
   1.154 +
   1.155 +   free( oldArray );
   1.156 + }
   1.157 +
   1.158 +/*Can't mix malloc locations with external malloc locations -- so use
   1.159 + * this version inside VMS, which will perform normal malloc in the core
   1.160 + * loop -- hopefully avoiding the annoying system-stack bugs..
   1.161 + */
   1.162 +void
   1.163 +increaseSizeOfDynArrayTo_Ext( PrivDynArrayInfo *info, int32 newSize )
   1.164 + { int32 oldSizeOfArray, i;
   1.165 +   void **newArray, **oldArray;
   1.166 +
   1.167 +   oldSizeOfArray   = info->sizeOfArray;
   1.168 +   if( newSize <= oldSizeOfArray ) return;
   1.169 +
   1.170 +   oldArray         = *(info->addrOfPtrToArray);
   1.171 +   newArray         = malloc( newSize * sizeof(void *) );
   1.172 +
   1.173 +   for( i = 0; i < oldSizeOfArray; i++ )
   1.174 +    {
   1.175 +      newArray[i] = oldArray[i];
   1.176 +    }
   1.177 +   *(info->addrOfPtrToArray) = newArray; //change location of array-ptr
   1.178 +   info->sizeOfArray = newSize;
   1.179 +
   1.180 +   free( oldArray );
   1.181 + }
   1.182 +
   1.183 +
   1.184 +/* Frees the array, plus the info
   1.185 + */
   1.186 +void
   1.187 +freeDynArrayDeep( PrivDynArrayInfo *info, FreeFnPtr freeFnPtr )
   1.188 + {
   1.189 +   forAllInDynArrayDo( info, freeFnPtr );
   1.190 +   free( *(info->addrOfPtrToArray) );
   1.191 +   free( info );
   1.192 + }
   1.193 +
   1.194 +/* Only frees the info
   1.195 + */
   1.196 +void
   1.197 +freeDynArrayFlat( PrivDynArrayInfo *info )
   1.198 + {
   1.199 +   free( info );
   1.200 + }
   1.201 +
   1.202 +
   1.203 +/*The function has a fixed prototype: takes a void * returns void
   1.204 + * So, the function has to internally cast void * to whatever data struc..
   1.205 + */
   1.206 +void
   1.207 +forAllInDynArrayDo( PrivDynArrayInfo *info, DynArrayFnPtr fnPtr )
   1.208 + { int32 idx;
   1.209 +   void **array;
   1.210 +
   1.211 +   array = *(info->addrOfPtrToArray);
   1.212 +   for( idx = 0; idx < info->numInArray; idx++ )
   1.213 +    {
   1.214 +      (*fnPtr)(array[idx]);
   1.215 +    }
   1.216 + }
   1.217 +
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/DynArray.h	Mon Feb 13 10:29:40 2012 -0800
     2.3 @@ -0,0 +1,61 @@
     2.4 +/* 
     2.5 + * File:   Vector.h
     2.6 + * Author: Me
     2.7 + *
     2.8 + * Created on May 14, 2010, 3:08 PM
     2.9 + */
    2.10 +
    2.11 +#ifndef _DYNARRAY_H
    2.12 +#define	_DYNARRAY_H
    2.13 +
    2.14 +
    2.15 +   //A dynamic array is same as any other array, but add a DynArrayInfo next
    2.16 +   // to it.  Accesses and updates of array indexes are done normally, it's
    2.17 +   // only when add a new element into array that use the extra info.
    2.18 +   // An add can cause the pointer to the normal array to change..  so must
    2.19 +   // be protected to single VP at a time.
    2.20 +typedef struct
    2.21 + {
    2.22 +   void ***addrOfPtrToArray; //addr of array of ptrs == triple *
    2.23 +   int32   numInArray;
    2.24 +   int32   sizeOfArray;
    2.25 + }
    2.26 +PrivDynArrayInfo;
    2.27 +
    2.28 +PrivDynArrayInfo *
    2.29 +makePrivDynArrayInfoFrom( void ***addrOfPtrToArray, int32 sizeOfArray );
    2.30 +
    2.31 +PrivDynArrayInfo *
    2.32 +makePrivDynArrayOfSize( void ***addrOfPtrToArray, int32 sizeOfArray );
    2.33 +
    2.34 +PrivDynArrayInfo *
    2.35 +makePrivDynArrayOfSize_Ext( void ***addrOfPtrToArray, int32 sizeOfArray );
    2.36 +
    2.37 +int32
    2.38 +addToDynArray( void *value, PrivDynArrayInfo *info );
    2.39 +
    2.40 +void
    2.41 +makeHighestDynArrayIndexBe( PrivDynArrayInfo *info, int32 highestIndex );
    2.42 +
    2.43 +void
    2.44 +makeHighestDynArrayIndexBeAtLeast(PrivDynArrayInfo *info,int32 highestIndex);
    2.45 +
    2.46 +void
    2.47 +increaseSizeOfDynArrayTo( PrivDynArrayInfo *info, int32 newSize );
    2.48 +
    2.49 +typedef void  (*FreeFnPtr)  ( void * ); //fn has to cast void * to whatever
    2.50 +
    2.51 +void
    2.52 +freeDynArrayDeep( PrivDynArrayInfo *info, FreeFnPtr freeFnPtr );
    2.53 +
    2.54 +void
    2.55 +freeDynArrayFlat( PrivDynArrayInfo *info );
    2.56 +
    2.57 +
    2.58 +typedef void  (*DynArrayFnPtr)  ( void * );  //fn has to cast void *
    2.59 +
    2.60 +void
    2.61 +forAllInDynArrayDo( PrivDynArrayInfo *info, DynArrayFnPtr fnPtr );
    2.62 +
    2.63 +#endif	/* _DYNARRAY_H */
    2.64 +
     3.1 --- a/__brch__DEPRECATED_README	Mon Feb 13 10:24:50 2012 -0800
     3.2 +++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
     3.3 @@ -1,8 +0,0 @@
     3.4 -
     3.5 -There are two versions of the library -- one for pure C use, the other for use inside VMS or within applications written in a VMS-based language (IE, inside a top-level function or a call descendant of a top-level function) -- but only when the VMS is the "MC_shared" version.
     3.6 -
     3.7 -The reason is that VMS that uses shared memory on multicores moves the SlaveVPs around among cores.  But, the libC and glibC malloc stores info at the top of the stack (a "clever" hack), for a speed improvement.  So, when VMS manipulates the stack pointer, and/or moves Slaves to different cores, the "free" seg faults (that was FUN to figure out ; )  So, this version of VMS implements its own malloc.  
     3.8 -
     3.9 -It is anticipated that the MC_split version, where each core has separate data, and messages are sent between cores, can handle malloc and free to use the glibC version.
    3.10 -
    3.11 -For now, update to the version of the library you wish to use..
     4.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     4.2 +++ b/__brch__pure_C	Mon Feb 13 10:29:40 2012 -0800
     4.3 @@ -0,0 +1,6 @@
     4.4 +
     4.5 +This branch is for use in pure C code (*not* inside VMS-based language application code)
     4.6 +
     4.7 +There are two versions of the library -- one for pure C use, the other for use inside VMS or within applications written in a VMS-based language (IE, inside a top-level function or a call descendant of a top-level function) -- but only when the VMS is the "MC_shared" version.
     4.8 +
     4.9 +The reason is that VMS that uses shared memory on multicores moves the SlaveVPs around among cores.  But, the libC and glibC malloc stores info at the top of the stack (a "clever" hack), for a speed improvement.  So, when VMS manipulates the stack pointer, and/or moves Slaves to different cores, the "free" seg faults (that was FUN to figure out ; )  So, this version of VMS implements its own malloc. 
    4.10 \ No newline at end of file