Branch data Line data Source code
1 : : /* 2 : : * This file is part of the MicroPython project, http://micropython.org/ 3 : : * 4 : : * The MIT License (MIT) 5 : : * 6 : : * Copyright (c) 2014 Fabian Vogt 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 <stdio.h> 28 : : #include <stdint.h> 29 : : #include <stdlib.h> 30 : : #include <string.h> 31 : : #include <sys/mman.h> 32 : : 33 : : #include "py/mpstate.h" 34 : : 35 : : #if MICROPY_EMIT_NATIVE 36 : : 37 : : #if defined(__OpenBSD__) || defined(__MACH__) 38 : : #define MAP_ANONYMOUS MAP_ANON 39 : : #endif 40 : : 41 : : // The memory allocated here is not on the GC heap (and it may contain pointers 42 : : // that need to be GC'd) so we must somehow trace this memory. We do it by 43 : : // keeping a linked list of all mmap'd regions, and tracing them explicitly. 44 : : 45 : : typedef struct _mmap_region_t { 46 : : void *ptr; 47 : : size_t len; 48 : : struct _mmap_region_t *next; 49 : : } mmap_region_t; 50 : : 51 : 6636 : void mp_unix_alloc_exec(size_t min_size, void **ptr, size_t *size) { 52 : : // size needs to be a multiple of the page size 53 : 6636 : *size = (min_size + 0xfff) & (~0xfff); 54 : 6636 : *ptr = mmap(NULL, *size, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); 55 [ - + ]: 6636 : if (*ptr == MAP_FAILED) { 56 : 0 : *ptr = NULL; 57 : : } 58 : : 59 : : // add new link to the list of mmap'd regions 60 : 6636 : mmap_region_t *rg = m_new_obj(mmap_region_t); 61 : 6636 : rg->ptr = *ptr; 62 : 6636 : rg->len = min_size; 63 : 6636 : rg->next = MP_STATE_VM(mmap_region_head); 64 : 6636 : MP_STATE_VM(mmap_region_head) = rg; 65 : 6636 : } 66 : : 67 : 0 : void mp_unix_free_exec(void *ptr, size_t size) { 68 : 0 : munmap(ptr, size); 69 : : 70 : : // unlink the mmap'd region from the list 71 [ # # ]: 0 : for (mmap_region_t **rg = (mmap_region_t **)&MP_STATE_VM(mmap_region_head); *rg != NULL; *rg = (*rg)->next) { 72 [ # # ]: 0 : if ((*rg)->ptr == ptr) { 73 : 0 : mmap_region_t *next = (*rg)->next; 74 : 0 : m_del_obj(mmap_region_t, *rg); 75 : 0 : *rg = next; 76 : 0 : return; 77 : : } 78 : : } 79 : : } 80 : : 81 : : MP_REGISTER_ROOT_POINTER(void *mmap_region_head); 82 : : 83 : : #endif // MICROPY_EMIT_NATIVE