memory.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* Declarations for the memory interface.
  2. This file is part of khipu.
  3. khipu is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Lesser General Public License as published by
  5. the Free Software Foundation; either version 3 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public License
  12. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  13. #ifndef __KP_MEMORY__
  14. #define __KP_MEMORY__ 1
  15. #include "interp.hpp"
  16. #include "initop.hpp"
  17. KP_DECLS_BEGIN
  18. /* Allocate a block of SIZE bytes, aligned for any type necessary.
  19. * Does NOT register the block (i.e: It wont' be visible to the GC). */
  20. KP_EXPORT void* xmalloc (size_t size);
  21. /* Reallocate a memory block for NSIZE bytes. Like above, this function
  22. * won't make the resulting block GC-visible. */
  23. KP_EXPORT void* xrealloc (void *ptr, size_t nsize);
  24. /* Allocate a varobject, given SIZE, TYPE and MASK, filling the
  25. * header with such data. Does not register the header. */
  26. KP_EXPORT void* alloch (size_t size, int type, int mask = TYPE_SHIFT);
  27. template <typename T>
  28. T* alloch (int mask = TYPE_SHIFT)
  29. {
  30. return ((T *)alloch (sizeof (T), T::code, mask));
  31. }
  32. // Deallocate memory for PTR. Does not unregister the object.
  33. KP_EXPORT void xfree (void *ptr);
  34. // Register a varobj of SIZE bytes, thus making it visible to the GC.
  35. KP_EXPORT void gc_register (interpreter *interp, varobj *ptr, size_t size);
  36. template <typename T>
  37. void gc_register (interpreter *interp, T *ptr)
  38. {
  39. gc_register (interp, ptr, sizeof (*ptr));
  40. }
  41. // Handler for the GC request event.
  42. KP_EXPORT result<object> gcreq_handler (interpreter *interp,
  43. object *argv, int argc);
  44. // Allocate a memory manager.
  45. KP_EXPORT memmgr* memmgr_alloc ();
  46. // Free a memory manager.
  47. KP_EXPORT void memmgr_free (memmgr *mgrp);
  48. // Raise an out-of-memory error.
  49. [[noreturn]] KP_EXPORT void raise_oom (interpreter *interp = nullptr);
  50. // Acquire the GC lock.
  51. KP_EXPORT void gc_lock (interpreter *interp);
  52. // Get a reference to the GC status (enabled or disabled).
  53. KP_EXPORT bool& gc_status ();
  54. inline bool
  55. gc_enable ()
  56. {
  57. bool ret = gc_status ();
  58. gc_status() = true;
  59. return (ret);
  60. }
  61. inline bool
  62. gc_disable ()
  63. {
  64. bool ret = gc_status ();
  65. gc_status() = false;
  66. return (ret);
  67. }
  68. struct gc_guard
  69. {
  70. bool prev;
  71. gc_guard () : prev (gc_disable ())
  72. {
  73. }
  74. ~gc_guard ()
  75. {
  76. if (this->prev)
  77. gc_enable ();
  78. }
  79. };
  80. // Release the GC lock.
  81. KP_EXPORT void gc_unlock ();
  82. // Perform a garbage collection. Collect all generations if FULL is true.
  83. KP_EXPORT result<void> gc (bool full);
  84. // Generate a write barrier for object OBJ contained in PARENT.
  85. KP_EXPORT result<void> gc_wbarrier (interpreter *interp,
  86. object parent, object obj);
  87. // Initialize the memory subsystem.
  88. KP_EXPORT bool memory_init ();
  89. // Call finalizers before exiting.
  90. KP_EXPORT void memory_exit ();
  91. KP_DECLS_END
  92. #endif