aboutsummaryrefslogtreecommitdiffstats
path: root/util/system/sys_alloc.h
diff options
context:
space:
mode:
authorDevtools Arcadia <arcadia-devtools@yandex-team.ru>2022-02-07 18:08:42 +0300
committerDevtools Arcadia <arcadia-devtools@mous.vla.yp-c.yandex.net>2022-02-07 18:08:42 +0300
commit1110808a9d39d4b808aef724c861a2e1a38d2a69 (patch)
treee26c9fed0de5d9873cce7e00bc214573dc2195b7 /util/system/sys_alloc.h
downloadydb-1110808a9d39d4b808aef724c861a2e1a38d2a69.tar.gz
intermediate changes
ref:cde9a383711a11544ce7e107a78147fb96cc4029
Diffstat (limited to 'util/system/sys_alloc.h')
-rw-r--r--util/system/sys_alloc.h43
1 files changed, 43 insertions, 0 deletions
diff --git a/util/system/sys_alloc.h b/util/system/sys_alloc.h
new file mode 100644
index 0000000000..4221a28f8c
--- /dev/null
+++ b/util/system/sys_alloc.h
@@ -0,0 +1,43 @@
+#pragma once
+
+#include <util/system/compiler.h>
+
+#include <cstdlib>
+#include <new>
+
+inline void* y_allocate(size_t n) {
+ void* r = malloc(n);
+
+ if (r == nullptr) {
+ throw std::bad_alloc();
+ }
+
+ return r;
+}
+
+inline void y_deallocate(void* p) {
+ free(p);
+}
+
+/**
+ * Behavior of realloc from C++99 to C++11 changed (http://www.cplusplus.com/reference/cstdlib/realloc/).
+ *
+ * Our implementation work as C++99: if new_sz == 0 free will be called on 'p' and nullptr returned.
+ */
+inline void* y_reallocate(void* p, size_t new_sz) {
+ if (!new_sz) {
+ if (p) {
+ free(p);
+ }
+
+ return nullptr;
+ }
+
+ void* r = realloc(p, new_sz);
+
+ if (r == nullptr) {
+ throw std::bad_alloc();
+ }
+
+ return r;
+}