Files
2025-10-25 03:02:53 +03:00

234 lines
6.6 KiB
Markdown
Raw Permalink Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

[mem.poly.allocator.mem]
# 20 Memory management library [[mem]](./#mem)
## 20.5 Memory resources [[mem.res]](mem.res#mem.poly.allocator.mem)
### 20.5.3 Class template polymorphic_allocator [[mem.poly.allocator.class]](mem.poly.allocator.class#mem.poly.allocator.mem)
#### 20.5.3.3 Member functions [mem.poly.allocator.mem]
[🔗](#lib:allocate,polymorphic_allocator)
`Tp* allocate(size_t n);
`
[1](#1)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L7777)
*Effects*: If numeric_limits<size_t>::max() / sizeof(Tp) < n,
throws bad_array_new_length[.](#1.sentence-1)
Otherwise equivalent to:return static_cast<Tp*>(memory_rsrc->allocate(n * sizeof(Tp), alignof(Tp)));
[🔗](#lib:deallocate,polymorphic_allocator)
`void deallocate(Tp* p, size_t n);
`
[2](#2)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L7793)
*Preconditions*: p was allocated from a memory resource x,
equal to *memory_rsrc,
using x.allocate(n * sizeof(Tp), alignof(Tp))[.](#2.sentence-1)
[3](#3)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L7799)
*Effects*: Equivalent to memory_rsrc->deallocate(p, n * sizeof(Tp), alignof(Tp))[.](#3.sentence-1)
[4](#4)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L7803)
*Throws*: Nothing[.](#4.sentence-1)
[🔗](#lib:allocate_bytes,polymorphic_allocator)
`void* allocate_bytes(size_t nbytes, size_t alignment = alignof(max_align_t));
`
[5](#5)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L7814)
*Effects*: Equivalent to: return memory_rsrc->allocate(nbytes, alignment);
[6](#6)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L7818)
[*Note [1](#note-1)*:
The return type is void* (rather than, e.g., byte*)
to support conversion to an arbitrary pointer type U* by static_cast<U*>, thus facilitating construction of a U object in the allocated memory[.](#6.sentence-1)
— *end note*]
[🔗](#lib:deallocate_bytes,polymorphic_allocator)
`void deallocate_bytes(void* p, size_t nbytes, size_t alignment = alignof(max_align_t));
`
[7](#7)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L7833)
*Effects*: Equivalent to memory_rsrc->deallocate(p, nbytes, alignment)[.](#7.sentence-1)
[🔗](#lib:allocate_object,polymorphic_allocator)
`template<class T>
T* allocate_object(size_t n = 1);
`
[8](#8)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L7845)
*Effects*: Allocates memory suitable for holding
an array of n objects of type T, as follows:
- [(8.1)](#8.1)
if numeric_limits<size_t>::max() / sizeof(T) < n,
throws bad_array_new_length,
- [(8.2)](#8.2)
otherwise equivalent to:return static_cast<T*>(allocate_bytes(n*sizeof(T), alignof(T)));
[9](#9)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L7860)
[*Note [2](#note-2)*:
T is not deduced and must therefore be provided as a template argument[.](#9.sentence-1)
— *end note*]
[🔗](#lib:deallocate_object,polymorphic_allocator)
`template<class T>
void deallocate_object(T* p, size_t n = 1);
`
[10](#10)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L7873)
*Effects*: Equivalent to deallocate_bytes(p, n*sizeof(T), alignof(T))[.](#10.sentence-1)
[🔗](#lib:new_object,polymorphic_allocator)
`template<class T, class... CtorArgs>
T* new_object(CtorArgs&&... ctor_args);
`
[11](#11)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L7885)
*Effects*: Allocates and constructs an object of type T, as follows[.](#11.sentence-1)
Equivalent to:T* p = allocate_object<T>();try { construct(p, std::forward<CtorArgs>(ctor_args)...);} catch (...) { deallocate_object(p); throw;}return p;
[12](#12)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L7900)
[*Note [3](#note-3)*:
T is not deduced and must therefore be provided as a template argument[.](#12.sentence-1)
— *end note*]
[🔗](#lib:new_object,polymorphic_allocator_)
`template<class T>
void delete_object(T* p);
`
[13](#13)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L7913)
*Effects*: Equivalent to:destroy(p);
deallocate_object(p);
[🔗](#lib:construct,polymorphic_allocator)
`template<class T, class... Args>
void construct(T* p, Args&&... args);
`
[14](#14)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L7929)
*Mandates*: Uses-allocator construction of T with allocator *this (see [[allocator.uses.construction]](allocator.uses.construction "20.2.8.2Uses-allocator construction"))
and constructor arguments std::forward<Args>(args)... is well-formed[.](#14.sentence-1)
[15](#15)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L7935)
*Effects*: Constructs a T object in the storage
whose address is represented by p by uses-allocator construction with allocator *this and constructor arguments std::forward<Args>(args)...[.](#15.sentence-1)
[16](#16)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L7942)
*Throws*: Nothing unless the constructor for T throws[.](#16.sentence-1)
[🔗](#lib:destroy,polymorphic_allocator)
`template<class T>
void destroy(T* p);
`
[17](#17)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L7954)
*Effects*: Equivalent to p->~T()[.](#17.sentence-1)
[🔗](#lib:select_on_container_copy_construction,polymorphic_allocator)
`polymorphic_allocator select_on_container_copy_construction() const;
`
[18](#18)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L7965)
*Returns*: polymorphic_allocator()[.](#18.sentence-1)
[19](#19)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L7969)
[*Note [4](#note-4)*:
The memory resource is not propagated[.](#19.sentence-1)
— *end note*]
[🔗](#lib:resource,polymorphic_allocator)
`memory_resource* resource() const;
`
[20](#20)
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L7981)
*Returns*: memory_rsrc[.](#20.sentence-1)