neverFreeMemory.h 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /**
  2. * PANDA 3D SOFTWARE
  3. * Copyright (c) Carnegie Mellon University. All rights reserved.
  4. *
  5. * All use of this software is subject to the terms of the revised BSD
  6. * license. You should have received a copy of this license along
  7. * with this source code in a file named "LICENSE."
  8. *
  9. * @file neverFreeMemory.h
  10. * @author drose
  11. * @date 2007-06-14
  12. */
  13. #ifndef NEVERFREEMEMORY_H
  14. #define NEVERFREEMEMORY_H
  15. #include "dtoolbase.h"
  16. #include "mutexImpl.h"
  17. #include <set>
  18. /**
  19. * This class is used to allocate bytes of memory from a pool that is never
  20. * intended to be freed. It is particularly useful to support DeletedChain,
  21. * which allocates memory in just such a fashion.
  22. *
  23. * When it is known that memory will not be freed, it is preferable to use
  24. * this instead of the standard malloc() (or global_operator_new()) call,
  25. * since this will help reduce fragmentation problems in the dynamic heap.
  26. * Also, memory allocated from here will exhibit less wasted space.
  27. */
  28. class EXPCL_DTOOL_DTOOLBASE NeverFreeMemory {
  29. private:
  30. NeverFreeMemory();
  31. public:
  32. INLINE static void *alloc(size_t size);
  33. PUBLISHED:
  34. INLINE static size_t get_total_alloc();
  35. INLINE static size_t get_total_used();
  36. INLINE static size_t get_total_unused();
  37. private:
  38. void *ns_alloc(size_t size);
  39. INLINE static NeverFreeMemory *get_global_ptr();
  40. static void make_global_ptr();
  41. private:
  42. class Page {
  43. public:
  44. INLINE Page(void *start, size_t size);
  45. INLINE bool operator < (const Page &other) const;
  46. INLINE void *alloc(size_t size);
  47. unsigned char *_next;
  48. size_t _remaining;
  49. };
  50. typedef std::multiset<Page> Pages;
  51. Pages _pages;
  52. size_t _total_alloc;
  53. size_t _total_used;
  54. MutexImpl _lock;
  55. static NeverFreeMemory * TVOLATILE _global_ptr;
  56. };
  57. #include "neverFreeMemory.I"
  58. #endif