131 lines
3.8 KiB
Markdown
131 lines
3.8 KiB
Markdown
[ptr.align]
|
||
|
||
# 20 Memory management library [[mem]](./#mem)
|
||
|
||
## 20.2 Memory [[memory]](memory#ptr.align)
|
||
|
||
### 20.2.5 Pointer alignment [ptr.align]
|
||
|
||
[ð](#lib:align)
|
||
|
||
`void* align(size_t alignment, size_t size, void*& ptr, size_t& space);
|
||
`
|
||
|
||
[1](#1)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L946)
|
||
|
||
*Preconditions*:
|
||
|
||
- [(1.1)](#1.1)
|
||
|
||
alignment is a power of two
|
||
|
||
- [(1.2)](#1.2)
|
||
|
||
ptr represents the address of contiguous storage of at leastspace bytes
|
||
|
||
[2](#2)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L957)
|
||
|
||
*Effects*: If it is possible to fit size bytes
|
||
of storage aligned by alignment into the buffer pointed to byptr with length space, the function updatesptr to represent the first possible address of such storage
|
||
and decreases space by the number of bytes used for alignment[.](#2.sentence-1)
|
||
|
||
Otherwise, the function does nothing[.](#2.sentence-2)
|
||
|
||
[3](#3)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L966)
|
||
|
||
*Returns*: A null pointer if the requested aligned buffer
|
||
would not fit into the available space, otherwise the adjusted value
|
||
of ptr[.](#3.sentence-1)
|
||
|
||
[4](#4)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L972)
|
||
|
||
[*Note [1](#note-1)*:
|
||
|
||
The function updates its ptr and space arguments so that it can be called repeatedly
|
||
with possibly different alignment and size arguments for the same buffer[.](#4.sentence-1)
|
||
|
||
â *end note*]
|
||
|
||
[ð](#lib:assume_aligned)
|
||
|
||
`template<size_t N, class T>
|
||
constexpr T* assume_aligned(T* ptr);
|
||
`
|
||
|
||
[5](#5)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L988)
|
||
|
||
*Mandates*: N is a power of two[.](#5.sentence-1)
|
||
|
||
[6](#6)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L992)
|
||
|
||
*Preconditions*: ptr points to an object X of
|
||
a type similar ([[conv.qual]](conv.qual "7.3.6 Qualification conversions")) to T,
|
||
where X has alignment N ([[basic.align]](basic.align "6.8.3 Alignment"))[.](#6.sentence-1)
|
||
|
||
[7](#7)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L998)
|
||
|
||
*Returns*: ptr[.](#7.sentence-1)
|
||
|
||
[8](#8)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L1002)
|
||
|
||
*Throws*: Nothing[.](#8.sentence-1)
|
||
|
||
[9](#9)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L1006)
|
||
|
||
[*Note [2](#note-2)*:
|
||
|
||
The alignment assumption on an object X expressed by a call to assume_aligned might result in generation of more efficient code[.](#9.sentence-1)
|
||
|
||
It is up to the program to ensure that the assumption actually holds[.](#9.sentence-2)
|
||
|
||
The call does not cause the implementation to verify or enforce this[.](#9.sentence-3)
|
||
|
||
An implementation might only make the assumption
|
||
for those operations on X that access X through the pointer returned by assume_aligned[.](#9.sentence-4)
|
||
|
||
â *end note*]
|
||
|
||
[ð](#lib:is_sufficiently_aligned)
|
||
|
||
`template<size_t Alignment, class T>
|
||
bool is_sufficiently_aligned(T* ptr);
|
||
`
|
||
|
||
[10](#10)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L1026)
|
||
|
||
*Preconditions*: p points to
|
||
an object X of a type similar ([[conv.qual]](conv.qual "7.3.6 Qualification conversions")) to T[.](#10.sentence-1)
|
||
|
||
[11](#11)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L1031)
|
||
|
||
*Returns*: true if X has alignment at least Alignment,
|
||
otherwise false[.](#11.sentence-1)
|
||
|
||
[12](#12)
|
||
|
||
[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/memory.tex#L1036)
|
||
|
||
*Throws*: Nothing[.](#12.sentence-1)
|