boostorg / align Goto Github PK
View Code? Open in Web Editor NEWBoost.Align
Home Page: http://boost.org/libs/align
Boost.Align
Home Page: http://boost.org/libs/align
It is sometimes useful to align integers other than size_t
to a certain boundary. Using size_t
regardless of the user's integer type is not possible if the type is larger than size_t
(which is difficult to know portably given that size_t
is platform-dependent).
This code:
#include <boost/align/align.hpp>
int main()
{
auto ptr ((void*)140665412970093);
std::size_t space (211);
if (boost::alignment::align (1024, 195, ptr, space) != nullptr)
{
std::abort();
}
}
aborts when compiled using -DBOOST_NO_CXX11_STD_ALIGN
.
This diff
diff --git a/include/boost/align/detail/align.hpp b/include/boost/align/detail/align.hpp
index 905940a..0483c55 100644
--- a/include/boost/align/detail/align.hpp
+++ b/include/boost/align/detail/align.hpp
@@ -22,8 +22,9 @@ align(std::size_t alignment, std::size_t size, void*& ptr,
if (size <= space) {
char* p = reinterpret_cast<char*>(~(alignment - 1) &
(reinterpret_cast<std::size_t>(ptr) + alignment - 1));
- std::size_t n = space - (p - static_cast<char*>(ptr));
- if (size <= n) {
+ std::size_t d = p - static_cast<char*>(ptr);
+ std::size_t n = space - d;
+ if (d <= space && size <= n) {
ptr = p;
space = n;
return p;
deals with the case that the pointer after alignment is already outside the available space.
Legacy MinGW targets native Windows and does not provide posix_memalign
.
Discovered by Boost.Atomic test failures.
Reproduction on Compiler Explorer: https://gcc.godbolt.org/z/kJuafk
#include <boost/align/aligned_allocator.hpp>
#include <type_traits>
struct alignas(64) C
{};
template<typename T>
using Allocator = boost::alignment::aligned_allocator<T, std::alignment_of_v<T>>;
auto f()
{
auto a = Allocator<C>{};
return std::allocate_shared<C>(a);
}
Error: error C2139: 'std::_Wrap_alloc<boost::alignment::aligned_allocator<U,64>>': an undefined class is not allowed as an argument to compiler intrinsic type trait '__is_empty'
Users have needs such as being able to customize which allocation function aligned_alloc
uses. Currently it detects the best available option on the given implementation and provides a fallback implementation which uses std::malloc
in combination with std::align
. We will do all of the following:
operator new
is availablestd::malloc(std::size_t)
users could choose to have ::operator new(std::size_t, const std::nothrow_t&)
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.