

//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
//_/_/
//_/_/  This file is part of CMSDK version 2.1.1.260726 (26 July 2026)
//_/_/  
//_/_/  developed by
//_/_/  
//_/_/  Communicative Machines Ltd. 
//_/_/    http://cmlabs.com/
//_/_/  Copyright (c) 2026
//_/_/  
//_/_/  in collaboration with 
//_/_/  
//_/_/  Icelandic Institute for Intelligent Machines ses
//_/_/    Menntavegur 1
//_/_/    101 Reykjavik
//_/_/    ICELAND
//_/_/    http://www.iiim.is/
//_/_/
//_/_/  and is released under the following
//_/_/  
//_/_/  EXTENDED BSD License
//_/_/
//_/_/  Redistribution and use in source and binary forms, with or without
//_/_/  modification, is permitted provided that the following conditions 
//_/_/  are met:
//_/_/
//_/_/  - Redistributions of source code must retain the above copyright 
//_/_/    and collaboration notice, this list of conditions and the following 
//_/_/    disclaimer.
//_/_/
//_/_/  - Redistributions in binary form must reproduce the above copyright 
//_/_/    notice, this list of conditions and the following disclaimer in the 
//_/_/    documentation and/or other materials provided with the distribution.
//_/_/
//_/_/  - Neither the name of its copyright holders nor the names of its 
//_/_/    contributors may be used to endorse or promote products derived from 
//_/_/    this software without specific prior written permission.
//_/_/
//_/_/  - CADIA Clause: The license granted in and to the software under this 
//_/_/    agreement is a limited-use license. The software may not be used in 
//_/_/    furtherance of: (i) intentionally causing bodily injury or severe 
//_/_/    emotional harm to any person; (ii) invading the personal privacy or 
//_/_/    violating the human rights of any person; or (iii) committing or 
//_/_/    preparing for any act of war. 
//_/_/
//_/_/  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
//_/_/  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
//_/_/  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 
//_/_/  PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER 
//_/_/  OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
//_/_/  EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
//_/_/  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
//_/_/  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
//_/_/  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
//_/_/  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 
//_/_/  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//_/_/
//_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/_/
/*! @file MemoryMaps.h
    @brief Legacy shared-memory maps for the page-based allocator: reserved page ids, map entry layouts and the static MemoryMaps facade.
    @ingroup Memory

    Part of the older MemoryManagerX design in which each name/id map (types, topics,
    cranks, queues, processes, requests) lived in its own reserved *system page* of the
    page allocator. Superseded by DataMapsMemory/ProcessMemory but retained for
    compatibility; large parts of the facade are already commented out.

    @warning Shared-memory layouts; changing any struct here requires a full rebuild of
    all binaries (`make clean && make`) — see MemoryManager.h for the ABI warning. */

#if !defined(_MEMORYMAPS_H_)
#define _MEMORYMAPS_H_

#include "MemoryManager.h"

namespace cmlabs {

/*! @name Reserved system-page ids of the legacy page allocator
    The first RESERVEDPAGECOUNT page ids are fixed system pages holding allocator
    structures and the shared maps.
    @{ */
#define ID_SYSPAGE			0	/*!< Allocator system data. */
#define ID_PAGEPOOL			1	/*!< Idle-page pool. */
#define ID_EOLPAGE			2	/*!< End-of-life time wheel. */
#define ID_QUEUEPAGE		3	/*!< Message queues. */
#define ID_COMPMAPPAGE		4	/*!< Component map. */
#define ID_TYPEMAPPAGE		5	/*!< Type map. */
#define ID_TOPICMAPPAGE		6	/*!< Topic map. */
//#define ID_NODEMAPPAGE		7
#define ID_QUEUEMAPPAGE		7	/*!< Queue map. */
#define ID_PROCESSMAPPAGE	8	/*!< Process map. */
#define ID_REQUESTMAPPAGE	9	/*!< Request map. */
#define ID_CRANKMAPPAGE		10	/*!< Crank map. */
#define ID_CONTEXTMAPPAGE	11	/*!< Context map. */
#define RESERVEDPAGECOUNT	12	/*!< Number of reserved system pages. */
/*! @} */

/*! @name Legacy per-process queue slot ids
    @{ */
#define SIGNALQ_ID 0	/*!< Signal queue slot. */
#define MSGQ_ID 1		/*!< Data-message queue slot. */
#define REQQ_ID 2		/*!< Request queue slot. */
#define CMDQ_ID 3		/*!< Command queue slot. */
/*! @} */

/*! @brief Legacy type-map header (bitfield + TypeMapEntry array). @warning Shared-memory layout. */
struct TypeMapHeader {
	uint32 size; // total size of map structure in bytes
	uint32 count; // number of entries
	uint32 bitFieldSize; // number of bytes in the bitfield
	// Bitfield
	// Entries
};

/*! @brief Legacy type-map entry. @warning Shared-memory layout. */
struct TypeMapEntry {
	uint16 id; // id, corresponds to the index in map
	uint64 time; // timestamp when entered
	char name[MAXKEYNAMELEN+1]; // name in text
	uint32 key;
};

/*! @brief Legacy context-map header. @warning Shared-memory layout. */
struct ContextMapHeader {
	uint32 size; // total size of map structure in bytes
	uint32 count; // number of entries
	uint32 bitFieldSize; // number of bytes in the bitfield
	// Bitfield
	// Entries
};

/*! @brief Legacy context-map entry. @warning Shared-memory layout. */
struct ContextMapEntry {
	uint16 id; // id, corresponds to the index in map
	uint64 time; // timestamp when entered
	char name[MAXKEYNAMELEN+1]; // name in text
	uint32 key;
};

/*! @brief Legacy topic-map header. @warning Shared-memory layout. */
struct TopicMapHeader {
	uint32 size; // total size of map structure in bytes
	uint32 count; // number of entries
	uint32 bitFieldSize; // number of bytes in the bitfield
	// Bitfield
	// Entries
};

/*! @brief Legacy topic-map entry. @warning Shared-memory layout. */
struct TopicMapEntry {
	uint32 id; // id, corresponds to the index in map
	uint64 time; // timestamp when entered
	char name[MAXKEYNAMELEN+1]; // name in text
	uint32 key;
};

//struct NodeMapHeader {
//	uint32 size; // total size of map structure in bytes
//	uint32 count; // number of entries
//	uint32 bitFieldSize; // number of bytes in the bitfield
//	// Bitfield
//	// Entries
//};
//
//struct NodeMapEntry {
//	uint16 id; // id, corresponds to the index in map
//	uint64 time; // timestamp when entered
//	char name[MAXKEYNAMELEN+1]; // name in text
//	uint64 key; // 32bit IP4, 16bit port, 16bit unused
//};

/*! @brief Legacy crank-map header. @warning Shared-memory layout. */
struct CrankMapHeader {
	uint32 size; // total size of map structure in bytes
	uint32 count; // number of entries
	uint32 bitFieldSize; // number of bytes in the bitfield
	// Bitfield
	// Entries
};

/*! @brief Legacy crank-map entry (callback name, function, library, inline script). @warning Shared-memory layout. */
struct CrankMapEntry {
	uint16 id; // id, corresponds to the index in map
	uint64 time; // timestamp when entered
	char name[MAXKEYNAMELEN+1]; // name in text
	char function[MAXKEYNAMELEN+1]; // name in text
	char libraryFilename[MAXKEYNAMELEN+1]; // name in text
	char script[MAXSCRIPTLEN+1]; // script in text
	uint64 key; // 32bit IP4, 16bit port, 16bit unused
};

/*! @brief Legacy queue-map header. @warning Shared-memory layout. */
struct QueueMapHeader {
	uint32 size; // total size of map structure in bytes
	uint32 count; // number of entries
	uint32 bitFieldSize; // number of bytes in the bitfield
	// Bitfield
	// Entries
};

/*! @brief Legacy queue-map entry. @warning Shared-memory layout. */
struct QueueMapEntry {
	uint16 id; // id, corresponds to the index in map
	uint64 time; // timestamp when entered
	char name[MAXKEYNAMELEN+1]; // name in text
	uint32 key; // 32bit IP4, 16bit port, 16bit unused
};

/*! @name Legacy process status values (ProcessMapEntry::status)
    @{ */
#define PROC_ERROR	0	/*!< Error state. */
#define PROC_INIT	1	/*!< Initialising. */
#define PROC_IDLE	2	/*!< Idle. */
#define PROC_READY	3	/*!< Ready. */
#define PROC_ACTIVE	4	/*!< Actively running. */
/*! @} */

/*! @brief Legacy process-map header. @warning Shared-memory layout. */
struct ProcessMapHeader {
	uint32 size; // total size of map structure in bytes
	uint32 count; // number of entries
	uint32 bitFieldSize; // number of bytes in the bitfield
	// Bitfield
	// Entries
};

/*! @brief Legacy process-map entry with the ids of the process's five queues. @warning Shared-memory layout. */
struct ProcessMapEntry {
	uint32 id; // id, corresponds to the index in map
	uint64 time; // timestamp when entered
	uint64 lastseen; // timestamp when entered
	uint8 status; // status of the process
	char name[MAXKEYNAMELEN+1]; // name in text
	uint32 key;
	char commandline[MAXCOMMANDLINELEN+1]; // commandline in text
	uint32 signalQID; // id of Signal queue
	uint32 messageQID; // id of Message queue
	uint32 timeQID; // id of Time queue
	uint32 requestQID; // id of Request queue
	uint32 commandQID; // id of Command queue
};

/*! @brief Legacy request-map header. @warning Shared-memory layout. */
struct RequestMapHeader {
	uint32 size; // total size of map structure in bytes
	uint32 count; // number of entries
	uint32 bitFieldSize; // number of bytes in the bitfield
	// Bitfield
	// Entries
};

/*! @brief Legacy request-map entry tracking one request/reply transaction. @warning Shared-memory layout. */
struct RequestMapEntry {
	uint32 id; // id, corresponds to the index in map
	uint64 time; // timestamp when entered
	char name[MAXKEYNAMELEN+1]; // name in text
	uint32 key;
	uint8 status; // status of the process
	uint32 from; // id of component making the request
	uint32 to; // id of component processing the request
	uint64 dataMessageID; // id of Data Message
	uint64 dataMessageEOL; // eol of Data Message
};

/*! @brief Legacy generic map header shared by all maps in this file. @warning Shared-memory layout. */
struct GenericMapHeader {
	uint32 size; // total size of map structure in bytes
	uint32 count; // number of entries
	uint32 bitFieldSize; // number of bytes in the bitfield
	// Bitfield
	// Entries
};


/*! @name Map-entry to page-id dispatch
    Overload resolution maps an entry type to the reserved system page storing its map.
    @{ */
inline uint16 TypePageID(TypeMapEntry* e) {return ID_TYPEMAPPAGE;}
inline uint16 TypePageID(TopicMapEntry* e) {return ID_TOPICMAPPAGE;}
//inline uint16 TypePageID(NodeMapEntry* e) {return ID_NODEMAPPAGE;}
inline uint16 TypePageID(QueueMapEntry* e) {return ID_QUEUEMAPPAGE;}
inline uint16 TypePageID(ProcessMapEntry* e) {return ID_PROCESSMAPPAGE;}
inline uint16 TypePageID(RequestMapEntry* e) {return ID_REQUESTMAPPAGE;}
inline uint16 TypePageID(CrankMapEntry* e) {return ID_CRANKMAPPAGE;}
inline uint16 TypePageID(ContextMapEntry* e) {return ID_CONTEXTMAPPAGE;}
/*! @} */

/*! @name Legacy request lifecycle status values
    @note Numbering differs from the newer REQ_* set in DataMapsMemory.h (no REQ_FAILED_TO_SEND here).
    @{ */
#define REQ_CREATED				1
#define REQ_RECEIVED_LOCAL		2
#define REQ_RECEIVED_REMOTE		3
#define REQ_PROCESSING_LOCAL	4
#define REQ_PROCESSING_REMOTE	5
#define REQ_REPLY_READY			6
#define REQ_FAILED				7
#define REQ_FAILED_DATA			8
#define REQ_FAILED_NODATA		9
#define REQ_FAILED_DATA_EOL		10
#define REQ_SUCCESS				11
#define REQ_SUCCESS_DATA		12
#define REQ_SUCCESS_NODATA		13
#define REQ_SUCCESS_DATA_EOL	14
/*! @} */



/*! @brief Legacy static facade over the page-based shared maps (types, contexts, topics, cranks).
    @ingroup Memory
    Operates on the reserved system pages via MemoryManagerX locking. Most sections are
    commented out; the remaining APIs are kept for compatibility with older tools.
    All calls copy names into caller buffers; ids are returned via out-parameters. */
class MemoryMaps {
public:
	// TypeLevelMap
	static bool CreateNewTypeLevel(uint16 id, const char* name, uint64 time = 0);
	static bool GetTypeLevelName(uint16 id, char* name, uint32 maxSize);
	static bool GetTypeLevelID(const char* name, uint16 &id);
	static bool DeleteTypeLevel(const char* name);
	static bool DeleteTypeLevel(uint16 id);
	static uint64 GetTypeLevelCreateTime(uint16 id);

	// ContextLevelMap
	static bool CreateNewContextLevel(uint16 id, const char* name, uint64 time = 0);
	static bool GetContextLevelName(uint16 id, char* name, uint32 maxSize);
	static bool GetContextLevelID(const char* name, uint16 &id);
	static bool DeleteContextLevel(const char* name);
	static bool DeleteContextLevel(uint16 id);
	static uint64 GetContextLevelCreateTime(uint16 id);

	// TopicMap
	static bool CreateNewTopic(uint8 id, const char* name);
	static bool GetTopicName(uint32 id, char* name, uint32 maxSize);
	static bool GetTopicID(const char* name, uint32 &id);
	static bool DeleteTopic(const char* name);
	static bool DeleteTopic(uint32 id);
	static uint64 GetTopicCreateTime(uint32 id);

	// NodeMap
	//static bool CreateNewNode(uint8 id, const char* name, uint64 address);
	//static uint64 GetNodeAddress(uint16 id);
	//static bool GetNodeID(uint64 address, uint16 &id);
	//static bool GetNodeName(uint16 id, char* name, uint32 maxSize);
	//static bool GetNodeID(const char* name, uint16 &id);
	//static bool DeleteNode(uint64 address);
	//static bool DeleteNode(uint16 id);
	//static bool DeleteNode(const char* name);
	//static uint64 GetNodeCreateTime(uint16 id);

	// CrankMap
	static bool CreateNewCrank(uint16 id, const char* name, const char* function, const char* libraryFilename, uint64 time = 0);
	static bool GetCrankName(uint16 id, char* name, uint32 maxSize);
	static bool GetCrankFunction(uint16 id, char* function, uint32 maxSize);
	static bool GetCrankLibraryFilename(uint16 id, char* libraryFilename, uint32 maxSize);
	static bool GetCrankID(const char* name, uint16 &id);
	static bool GetCrankScript(uint16 id, char* script, uint32 maxSize);
	static bool SetCrankScript(uint16 id, const char* script);
	static bool DeleteCrank(uint16 id);
	static bool DeleteCrank(const char* name);
	static uint64 GetCrankCreateTime(uint16 id);

	//// QueueMap
	//static bool CreateNewQueue(uint32 pageID, const char* name, uint32 &id);
	//static bool GetQueueName(uint32 id, char* name, uint32 maxSize);
	//static uint32 GetQueuePageID(uint32 id);
	//static bool GetQueueID(const char* name, uint32 &id);
	//static bool GetQueueID(uint32 pageID, uint32 &id);
	//static bool DeleteQueue(uint32 id);
	//static bool DeleteQueue(const char* name);
	//static uint64 GetQueueCreateTime(uint32 id);

	//// ProcessMap
	//static bool CreateNewProcess(const char* name, uint16 &id);
	//static bool GetProcessName(uint16 id, char* name, uint32 maxSize);
	//static bool GetProcessID(const char* name, uint16 &id);
	//static bool GetProcessQueueID(uint16 id, uint8 queueType, uint32& qid);
	//static uint8 GetProcessStatus(uint16 id, uint64& lastseen);
	//static bool GetProcessCommandLine(uint16 id, char* cmdline, uint32 maxSize);
	//static bool SetProcessQueueID(uint16 id, uint8 queueType, uint32 qid);
	//static bool SetProcessStatus(uint16 id, uint8 status);
	//static bool SetProcessCommandLine(uint16 id, const char* cmdline);
	//static uint16 GetProcessIDFromOSID(uint32 osid);
	//static uint32 GetProcessOSID(uint16 id);
	//static bool SetProcessOSID(uint16 id, uint32 osid);
	//static bool DeleteProcess(const char* name);
	//static bool DeleteProcess(uint16 id);
	//static uint64 GetProcessCreateTime(uint16 id);

	//// RequestMap
	//static RequestMapEntry* CreateAndLockNewRequest(uint32 from, uint32 to, uint32 &id);
	//static RequestMapEntry* GetAndLockRequestEntry(uint32 id);
	//static bool UnlockRequestMap();
	//static bool DeleteRequest(uint32 id);
	//static bool GetRequestCount(uint32& count);

protected:
//	static ProcessMapEntry* GetAndLockProcessEntry(uint16 id);
//	static bool UnlockProcessMap();
};



















/*! @brief Legacy generic map algorithms (3-parameter variant with unused KEY type).
    @ingroup Memory
    @tparam T Entry type. @tparam ID Id type. @tparam KEY Unused key type (historical).
    Superseded by the 2-parameter GenericMemoryMap in DataMapsMemory.h; bodies live in
    MemoryMaps.tpl.h. Entry pointers returned point into shared memory. */
template <typename T, typename ID, typename KEY>
class GenericMemoryMap {
public:
	// Generic MemoryMap
	static uint32 GetCount(char* data);						/*!< @return Number of entries in use. */
	static T* CreateEntry(char* data, ID id, const char* name);	/*!< Reserve entry @a id. @return Entry or NULL. */
	static T* GetEntry(char* data, ID id);					/*!< @return Entry @a id or NULL. */
	static bool DeleteEntry(char* data, ID id);				/*!< Free the entry. @return true on success. */
	static uint64 GetEntryTime(char* data, ID id);			/*!< @return Creation time, 0 if absent. */
	static const char* GetEntryName(char* data, ID id, uint32& size);	/*!< @return Name pointer into shared memory, or NULL. */
	static ID GetEntryID(char* data, const char* name);		/*!< @return Id for @a name. */
};



} // namespace cmlabs

#include "PsyTime.h"
#include "MemoryMaps.tpl.h"

#endif // _MEMORYMAPS_H_

	