// shmrot.h :
//

#if !defined(AGD_SHMROT_H__58C9857B_4E4E_4AE1_B093_DFECBB85F652__INCLUDED_)
#define AGD_SHMROT_H__58C9857B_4E4E_4AE1_B093_DFECBB85F652__INCLUDED_

#include "gfaipc.h"
#include "shm.h"

/////////////////////////////////////////////////////////////////////////////
// shmrot.h - Declarations:

typedef struct _SHM_HANDLE
{
	CShm *pShm;
}SHM_HANDLE, *LPSHM_HANDLE;
typedef const SHM_HANDLE *LPCSHM_HANDLE;

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

#define _IPCSHM_MAX_HANDLES			32

class CShmHandleMap
{
public:
	CShmHandleMap(void);
	~CShmHandleMap(void);

	HSHM AcquireHandle(CShm *pShm);
	void ReleaseHandle(HSHM hShm);
	CShm* LookupShm(const uuid_t &uuid);

private:
	SHM_HANDLE m_handles[_IPCSHM_MAX_HANDLES];
};

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

#define _ROT_MAX_TABLE_ENTRIES			32
#define _ROT_MAX_ENTRY_NAME				128

#define _ROT_MAKE_VERSION(ma, mi)		(((ma) << 16) | (mi))
#define _ROT_VERSION_MAJOR(ver)			(((ver) >> 16) & 0x0000FFFF)
#define _ROT_VERSION_MINOR(ver)			((ver) & 0x0000FFFF)
#define _ROT_CURRENT_VERSION			_ROT_MAKE_VERSION(1, 0)

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

typedef struct _SHMROT_ENTRY
{
	uuid_t uuid;
	uint32_t flags;
	uint32_t nSizeElement;
	uint32_t nCntElements;
	uint32_t nCntHandles;
	uint32_t reserved[4];
	char szDescName[_ROT_MAX_ENTRY_NAME];
}SHMROT_ENTRY, *LPSHMROT_ENTRY;
typedef const SHMROT_ENTRY *LPCSHMROT_ENTRY;

typedef struct _SHMROT
{
	uint32_t version;		// major << 16, minor version
	uint32_t maxEntries;	// sollte _ROT_MAX_TABLE_ENTRIES sein
	uint32_t entrySize;
	uint32_t reserved[5];
	SHMROT_ENTRY rot[_ROT_MAX_TABLE_ENTRIES];
}SHMROT, *LPSHMROT;
typedef const SHMROT *LPCSHMROT;

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

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

	bool Create(void);
	void Release(void);

	CShm* AcquireShm(const char *pszUuid, size_t nSizeElement, size_t nCntElements, const char *pszDescName);
	void ReleaseShm(CShm *pShm);

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

	bool Created(void) const {
		return !!m_pTable;}

	void DumpEntries(CShmHandleMap &map);

private:
	CShm m_shmRot;
	LPSHMROT m_pTable;
};

/////////////////////////////////////////////////////////////////////////////
#endif	//	!defined(AGD_SHMROT_H__58C9857B_4E4E_4AE1_B093_DFECBB85F652__INCLUDED_)
