aboutsummaryrefslogtreecommitdiff
path: root/src/init
diff options
context:
space:
mode:
Diffstat (limited to 'src/init')
-rw-r--r--src/init/alloc.cc50
-rw-r--r--src/init/alloc.h22
2 files changed, 72 insertions, 0 deletions
diff --git a/src/init/alloc.cc b/src/init/alloc.cc
new file mode 100644
index 0000000..d18917b
--- /dev/null
+++ b/src/init/alloc.cc
@@ -0,0 +1,50 @@
+#include "alloc.h"
+
+#include <cstdio>
+#include <cstdint>
+
+namespace init {
+
+std::uint8_t dlsym_level = 0;
+size_t buffer_offset = 0;
+char buffer[4096];
+
+void* static_malloc(size_t size) {
+ if ( buffer_offset + size >= sizeof(buffer) ) {
+ std::fprintf(stderr, "static allocator out of memory: %lu\n", buffer_offset);
+ exit(1);
+ }
+
+ buffer_offset += size;
+
+ return buffer + buffer_offset;
+}
+
+void* static_calloc(size_t block, size_t size) {
+ void* const mem = static_malloc(block * size);
+
+ for ( size_t i = 0; i < (block * size); ++i ) {
+ *(static_cast<char* const>(mem) + i) = '\0';
+ }
+
+ return mem;
+}
+
+bool from_static_buffer(void* ptr) {
+ return static_cast<char*>(ptr) >= buffer
+ && static_cast<char*>(ptr) <= buffer + sizeof(buffer);
+}
+
+dlsymContext::dlsymContext() {
+ ++dlsym_level;
+}
+
+dlsymContext::~dlsymContext() {
+ --dlsym_level;
+}
+
+bool dlsymContext::is_active() {
+ return dlsym_level > 0;
+}
+
+}
diff --git a/src/init/alloc.h b/src/init/alloc.h
new file mode 100644
index 0000000..cd1228d
--- /dev/null
+++ b/src/init/alloc.h
@@ -0,0 +1,22 @@
+#ifndef CHANGE_SRC_INIT_ALLOC_H_
+#define CHANGE_SRC_INIT_ALLOC_H_
+
+#include <cstdlib>
+
+namespace init {
+
+void* static_malloc(size_t size);
+void* static_calloc(size_t block, size_t size);
+
+bool from_static_buffer(void* ptr);
+
+struct dlsymContext {
+ dlsymContext();
+ ~dlsymContext();
+
+ static bool is_active();
+};
+
+}
+
+#endif // CHANGE_SRC_INIT_ALLOC_H_