|
| 1 | +/* |
| 2 | + * This file is part of the MicroPython project, http://micropython.org/ |
| 3 | + * |
| 4 | + * The MIT License (MIT) |
| 5 | + * |
| 6 | + * Copyright (c) 2018 Scott Shawcroft for Adafruit Industries |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + * of this software and associated documentation files (the "Software"), to deal |
| 10 | + * in the Software without restriction, including without limitation the rights |
| 11 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | + * copies of the Software, and to permit persons to whom the Software is |
| 13 | + * furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in |
| 16 | + * all copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 24 | + * THE SOFTWARE. |
| 25 | + */ |
| 26 | + |
| 27 | +#include "supervisor/memory.h" |
| 28 | + |
| 29 | +#include <stddef.h> |
| 30 | + |
| 31 | +#define CIRCUITPY_SUPERVISOR_ALLOC_COUNT 8 |
| 32 | + |
| 33 | +static supervisor_allocation allocations[CIRCUITPY_SUPERVISOR_ALLOC_COUNT]; |
| 34 | +// We use uint32_t* to ensure word (4 byte) alignment. |
| 35 | +uint32_t* low_address; |
| 36 | +uint32_t* high_address; |
| 37 | +extern uint32_t _ebss; |
| 38 | +extern uint32_t _estack; |
| 39 | + |
| 40 | +void memory_init(void) { |
| 41 | + low_address = &_ebss; |
| 42 | + high_address = &_estack; |
| 43 | +} |
| 44 | + |
| 45 | +void free_memory(supervisor_allocation* allocation) { |
| 46 | + uint8_t index = 0; |
| 47 | + bool found = false; |
| 48 | + for (index = 0; index < CIRCUITPY_SUPERVISOR_ALLOC_COUNT; index++) { |
| 49 | + found = allocation == &allocations[index]; |
| 50 | + if (found) { |
| 51 | + break; |
| 52 | + } |
| 53 | + } |
| 54 | + if (allocation->ptr == high_address) { |
| 55 | + high_address += allocation->length / 4; |
| 56 | + for (index++; index < CIRCUITPY_SUPERVISOR_ALLOC_COUNT; index++) { |
| 57 | + if (allocations[index].ptr != NULL) { |
| 58 | + break; |
| 59 | + } |
| 60 | + high_address += allocations[index].length / 4; |
| 61 | + } |
| 62 | + } else if (allocation->ptr + allocation->length / 4 == low_address) { |
| 63 | + low_address = allocation->ptr; |
| 64 | + for (index--; index >= 0; index--) { |
| 65 | + if (allocations[index].ptr != NULL) { |
| 66 | + break; |
| 67 | + } |
| 68 | + low_address -= allocations[index].length / 4; |
| 69 | + } |
| 70 | + } else { |
| 71 | + // Freed memory isn't in the middle so skip updating bounds. The memory will be added to the |
| 72 | + // middle when the memory to the inside is freed. |
| 73 | + } |
| 74 | + allocation->ptr = NULL; |
| 75 | +} |
| 76 | + |
| 77 | +supervisor_allocation* allocate_remaining_memory(void) { |
| 78 | + if (low_address == high_address) { |
| 79 | + return NULL; |
| 80 | + } |
| 81 | + return allocate_memory((high_address - low_address) * 4, false); |
| 82 | +} |
| 83 | + |
| 84 | +supervisor_allocation* allocate_memory(uint32_t length, bool high) { |
| 85 | + if ((high_address - low_address) * 4 < (int32_t) length) { |
| 86 | + return NULL; |
| 87 | + } |
| 88 | + uint8_t index = 0; |
| 89 | + int8_t direction = 1; |
| 90 | + if (high) { |
| 91 | + index = CIRCUITPY_SUPERVISOR_ALLOC_COUNT - 1; |
| 92 | + direction = -1; |
| 93 | + } |
| 94 | + for (; index < CIRCUITPY_SUPERVISOR_ALLOC_COUNT; index += direction) { |
| 95 | + if (allocations[index].ptr == NULL) { |
| 96 | + break; |
| 97 | + } |
| 98 | + } |
| 99 | + if (index >= CIRCUITPY_SUPERVISOR_ALLOC_COUNT) { |
| 100 | + return NULL; |
| 101 | + } |
| 102 | + supervisor_allocation* alloc = &allocations[index]; |
| 103 | + if (high) { |
| 104 | + high_address -= length / 4; |
| 105 | + alloc->ptr = high_address; |
| 106 | + } else { |
| 107 | + alloc->ptr = low_address; |
| 108 | + low_address += length / 4; |
| 109 | + } |
| 110 | + alloc->length = length; |
| 111 | + return alloc; |
| 112 | +} |
0 commit comments