distortos  v0.7.0
object-oriented C++ RTOS for microcontrollers
Stack.hpp
Go to the documentation of this file.
1 
12 #ifndef INCLUDE_DISTORTOS_INTERNAL_SCHEDULER_STACK_HPP_
13 #define INCLUDE_DISTORTOS_INTERNAL_SCHEDULER_STACK_HPP_
14 
16 
17 #include <memory>
18 
19 #if defined(DISTORTOS_ARCHITECTURE_ASCENDING_STACK) || defined(DISTORTOS_ARCHITECTURE_EMPTY_STACK)
20 #error "Support for \"empty ascending\", \"empty descending\" or \"full ascending\" stack not implemented!"
21 #endif // defined(DISTORTOS_ARCHITECTURE_ASCENDING_STACK) || defined(DISTORTOS_ARCHITECTURE_EMPTY_STACK)
22 
23 namespace distortos
24 {
25 
26 namespace internal
27 {
28 
29 class RunnableThread;
30 
32 class Stack
33 {
34 public:
35 
37  using StorageUniquePointer = std::unique_ptr<void, void(&)(void*)>;
38 
49  Stack(StorageUniquePointer&& storageUniquePointer, size_t size);
50 
63  Stack(void* storage, size_t size);
64 
69  ~Stack();
70 
75  bool checkStackGuard() const;
76 
87  bool checkStackPointer(const void* const stackPointer) const
88  {
89  return stackPointer >= static_cast<uint8_t*>(adjustedStorage_) + stackGuardSize &&
90  stackPointer <= static_cast<uint8_t*>(adjustedStorage_) + adjustedSize_;
91  }
92 
97  size_t getHighWaterMark() const;
98 
103  size_t getSize() const
104  {
106  }
107 
114  void* getStackPointer() const
115  {
116  return stackPointer_;
117  }
118 
128  int initialize(RunnableThread& runnableThread);
129 
136  void setStackPointer(void* const stackPointer)
137  {
138  stackPointer_ = stackPointer;
139  }
140 
141  Stack(const Stack&) = delete;
142  Stack(Stack&&) = default;
143  const Stack& operator=(const Stack&) = delete;
144  Stack& operator=(Stack&&) = delete;
145 
146 private:
147 
150 
152  void* const adjustedStorage_;
153 
155  const size_t adjustedSize_;
156 
159 };
160 
161 } // namespace internal
162 
163 } // namespace distortos
164 
165 #endif // INCLUDE_DISTORTOS_INTERNAL_SCHEDULER_STACK_HPP_
size_t getSize() const
Definition: Stack.hpp:103
RunnableThread class defines additional interface functions required for the thread to be actually ru...
Definition: RunnableThread.hpp:24
Stack(StorageUniquePointer &&storageUniquePointer, size_t size)
Stack's constructor.
Definition: Stack.cpp:85
const size_t adjustedSize_
adjusted size of stack's storage
Definition: Stack.hpp:155
bool checkStackPointer(const void *const stackPointer) const
Checks whether stack pointer value is within range of this stack.
Definition: Stack.hpp:87
std::unique_ptr< void, void(&)(void *)> StorageUniquePointer
unique_ptr (with deleter) to storage
Definition: Stack.hpp:37
bool checkStackGuard() const
Definition: Stack.cpp:108
~Stack()
Stack's destructor.
Definition: Stack.cpp:103
void *const adjustedStorage_
adjusted address of stack's storage
Definition: Stack.hpp:152
Top-level namespace of distortos project.
Definition: buttons.hpp:33
stackGuardSize constant
Stack class is an abstraction of architecture's stack.
Definition: Stack.hpp:32
StorageUniquePointer storageUniquePointer_
storage for stack
Definition: Stack.hpp:149
constexpr size_t stackGuardSize
size of "stack guard", bytes
Definition: stackGuardSize.hpp:26
void setStackPointer(void *const stackPointer)
Sets value of stack pointer.
Definition: Stack.hpp:136
size_t getHighWaterMark() const
Definition: Stack.cpp:118
void * stackPointer_
current value of stack pointer register
Definition: Stack.hpp:158
int initialize(RunnableThread &runnableThread)
Fills the stack with stack sentinel, initializes its contents and stack pointer value.
Definition: Stack.cpp:131
void * getStackPointer() const
Gets current value of stack pointer.
Definition: Stack.hpp:114