64 lines
931 B
C++
64 lines
931 B
C++
#ifndef SHM_H
|
|
#define SHM_H
|
|
|
|
#include <stdlib.h>
|
|
|
|
class ShmApi;
|
|
typedef int ShmPlace;
|
|
typedef int ShmSlot;
|
|
|
|
#define SHM_MAX_SLOTS 100
|
|
#define SHM_NULL -1
|
|
|
|
class ShmSem {
|
|
private:
|
|
void *_sem;
|
|
char *_name;
|
|
bool _owner;
|
|
|
|
public:
|
|
void post();
|
|
void wait();
|
|
bool trywait();
|
|
|
|
public:
|
|
ShmSem(const char *n, bool owner);
|
|
~ShmSem();
|
|
};
|
|
|
|
class Shm
|
|
{
|
|
private:
|
|
ShmApi *_shm_api;
|
|
|
|
public:
|
|
ShmPlace alloc(size_t mem);
|
|
void free(ShmPlace place);
|
|
void lock();
|
|
void unlock();
|
|
|
|
public:
|
|
ShmPlace slot(ShmSlot s);
|
|
void setSlot(ShmSlot s, ShmPlace pl);
|
|
|
|
public:
|
|
ShmSem *sem(const char *name, bool owner);
|
|
|
|
public:
|
|
const char *name();
|
|
|
|
public:
|
|
void *ref(int place);
|
|
|
|
public:
|
|
Shm(const char *name, size_t bytes, bool owner);
|
|
~Shm();
|
|
};
|
|
|
|
template <class T> inline void ref(Shm *shm, int place, T **p)
|
|
{
|
|
*p = reinterpret_cast<T *>(shm->ref(place));
|
|
}
|
|
|
|
#endif // SHM_H
|