Main Page   Class Hierarchy   Alphabetical List   Data Structures   File List   Data Fields   Globals  

shm.cc

Go to the documentation of this file.
00001 #include "rcs_defs.hh"
00002 
00003 #include "shm.hh"
00004 #include "_shm.h"               /* rcs_shm_open(), rcs_shm_close() */
00005 #include "rcs_prnt.hh"          // rcs_print_error()
00006 
00007 
00008 extern "C"
00009 {
00010 #ifndef NO_STDIO
00011 #include <stdio.h>              /* fprintf(), stderr */
00012 #endif
00013 #ifndef UNDER_CE
00014 #include <sys/types.h>          /* key_t */
00015 #include <stddef.h>             /* size_t */
00016 #include <errno.h>              // errno
00017 #endif
00018 #if !defined(VXWORKS) && !defined(WIN32)
00019 #include <sys/ipc.h>            // IPC_CREAT
00020 #endif
00021 }
00022 
00023 #ifdef WIN32
00024 #if defined(WIN32) && !defined(USE_OLD_WINSOCK)
00025 // Lame problem if windows.h is included before winsock2.h many redefined
00026 // compiler errors result.
00027 #include <winsock2.h>
00028 #endif
00029 #include <windows.h>            // GetLastError()
00030 #endif
00031 
00032 RCS_SHAREDMEM::RCS_SHAREDMEM (key_t key, size_t size, int oflag, int mode)
00033 {
00034   shm = NULL;
00035   addr = NULL;
00036   delete_totally = 0;
00037   create_errno = 0;
00038   created = 0;
00039 
00040   if (oflag & RCS_SHAREDMEM_CREATE)
00041     {
00042       /* create shared memory */
00043 #if defined(VXWORKS) || defined(_Windows)
00044       shm = rcs_shm_open (key, size, 1);
00045 #else
00046 #ifdef USE_POSIX_SHAREDMEM
00047       shm = rcs_shm_open (key, size, O_CREAT, mode);
00048 #else
00049       shm = rcs_shm_open (key, size, IPC_CREAT, mode);
00050 #endif
00051 #endif
00052       if (shm == NULL)
00053         {
00054           create_errno = errno;
00055           rcs_print_error ("can't create shared memory\n");
00056           return;
00057         }
00058     }
00059   else
00060     {
00061       /* attach to existing shared memory */
00062       shm = rcs_shm_open (key, size, 0);
00063       if (shm == NULL)
00064         {
00065           create_errno = errno;
00066           rcs_print_error
00067             ("can't attach to shared memory-- is master started?\n");
00068           return;
00069         }
00070     }
00071   create_errno = shm->create_errno;
00072   created = shm->created;
00073   /* duplicate the pointer, so users don't
00074      have to dig into shm->addr */
00075   addr = shm->addr;
00076 }
00077 
00078 RCS_SHAREDMEM::~RCS_SHAREDMEM ()
00079 {
00080   if (shm == NULL)
00081     {
00082       return;
00083     }
00084   else
00085     {
00086       if (delete_totally)
00087         {
00088           rcs_shm_delete (shm);
00089         }
00090       else
00091         {
00092           rcs_shm_close (shm);
00093         }
00094       shm = NULL;
00095     }
00096 }
00097 
00098 int
00099 RCS_SHAREDMEM::nattch ()
00100 {
00101   if (shm == NULL)
00102     {
00103       return -1;
00104     }
00105   else
00106     {
00107       return rcs_shm_nattch (shm);
00108     }
00109 }
00110 
00111 // This constructor declared private to prevent copying
00112 RCS_SHAREDMEM::RCS_SHAREDMEM (RCS_SHAREDMEM & shm)
00113 {
00114 }

Generated on Sun Dec 2 15:56:52 2001 for rcslib by doxygen1.2.11.1 written by Dimitri van Heesch, © 1997-2001