memory_pool_dynamic_static.h 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /*************************************************************************/
  2. /* memory_pool_dynamic_static.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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 MEMORY_POOL_DYNAMIC_STATIC_H
  31. #define MEMORY_POOL_DYNAMIC_STATIC_H
  32. #include "os/memory_pool_dynamic.h"
  33. #include "os/thread_safe.h"
  34. #include "typedefs.h"
  35. class MemoryPoolDynamicStatic : public MemoryPoolDynamic {
  36. _THREAD_SAFE_CLASS_
  37. enum {
  38. MAX_CHUNKS = 65536
  39. };
  40. struct Chunk {
  41. uint64_t lock;
  42. uint64_t check;
  43. void *mem;
  44. size_t size;
  45. const char *descr;
  46. Chunk() {
  47. mem = NULL;
  48. lock = 0;
  49. check = 0;
  50. }
  51. };
  52. Chunk chunk[MAX_CHUNKS];
  53. uint64_t last_check;
  54. int last_alloc;
  55. size_t total_usage;
  56. size_t max_usage;
  57. Chunk *get_chunk(ID p_id);
  58. const Chunk *get_chunk(ID p_id) const;
  59. public:
  60. virtual ID alloc(size_t p_amount, const char *p_description);
  61. virtual void free(ID p_id);
  62. virtual Error realloc(ID p_id, size_t p_amount);
  63. virtual bool is_valid(ID p_id);
  64. virtual size_t get_size(ID p_id) const;
  65. virtual const char *get_description(ID p_id) const;
  66. virtual bool is_locked(ID p_id) const;
  67. virtual Error lock(ID p_id);
  68. virtual void *get(ID p_ID);
  69. virtual Error unlock(ID p_id);
  70. virtual size_t get_available_mem() const;
  71. virtual size_t get_total_usage() const;
  72. MemoryPoolDynamicStatic();
  73. virtual ~MemoryPoolDynamicStatic();
  74. };
  75. #endif