This is the mail archive of the cygwin@sourceware.cygnus.com mailing list for the Cygwin project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

sys V shm with _very_ limited functionality



Well, As promised, here are the files with _very_ limited functionality. 
This is my first trip into WIN32 programming (mostly unix and OS/2) so
please forgive any poor WIN32 programming practices. 

--Ron
      o           Work to live; \   vaniwaar@ca.metsci.com
     /\            Live to bike; \   http://www-math.cudenver.edu/~rvan
   _`\ `_<===       Bike to work! \  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
__(_)/_(_)___.-._                  \   Note the new addresses!

#include "SharedMemoryCalls.H"


#ifdef __CYGWIN32__

struct _SharedMemoryPointers{
  HANDLE h;
  HANDLE ProcessHandle;
  char * ptr;
  struct shmid_ds Shmid_DS;
} SharedMemoryHandle;


SharedMemoryHandle * ShmemHandles = NULL;
int totShmemHandles = 0;
int totAllocatedHandles = 0;

HANDLE ParentProcessHandle;

#endif

int SHMGET(int key, int size, int shmflg)
{

#ifdef __CYGWIN32__

  HANDLE retVal;
  HANDLE ProcHandle = GetCurrentProcess();
  SECURITY_ATTRIBUTES SecAtt;
  SecAtt.nLength =sizeof(SECURITY_ATTRIBUTES);
  SecAtt.lpSecurityDescriptor = NULL;
  SecAtt.bInheritHandle = TRUE;
  int i;
  int pos = totShmemHandles;
  int found = 0;
  int NewKey = 1;

  if (key < totAllocatedHandles){
    if (ShmemHandles[key] != -1){
      NewKey = 1;
    }
  }
  else {
    NewKey = 1;
  }

  if ((shmflg & IPC_PRIVATE) || ((shmflg & IPC_CREAT) &&
				 NewKey && !(shmflg & IPC_PRIVATE))){
    retVal =  CreateFileMapping( (HANDLE) 0xFFFFFFFF, &SecAtt, PAGE_READWRITE,
				 0, size, NULL);
    
    if (totAllocatedHandles == totShmemHandles){
      ShmemHandles = realloc(ShmemHandles,
			     sizeof(SharedMemoryHandle)*
			     (totShmemHandles+10));
      totShmemHandles;
      totFreeHandles += 10;
      for (i=0; i < 10; ++i){
	ShmemHandles[i+totShmemHandles].h = -1;
      }
    }
    for(i=0; i < totShmemHandles; ++i){
      if (ShmemHandles[i].h == retVal){
	found = 1;
	pos = i;
      }
    }
    ShmemHandles[pos].h = retval;
    ShmemHandles[pos].ProcessHandle = ProcHandle;
    ShmemHandles[pos].Shmid_DS.shm_segsz = size;
    ShmemHandles[pos].Shmid_DS.shm_cpid = getpid();

    ++totShmemHandles;
    
    return pos;
  }
  else{
    printf("You cannot yet specify what key you want in WIN32 \n");
  }

#else
  return shmget(key, size, shmflg);
#endif
}


char * SHMAT(int handle, char * shmaddr, int shmflg)
{
#ifdef __CYGWIN32__
  HANDLE ChildMemoryHandle;

  if (handle >= totShmemHandles){
    if (ShmemHandles[handle].h != -1){
      DuplicateHandle(ShmemHandles[handle].ProcessHandle,
		      ShmemHandles[handle].h,
		      GetCurrentProcess(),
		      &ChildMemoryHandle,
		      0,
		      TRUE,
		      DUPLICATE_SAME_ACCESS);
      ShmemHandles[handle].ptr = 
	(char *)MapViewOfFileEx(ChildMapping,
				FILE_MAP_ALL_ACCESS,
				0,
				0,
				shmaddr);
      ++ShmemHandles[pos].Shmid_DS.shm_nattch;
      return ShmemHandles[handle].ptr;
    }
  }
#else
  return (char *)shmat(handle, shmaddr, shmflg);
#endif

}


int SHMDT(char * ShmPtr)
{
#ifdef __CYGWIN32__
  int found = 0;
  int i;
  int pos;

  if (handle >= totShmemHandles){
    if (ShmemHandles[handle].h != -1){
      for(i=0; i < totShmemHandles; ++i){
	if (ShmemHandles[i].ptr == ShmPtr){
	  found = 1;
	  pos = i;
	}
      }
      if (found){
	CloseHandle(ShmemHandles[pos].h);
      }
      else{
	return -1;
      }
    }
  }
  return 0;

#else
  shmdt(ShmPtr);
#endif
}


int SHMCTL(int handle, int action, struct shmid_ds * buf)
{
#ifdef __CYGWIN32__
  int found = 0;
  int i;
  int pos;

  if (handle >= totShmemHandles){
    if (ShmemHandles[handle].h != -1){
      for(i=0; i < totShmemHandles; ++i){
	if (ShmemHandles[i].h == handle){
	  found = 1;
	  pos = i;
	}
      }
      if (found){
	switch(action){
	case IPC_STAT:
	  errno = EINVAL;
	  printf("IPC_STAT not implemented yet\n");
	  break;
	case IPC_SET:
	  errno = EINVAL;
	  printf("IPC_SET not implemented yet\n");
	  break;
	case IPC_RMID:
	  CloseHandle(ShmemHandles[pos].h);
	  UnmapViewOfFile(ShmemHandles[pos].h);
	  ShmemHandles[pos].h = -1;
	  break;
	}
      }
      else{
	errno = EINVAL;
      }
    }
  }

#else
  shmctl(handle, action, buf);
#endif
}




#ifndef SHARED_MEMORY_CALLS_H
#define SHARED_MEMORY_CALLS_H

#ifdef __CYGWIN32__
#include <windows.h>

struct shmid_ds {
  struct    ipc_perm shm_perm;  /* operation perms */
  int  shm_segsz;               /* size of segment (bytes) */
  time_t    shm_atime;          /* last attach time */
  time_t    shm_dtime;          /* last detach time */
  time_t    shm_ctime;          /* last change time */
  unsigned short shm_cpid;      /* pid of creator */
  unsigned short shm_lpid;      /* pid of last operator */
  short     shm_nattch;         /* no. of current attaches */
                                /* the following are private */
  unsigned short   shm_npages;  /* size of segment (pages) */
  unsigned long   *shm_pages;
  struct shm_desc *attaches;    /* descriptors for attaches */
};

/* permission flag for shmget */
#define SHM_R		0400	/* or S_IRUGO from <linux/stat.h> */
#define SHM_W		0200	/* or S_IWUGO from <linux/stat.h> */

/* mode for attach */
#define	SHM_RDONLY	010000	/* read-only access */
#define	SHM_RND		020000	/* round attach address to SHMLBA boundary */
#define	SHM_REMAP	040000	/* take-over region on attach */

/* super user shmctl commands */
#define SHM_LOCK 	11
#define SHM_UNLOCK 	12

struct	shminfo {
    int shmmax;	
    int shmmin;	
    int shmmni;	
    int shmseg;	
    int shmall;	
};

struct ipc_perm
{
  key_t  key;
  ushort uid;   /* owner euid and egid */
  ushort gid;
  ushort cuid;  /* creator euid and egid */
  ushort cgid;
  ushort mode;  /* access modes see mode flags below */
  ushort seq;   /* sequence number */
};


/* resource get request flags */
#define IPC_CREAT  00001000   /* create if key is nonexistent */
#define IPC_EXCL   00002000   /* fail if key exists */
#define IPC_NOWAIT 00004000   /* return error on wait */


/* 
 * Control commands used with semctl, msgctl and shmctl 
 * see also specific commands in sem.h, msg.h and shm.h
 */
#define IPC_RMID 0     /* remove resource */
#define IPC_SET  1     /* set ipc_perm options */
#define IPC_STAT 2     /* get ipc_perm options */
#define IPC_INFO 3     /* see ipcs */


#else
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#endif



int SHMGET(int key, int size, int shmflg);
char * SHMAT(int handle, char * shmaddr, int shmflg);
int SHMCTL(int handle, int action, struct shmid_ds * buf);
int shmdt(char * shmaddr);



#endif

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]