Commit 47b2f8df authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook Github Bot

Promote aligned_malloc and aligned_free

Summary:
[Folly] Promote `aligned_malloc` and `aligned_free` from `namespace folly::detail` to `namespace folly`.

And move them from `folly/portability/Memory.h` to `folly/Memory.h`.

Differential Revision: D6153394

fbshipit-source-id: eef314d2bc171910ea3c8403da9e9e1d1858ce15
parent 337e897a
...@@ -594,7 +594,6 @@ libfolly_la_SOURCES = \ ...@@ -594,7 +594,6 @@ libfolly_la_SOURCES = \
portability/Fcntl.cpp \ portability/Fcntl.cpp \
portability/Libgen.cpp \ portability/Libgen.cpp \
portability/Malloc.cpp \ portability/Malloc.cpp \
portability/Memory.cpp \
portability/OpenSSL.cpp \ portability/OpenSSL.cpp \
portability/PThread.cpp \ portability/PThread.cpp \
portability/Sockets.cpp \ portability/Sockets.cpp \
......
/* /*
* Copyright 2017 Facebook, Inc. * Copyright 2013-present Facebook, Inc.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -16,9 +16,8 @@ ...@@ -16,9 +16,8 @@
#pragma once #pragma once
#include <folly/Traits.h> #include <cassert>
#include <folly/functional/Invoke.h> #include <cerrno>
#include <cstddef> #include <cstddef>
#include <cstdlib> #include <cstdlib>
#include <exception> #include <exception>
...@@ -28,8 +27,52 @@ ...@@ -28,8 +27,52 @@
#include <type_traits> #include <type_traits>
#include <utility> #include <utility>
#include <folly/Traits.h>
#include <folly/functional/Invoke.h>
#include <folly/portability/Config.h>
#include <folly/portability/Malloc.h>
namespace folly { namespace folly {
#if _POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600 || \
(defined(__ANDROID__) && (__ANDROID_API__ > 15)) || \
(defined(__APPLE__) && \
(__MAC_OS_X_VERSION_MIN_REQUIRED >= __MAC_10_6 || \
__IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_3_0))
inline void* aligned_malloc(size_t size, size_t align) {
// use posix_memalign, but mimic the behaviour of memalign
void* ptr = nullptr;
int rc = posix_memalign(&ptr, align, size);
return rc == 0 ? (errno = 0, ptr) : (errno = rc, nullptr);
}
inline void aligned_free(void* aligned_ptr) {
free(aligned_ptr);
}
#elif defined(_WIN32)
inline void* aligned_malloc(size_t size, size_t align) {
return _aligned_malloc(size, align);
}
inline void aligned_free(void* aligned_ptr) {
_aligned_free(aligned_ptr);
}
#else
inline void* aligned_malloc(size_t size, size_t align) {
return memalign(align, size);
}
inline void aligned_free(void* aligned_ptr) {
free(aligned_ptr);
}
#endif
/** /**
* For exception safety and consistency with make_shared. Erase me when * For exception safety and consistency with make_shared. Erase me when
* we have std::make_unique(). * we have std::make_unique().
......
/* /*
* Copyright 2017 Facebook, Inc. * Copyright 2011-present Facebook, Inc.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
......
/* /*
* Copyright 2017 Facebook, Inc. * Copyright 2013-present Facebook, Inc.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -241,13 +241,13 @@ SimpleAllocator::SimpleAllocator(size_t allocSize, size_t sz) ...@@ -241,13 +241,13 @@ SimpleAllocator::SimpleAllocator(size_t allocSize, size_t sz)
SimpleAllocator::~SimpleAllocator() { SimpleAllocator::~SimpleAllocator() {
std::lock_guard<std::mutex> g(m_); std::lock_guard<std::mutex> g(m_);
for (auto& block : blocks_) { for (auto& block : blocks_) {
detail::aligned_free(block); folly::aligned_free(block);
} }
} }
void* SimpleAllocator::allocateHard() { void* SimpleAllocator::allocateHard() {
// Allocate a new slab. // Allocate a new slab.
mem_ = static_cast<uint8_t*>(detail::aligned_malloc(allocSize_, allocSize_)); mem_ = static_cast<uint8_t*>(folly::aligned_malloc(allocSize_, allocSize_));
if (!mem_) { if (!mem_) {
std::__throw_bad_alloc(); std::__throw_bad_alloc();
} }
......
/* /*
* Copyright 2017 Facebook, Inc. * Copyright 2013-present Facebook, Inc.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -35,7 +35,6 @@ ...@@ -35,7 +35,6 @@
#include <folly/hash/Hash.h> #include <folly/hash/Hash.h>
#include <folly/lang/Align.h> #include <folly/lang/Align.h>
#include <folly/portability/BitsFunctexcept.h> #include <folly/portability/BitsFunctexcept.h>
#include <folly/portability/Memory.h>
#include <folly/system/ThreadId.h> #include <folly/system/ThreadId.h>
namespace folly { namespace folly {
...@@ -435,8 +434,8 @@ class CoreAllocator { ...@@ -435,8 +434,8 @@ class CoreAllocator {
// Align to a cacheline // Align to a cacheline
size = size + (hardware_destructive_interference_size - 1); size = size + (hardware_destructive_interference_size - 1);
size &= ~size_t(hardware_destructive_interference_size - 1); size &= ~size_t(hardware_destructive_interference_size - 1);
void* mem = detail::aligned_malloc( void* mem =
size, hardware_destructive_interference_size); aligned_malloc(size, hardware_destructive_interference_size);
if (!mem) { if (!mem) {
std::__throw_bad_alloc(); std::__throw_bad_alloc();
} }
...@@ -456,7 +455,7 @@ class CoreAllocator { ...@@ -456,7 +455,7 @@ class CoreAllocator {
auto allocator = *static_cast<SimpleAllocator**>(addr); auto allocator = *static_cast<SimpleAllocator**>(addr);
allocator->deallocate(mem); allocator->deallocate(mem);
} else { } else {
detail::aligned_free(mem); aligned_free(mem);
} }
} }
}; };
......
/* /*
* Copyright 2017 Facebook, Inc. * Copyright 2016-present Facebook, Inc.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
......
/* /*
* Copyright 2017 Facebook, Inc. * Copyright 2013-present Facebook, Inc.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
......
/* /*
* Copyright 2017 Facebook, Inc. * Copyright 2015-present Facebook, Inc.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
......
/* /*
* Copyright 2017 Facebook, Inc. * Copyright 2015-present Facebook, Inc.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
......
/* /*
* Copyright 2017 Facebook, Inc. * Copyright 2015-present Facebook, Inc.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
......
/* /*
* Copyright 2017 Facebook, Inc. * Copyright 2015-present Facebook, Inc.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
......
/*
* Copyright 2017 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <folly/portability/Memory.h>
#include <folly/portability/Config.h>
#include <folly/portability/Malloc.h>
#include <errno.h>
namespace folly {
namespace detail {
#if _POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600 || \
(defined(__ANDROID__) && (__ANDROID_API__ > 15)) || \
(defined(__APPLE__) && \
(__MAC_OS_X_VERSION_MIN_REQUIRED >= __MAC_10_6 || \
__IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_3_0))
// Use posix_memalign, but mimic the behaviour of memalign
void* aligned_malloc(size_t size, size_t align) {
void* ptr = nullptr;
int rc = posix_memalign(&ptr, align, size);
if (rc == 0) {
return ptr;
}
errno = rc;
return nullptr;
}
void aligned_free(void* aligned_ptr) {
free(aligned_ptr);
}
#elif defined(_WIN32)
void* aligned_malloc(size_t size, size_t align) {
return _aligned_malloc(size, align);
}
void aligned_free(void* aligned_ptr) {
_aligned_free(aligned_ptr);
}
#else
void* aligned_malloc(size_t size, size_t align) {
return memalign(align, size);
}
void aligned_free(void* aligned_ptr) {
free(aligned_ptr);
}
#endif
} // namespace detail
} // namespace folly
/* /*
* Copyright 2017 Facebook, Inc. * Copyright 2016-present Facebook, Inc.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -16,11 +16,11 @@ ...@@ -16,11 +16,11 @@
#pragma once #pragma once
#include <stdlib.h> #include <folly/Memory.h> // @shim
namespace folly { namespace folly {
namespace detail { namespace detail {
void* aligned_malloc(size_t size, size_t align); using folly::aligned_free;
void aligned_free(void* aligned_ptr); using folly::aligned_malloc;
} // namespace detail } // namespace detail
} // namespace folly } // namespace folly
/* /*
* Copyright 2017 Facebook, Inc. * Copyright 2013-present Facebook, Inc.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -27,6 +27,21 @@ ...@@ -27,6 +27,21 @@
using namespace folly; using namespace folly;
TEST(aligned_malloc, examples) {
auto trial = [](size_t align) {
auto const ptr = aligned_malloc(1, align);
return (aligned_free(ptr), uintptr_t(ptr));
};
if (!kIsSanitize) { // asan allocator raises SIGABRT instead
EXPECT_EQ(EINVAL, (trial(2), errno)) << "too small";
EXPECT_EQ(EINVAL, (trial(513), errno)) << "not power of two";
}
EXPECT_EQ(0, trial(512) % 512);
EXPECT_EQ(0, trial(8192) % 8192);
}
TEST(make_unique, compatible_with_std_make_unique) { TEST(make_unique, compatible_with_std_make_unique) {
// HACK: To enforce that `folly::` is imported here. // HACK: To enforce that `folly::` is imported here.
to_shared_ptr(std::unique_ptr<std::string>()); to_shared_ptr(std::unique_ptr<std::string>());
......
/* /*
* Copyright 2017 Facebook, Inc. * Copyright 2012-present Facebook, Inc.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
......
/* /*
* Copyright 2017 Facebook, Inc. * Copyright 2011-present Facebook, Inc.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
...@@ -30,9 +30,9 @@ ...@@ -30,9 +30,9 @@
#include <boost/algorithm/string/trim.hpp> #include <boost/algorithm/string/trim.hpp>
#include <boost/range/concepts.hpp> #include <boost/range/concepts.hpp>
#include <folly/Memory.h>
#include <folly/portability/GMock.h> #include <folly/portability/GMock.h>
#include <folly/portability/GTest.h> #include <folly/portability/GTest.h>
#include <folly/portability/Memory.h>
#include <folly/portability/SysMman.h> #include <folly/portability/SysMman.h>
using namespace folly; using namespace folly;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment