// shm.h :
//

#if !defined(AGD_SHM_H__AD9FF941_0114_4585_8EF1_0A4ACA7DC0AD__INCLUDED_)
#define AGD_SHM_H__AD9FF941_0114_4585_8EF1_0A4ACA7DC0AD__INCLUDED_

#include "mutex.h"
#include "sema.h"

/////////////////////////////////////////////////////////////////////////////
// shm.h - Declarations:

#ifdef _WIN32
#define _DEF_SHM_DIR						"."
#endif	//	_WIN32

#ifdef __linux__
#define _DEF_SHM_DIR						"/tmp"
#endif	//	__linux__

/////////////////////////////////////////////////////////////////////////////

class CShm
{
public:
	CShm(void);
	virtual ~CShm(void);

	int Create(const uuid_t &ruuid, size_t nCbShm, const char *pszDir = _DEF_SHM_DIR);
	long Release(void);

	void* Attach(const void *pAddr = NULL, int shmflg = 0);
	void Detach(const void *pAddr);
	long GetNumAttached(void);

	int Ctl(int cmd);
	int Ctl(int cmd, struct shmid_ds *pshd);
	int Ctl(int cmd, struct shminfo *pinfo);
	int Ctl(int cmd, struct shm_info *pinfo);

	int IpcStat(struct shmid_ds *buf);
	int IpcInfo(struct shminfo *si);
	int IpcSet(struct shmid_ds *buf);
	int IpcRmID(void);

	int ShmStat(int index, struct shmid_ds *buf);
	int ShmInfo(struct shm_info *pinfo);

	int Lock(void);
	int Unlock(void);

	const uuid_t& Uuid() const {
		return m_uuid;}

	long InterlockedIncrement(volatile long *pl);
	long InterlockedDecrement(volatile long *pl);
	long InterlockedCompare(volatile long *pl, long comparand);
	long InterlockedSet(volatile long *pl, long val);
	long InterlockedClear(volatile long *pl);

	static void DumpIpcPerm(struct ipc_perm *ip);
	static void DumpShmidDs(struct shmid_ds *sid);

private:

private:
	int m_nShmID;
	CGlobalMutex m_mutex;
	uuid_t m_uuid;
};

/////////////////////////////////////////////////////////////////////////////
#endif	//	!defined(AGD_SHM_H__AD9FF941_0114_4585_8EF1_0A4ACA7DC0AD__INCLUDED_)
