pool_allocator.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*************************************************************************/
  2. /* pool_allocator.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
  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. @author Juan Linietsky <reduzio@gmail.com>
  35. * Generic Pool Allocator.
  36. * This is a generic memory pool allocator, with locking, compacting and alignment. (@TODO alignment)
  37. * It used as a standard way to manage alloction in a specific region of memory, such as texture memory,
  38. * audio sample memory, or just any kind of memory overall.
  39. * (@TODO) abstraction should be greater, because in many platforms, you need to manage a nonreachable memory.
  40. */
  41. enum {
  42. POOL_ALLOCATOR_INVALID_ID = -1 ///< default invalid value. use INVALID_ID( id ) to test
  43. };
  44. class PoolAllocator {
  45. public:
  46. typedef int ID;
  47. private:
  48. enum {
  49. CHECK_BITS = 8,
  50. CHECK_LEN = (1 << CHECK_BITS),
  51. CHECK_MASK = CHECK_LEN - 1
  52. };
  53. struct Entry {
  54. unsigned int pos;
  55. unsigned int len;
  56. unsigned int lock;
  57. unsigned int check;
  58. inline void clear() {
  59. pos = 0;
  60. len = 0;
  61. lock = 0;
  62. check = 0;
  63. }
  64. Entry() { clear(); }
  65. };
  66. typedef int EntryArrayPos;
  67. typedef int EntryIndicesPos;
  68. Entry *entry_array;
  69. int *entry_indices;
  70. int entry_max;
  71. int entry_count;
  72. uint8_t *pool;
  73. void *mem_ptr;
  74. int pool_size;
  75. int free_mem;
  76. int free_mem_peak;
  77. unsigned int check_count;
  78. int align;
  79. bool needs_locking;
  80. inline int entry_end(const Entry &p_entry) const {
  81. return p_entry.pos + aligned(p_entry.len);
  82. }
  83. inline int aligned(int p_size) const {
  84. int rem = p_size % align;
  85. if (rem)
  86. p_size += align - rem;
  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, 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