Skip to content

Resource::ContextManager (@com.silcos.rsmgr.*)

Sukant Pal edited this page Jan 2, 2018 · 1 revision

ContextManager

The ContextManager is present in the ResourceManager module and handles the memory management of an address space.

`

class ContextManager
{
public:
	/* Specifies the type for this context manager */
	enum TypeId
	{
		psmmManager = 0
	};

	const char *name;
	const int typeId;

	virtual RegionInsertionResult insertRegion(unsigned long initialAddress,
			unsigned long pageCount, unsigned long cfgFlags,
			PAGE_ATTRIBUTES permissions) = 0;

	virtual RegionRemovalResult removeRegion(unsigned long initialAddress,
			unsigned long pageCount, unsigned short typeId) = 0;

//	virtual unsigned long includeInRegion(unsigned long initialAddress,
//			unsigned long addressExtension) = 0;

//	virtual MemorySection* validateRegion(unsigned long address) = 0;

	void printAll();
protected:
	MemorySection *code;
	MemorySection *data;
	MemorySection *bss;
	MemorySection *mainStack;
	MemorySection *recentCache;
	unsigned long referCount;
	unsigned long regionCount;

	bool treePopulated;
	MemorySection *firstArena;
	MemorySection *lastArena;
	RBTree *arenaTree;

	ContextManager(const char *name, const int typeId);
	virtual ~ContextManager();

	RegionInsertionResult add(MemorySection *sec);
	void carve(MemorySection *arena, unsigned long iaddr, unsigned long faddr);
	void split(MemorySection *arena, unsigned long laddr, unsigned long raddr);
	bool populateTree();
	void remove(MemorySection *sec);
	unsigned long removeAll(MemorySection *from, MemorySection *till,
					unsigned short typeId, unsigned long &pageCount);

	virtual unsigned long * getIDFilter()
	{
		return (NULL);
	}
};

`

The ContextManager provides the basic implementation of memory-section handling such as carving, splitting, removing, searching and recording of sections based on their types (ID-filters).

The `ContextManager' provides the following functionality -

1. It caches the core core, data, bss, stack & heap sections of the address space.

2. It implements storage of sections in two data structures - linked-list and a red-black tree. When the no. of sections crosses a certain threshold (currently 32), all of them are inserted in a binary search tree. After that, binary search tree and the linked-list are both managed.

3. It also caches the most recently accessed memory section for faster lookups and operations.

4. Carving - A section is carved when it is shrinked. The left and right margins of the old section are released and a new section is formed. The ContextManager implements that in the ContextManager::carve method.

5. Splitting - A section is split when a hole is created in it. Then two new sections are created - left & right the hole in the old section. The ContextManager implements that in the ContextManager::split method.

6. Adding and removing section structures from linked-list and the binary search tree.

IDFilter

The base class must also store an array of unsigned long in which each element, at the index of the id of any section, contains the no. of sections for a corresponding memory section type.

Clone this wiki locally