memory_pool_dynamic_static.h 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*************************************************************************/
  2. /* memory_pool_dynamic_static.h */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* http://www.godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2015 Juan Linietsky, Ariel Manzur. */
  9. /* */
  10. /* Permission is hereby granted, free of charge, to any person obtaining */
  11. /* a copy of this software and associated documentation files (the */
  12. /* "Software"), to deal in the Software without restriction, including */
  13. /* without limitation the rights to use, copy, modify, merge, publish, */
  14. /* distribute, sublicense, and/or sell copies of the Software, and to */
  15. /* permit persons to whom the Software is furnished to do so, subject to */
  16. /* the following conditions: */
  17. /* */
  18. /* The above copyright notice and this permission notice shall be */
  19. /* included in all copies or substantial portions of the Software. */
  20. /* */
  21. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  22. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  23. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  24. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  25. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  26. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  27. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  28. /*************************************************************************/
  29. #ifndef MEMORY_POOL_DYNAMIC_STATIC_H
  30. #define MEMORY_POOL_DYNAMIC_STATIC_H
  31. #include "os/memory_pool_dynamic.h"
  32. #include "typedefs.h"
  33. #include "os/thread_safe.h"
  34. class MemoryPoolDynamicStatic : public MemoryPoolDynamic {
  35. _THREAD_SAFE_CLASS_
  36. enum {
  37. MAX_CHUNKS=65536
  38. };
  39. struct Chunk {
  40. uint64_t lock;
  41. uint64_t check;
  42. void *mem;
  43. size_t size;
  44. const char *descr;
  45. Chunk() { mem=NULL; lock=0; check=0; }
  46. };
  47. Chunk chunk[MAX_CHUNKS];
  48. uint64_t last_check;
  49. int last_alloc;
  50. size_t total_usage;
  51. size_t max_usage;
  52. Chunk *get_chunk(ID p_id);
  53. const Chunk *get_chunk(ID p_id) const;
  54. public:
  55. virtual ID alloc(size_t p_amount,const char* p_description);
  56. virtual void free(ID p_id);
  57. virtual Error realloc(ID p_id, size_t p_amount);
  58. virtual bool is_valid(ID p_id);
  59. virtual size_t get_size(ID p_id) const;
  60. virtual const char* get_description(ID p_id) const;
  61. virtual bool is_locked(ID p_id) const;
  62. virtual Error lock(ID p_id);
  63. virtual void * get(ID p_ID);
  64. virtual Error unlock(ID p_id);
  65. virtual size_t get_available_mem() const;
  66. virtual size_t get_total_usage() const;
  67. MemoryPoolDynamicStatic();
  68. virtual ~MemoryPoolDynamicStatic();
  69. };
  70. #endif