1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
|
// SharedMemory.cpp
// Gery Vessere - gvessere@illumina.com, gery@vessere.com
// An abstraction over both SysV and POSIX shared memory APIs
#ifndef SHAREDMEMORY_H
#define SHAREDMEMORY_H
#include <string>
#include <semaphore.h>
#include <unistd.h>
#include <exception>
#include <iostream>
enum ErrorState {
ENONE,
ENOTALLOCATED,
ETRYAGAIN,
EALREADYALLOCATED,
EOPENFAILED,
EEXISTS,
EFTRUNCATE,
EMAPFAILED,
ECLOSE,
EUNLINK,
ECOUNTERCREATE,
ECOUNTERREMOVE,
ECOUNTERUSE
};
class SharedMemoryException: public std::exception
{
private:
bool _hasError;
ErrorState _error;
int _errorDetail;
public:
SharedMemoryException()
{
_hasError = false;
_error = ENONE;
_errorDetail = 0;
};
SharedMemoryException(ErrorState error): _error(error)
{};
ErrorState GetErrorCode() const
{
return _error;
};
int GetErrorDetail() const
{
return _errorDetail;
}
bool HasError() const
{
return _hasError;
};
void SetError(ErrorState error, int detail)
{
if (!_hasError)
{
_hasError = true;
_error = error;
_errorDetail = detail;
}
}
void ClearError()
{
_hasError = false;
_error = ENONE;
_errorDetail = 0;
};
};
class SharedMemory
{
public:
void * GetMapped()
{
return (void *) ((char*) _mapped + sizeof(size_t));
};
size_t GetSize()
{
if (!_needsAllocation)
return *_length - sizeof(size_t);
_exception.SetError(ENOTALLOCATED, 0);
return -1;
};
bool NeedsAllocation() const
{
return _needsAllocation;
};
// the owner is the first one that created the named shared memory segment
bool IsAllocator() const
{
return _isAllocator;
};
int GetId() const
{
return _shmID;
};
bool HasError() const
{
return _exception.HasError();
}
void ThrowError(ErrorState error, int detail)
{
if (!_exception.HasError())
{
_exception.SetError(error, detail);
}
throw _exception;
};
void ThrowError(ErrorState error)
{
ThrowError(error, 0);
};
void SetErrorStream(std::ostream * err)
{
_err = err;
};
SharedMemory(key_t key, bool unloadLast);
~SharedMemory();
void Allocate(size_t shmSize);
void Clean();
private:
SharedMemoryException _exception;
int _shmID;
int _sharedCounterID;
void * _counterMem;
void * _mapped;
size_t * _length;
sem_t * _sem;
bool _isAllocator;
bool _needsAllocation;
key_t _key;
key_t _counterKey;
bool _unloadLast;
std::ostream * _err;
int SharedObjectsUseCount();
void OpenIfExists();
void CreateAndInitSharedObject(size_t shmSize);
void MapSharedObjectToMemory();
std::string GetPosixObjectKey();
struct stat GetSharedObjectInfo();
void Close();
void Unlink();
std::string CounterName();
void EnsureCounter();
void RemoveSharedCounter();
void SharedUseIncrement();
void SharedUseDecrement();
};
#endif
|