Buf.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * Copyright (c)2013-2020 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2024-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. #include "Buf.hpp"
  14. #ifndef __GNUC__
  15. #include <atomic>
  16. #endif
  17. namespace ZeroTier {
  18. #ifdef __GNUC__
  19. static uintptr_t s_pool = 0;
  20. #else
  21. static std::atomic<uintptr_t> s_pool(0);
  22. #endif
  23. void Buf::operator delete(void *ptr,std::size_t sz)
  24. {
  25. if (ptr) {
  26. uintptr_t bb;
  27. const uintptr_t locked = ~((uintptr_t)0);
  28. for (;;) {
  29. #ifdef __GNUC__
  30. bb = __sync_fetch_and_or(&s_pool,locked); // get value of s_pool and "lock" by filling with all 1's
  31. #else
  32. bb = s_pool.fetch_or(locked);
  33. #endif
  34. if (bb != locked)
  35. break;
  36. }
  37. ((Buf *)ptr)->__nextInPool = bb;
  38. #ifdef __GNUC__
  39. __sync_fetch_and_and(&s_pool,(uintptr_t)ptr);
  40. #else
  41. s_pool.store((uintptr_t)ptr);
  42. #endif
  43. }
  44. }
  45. SharedPtr<Buf> Buf::get()
  46. {
  47. uintptr_t bb;
  48. const uintptr_t locked = ~((uintptr_t)0);
  49. for (;;) {
  50. #ifdef __GNUC__
  51. bb = __sync_fetch_and_or(&s_pool,locked); // get value of s_pool and "lock" by filling with all 1's
  52. #else
  53. bb = s_pool.fetch_or(locked);
  54. #endif
  55. if (bb != locked)
  56. break;
  57. }
  58. Buf *b;
  59. if (bb == 0) {
  60. #ifdef __GNUC__
  61. __sync_fetch_and_and(&s_pool,bb);
  62. #else
  63. s_pool.store(bb);
  64. #endif
  65. b = (Buf *)malloc(sizeof(Buf));
  66. if (!b)
  67. return SharedPtr<Buf>();
  68. } else {
  69. b = (Buf *)bb;
  70. #ifdef __GNUC__
  71. __sync_fetch_and_and(&s_pool,b->__nextInPool);
  72. #else
  73. s_pool.store(b->__nextInPool);
  74. #endif
  75. }
  76. b->__refCount.zero();
  77. return SharedPtr<Buf>(b);
  78. }
  79. void Buf::freePool()
  80. {
  81. uintptr_t bb;
  82. const uintptr_t locked = ~((uintptr_t)0);
  83. for (;;) {
  84. #ifdef __GNUC__
  85. bb = __sync_fetch_and_or(&s_pool,locked); // get value of s_pool and "lock" by filling with all 1's
  86. #else
  87. bb = s_pool.fetch_or(locked);
  88. #endif
  89. if (bb != locked)
  90. break;
  91. }
  92. #ifdef __GNUC__
  93. __sync_fetch_and_and(&s_pool,(uintptr_t)0);
  94. #else
  95. s_pool.store((uintptr_t)0);
  96. #endif
  97. while (bb != 0) {
  98. uintptr_t next = ((Buf *)bb)->__nextInPool;
  99. free((void *)bb);
  100. bb = next;
  101. }
  102. }
  103. } // namespace ZeroTier