2002-09-26 18:11:06 +00:00
|
|
|
// The contents of this file are subject to the Mozilla Public License
|
|
|
|
// Version 1.0 (the "License"); you may not use this file except in
|
|
|
|
// compliance with the License. You may obtain a copy of the License at
|
|
|
|
// http://www.mozilla.org/MPL/
|
|
|
|
//
|
|
|
|
// Software distributed under the License is distributed on an "AS IS"
|
|
|
|
// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
|
|
|
|
// License for the specific language governing rights and limitations
|
|
|
|
// under the License.
|
|
|
|
//
|
|
|
|
// The Original Code is the Berkeley Open Infrastructure for Network Computing.
|
|
|
|
//
|
|
|
|
// The Initial Developer of the Original Code is the SETI@home project.
|
|
|
|
// Portions created by the SETI@home project are Copyright (C) 2002
|
|
|
|
// University of California at Berkeley. All Rights Reserved.
|
|
|
|
//
|
|
|
|
// Contributor(s):
|
|
|
|
//
|
|
|
|
|
|
|
|
// interfaces for accessing shared memory segments
|
|
|
|
|
2002-05-24 04:29:10 +00:00
|
|
|
#include <stdio.h>
|
2003-03-18 19:37:09 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <windows.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if HAVE_SYS_IPC_H
|
2002-05-24 04:29:10 +00:00
|
|
|
#include <sys/ipc.h>
|
2003-03-18 19:37:09 +00:00
|
|
|
#endif
|
2002-07-11 01:09:53 +00:00
|
|
|
#include <assert.h>
|
2002-05-24 04:29:10 +00:00
|
|
|
|
|
|
|
#include "shmem.h"
|
|
|
|
|
2003-03-18 19:37:09 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
|
|
|
|
HANDLE create_shmem(LPCTSTR seg_name, int size, void** pp) {
|
|
|
|
SECURITY_ATTRIBUTES security;
|
|
|
|
HANDLE hSharedMem;
|
|
|
|
LPVOID pMemPtr;
|
|
|
|
|
|
|
|
security.nLength = sizeof(security);
|
|
|
|
security.lpSecurityDescriptor = NULL;
|
|
|
|
security.bInheritHandle = TRUE;
|
|
|
|
|
|
|
|
hSharedMem = CreateFileMapping(INVALID_HANDLE_VALUE, &security,
|
|
|
|
PAGE_READWRITE, 0, size, seg_name);
|
|
|
|
if (!hSharedMem) return NULL;
|
|
|
|
|
|
|
|
pMemPtr = MapViewOfFile( hSharedMem, FILE_MAP_ALL_ACCESS, 0, 0, 0 );
|
|
|
|
*pp = pMemPtr;
|
|
|
|
|
|
|
|
return hSharedMem;
|
|
|
|
}
|
|
|
|
|
|
|
|
HANDLE attach_shmem(LPCTSTR seg_name, void** pp) {
|
|
|
|
HANDLE hSharedMem;
|
|
|
|
LPVOID pMemPtr;
|
|
|
|
|
|
|
|
hSharedMem = OpenFileMapping(FILE_MAP_ALL_ACCESS, FALSE, seg_name);
|
|
|
|
if (!hSharedMem) return NULL;
|
|
|
|
|
|
|
|
pMemPtr = MapViewOfFile( hSharedMem, FILE_MAP_ALL_ACCESS, 0, 0, 0 );
|
|
|
|
*pp = pMemPtr;
|
|
|
|
|
|
|
|
return hSharedMem;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int detach_shmem(HANDLE hSharedMem, void* p) {
|
|
|
|
UnmapViewOfFile(p);
|
|
|
|
CloseHandle(hSharedMem);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
2003-03-17 19:24:38 +00:00
|
|
|
int create_shmem(key_t key, int size, void** pp) {
|
2002-05-24 04:29:10 +00:00
|
|
|
int id;
|
2003-03-17 19:24:38 +00:00
|
|
|
char buf[256];
|
2002-07-11 01:09:53 +00:00
|
|
|
assert(pp!=NULL);
|
2002-05-24 04:29:10 +00:00
|
|
|
id = shmget(key, size, IPC_CREAT|0777);
|
|
|
|
if (id < 0) {
|
2003-03-17 19:24:38 +00:00
|
|
|
sprintf(buf, "create_shmem: shmget: key: %x size: %d", (unsigned int)key, size);
|
|
|
|
perror(buf);
|
2002-05-24 04:29:10 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return attach_shmem(key, pp);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
int destroy_shmem(key_t key){
|
|
|
|
struct shmid_ds buf;
|
|
|
|
int id, retval;
|
|
|
|
|
|
|
|
id = shmget(key, 0, 0);
|
|
|
|
if (id < 0) return 0; // assume it doesn't exist
|
|
|
|
retval = shmctl(id, IPC_STAT, &buf);
|
|
|
|
if (retval) return -1;
|
|
|
|
if (buf.shm_nattch > 0) {
|
|
|
|
fprintf(stderr,
|
|
|
|
"destroy_shmem: can't destroy segment; %d attachments\n",
|
|
|
|
buf.shm_nattch
|
|
|
|
);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
retval = shmctl(id, IPC_RMID, 0);
|
|
|
|
if (retval) {
|
|
|
|
fprintf(stderr, "destroy_shmem: remove failed %d\n", retval);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int attach_shmem(key_t key, void** pp){
|
|
|
|
void* p;
|
2003-03-17 19:24:38 +00:00
|
|
|
char buf[256];
|
2002-05-24 04:29:10 +00:00
|
|
|
int id;
|
2002-07-11 01:09:53 +00:00
|
|
|
assert(pp!=NULL);
|
2002-05-24 04:29:10 +00:00
|
|
|
id = shmget(key, 0, 0);
|
|
|
|
if (id < 0) {
|
2003-03-17 19:24:38 +00:00
|
|
|
sprintf(buf, "attach_shmem: shmget: key: %x mem_addr: %d", (unsigned int)key, (int)pp);
|
|
|
|
perror(buf);
|
2002-05-24 04:29:10 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
p = shmat(id, 0, 0);
|
|
|
|
if ((int)p == -1) {
|
2003-03-17 19:24:38 +00:00
|
|
|
sprintf(buf, "attach_shmem: shmat: key: %x mem_addr: %d", (unsigned int)key, (int)pp);
|
|
|
|
perror(buf);
|
2002-05-24 04:29:10 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
*pp = p;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-06-01 20:26:21 +00:00
|
|
|
int detach_shmem(void* p) {
|
2002-05-24 04:29:10 +00:00
|
|
|
int retval;
|
2002-07-11 01:09:53 +00:00
|
|
|
assert(p!=NULL);
|
2002-06-03 19:15:19 +00:00
|
|
|
retval = shmdt((char *)p);
|
2002-05-24 04:29:10 +00:00
|
|
|
if (retval) perror("detach_shmem: shmdt");
|
|
|
|
return retval;
|
|
|
|
}
|
2003-03-17 19:24:38 +00:00
|
|
|
|
|
|
|
int shmem_info(key_t key) {
|
|
|
|
int id;
|
|
|
|
struct shmid_ds buf;
|
|
|
|
char buf2[256];
|
|
|
|
|
|
|
|
id = shmget(key, 0, 0);
|
|
|
|
if (id < 0) {
|
|
|
|
sprintf(buf2, "shmem_info: shmget: key: %x", (unsigned int)key);
|
|
|
|
perror(buf2);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
shmctl(id, IPC_STAT, &buf);
|
|
|
|
fprintf( stderr, "id: %d, size: %d, nattach: %d\n", id, buf.shm_segsz, buf.shm_nattch );
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2003-03-18 19:37:09 +00:00
|
|
|
|
|
|
|
#endif
|