pool_allocator.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /**************************************************************************/
  2. /* pool_allocator.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef POOL_ALLOCATOR_H
  31. #define POOL_ALLOCATOR_H
  32. #include "core/typedefs.h"
  33. /**
  34. * Generic Pool Allocator.
  35. * This is a generic memory pool allocator, with locking, compacting and alignment. (@TODO alignment)
  36. * It used as a standard way to manage allocation in a specific region of memory, such as texture memory,
  37. * audio sample memory, or just any kind of memory overall.
  38. * (@TODO) abstraction should be greater, because in many platforms, you need to manage a nonreachable memory.
  39. */
  40. enum {
  41. POOL_ALLOCATOR_INVALID_ID = -1 ///< default invalid value. use INVALID_ID( id ) to test
  42. };
  43. class PoolAllocator {
  44. public:
  45. typedef int ID;
  46. private:
  47. enum {
  48. CHECK_BITS = 8,
  49. CHECK_LEN = (1 << CHECK_BITS),
  50. CHECK_MASK = CHECK_LEN - 1
  51. };
  52. struct Entry {
  53. unsigned int pos = 0;
  54. unsigned int len = 0;
  55. unsigned int lock = 0;
  56. unsigned int check = 0;
  57. inline void clear() {
  58. pos = 0;
  59. len = 0;
  60. lock = 0;
  61. check = 0;
  62. }
  63. Entry() {}
  64. };
  65. typedef int EntryArrayPos;
  66. typedef int EntryIndicesPos;
  67. Entry *entry_array = nullptr;
  68. int *entry_indices = nullptr;
  69. int entry_max = 0;
  70. int entry_count = 0;
  71. uint8_t *pool = nullptr;
  72. void *mem_ptr = nullptr;
  73. int pool_size = 0;
  74. int free_mem = 0;
  75. int free_mem_peak = 0;
  76. unsigned int check_count = 0;
  77. int align = 1;
  78. bool needs_locking = false;
  79. inline int entry_end(const Entry &p_entry) const {
  80. return p_entry.pos + aligned(p_entry.len);
  81. }
  82. inline int aligned(int p_size) const {
  83. int rem = p_size % align;
  84. if (rem) {
  85. p_size += align - rem;
  86. }
  87. return p_size;
  88. }
  89. void compact(int p_up_to = -1);
  90. void compact_up(int p_from = 0);
  91. bool get_free_entry(EntryArrayPos *p_pos);
  92. bool find_hole(EntryArrayPos *p_pos, int p_for_size);
  93. bool find_entry_index(EntryIndicesPos *p_map_pos, const Entry *p_entry);
  94. Entry *get_entry(ID p_mem);
  95. const Entry *get_entry(ID p_mem) const;
  96. void create_pool(void *p_mem, int p_size, int p_max_entries);
  97. protected:
  98. virtual void mt_lock() const; ///< Reimplement for custom mt locking
  99. virtual void mt_unlock() const; ///< Reimplement for custom mt locking
  100. public:
  101. enum {
  102. DEFAULT_MAX_ALLOCS = 4096,
  103. };
  104. ID alloc(int p_size); ///< Alloc memory, get an ID on success, POOL_ALOCATOR_INVALID_ID on failure
  105. void free(ID p_mem); ///< Free allocated memory
  106. Error resize(ID p_mem, int p_new_size); ///< resize a memory chunk
  107. int get_size(ID p_mem) const;
  108. int get_free_mem(); ///< get free memory
  109. int get_used_mem() const;
  110. int get_free_peak(); ///< get free memory
  111. Error lock(ID p_mem); //@todo move this out
  112. void *get(ID p_mem);
  113. const void *get(ID p_mem) const;
  114. void unlock(ID p_mem);
  115. bool is_locked(ID p_mem) const;
  116. PoolAllocator(int p_size, bool p_needs_locking = false, int p_max_entries = DEFAULT_MAX_ALLOCS);
  117. PoolAllocator(void *p_mem, int p_size, int p_align = 1, bool p_needs_locking = false, int p_max_entries = DEFAULT_MAX_ALLOCS);
  118. PoolAllocator(int p_align, int p_size, bool p_needs_locking = false, int p_max_entries = DEFAULT_MAX_ALLOCS);
  119. virtual ~PoolAllocator();
  120. };
  121. #endif // POOL_ALLOCATOR_H