00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061 #include <fcntl.h>
00062 #include <sys/mman.h>
00063 #include <stdio.h>
00064 #include <limits.h>
00065 #include <sys/types.h>
00066 #include <unistd.h>
00067 #include <alloca.h>
00068
00069 #define MAX_DP 100
00070 static int *count_ptr[MAX_DP];
00071 static char fn_save[MAX_DP][PATH_MAX];
00072 static int ndp = 0;
00073
00074 static char *cpath;
00075 static struct flock flk;
00076 static int ltype;
00077 extern int errno;
00078
00079 int DEBUG_MAP_LOCALDATA = 0;
00080
00081
00082
00083
00084
00085
00086
00087
00088 void
00089 f77_map_datapool_(const char* dpname, char *dp_address, int dp_length)
00090 {
00091 int length;
00092 char *getenv(), *dp_dir;
00093 int plen;
00094 int fd;
00095 int pagesize = sysconf(_SC_PAGESIZE);
00096 int plength, poff, nusers;
00097
00098 length = strlen(dpname);
00099 if (dp_dir = getenv("DATAPOOL_DIR")) {
00100 plen = strlen(dp_dir);
00101 cpath = (char*)alloca(plen + length + 2);
00102 strcpy( cpath, dp_dir );
00103 if (dp_dir[plen-1] != '/') cpath[plen++] = '/';
00104 }
00105 else {
00106 cpath = (char*) alloca(length + 10);
00107 strcpy( cpath, "/usr/tmp/" );
00108 plen = 9;
00109 }
00110 *(cpath+plen++) = 'D';
00111 *(cpath+plen++) = 'P';
00112 *(cpath+plen++) = '_';
00113 strncpy(cpath+plen, dpname, length + 1);
00114 cpath[plen+length] = '\0';
00115 strcpy(fn_save[ndp], cpath);
00116
00117 dp_address -= sizeof( int );
00118 poff = (long)dp_address % pagesize;
00119 dp_address -= poff;
00120 plength = dp_length + poff + sizeof( int );
00121
00122 if ((fd = open(cpath, O_RDWR | O_CREAT, 0666)) == -1)
00123 {
00124 fprintf(stderr,
00125 "map_localdata_: cannot open file %s for mmap\n", cpath );
00126 abort();
00127 }
00128
00129 if (DEBUG_MAP_LOCALDATA)
00130 printf("mmap address = %x , length = %d\n", dp_address, plength );
00131 if (mmap(dp_address, plength, PROT_READ | PROT_WRITE,
00132 MAP_AUTOGROW | MAP_SHARED | MAP_FIXED,
00133 fd, 0) == (caddr_t) -1)
00134 {
00135 fprintf(stderr,
00136 "map_datapool: trouble sharing %s at address %X\n",
00137 cpath, dp_address);
00138 perror("map_datapool (mmap)");
00139 fflush(stderr);
00140 close(fd);
00141 abort();
00142 }
00143 else
00144 {
00145 if (DEBUG_MAP_LOCALDATA)
00146 {
00147 fprintf(stderr, "map_datapool: sharing file %s at address %X\n",
00148 cpath, dp_address);
00149 fflush(stderr);
00150 }
00151 }
00152
00153 count_ptr[ndp] = (int *) dp_address;
00154
00155 while ((nusers = _test_and_set( count_ptr[ ndp ], -1 )) < 0)
00156 ;
00157 *count_ptr[ ndp++ ] = ++nusers;
00158 }
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168 void
00169 f77_map_datapool_pad_(const char* dpname, char *dp_address, int dp_length,
00170 int pagesize)
00171 {
00172 int length;
00173 char *getenv(), *dp_dir;
00174 int plen;
00175 int fd;
00176 int plength, poff, nusers;
00177
00178 length = strlen(dpname);
00179 if (dp_dir = getenv("DATAPOOL_DIR")) {
00180 plen = strlen(dp_dir);
00181 cpath = (char*)alloca(plen + length + 2);
00182 strcpy( cpath, dp_dir );
00183 if (dp_dir[plen-1] != '/') cpath[plen++] = '/';
00184 }
00185 else {
00186 cpath = (char*) alloca(length + 10);
00187 strcpy( cpath, "/usr/tmp/" );
00188 plen = 9;
00189 }
00190 *(cpath+plen++) = 'D';
00191 *(cpath+plen++) = 'P';
00192 *(cpath+plen++) = '_';
00193 strncpy(cpath+plen, dpname, length + 1);
00194 cpath[plen+length] = '\0';
00195 strcpy(fn_save[ndp], cpath);
00196
00197 dp_address -= sizeof( int );
00198 poff = (long)dp_address % pagesize;
00199 dp_address -= poff;
00200 plength = dp_length + poff + sizeof( int );
00201
00202 if ((fd = open(cpath, O_RDWR | O_CREAT, 0666)) == -1)
00203 {
00204 fprintf(stderr,
00205 "map_localdata_: cannot open file %s for mmap\n", cpath );
00206 abort();
00207 }
00208
00209 if (DEBUG_MAP_LOCALDATA)
00210 printf("mmap address = %x , length = %d\n", dp_address, plength );
00211 if (mmap(dp_address, plength, PROT_READ | PROT_WRITE,
00212 MAP_AUTOGROW | MAP_SHARED | MAP_FIXED,
00213 fd, 0) == (caddr_t) -1)
00214 {
00215 fprintf(stderr,
00216 "map_datapool: trouble sharing %s at address %X\n",
00217 cpath, dp_address);
00218 perror("map_datapool (mmap)");
00219 fflush(stderr);
00220 close(fd);
00221 abort();
00222 }
00223 else
00224 {
00225 if (DEBUG_MAP_LOCALDATA)
00226 {
00227 fprintf(stderr, "map_datapool: sharing file %s at address %X\n",
00228 cpath, dp_address);
00229 fflush(stderr);
00230 }
00231 }
00232
00233 count_ptr[ndp] = (int *) dp_address;
00234
00235 while ((nusers = _test_and_set( count_ptr[ ndp ], -1 )) < 0)
00236 ;
00237 *count_ptr[ ndp++ ] = ++nusers;
00238 }
00239
00240
00241 void
00242 f77_unmap_datapool_()
00243 {
00244
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254 int nusers;
00255
00256 ndp--;
00257 while ((nusers = _test_and_set( count_ptr[ ndp ], -1 )) < 0)
00258 ;
00259 *count_ptr[ ndp ] = --nusers;
00260
00261
00262 if (!nusers) {
00263 unlink( fn_save[ndp] );
00264 }
00265 }