alloc.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #include <new>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include "alloc.h"
  5. #include "enable.h"
  6. #include <util/system/platform.h>
  7. namespace NAllocSetup {
  8. size_t softLimit = size_t(-1);
  9. size_t hardLimit = size_t(-1);
  10. size_t allocationThreshold = size_t(-1);
  11. size_t softReclaimDivisor = 100;
  12. size_t angryReclaimDivisor = 100;
  13. struct TThrowInfo {
  14. size_t CurrSize;
  15. size_t MaxSize;
  16. };
  17. #if defined(_unix_) && !defined(_darwin_)
  18. __thread TThrowInfo info;
  19. void ThrowOnError(size_t allocSize) {
  20. info.CurrSize += allocSize;
  21. if (info.MaxSize && info.MaxSize < info.CurrSize) {
  22. #ifndef NDEBUG
  23. __builtin_trap();
  24. #endif
  25. info.CurrSize = 0;
  26. throw std::bad_alloc();
  27. }
  28. }
  29. void SetThrowConditions(size_t currSize, size_t maxSize) {
  30. info.CurrSize = currSize;
  31. info.MaxSize = maxSize;
  32. }
  33. #else // _unix_ && ! _darwin_
  34. void ThrowOnError(size_t /*allocSize*/) {
  35. }
  36. void SetThrowConditions(size_t /*currSize*/, size_t /*maxSize*/) {
  37. }
  38. #endif // _unix_ && ! _darwin_
  39. void SetSoftLimit(size_t softLimit_) {
  40. softLimit = softLimit_;
  41. }
  42. void SetHardLimit(size_t hardLimit_) {
  43. hardLimit = hardLimit_;
  44. }
  45. void SetAllocationThreshold(size_t allocationThreshold_) {
  46. allocationThreshold = allocationThreshold_;
  47. }
  48. void SetSoftReclaimDivisor(size_t softReclaimDivisor_) {
  49. softReclaimDivisor = softReclaimDivisor_;
  50. }
  51. void SetAngryReclaimDivisor(size_t angryReclaimDivisor_) {
  52. angryReclaimDivisor = angryReclaimDivisor_;
  53. }
  54. size_t GetSoftLimit() {
  55. return softLimit;
  56. }
  57. size_t GetHardLimit() {
  58. return hardLimit;
  59. }
  60. size_t GetAllocationThreshold() {
  61. return allocationThreshold;
  62. }
  63. size_t GetSoftReclaimDivisor() {
  64. return softReclaimDivisor;
  65. }
  66. size_t GetAngryReclaimDivisor() {
  67. return angryReclaimDivisor;
  68. }
  69. size_t allocSize;
  70. size_t totalAllocSize;
  71. size_t gcSize;
  72. size_t GetTotalAllocSize() {
  73. return totalAllocSize;
  74. }
  75. size_t GetCurSize() {
  76. return allocSize;
  77. }
  78. size_t GetGCSize() {
  79. return gcSize;
  80. }
  81. bool CanAlloc(size_t allocSize_, size_t totalAllocSize_) {
  82. allocSize = allocSize_;
  83. totalAllocSize = totalAllocSize_;
  84. return allocSize_ < hardLimit || totalAllocSize_ < allocationThreshold;
  85. }
  86. bool NeedReclaim(size_t gcSize_, size_t counter) {
  87. gcSize = gcSize_;
  88. size_t limit = gcSize_ < softLimit ? softReclaimDivisor : angryReclaimDivisor;
  89. return counter > limit;
  90. }
  91. bool IsEnabledByDefault() {
  92. return EnableByDefault;
  93. }
  94. }