EpcTools
An event based multi-threaded C++ development framework.
eshmem.h
Go to the documentation of this file.
1 /*
2 * Copyright (c) 2009-2019 Brian Waters
3 * Copyright (c) 2019 Sprint
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 
18 #ifndef __eshmem_h_included
19 #define __eshmem_h_included
20 
23 
24 #include "esynch.h"
25 
27 DECLARE_ERROR(ESharedMemoryError_NotInitialized);
28 DECLARE_ERROR_ADVANCED4(ESharedMemoryError_UnableToCreate);
29 DECLARE_ERROR_ADVANCED(ESharedMemoryError_UnableToMap);
30 
31 class ESharedMemoryError_UnableToCreateKeyFile : public EError
32 {
33 public:
34  ESharedMemoryError_UnableToCreateKeyFile(cpStr pszFile);
35  virtual const cpStr Name() const { return "ESharedMemoryError_UnableToCreateKeyFile"; }
36 };
38 
41 
44 {
45 public:
47  ESharedMemory();
52  ESharedMemory(cpStr file, Int id, Int size);
55 
61  Void init(cpStr file, Int id, Int size);
62 
65  pVoid getDataPtr()
66  {
67  return m_pData;
68  }
69 
71  virtual Void onDestroy()
72  {
73  }
74 
76  Int getUsageCount();
77 
78 private:
79  typedef struct
80  {
81  Int s_usageCnt;
82  EMutexPrivate s_mutex;
83  } eshmemctrl_t;
84 
85  EMutexPrivate &getMutex()
86  {
87  return m_pCtrl->s_mutex;
88  }
89 
90  Char m_szShMem[EPC_FILENAME_MAX + 1];
91  Char m_szMutex[EPC_FILENAME_MAX + 1];
92  pVoid m_pShMem;
93  pVoid m_pData;
94  eshmemctrl_t *m_pCtrl;
95 
96  Int m_shmid;
97  key_t m_key;
98 };
99 
100 #endif // #define __eshmem_h_included
ESharedMemory::~ESharedMemory
~ESharedMemory()
Class destructor.
Definition: eshmem.cpp:77
DECLARE_ERROR
#define DECLARE_ERROR(__e__)
Declares exception class derived from EError with no constructor parameters.
Definition: eerror.h:53
ESharedMemory::ESharedMemory
ESharedMemory()
Default constructor.
Definition: eshmem.cpp:60
EMutexPrivate
A private mutex (the mutex data is allocated from either the heap or stack).
Definition: esynch.h:175
esynch.h
Contains definitions for synchronization objects.
EError::Name
virtual const cpStr Name() const
Returns the name of this object.
Definition: eerror.h:189
DECLARE_ERROR_ADVANCED4
#define DECLARE_ERROR_ADVANCED4(__e__)
Declares exception class derived from EError with an const char* as a constructor parameter and devel...
Definition: eerror.h:83
ESharedMemory::getDataPtr
pVoid getDataPtr()
Retrieves a pointer to the first location of the shated memory.
Definition: eshmem.h:65
ESharedMemory::init
Void init(cpStr file, Int id, Int size)
Initializes this shared memory object. This method is called by the parameterized constructor.
Definition: eshmem.cpp:109
ESharedMemory::getUsageCount
Int getUsageCount()
Retrieves the number of clients accessing the shared memory.
Definition: eshmem.cpp:169
DECLARE_ERROR_ADVANCED
#define DECLARE_ERROR_ADVANCED(__e__)
Declares exception class derived from EError with no constructor parameters and developer defined con...
Definition: eerror.h:59
ESharedMemory
The shared memory access class.
Definition: eshmem.h:43
ESharedMemory::onDestroy
virtual Void onDestroy()
Called when teh shared memory is destroyed.
Definition: eshmem.h:71
EPC_FILENAME_MAX
#define EPC_FILENAME_MAX
maximum file name length
Definition: ebase.h:37
EError
The base class for exceptions derived from std::exception.
Definition: eerror.h:92