summaryrefslogtreecommitdiff
path: root/shared-module/_protomatter/allocator.h
diff options
context:
space:
mode:
authorLucian Copeland <hierophect@gmail.com>2020-04-15 10:29:05 -0400
committerLucian Copeland <hierophect@gmail.com>2020-04-15 10:29:05 -0400
commit54abfc2e8b88ed0dd1caaf3ac926466b0aba717a (patch)
treeb5154c84f807ee6ce3b0716b3a8d7b64dca46061 /shared-module/_protomatter/allocator.h
parent0f87a75f4f0675ff55e31437907101226d5fb66b (diff)
parentae549fc9da555778884ff64a2ffe45df0a5248db (diff)
translations-merge
Diffstat (limited to 'shared-module/_protomatter/allocator.h')
-rw-r--r--shared-module/_protomatter/allocator.h29
1 files changed, 29 insertions, 0 deletions
diff --git a/shared-module/_protomatter/allocator.h b/shared-module/_protomatter/allocator.h
new file mode 100644
index 000000000..b7f517ce5
--- /dev/null
+++ b/shared-module/_protomatter/allocator.h
@@ -0,0 +1,29 @@
+#ifndef MICROPY_INCLUDED_SHARED_MODULE_PROTOMATTER_ALLOCATOR_H
+#define MICROPY_INCLUDED_SHARED_MODULE_PROTOMATTER_ALLOCATOR_H
+
+#include <stdbool.h>
+#include "py/gc.h"
+#include "py/misc.h"
+#include "supervisor/memory.h"
+
+#define _PM_ALLOCATOR _PM_allocator_impl
+#define _PM_FREE(x) (_PM_free_impl((x)), (x)=NULL, (void)0)
+
+static inline void *_PM_allocator_impl(size_t sz) {
+ if (gc_alloc_possible()) {
+ return m_malloc(sz + sizeof(void*), true);
+ } else {
+ supervisor_allocation *allocation = allocate_memory(align32_size(sz), false);
+ return allocation ? allocation->ptr : NULL;
+ }
+}
+
+static inline void _PM_free_impl(void *ptr_in) {
+ supervisor_allocation *allocation = allocation_from_ptr(ptr_in);
+
+ if (allocation) {
+ free_memory(allocation);
+ }
+}
+
+#endif