Веб-сайт самохостера Lotigara

summaryrefslogtreecommitdiff
path: root/source/core/StarMemory.cpp
diff options
context:
space:
mode:
authorKae <80987908+Novaenia@users.noreply.github.com>2023-06-20 14:33:09 +1000
committerKae <80987908+Novaenia@users.noreply.github.com>2023-06-20 14:33:09 +1000
commit6352e8e3196f78388b6c771073f9e03eaa612673 (patch)
treee23772f79a7fbc41bc9108951e9e136857484bf4 /source/core/StarMemory.cpp
parent6741a057e5639280d85d0f88ba26f000baa58f61 (diff)
everything everywhere
all at once
Diffstat (limited to 'source/core/StarMemory.cpp')
-rw-r--r--source/core/StarMemory.cpp93
1 files changed, 93 insertions, 0 deletions
diff --git a/source/core/StarMemory.cpp b/source/core/StarMemory.cpp
new file mode 100644
index 0000000..3855283
--- /dev/null
+++ b/source/core/StarMemory.cpp
@@ -0,0 +1,93 @@
+#include "StarMemory.hpp"
+
+#ifdef STAR_USE_JEMALLOC
+#include "jemalloc/jemalloc.h"
+#endif
+
+namespace Star {
+
+#ifdef STAR_USE_JEMALLOC
+ void* malloc(size_t size) {
+ return je_malloc(size);
+ }
+
+ void* realloc(void* ptr, size_t size) {
+ return je_realloc(ptr, size);
+ }
+
+ void free(void* ptr) {
+ je_free(ptr);
+ }
+
+ void free(void* ptr, size_t size) {
+ if (ptr)
+ je_sdallocx(ptr, size, 0);
+ }
+#else
+ void* malloc(size_t size) {
+ return ::malloc(size);
+ }
+
+ void* realloc(void* ptr, size_t size) {
+ return ::realloc(ptr, size);
+ }
+
+ void free(void* ptr) {
+ return ::free(ptr);
+ }
+
+ void free(void* ptr, size_t) {
+ return ::free(ptr);
+ }
+#endif
+
+}
+
+void* operator new(std::size_t size) {
+ auto ptr = Star::malloc(size);
+ if (!ptr)
+ throw std::bad_alloc();
+ return ptr;
+}
+
+void* operator new[](std::size_t size) {
+ auto ptr = Star::malloc(size);
+ if (!ptr)
+ throw std::bad_alloc();
+ return ptr;
+}
+
+// Globally override new and delete. As the per standard, new and delete must
+// be defined in global scope, and must not be inline.
+
+void* operator new(std::size_t size, std::nothrow_t const&) noexcept {
+ return Star::malloc(size);
+}
+
+void* operator new[](std::size_t size, std::nothrow_t const&) noexcept {
+ return Star::malloc(size);
+}
+
+void operator delete(void* ptr) noexcept {
+ Star::free(ptr);
+}
+
+void operator delete[](void* ptr) noexcept {
+ Star::free(ptr);
+}
+
+void operator delete(void* ptr, std::nothrow_t const&) noexcept {
+ Star::free(ptr);
+}
+
+void operator delete[](void* ptr, std::nothrow_t const&) noexcept {
+ Star::free(ptr);
+}
+
+void operator delete(void* ptr, std::size_t size) noexcept {
+ Star::free(ptr, size);
+}
+
+void operator delete[](void* ptr, std::size_t size) noexcept {
+ Star::free(ptr, size);
+}