alloc.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. #include <stdio.h>
  27. #include <stdint.h>
  28. #include <stdlib.h>
  29. #include <string.h>
  30. #include <sys/mman.h>
  31. #include "py/mpstate.h"
  32. #include "py/gc.h"
  33. #if MICROPY_EMIT_NATIVE || (MICROPY_PY_FFI && MICROPY_FORCE_PLAT_ALLOC_EXEC)
  34. #if defined(__OpenBSD__) || defined(__MACH__)
  35. #define MAP_ANONYMOUS MAP_ANON
  36. #endif
  37. // The memory allocated here is not on the GC heap (and it may contain pointers
  38. // that need to be GC'd) so we must somehow trace this memory. We do it by
  39. // keeping a linked list of all mmap'd regions, and tracing them explicitly.
  40. typedef struct _mmap_region_t {
  41. void *ptr;
  42. size_t len;
  43. struct _mmap_region_t *next;
  44. } mmap_region_t;
  45. void mp_unix_alloc_exec(size_t min_size, void **ptr, size_t *size) {
  46. // size needs to be a multiple of the page size
  47. *size = (min_size + 0xfff) & (~0xfff);
  48. *ptr = mmap(NULL, *size, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  49. if (*ptr == MAP_FAILED) {
  50. *ptr = NULL;
  51. }
  52. // add new link to the list of mmap'd regions
  53. mmap_region_t *rg = m_new_obj(mmap_region_t);
  54. rg->ptr = *ptr;
  55. rg->len = min_size;
  56. rg->next = MP_STATE_VM(mmap_region_head);
  57. MP_STATE_VM(mmap_region_head) = rg;
  58. }
  59. void mp_unix_free_exec(void *ptr, size_t size) {
  60. munmap(ptr, size);
  61. // unlink the mmap'd region from the list
  62. for (mmap_region_t **rg = (mmap_region_t**)&MP_STATE_VM(mmap_region_head); *rg != NULL; *rg = (*rg)->next) {
  63. if ((*rg)->ptr == ptr) {
  64. mmap_region_t *next = (*rg)->next;
  65. m_del_obj(mmap_region_t, *rg);
  66. *rg = next;
  67. return;
  68. }
  69. }
  70. }
  71. void mp_unix_mark_exec(void) {
  72. for (mmap_region_t *rg = MP_STATE_VM(mmap_region_head); rg != NULL; rg = rg->next) {
  73. gc_collect_root(rg->ptr, rg->len / sizeof(mp_uint_t));
  74. }
  75. }
  76. #if MICROPY_FORCE_PLAT_ALLOC_EXEC
  77. // Provide implementation of libffi ffi_closure_* functions in terms
  78. // of the functions above. On a normal Linux system, this save a lot
  79. // of code size.
  80. void *ffi_closure_alloc(size_t size, void **code);
  81. void ffi_closure_free(void *ptr);
  82. void *ffi_closure_alloc(size_t size, void **code) {
  83. size_t dummy;
  84. mp_unix_alloc_exec(size, code, &dummy);
  85. return *code;
  86. }
  87. void ffi_closure_free(void *ptr) {
  88. (void)ptr;
  89. // TODO
  90. }
  91. #endif
  92. #endif // MICROPY_EMIT_NATIVE || (MICROPY_PY_FFI && MICROPY_FORCE_PLAT_ALLOC_EXEC)