3030#include < cstring>
3131#include < cassert>
3232
33- #include " network_private .h"
33+ #include " common/utils/utils_print .h"
3434
3535namespace network ::memory {
36+ inline constexpr uint32_t kBlocksMin = 1 ;
37+ inline constexpr uint32_t kBlocksMax = 32 ;
38+
3639inline constexpr uint32_t kBlocks =
37- #if !defined( CONFIG_NETWORK_MEMORY_BLOCKS)
40+ #ifndef CONFIG_NETWORK_MEMORY_BLOCKS
3841 12 ;
3942#else
4043 CONFIG_NETWORK_MEMORY_BLOCKS ;
4144#endif // CONFIG_NETWORK_MEMORY_BLOCKS
4245
43- static_assert (kBlocks >= 1 );
44- static_assert (kBlocks <= 32 );
46+ static_assert (kBlocks >= kBlocksMin );
47+ static_assert (kBlocks <= kBlocksMax );
4548
4649inline constexpr uint32_t kBlockSize =
47- #if !defined( CONFIG_NETWORK_MEMORY_BLOCKSIZE)
50+ #ifndef CONFIG_NETWORK_MEMORY_BLOCKSIZE
4851 1460 ;
4952#else
5053 CONFIG_NETWORK_MEMORY_BLOCKSIZE ;
@@ -79,16 +82,16 @@ class Allocator {
7982 Allocator (Allocator&&) = delete ;
8083 Allocator& operator =(Allocator&&) = delete ;
8184
82- bool IsEmpty () const { return free_mask_ == kAllMask ; }
83- bool IsFull () const { return free_mask_ == 0 ; }
85+ [[nodiscard]] bool IsEmpty () const { return free_mask_ == kAllMask ; }
86+ [[nodiscard]] bool IsFull () const { return free_mask_ == 0 ; }
8487
8588 uint8_t * Allocate () {
8689 if (IsFull ()) {
87- network::Error (__func__, " Allocate:Full!" );
90+ ERROR ( " Allocate:Full!" );
8891 return nullptr ;
8992 }
9093
91- const uint32_t kIndex = static_cast <uint32_t >(__builtin_ctz (free_mask_));
94+ const auto kIndex = static_cast <uint32_t >(__builtin_ctz (free_mask_));
9295 free_mask_ &= ~(1U << kIndex );
9396
9497 Status ();
@@ -102,11 +105,11 @@ class Allocator {
102105 assert (size <= kBlockSize );
103106
104107 if (IsFull ()) {
105- network::Error (__func__, " Allocate:Full!" );
108+ ERROR ( " Allocate:Full!" );
106109 return UINT16_MAX ;
107110 }
108111
109- const uint32_t kIndex = static_cast <uint32_t >(__builtin_ctz (free_mask_));
112+ const auto kIndex = static_cast <uint32_t >(__builtin_ctz (free_mask_));
110113 free_mask_ &= ~(1U << kIndex );
111114
112115 size_[kIndex ] = size;
@@ -155,7 +158,7 @@ class Allocator {
155158 }
156159
157160 void Status () const {
158- #if defined DEBUG_NETWORK_MEMORY
161+ #ifdef DEBUG_NETWORK_MEMORY
159162 const uint32_t kUsedMask = (~free_mask_) & kAllMask ;
160163 printf (" free_mask=0x%08x used_mask=0x%08x free=%u used=%u\n " , free_mask_, kUsedMask , __builtin_popcount (free_mask_), __builtin_popcount (kUsedMask ));
161164 printf (" IsEmpty=%c IsFull=%c\n " , IsEmpty () ? ' Y' : ' N' , IsFull () ? ' Y' : ' N' );
@@ -164,7 +167,7 @@ class Allocator {
164167
165168 private:
166169 Allocator () = default ;
167- static constexpr uint32_t kAllMask = (kBlocks == 32 ) ? UINT32_MAX : ((1U << kBlocks ) - 1U );
170+ static constexpr uint32_t kAllMask = (kBlocks == kBlocksMax ) ? UINT32_MAX : ((1U << kBlocks ) - 1U );
168171 uint32_t free_mask_{0 };
169172 uint16_t size_[kBlocks ]{0 };
170173};
0 commit comments