/* shared_memory.c */ #ifdef MAC #define IPC_PRIVATE 0 #define IPC_RMID 0 #define shmget(ipc_flag,size,flag) (int)NewHandleSys(size) #define shmctl(shmid,ipc_flag,flag) DisposHandle((Handle)shmid) #define shmat(shmid,tc,flag) (*((Handle)shmid)) void e04gcf(int *p1, int *p2, double *p3, double *p4, int *p5, int *p6, int *p7, int *p8,int *p9) {*p9=100;} #else #include #ifdef WIN32 #include #include #define e04gcf _stdcall E04GCF void e04gcf(int *p1, int *p2, double *p3, double *p4, int *p5, int *p6, int *p7, int *p8,int *p9) {*p9=100;} #else #if defined(_AIX) #include #endif #include /* /usr/include/sys/ipc.h */ #include /* /usr/include/sys/shm.h */ #endif #endif #include #ifdef underscore #define create_shared_memory create_shared_memory_ #define attach_memory attach_memory_ #define remove_memory remove_memory_ #define e04gcf e04gcf_ #endif #ifdef f2c #define create_shared_memory create_shared_memory__ #define attach_memory attach_memory__ #define remove_memory remove_memory__ #define e04gcf e04gcf_ #endif #ifdef NONAG void e04gcf(int *p1, int *p2, double *p3, double *p4, int *p5, int *p6, int *p7, int *p8,int *p9) {*p9=100;} #endif /***************************************************/ /* */ /* create_shared_memory(myshmid,size) */ /* */ /***************************************************/ /* create shared memory */ #ifdef WIN32 void _stdcall CREATE_SHARED_MEMORY(int *myshmid, int *size) { *myshmid = CreateFileMapping( (HANDLE) 0xFFFFFFFF, NULL, PAGE_READWRITE, 0, *size, "MAP"); if (*myshmid == NULL) { printf(" *** CreateFileMapping: %s\n", GetLastError()); } } #else void create_shared_memory(int *myshmid, int *size) { *myshmid=shmget(IPC_PRIVATE,*size,0666); if(*myshmid<=0) { printf(" *** shmget: errno = %d ***\n",errno); exit(); } } #endif /***************************************************/ /* */ /* open_shared_memory(myshmid) */ /* */ /***************************************************/ #ifdef WIN32 void _stdcall OPEN_SHARED_MEMORY(int *myshmid) { /* open shared memory */ *myshmid = OpenFileMapping( FILE_MAP_READ, TRUE, "MAP"); if (*myshmid == NULL) { printf(" *** OpenFileMapping: %s\n", GetLastError()); } } #endif /**************************************************************/ /* */ /* attach_memory() : attach shared memory to the process */ /* */ /**************************************************************/ #ifdef WIN32 void _stdcall ATTACH_MEMORY(int *myshmid, size_t *pmemory) { *pmemory = MapViewOfFile( *myshmid, FILE_MAP_WRITE, 0, 0, 0); if (*pmemory == NULL) { printf(" MapViewOfFile error\n"); } } #else void attach_memory(int *myshmid, size_t *pmemory) { if(*myshmid<=0) { printf("attach_memory: myshmid <0\n"); exit(0); } /* printf("attach_memory %d\n",*myshmid);*/ #ifdef MAC HLock((Handle)*myshmid); #endif *pmemory = (size_t)shmat(*myshmid,0,0); if(*pmemory==-1) { printf(" *** attach_memory *** errno = %d\n",errno); exit(); } } #endif /***************************************************/ /* */ /* Remove shared memory */ /* */ /***************************************************/ #ifdef WIN32 void _stdcall REMOVE_MEMORY(int *myshmid, size_t *pmemory) { UnmapViewOfFile(*pmemory); } #else void remove_memory(int *myshmid, size_t *pmemory) { /*** printf("remove_memory %d\n",*myshmid); ***/ /* detaches the shared memory segment */ #ifdef MAC HUnlock((Handle)*myshmid); #else shmdt((char *)*pmemory); #endif /* deletes the shared memory segment */ shmctl(*myshmid,IPC_RMID,0); if(*myshmid<=0) { printf("*** remove_memory: errno = %d\n",errno); exit(); } } #endif