summaryrefslogtreecommitdiff
path: root/src/utils/memory.tpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils/memory.tpp')
-rw-r--r--src/utils/memory.tpp48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/utils/memory.tpp b/src/utils/memory.tpp
new file mode 100644
index 0000000..276b0b4
--- /dev/null
+++ b/src/utils/memory.tpp
@@ -0,0 +1,48 @@
+#include "memory.hpp"
+
+#include "Arduino.h"
+#include "general.hpp"
+
+template <typename memType>
+memType *malloc_s(unsigned int size)
+{
+ auto *mem = malloc(size);
+
+ if (mem == nullptr)
+ {
+ Serial.println("Error: Memory allocation failed");
+ while (true) {}
+ }
+
+ return static_cast<memType *>(mem);
+}
+
+template <class Target>
+unique_ptr<Target>::unique_ptr(Target *target)
+{
+ this->_target = target;
+}
+
+template <class Target>
+unique_ptr<Target>::~unique_ptr()
+{
+ delete this->_target;
+}
+
+template <class Target>
+Target unique_ptr<Target>::operator*() const
+{
+ return *(this->_target);
+}
+
+template <class Target>
+Target *unique_ptr<Target>::operator->() const
+{
+ return this->_target;
+}
+
+template <class Target, typename... Args>
+unique_ptr<Target> make_unique(Args... args)
+{
+ return unique_ptr<Target>(new Target(args...));
+}