MCPcopy Create free account
hub / github.com/bytedance/bolt / allocateNonContiguousWithoutRetry

Method allocateNonContiguousWithoutRetry

bolt/common/memory/MallocAllocator.cpp:67–136  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

65}
66
67bool MallocAllocator::allocateNonContiguousWithoutRetry(
68 const SizeMix& sizeMix,
69 Allocation& out) {
70 freeNonContiguous(out);
71 if (sizeMix.totalPages == 0) {
72 return true;
73 }
74 const auto totalBytes = AllocationTraits::pageBytes(sizeMix.totalPages);
75 if (testingHasInjectedFailure(InjectedFailure::kCap) ||
76 !incrementUsage(totalBytes)) {
77 const auto errorMsg = fmt::format(
78 "Exceeded memory allocator limit when allocating {} new pages"
79 ", the memory allocator capacity is {}, the allocated bytes is {}",
80 sizeMix.totalPages,
81 succinctBytes(capacity_),
82 succinctBytes(allocatedBytes_));
83 BOLT_MEM_LOG_EVERY_MS(WARNING, 1000) << errorMsg;
84 setAllocatorFailureMessage(errorMsg);
85 return false;
86 }
87
88 std::vector<void*> buffers;
89 buffers.reserve(sizeMix.numSizes);
90 for (int32_t i = 0; i < sizeMix.numSizes; ++i) {
91 MachinePageCount numSizeClassPages =
92 sizeMix.sizeCounts[i] * sizeClassSizes_[sizeMix.sizeIndices[i]];
93 void* ptr = nullptr;
94 // Trigger allocation failure by skipping malloc
95 if (!testingHasInjectedFailure(InjectedFailure::kAllocate)) {
96 stats_.recordAllocate(
97 AllocationTraits::pageBytes(sizeClassSizes_[sizeMix.sizeIndices[i]]),
98 sizeMix.sizeCounts[i],
99 [&]() {
100 ptr = ::malloc(
101 AllocationTraits::pageBytes(numSizeClassPages)); // NOLINT
102 });
103 }
104 if (ptr == nullptr) {
105 // Failed to allocate memory from memory.
106 const auto errorMsg = fmt::format(
107 "Malloc failed to allocate {} of memory while allocating for "
108 "non-contiguous allocation of {} pages",
109 succinctBytes(AllocationTraits::pageBytes(numSizeClassPages)),
110 sizeMix.totalPages);
111 BOLT_MEM_LOG(WARNING) << errorMsg;
112 setAllocatorFailureMessage(errorMsg);
113 break;
114 }
115 buffers.push_back(ptr);
116 out.append(reinterpret_cast<uint8_t*>(ptr), numSizeClassPages); // NOLINT
117 }
118
119 if (buffers.size() != sizeMix.numSizes) {
120 // Failed to allocate memory using malloc. Free any malloced pages and
121 // return false.
122 for (auto* buffer : buffers) {
123 ::free(buffer);
124 }

Callers

nothing calls this directly

Calls 7

succinctBytesFunction · 0.85
recordAllocateMethod · 0.80
reserveMethod · 0.45
push_backMethod · 0.45
appendMethod · 0.45
sizeMethod · 0.45
clearMethod · 0.45

Tested by

no test coverage detected