Lines Matching +full:align +full:- +full:end
1 //===----------------------------------------------------------------------===//
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
32 static bool is_aligned_to(void* ptr, size_t align) {
35 void* result = std::align(align, 1, p2, space);
41 void* do_allocate(size_t bytes, size_t align) override {
43 return std::__libcpp_allocate(bytes, align);
47 void* result = std::__libcpp_allocate(bytes, align);
48 if (!is_aligned_to(result, align)) {
49 std::__libcpp_deallocate(result, bytes, align);
56 void do_deallocate(void* p, size_t bytes, size_t align) override { std::__libcpp_deallocate(p, bytes, align); }
85 } // end namespace
138 size_t mask = alignment - 1;
146 size_t __allocation_size() { return (reinterpret_cast<char*>(this) - __start_) + sizeof(*this); }
151 __chunk_footer* next = __first_->__next_;
152 upstream->deallocate(__first_->__start_, __first_->__allocation_size(), __first_->__align_);
157 void* unsynchronized_pool_resource::__adhoc_pool::__do_allocate(memory_resource* upstream, size_t bytes, size_t align) {
161 if (align < footer_align)
162 align = footer_align;
166 void* result = upstream->allocate(aligned_capacity, align);
168 __chunk_footer* h = (__chunk_footer*)((char*)result + aligned_capacity - footer_size);
169 h->__next_ = __first_;
170 h->__start_ = (char*)result;
171 h->__align_ = align;
177 memory_resource* upstream, void* p, size_t bytes, size_t align) {
179 if (__first_->__start_ == p) {
180 __chunk_footer* next = __first_->__next_;
181 upstream->deallocate(p, __first_->__allocation_size(), __first_->__align_);
184 for (__chunk_footer* h = __first_; h->__next_ != nullptr; h = h->__next_) {
185 if (h->__next_->__start_ == p) {
186 __chunk_footer* next = h->__next_->__next_;
187 upstream->deallocate(p, h->__next_->__allocation_size(), h->__next_->__align_);
188 h->__next_ = next;
192 // The request to deallocate memory ends up being a no-op, likely resulting in a memory leak.
202 size_t __allocation_size() { return (reinterpret_cast<char*>(this) - __start_) + sizeof(*this); }
218 __chunk_footer* next = __first_chunk_->__next_;
219 upstream->deallocate(__first_chunk_->__start_, __first_chunk_->__allocation_size(), __first_chunk_->__align_);
227 __first_vacancy_ = __first_vacancy_->__next_vacancy_;
244 void* result = upstream->allocate(aligned_capacity, __default_alignment);
246 __chunk_footer* h = (__chunk_footer*)((char*)result + aligned_capacity - footer_size);
247 h->__next_ = __first_chunk_;
248 h->__start_ = (char*)result;
249 h->__align_ = __default_alignment;
253 __vacancy_header* last_vh = this->__first_vacancy_;
256 vh->__next_vacancy_ = last_vh;
259 this->__first_vacancy_ = last_vh;
266 vh->__next_vacancy_ = __first_vacancy_;
270 size_t __previous_chunk_size_in_bytes() const { return __first_chunk_ ? __first_chunk_->__allocation_size() : 0; }
279 int unsynchronized_pool_resource::__pool_index(size_t bytes, size_t align) const {
280 if (align > alignof(std::max_align_t) || bytes > (size_t(1) << __num_fixed_pools_))
284 bytes = (bytes > align) ? bytes : align;
285 bytes -= 1;
327 p.largest_required_pool_block = __pool_block_size(__num_fixed_pools_ - 1);
337 __res_->deallocate(__fixed_pools_, __num_fixed_pools_ * sizeof(__fixed_pool), alignof(__fixed_pool));
342 void* unsynchronized_pool_resource::do_allocate(size_t bytes, size_t align) {
347 // from its own internal data structures, it will call upstream_resource()->allocate()
349 // then memory will be allocated using upstream_resource()->allocate().
351 int i = __pool_index(bytes, align);
353 return __adhoc_pool_.__do_allocate(__res_, bytes, align);
357 (__fixed_pool*)__res_->allocate(__num_fixed_pools_ * sizeof(__fixed_pool), alignof(__fixed_pool));
377 static_assert(__max_bytes_per_chunk <= SIZE_MAX - (__max_bytes_per_chunk / 4), "unsigned overflow is possible");
396 void unsynchronized_pool_resource::do_deallocate(void* p, size_t bytes, size_t align) {
399 // a call to upstream_resource()->deallocate().
401 int i = __pool_index(bytes, align);
403 return __adhoc_pool_.__do_deallocate(__res_, p, bytes, align);
415 static void* align_down(size_t align, size_t size, void*& ptr, size_t& space) {
420 char* new_ptr = reinterpret_cast<char*>(reinterpret_cast<uintptr_t>(p1 - size) & ~(align - 1));
422 if (new_ptr < (p1 - space))
426 space -= p1 - new_ptr;
431 void* monotonic_buffer_resource::__initial_descriptor::__try_allocate_from_chunk(size_t bytes, size_t align) {
435 size_t new_capacity = (__cur_ - __start_);
436 void* aligned_ptr = align_down(align, bytes, new_ptr, new_capacity);
442 void* monotonic_buffer_resource::__chunk_footer::__try_allocate_from_chunk(size_t bytes, size_t align) {
444 size_t new_capacity = (__cur_ - __start_);
445 void* aligned_ptr = align_down(align, bytes, new_ptr, new_capacity);
451 void* monotonic_buffer_resource::do_allocate(size_t bytes, size_t align) {
457 return __chunks_->__allocation_size();
459 size_t newsize = (__initial_.__start_ != nullptr) ? (__initial_.__end_ - __initial_.__start_) : __initial_.__size_;
464 if (void* result = __initial_.__try_allocate_from_chunk(bytes, align))
467 if (void* result = __chunks_->__try_allocate_from_chunk(bytes, align))
471 // Allocate a brand-new chunk.
473 if (align < footer_align)
474 align = footer_align;
480 size_t newsize = 2 * (previous_capacity - footer_size);
484 char* start = (char*)__res_->allocate(aligned_capacity, align);
485 auto end = start + aligned_capacity - footer_size;
486 __chunk_footer* footer = (__chunk_footer*)(end);
487 footer->__next_ = __chunks_;
488 footer->__start_ = start;
489 footer->__cur_ = end;
490 footer->__align_ = align;
493 return __chunks_->__try_allocate_from_chunk(bytes, align);