2018-05-24 16:12:18 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2018 Scott Shawcroft for Adafruit Industries
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2018-08-02 17:35:46 -04:00
|
|
|
// Basic allocations outside them for areas such as the VM heap and stack.
|
|
|
|
// supervisor/shared/memory.c has a basic implementation for a continuous chunk of memory. Add it
|
|
|
|
// to a SRC_ in a Makefile to use it.
|
|
|
|
|
2018-05-24 16:12:18 -04:00
|
|
|
#ifndef MICROPY_INCLUDED_SUPERVISOR_MEMORY_H
|
|
|
|
#define MICROPY_INCLUDED_SUPERVISOR_MEMORY_H
|
|
|
|
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdint.h>
|
2020-10-11 08:59:33 -04:00
|
|
|
#include <stddef.h>
|
2018-05-24 16:12:18 -04:00
|
|
|
|
|
|
|
typedef struct {
|
2021-03-15 09:57:36 -04:00
|
|
|
uint32_t *ptr;
|
2018-05-24 16:12:18 -04:00
|
|
|
} supervisor_allocation;
|
|
|
|
|
2019-01-10 14:00:40 -05:00
|
|
|
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
void free_memory(supervisor_allocation *allocation);
|
2020-10-11 08:59:33 -04:00
|
|
|
|
|
|
|
// Find the allocation with the given ptr, NULL if not found. When called from the context of a
|
|
|
|
// supervisor_move_memory() callback, finds the allocation that had that ptr *before* the move, but
|
|
|
|
// the returned allocation already contains the ptr after the move.
|
|
|
|
// When called with NULL, may return either NULL or an unused allocation whose ptr is NULL (this is
|
|
|
|
// a feature used internally in allocate_memory to save code size). Passing the return value to
|
|
|
|
// free_memory() is a permissible no-op in either case.
|
2021-03-15 09:57:36 -04:00
|
|
|
supervisor_allocation *allocation_from_ptr(void *ptr);
|
2020-10-11 08:59:33 -04:00
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
supervisor_allocation *allocate_remaining_memory(void);
|
2018-08-02 17:35:46 -04:00
|
|
|
|
|
|
|
// Allocate a piece of a given length in bytes. If high_address is true then it should be allocated
|
|
|
|
// at a lower address from the top of the stack. Otherwise, addresses will increase starting after
|
2020-10-11 08:59:33 -04:00
|
|
|
// statically allocated memory. If movable is false, memory will be taken from outside the GC heap
|
|
|
|
// and will stay stationary until freed. While the VM is running, this will fail unless a previous
|
|
|
|
// allocation of exactly matching length has recently been freed. If movable is true, memory will be
|
|
|
|
// taken from either outside or inside the GC heap, and when the VM exits, will be moved outside.
|
|
|
|
// The ptr of the returned supervisor_allocation will change at that point. If you need to be
|
|
|
|
// notified of that, add your own callback function at the designated place near the end of
|
|
|
|
// supervisor_move_memory().
|
2021-03-15 09:57:36 -04:00
|
|
|
supervisor_allocation *allocate_memory(uint32_t length, bool high_address, bool movable);
|
2018-05-24 16:12:18 -04:00
|
|
|
|
2020-10-23 16:35:56 -04:00
|
|
|
static inline size_t align32_size(size_t size) {
|
|
|
|
return (size + 3) & ~3;
|
2019-01-10 14:00:40 -05:00
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
size_t get_allocation_length(supervisor_allocation *allocation);
|
2020-10-11 08:59:33 -04:00
|
|
|
|
|
|
|
// Called after the GC heap is freed, transfers movable allocations from the GC heap to the
|
|
|
|
// supervisor heap and compacts the supervisor heap.
|
2019-01-25 19:59:18 -05:00
|
|
|
void supervisor_move_memory(void);
|
|
|
|
|
2018-05-24 16:12:18 -04:00
|
|
|
#endif // MICROPY_INCLUDED_SUPERVISOR_MEMORY_H
|