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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "supervisor/memory.h"
|
2019-10-18 05:05:08 -04:00
|
|
|
#include "supervisor/port.h"
|
2018-05-24 16:12:18 -04:00
|
|
|
|
2020-10-11 08:59:33 -04:00
|
|
|
#include <string.h>
|
2018-05-24 16:12:18 -04:00
|
|
|
|
2020-10-11 08:59:33 -04:00
|
|
|
#include "py/gc.h"
|
2019-01-25 19:59:18 -05:00
|
|
|
#include "supervisor/shared/display.h"
|
|
|
|
|
2020-10-28 16:50:28 -04:00
|
|
|
enum {
|
2020-11-29 10:27:36 -05:00
|
|
|
CIRCUITPY_SUPERVISOR_IMMOVABLE_ALLOC_COUNT =
|
2021-04-30 10:40:12 -04:00
|
|
|
// stack + heap
|
|
|
|
2
|
2021-06-20 15:25:37 -04:00
|
|
|
// next_code_allocation
|
|
|
|
+ 1
|
2021-04-28 13:00:44 -04:00
|
|
|
|
2021-04-30 10:40:12 -04:00
|
|
|
#if INTERNAL_FLASH_FILESYSTEM == 0
|
|
|
|
+ 1
|
|
|
|
#endif
|
2021-04-28 13:00:44 -04:00
|
|
|
|
2021-04-30 10:40:12 -04:00
|
|
|
#if CIRCUITPY_USB
|
|
|
|
+ 1 // device_descriptor_allocation
|
|
|
|
+ 1 // configuration_descriptor_allocation
|
|
|
|
+ 1 // string_descriptors_allocation
|
|
|
|
#endif
|
2021-04-28 13:00:44 -04:00
|
|
|
|
2021-04-30 10:40:12 -04:00
|
|
|
#if CIRCUITPY_USB_HID
|
|
|
|
+ 1 // hid_report_descriptor_allocation
|
|
|
|
+ 1 // hid_devices_allocation
|
|
|
|
#endif
|
2020-11-29 10:27:36 -05:00
|
|
|
,
|
2021-04-28 13:00:44 -04:00
|
|
|
|
2020-11-29 10:27:36 -05:00
|
|
|
CIRCUITPY_SUPERVISOR_MOVABLE_ALLOC_COUNT =
|
2021-03-15 09:57:36 -04:00
|
|
|
0
|
2021-04-30 10:40:12 -04:00
|
|
|
#if CIRCUITPY_DISPLAYIO
|
|
|
|
#if CIRCUITPY_TERMINALIO
|
|
|
|
+ 1
|
|
|
|
#endif
|
|
|
|
+ CIRCUITPY_DISPLAY_LIMIT * (
|
|
|
|
// Maximum needs of one display: max(4 if RGBMATRIX, 1 if SHARPDISPLAY, 0)
|
|
|
|
#if CIRCUITPY_RGBMATRIX
|
|
|
|
4
|
|
|
|
#elif CIRCUITPY_SHARPDISPLAY
|
|
|
|
1
|
|
|
|
#else
|
|
|
|
0
|
|
|
|
#endif
|
|
|
|
)
|
2020-10-28 16:50:28 -04:00
|
|
|
#endif
|
2020-11-29 10:27:36 -05:00
|
|
|
,
|
2021-04-28 13:00:44 -04:00
|
|
|
|
2020-11-29 10:27:36 -05:00
|
|
|
CIRCUITPY_SUPERVISOR_ALLOC_COUNT = CIRCUITPY_SUPERVISOR_IMMOVABLE_ALLOC_COUNT + CIRCUITPY_SUPERVISOR_MOVABLE_ALLOC_COUNT
|
2020-10-28 16:50:28 -04:00
|
|
|
};
|
2018-05-24 16:12:18 -04:00
|
|
|
|
2020-09-29 16:21:29 -04:00
|
|
|
// The lowest two bits of a valid length are always zero, so we can use them to mark an allocation
|
2020-10-11 08:59:33 -04:00
|
|
|
// as a hole (freed by the client but not yet reclaimed into the free middle) and as movable.
|
|
|
|
#define FLAGS 3
|
2020-09-29 16:21:29 -04:00
|
|
|
#define HOLE 1
|
2020-10-11 08:59:33 -04:00
|
|
|
#define MOVABLE 2
|
2020-09-29 16:21:29 -04:00
|
|
|
|
2018-05-24 16:12:18 -04:00
|
|
|
static supervisor_allocation allocations[CIRCUITPY_SUPERVISOR_ALLOC_COUNT];
|
2021-03-15 09:57:36 -04:00
|
|
|
supervisor_allocation *old_allocations;
|
2018-05-24 16:12:18 -04:00
|
|
|
|
2020-10-11 08:59:33 -04:00
|
|
|
typedef struct _supervisor_allocation_node {
|
2021-03-15 09:57:36 -04:00
|
|
|
struct _supervisor_allocation_node *next;
|
2020-10-11 08:59:33 -04:00
|
|
|
size_t length;
|
|
|
|
// We use uint32_t to ensure word (4 byte) alignment.
|
|
|
|
uint32_t data[];
|
|
|
|
} supervisor_allocation_node;
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
supervisor_allocation_node *low_head;
|
|
|
|
supervisor_allocation_node *high_head;
|
2020-10-11 08:59:33 -04:00
|
|
|
|
|
|
|
// Intermediate (void*) is to suppress -Wcast-align warning. Alignment will always be correct
|
|
|
|
// because this only reverses how (alloc)->ptr was obtained as &(node->data[0]).
|
2021-03-15 09:57:36 -04:00
|
|
|
#define ALLOCATION_NODE(alloc) ((supervisor_allocation_node *)(void *)((char *)((alloc)->ptr) - sizeof(supervisor_allocation_node)))
|
2018-05-24 16:12:18 -04:00
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
void free_memory(supervisor_allocation *allocation) {
|
2020-10-11 08:59:33 -04:00
|
|
|
if (allocation == NULL || allocation->ptr == NULL) {
|
2020-01-08 23:32:45 -05:00
|
|
|
return;
|
|
|
|
}
|
2021-03-15 09:57:36 -04:00
|
|
|
supervisor_allocation_node *node = ALLOCATION_NODE(allocation);
|
2020-10-11 08:59:33 -04:00
|
|
|
if (node == low_head) {
|
|
|
|
do {
|
|
|
|
low_head = low_head->next;
|
|
|
|
} while (low_head != NULL && (low_head->length & HOLE));
|
2021-03-15 09:57:36 -04:00
|
|
|
} else if (node == high_head) {
|
2020-10-11 08:59:33 -04:00
|
|
|
do {
|
|
|
|
high_head = high_head->next;
|
|
|
|
} while (high_head != NULL && (high_head->length & HOLE));
|
2021-03-15 09:57:36 -04:00
|
|
|
} else {
|
2020-10-11 08:59:33 -04:00
|
|
|
// Check if it's in the list of embedded allocations.
|
2021-03-15 09:57:36 -04:00
|
|
|
supervisor_allocation_node **emb = &MP_STATE_VM(first_embedded_allocation);
|
2020-11-30 17:33:07 -05:00
|
|
|
while (*emb != NULL && *emb != node) {
|
|
|
|
emb = &((*emb)->next);
|
|
|
|
}
|
|
|
|
if (*emb != NULL) {
|
|
|
|
// Found, remove it from the list.
|
|
|
|
*emb = node->next;
|
|
|
|
m_free(node
|
2021-03-15 09:57:36 -04:00
|
|
|
#if MICROPY_MALLOC_USES_ALLOCATED_SIZE
|
2020-11-30 17:33:07 -05:00
|
|
|
, sizeof(supervisor_allocation_node) + (node->length & ~FLAGS)
|
2021-03-15 09:57:36 -04:00
|
|
|
#endif
|
|
|
|
);
|
|
|
|
} else {
|
2020-11-30 17:33:07 -05:00
|
|
|
// Else it must be within the low or high ranges and becomes a hole.
|
|
|
|
node->length = ((node->length & ~FLAGS) | HOLE);
|
2018-07-25 08:06:30 -04:00
|
|
|
}
|
2018-05-24 16:12:18 -04:00
|
|
|
}
|
2020-10-11 08:59:33 -04:00
|
|
|
allocation->ptr = NULL;
|
2018-05-24 16:12:18 -04:00
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
supervisor_allocation *allocation_from_ptr(void *ptr) {
|
2020-10-11 08:59:33 -04:00
|
|
|
// When called from the context of supervisor_move_memory() (old_allocations != NULL), search
|
|
|
|
// by old pointer to give clients a way of mapping from old to new pointer. But not if
|
|
|
|
// ptr == NULL, then the caller wants an allocation whose current ptr is NULL.
|
2021-03-15 09:57:36 -04:00
|
|
|
supervisor_allocation *list = (old_allocations && ptr) ? old_allocations : &allocations[0];
|
2020-03-25 14:40:48 -04:00
|
|
|
for (size_t index = 0; index < CIRCUITPY_SUPERVISOR_ALLOC_COUNT; index++) {
|
2020-10-11 08:59:33 -04:00
|
|
|
if (list[index].ptr == ptr) {
|
2020-03-25 14:40:48 -04:00
|
|
|
return &allocations[index];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
supervisor_allocation *allocate_remaining_memory(void) {
|
2020-11-29 10:04:31 -05:00
|
|
|
return allocate_memory((uint32_t)-1, false, false);
|
2020-10-11 08:59:33 -04:00
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
static supervisor_allocation_node *find_hole(supervisor_allocation_node *node, size_t length) {
|
2020-10-11 08:59:33 -04:00
|
|
|
for (; node != NULL; node = node->next) {
|
|
|
|
if (node->length == (length | HOLE)) {
|
|
|
|
break;
|
|
|
|
}
|
2018-05-24 16:12:18 -04:00
|
|
|
}
|
2020-10-11 08:59:33 -04:00
|
|
|
return node;
|
2018-05-24 16:12:18 -04:00
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
static supervisor_allocation_node *allocate_memory_node(uint32_t length, bool high, bool movable) {
|
2020-11-29 10:27:36 -05:00
|
|
|
if (CIRCUITPY_SUPERVISOR_MOVABLE_ALLOC_COUNT == 0) {
|
|
|
|
assert(!movable);
|
|
|
|
}
|
2020-10-11 08:59:33 -04:00
|
|
|
// supervisor_move_memory() currently does not support movable allocations on the high side, it
|
|
|
|
// must be extended first if this is ever needed.
|
|
|
|
assert(!(high && movable));
|
2021-03-15 09:57:36 -04:00
|
|
|
uint32_t *low_address = low_head ? low_head->data + low_head->length / 4 : port_heap_get_bottom();
|
|
|
|
uint32_t *high_address = high_head ? (uint32_t *)high_head : port_heap_get_top();
|
2020-11-29 10:04:31 -05:00
|
|
|
// Special case for allocate_remaining_memory(), avoids computing low/high_address twice.
|
|
|
|
if (length == (uint32_t)-1) {
|
|
|
|
length = (high_address - low_address) * 4 - sizeof(supervisor_allocation_node);
|
|
|
|
}
|
2020-09-29 16:21:29 -04:00
|
|
|
if (length == 0 || length % 4 != 0) {
|
2018-05-24 16:12:18 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
2020-10-11 08:59:33 -04:00
|
|
|
// 1. Matching hole on the requested side?
|
2021-03-15 09:57:36 -04:00
|
|
|
supervisor_allocation_node *node = find_hole(high ? high_head : low_head, length);
|
2020-10-11 08:59:33 -04:00
|
|
|
if (!node) {
|
|
|
|
// 2. Enough free space in the middle?
|
|
|
|
if ((high_address - low_address) * 4 >= (int32_t)(sizeof(supervisor_allocation_node) + length)) {
|
|
|
|
if (high) {
|
|
|
|
high_address -= (sizeof(supervisor_allocation_node) + length) / 4;
|
2021-03-15 09:57:36 -04:00
|
|
|
node = (supervisor_allocation_node *)high_address;
|
2020-10-11 08:59:33 -04:00
|
|
|
node->next = high_head;
|
|
|
|
high_head = node;
|
2021-03-15 09:57:36 -04:00
|
|
|
} else {
|
|
|
|
node = (supervisor_allocation_node *)low_address;
|
2020-10-11 08:59:33 -04:00
|
|
|
node->next = low_head;
|
|
|
|
low_head = node;
|
|
|
|
}
|
2021-03-15 09:57:36 -04:00
|
|
|
} else {
|
2020-10-11 08:59:33 -04:00
|
|
|
// 3. Matching hole on the other side?
|
|
|
|
node = find_hole(high ? low_head : high_head, length);
|
|
|
|
if (!node) {
|
|
|
|
// 4. GC allocation?
|
|
|
|
if (movable && gc_alloc_possible()) {
|
|
|
|
node = m_malloc_maybe(sizeof(supervisor_allocation_node) + length, true);
|
|
|
|
if (node) {
|
|
|
|
node->next = MP_STATE_VM(first_embedded_allocation);
|
|
|
|
MP_STATE_VM(first_embedded_allocation) = node;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!node) {
|
|
|
|
// 5. Give up.
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2020-09-29 16:21:29 -04:00
|
|
|
}
|
2018-05-24 16:12:18 -04:00
|
|
|
}
|
2020-10-11 08:59:33 -04:00
|
|
|
node->length = length;
|
|
|
|
if (movable) {
|
|
|
|
node->length |= MOVABLE;
|
|
|
|
}
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2021-03-15 09:57:36 -04:00
|
|
|
supervisor_allocation *allocate_memory(uint32_t length, bool high, bool movable) {
|
|
|
|
supervisor_allocation_node *node = allocate_memory_node(length, high, movable);
|
2020-10-11 08:59:33 -04:00
|
|
|
if (!node) {
|
2018-05-24 16:12:18 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
2020-10-11 08:59:33 -04:00
|
|
|
// Find the first free allocation.
|
2021-03-15 09:57:36 -04:00
|
|
|
supervisor_allocation *alloc = allocation_from_ptr(NULL);
|
2020-10-11 08:59:33 -04:00
|
|
|
if (!alloc) {
|
|
|
|
// We should free node again to avoid leaking, but something is wrong anyway if clients try
|
|
|
|
// to make more allocations than available, so don't bother.
|
|
|
|
return NULL;
|
2018-05-24 16:12:18 -04:00
|
|
|
}
|
2020-10-11 08:59:33 -04:00
|
|
|
alloc->ptr = &(node->data[0]);
|
2018-05-24 16:12:18 -04:00
|
|
|
return alloc;
|
|
|
|
}
|
2019-01-25 19:59:18 -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
|
|
|
return ALLOCATION_NODE(allocation)->length & ~FLAGS;
|
|
|
|
}
|
|
|
|
|
2021-06-09 12:26:22 -04:00
|
|
|
|
2019-01-25 19:59:18 -05:00
|
|
|
void supervisor_move_memory(void) {
|
2020-11-29 10:27:36 -05:00
|
|
|
// This whole function is not needed when there are no movable allocations, let it be optimized
|
|
|
|
// out.
|
|
|
|
if (CIRCUITPY_SUPERVISOR_MOVABLE_ALLOC_COUNT == 0) {
|
|
|
|
return;
|
|
|
|
}
|
2020-10-11 08:59:33 -04:00
|
|
|
// This must be called exactly after freeing the heap, so that the embedded allocations, if any,
|
|
|
|
// are now in the free region.
|
2021-06-09 12:26:22 -04:00
|
|
|
assert(MP_STATE_VM(first_embedded_allocation) == NULL || (
|
|
|
|
(low_head == NULL || low_head < MP_STATE_VM(first_embedded_allocation)) &&
|
|
|
|
(high_head == NULL || MP_STATE_VM(first_embedded_allocation) < high_head)));
|
2020-10-11 08:59:33 -04:00
|
|
|
|
|
|
|
// Save the old pointers for allocation_from_ptr().
|
|
|
|
supervisor_allocation old_allocations_array[CIRCUITPY_SUPERVISOR_ALLOC_COUNT];
|
|
|
|
memcpy(old_allocations_array, allocations, sizeof(allocations));
|
|
|
|
|
|
|
|
// Compact the low side. Traverse the list repeatedly, finding movable allocations preceded by a
|
|
|
|
// hole and swapping them, until no more are found. This is not the most runtime-efficient way,
|
|
|
|
// but probably the shortest and simplest code.
|
|
|
|
bool acted;
|
|
|
|
do {
|
|
|
|
acted = false;
|
2021-03-15 09:57:36 -04:00
|
|
|
supervisor_allocation_node **nodep = &low_head;
|
2020-10-11 08:59:33 -04:00
|
|
|
while (*nodep != NULL && (*nodep)->next != NULL) {
|
|
|
|
if (((*nodep)->length & MOVABLE) && ((*nodep)->next->length & HOLE)) {
|
2021-03-15 09:57:36 -04:00
|
|
|
supervisor_allocation_node *oldnode = *nodep;
|
|
|
|
supervisor_allocation_node *start = oldnode->next;
|
|
|
|
supervisor_allocation *alloc = allocation_from_ptr(&(oldnode->data[0]));
|
2020-10-11 08:59:33 -04:00
|
|
|
assert(alloc != NULL);
|
|
|
|
alloc->ptr = &(start->data[0]);
|
|
|
|
oldnode->next = start->next;
|
|
|
|
size_t holelength = start->length;
|
|
|
|
size_t size = sizeof(supervisor_allocation_node) + (oldnode->length & ~FLAGS);
|
|
|
|
memmove(start, oldnode, size);
|
2021-03-15 09:57:36 -04:00
|
|
|
supervisor_allocation_node *newhole = (supervisor_allocation_node *)(void *)((char *)start + size);
|
2020-10-11 08:59:33 -04:00
|
|
|
newhole->next = start;
|
|
|
|
newhole->length = holelength;
|
|
|
|
*nodep = newhole;
|
|
|
|
acted = true;
|
|
|
|
}
|
|
|
|
nodep = &((*nodep)->next);
|
|
|
|
}
|
|
|
|
} while (acted);
|
|
|
|
// Any holes bubbled to the top can be absorbed into the free middle.
|
|
|
|
while (low_head != NULL && (low_head->length & HOLE)) {
|
|
|
|
low_head = low_head->next;
|
2021-03-15 09:57:36 -04:00
|
|
|
}
|
|
|
|
;
|
2020-10-11 08:59:33 -04:00
|
|
|
|
|
|
|
// Don't bother compacting the high side, there are no movable allocations and no holes there in
|
|
|
|
// current usage.
|
|
|
|
|
|
|
|
// Promote the embedded allocations to top-level ones, compacting them at the beginning of the
|
|
|
|
// now free region (or possibly in matching holes).
|
|
|
|
// The linked list is unordered, but allocations must be processed in order to avoid risking
|
|
|
|
// overwriting each other. To that end, repeatedly find the lowest element of the list, remove
|
|
|
|
// it from the list, and process it. This ad-hoc selection sort results in substantially shorter
|
|
|
|
// code than using the qsort() function from the C library.
|
|
|
|
while (MP_STATE_VM(first_embedded_allocation)) {
|
|
|
|
// First element is first candidate.
|
2021-03-15 09:57:36 -04:00
|
|
|
supervisor_allocation_node **pminnode = &MP_STATE_VM(first_embedded_allocation);
|
2020-10-11 08:59:33 -04:00
|
|
|
// Iterate from second element (if any) on.
|
2021-03-15 09:57:36 -04:00
|
|
|
for (supervisor_allocation_node **pnode = &(MP_STATE_VM(first_embedded_allocation)->next); *pnode != NULL; pnode = &(*pnode)->next) {
|
2020-10-11 08:59:33 -04:00
|
|
|
if (*pnode < *pminnode) {
|
|
|
|
pminnode = pnode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Remove from list.
|
2021-03-15 09:57:36 -04:00
|
|
|
supervisor_allocation_node *node = *pminnode;
|
2020-10-11 08:59:33 -04:00
|
|
|
*pminnode = node->next;
|
|
|
|
// Process.
|
|
|
|
size_t length = (node->length & ~FLAGS);
|
2021-03-15 09:57:36 -04:00
|
|
|
supervisor_allocation *alloc = allocation_from_ptr(&(node->data[0]));
|
2020-10-11 08:59:33 -04:00
|
|
|
assert(alloc != NULL);
|
|
|
|
// This may overwrite the header of node if it happened to be there already, but not the
|
|
|
|
// data.
|
2021-03-15 09:57:36 -04:00
|
|
|
supervisor_allocation_node *new_node = allocate_memory_node(length, false, true);
|
2020-10-11 08:59:33 -04:00
|
|
|
// There must be enough free space.
|
|
|
|
assert(new_node != NULL);
|
|
|
|
memmove(&(new_node->data[0]), &(node->data[0]), length);
|
|
|
|
alloc->ptr = &(new_node->data[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Notify clients that their movable allocations may have moved.
|
|
|
|
old_allocations = &old_allocations_array[0];
|
2021-04-23 21:44:13 -04:00
|
|
|
|
2020-04-17 19:23:28 -04:00
|
|
|
#if CIRCUITPY_DISPLAYIO
|
2019-01-25 19:59:18 -05:00
|
|
|
supervisor_display_move_memory();
|
2020-04-17 19:23:28 -04:00
|
|
|
#endif
|
2021-04-23 21:44:13 -04:00
|
|
|
|
2020-10-11 08:59:33 -04:00
|
|
|
// Add calls to further clients here.
|
|
|
|
old_allocations = NULL;
|
2019-01-25 19:59:18 -05:00
|
|
|
}
|