You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

131 lines
4.3 KiB

  1. /*****************************************************************************************[Alloc.h]
  2. Copyright (c) 2008-2010, Niklas Sorensson
  3. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
  4. associated documentation files (the "Software"), to deal in the Software without restriction,
  5. including without limitation the rights to use, copy, modify, merge, publish, distribute,
  6. sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
  7. furnished to do so, subject to the following conditions:
  8. The above copyright notice and this permission notice shall be included in all copies or
  9. substantial portions of the Software.
  10. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT
  11. NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  12. NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  13. DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT
  14. OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  15. **************************************************************************************************/
  16. #ifndef Minisat_Alloc_h
  17. #define Minisat_Alloc_h
  18. #include "mtl/XAlloc.h"
  19. #include "mtl/Vec.h"
  20. namespace Minisat {
  21. //=================================================================================================
  22. // Simple Region-based memory allocator:
  23. template<class T>
  24. class RegionAllocator
  25. {
  26. T* memory;
  27. uint32_t sz;
  28. uint32_t cap;
  29. uint32_t wasted_;
  30. void capacity(uint32_t min_cap);
  31. public:
  32. // TODO: make this a class for better type-checking?
  33. typedef uint32_t Ref;
  34. enum { Ref_Undef = UINT32_MAX };
  35. enum { Unit_Size = sizeof(uint32_t) };
  36. explicit RegionAllocator(uint32_t start_cap = 1024*1024) : memory(NULL), sz(0), cap(0), wasted_(0){ capacity(start_cap); }
  37. ~RegionAllocator()
  38. {
  39. if (memory != NULL)
  40. ::free(memory);
  41. }
  42. uint32_t size () const { return sz; }
  43. uint32_t wasted () const { return wasted_; }
  44. Ref alloc (int size);
  45. void free (int size) { wasted_ += size; }
  46. // Deref, Load Effective Address (LEA), Inverse of LEA (AEL):
  47. T& operator[](Ref r) { assert(r >= 0 && r < sz); return memory[r]; }
  48. const T& operator[](Ref r) const { assert(r >= 0 && r < sz); return memory[r]; }
  49. T* lea (Ref r) { assert(r >= 0 && r < sz); return &memory[r]; }
  50. const T* lea (Ref r) const { assert(r >= 0 && r < sz); return &memory[r]; }
  51. Ref ael (const T* t) { assert((void*)t >= (void*)&memory[0] && (void*)t < (void*)&memory[sz-1]);
  52. return (Ref)(t - &memory[0]); }
  53. void moveTo(RegionAllocator& to) {
  54. if (to.memory != NULL) ::free(to.memory);
  55. to.memory = memory;
  56. to.sz = sz;
  57. to.cap = cap;
  58. to.wasted_ = wasted_;
  59. memory = NULL;
  60. sz = cap = wasted_ = 0;
  61. }
  62. };
  63. template<class T>
  64. void RegionAllocator<T>::capacity(uint32_t min_cap)
  65. {
  66. if (cap >= min_cap) return;
  67. uint32_t prev_cap = cap;
  68. while (cap < min_cap){
  69. // NOTE: Multiply by a factor (13/8) without causing overflow, then add 2 and make the
  70. // result even by clearing the least significant bit. The resulting sequence of capacities
  71. // is carefully chosen to hit a maximum capacity that is close to the '2^32-1' limit when
  72. // using 'uint32_t' as indices so that as much as possible of this space can be used.
  73. uint32_t delta = ((cap >> 1) + (cap >> 3) + 2) & ~1;
  74. cap += delta;
  75. if (cap <= prev_cap)
  76. throw OutOfMemoryException();
  77. }
  78. // printf(" .. (%p) cap = %u\n", this, cap);
  79. assert(cap > 0);
  80. memory = (T*)xrealloc(memory, sizeof(T)*cap);
  81. }
  82. template<class T>
  83. typename RegionAllocator<T>::Ref
  84. RegionAllocator<T>::alloc(int size)
  85. {
  86. // printf("ALLOC called (this = %p, size = %d)\n", this, size); fflush(stdout);
  87. assert(size > 0);
  88. capacity(sz + size);
  89. uint32_t prev_sz = sz;
  90. sz += size;
  91. // Handle overflow:
  92. if (sz < prev_sz)
  93. throw OutOfMemoryException();
  94. return prev_sz;
  95. }
  96. //=================================================================================================
  97. }
  98. #endif