Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Heap.h « Platform « src - github.com/Duet3D/RepRapFirmware.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 01458d990c3ffd63708709c8a6310e0174c11de0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
 * Heap.h
 *
 *  Created on: 5 Mar 2021
 *      Author: David
 */

#ifndef SRC_PLATFORM_HEAP_H_
#define SRC_PLATFORM_HEAP_H_

#include <RepRapFirmware.h>
#include <RTOSIface/RTOSIface.h>			// for class ReadLockedPointer

#include <atomic>

class IndexSlot;
class StorageSpace;
class HeapBlock;
class IndexBlock;

// Note: StringHandle is a union member in ExpressionValue, therefore it cannot have a non-trivial destructor, copy constructor etc.
// This means that when an object containing a StringHandle is copied or destroyed, that object must handle the reference count.
// Classes other than ExpressionValue should use AutoStringHandle instead;
class StringHandle
{
public:
	StringHandle() noexcept { slotPtr = nullptr; }
	StringHandle(const char *s) noexcept;
	StringHandle(const char *s, size_t len) noexcept;

#if 0	// unused
	StringHandle(const char *s1, const char *s2) noexcept;
#endif

	ReadLockedPointer<const char> Get() const noexcept;
	size_t GetLength() const noexcept;
	void Delete() noexcept;
	const StringHandle& IncreaseRefCount() const noexcept;
	bool IsNull() const noexcept { return slotPtr == nullptr; }
	void Assign(const char *s) noexcept;

	static void GarbageCollect() noexcept;
//	static size_t GetWastedSpace() noexcept { return spaceToRecycle; }
//	static size_t GetIndexSpace() noexcept { return totalIndexSpace; }
//	static size_t GetHeapSpace() noexcept { return totalHeapSpace; }
	static bool CheckIntegrity(const StringRef& errmsg) noexcept;
	static void Diagnostics(MessageType mt) noexcept;

protected:
	void InternalAssign(const char *s, size_t len) noexcept;
	void InternalDelete() noexcept;

	static IndexSlot *AllocateHandle() noexcept;
	static StorageSpace *AllocateSpace(size_t length) noexcept;
	static void GarbageCollectInternal() noexcept;
	static void AdjustHandles(char *startAddr, char *endAddr, size_t moveDown, unsigned int numHandles) noexcept;

	IndexSlot *slotPtr;

	static ReadWriteLock heapLock;
	static IndexBlock *indexRoot;
	static HeapBlock *heapRoot;
	static size_t handlesAllocated;
	static std::atomic<size_t> handlesUsed;
	static size_t heapAllocated;
	static size_t heapUsed;
	static std::atomic<size_t> heapToRecycle;
	static unsigned int gcCyclesDone;
};

// Version of StringHandle that updates the reference counts automatically
class AutoStringHandle : public StringHandle
{
public:
	AutoStringHandle() noexcept : StringHandle() { }
	AutoStringHandle(const char *s) noexcept : StringHandle(s) { }
	AutoStringHandle(const char *s, size_t len) noexcept : StringHandle(s, len) { }
	AutoStringHandle(const AutoStringHandle& other) noexcept;
	AutoStringHandle(AutoStringHandle&& other) noexcept;
	AutoStringHandle& operator=(const AutoStringHandle& other) noexcept;
	~AutoStringHandle();
};

#endif /* SRC_PLATFORM_HEAP_H_ */