2020-03-10 14:12:01 -04:00
|
|
|
#ifndef MICROPY_INCLUDED_SHARED_MODULE_PROTOMATTER_ALLOCATOR_H
|
|
|
|
#define MICROPY_INCLUDED_SHARED_MODULE_PROTOMATTER_ALLOCATOR_H
|
|
|
|
|
|
|
|
#include <stdbool.h>
|
2020-04-07 12:19:24 -04:00
|
|
|
#include "py/gc.h"
|
2020-03-10 14:12:01 -04:00
|
|
|
#include "py/misc.h"
|
|
|
|
#include "supervisor/memory.h"
|
|
|
|
|
|
|
|
#define _PM_ALLOCATOR _PM_allocator_impl
|
2020-04-01 12:59:15 -04:00
|
|
|
#define _PM_FREE(x) (_PM_free_impl((x)), (x)=NULL, (void)0)
|
2020-03-10 14:12:01 -04:00
|
|
|
|
|
|
|
static inline void *_PM_allocator_impl(size_t sz) {
|
2020-04-07 12:19:24 -04:00
|
|
|
if (gc_alloc_possible()) {
|
2020-03-10 14:12:01 -04:00
|
|
|
return m_malloc(sz + sizeof(void*), true);
|
2020-04-07 12:19:24 -04:00
|
|
|
} else {
|
2020-04-13 09:51:35 -04:00
|
|
|
supervisor_allocation *allocation = allocate_memory(align32_size(sz), false);
|
2020-04-07 12:19:24 -04:00
|
|
|
return allocation ? allocation->ptr : NULL;
|
2020-03-10 14:12:01 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void _PM_free_impl(void *ptr_in) {
|
|
|
|
supervisor_allocation *allocation = allocation_from_ptr(ptr_in);
|
2020-04-13 09:51:35 -04:00
|
|
|
|
2020-03-10 14:12:01 -04:00
|
|
|
if (allocation) {
|
|
|
|
free_memory(allocation);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|