portability/Fcntl.cpp \
portability/Libgen.cpp \
portability/Malloc.cpp \
- portability/Memory.cpp \
portability/OpenSSL.cpp \
portability/PThread.cpp \
portability/Sockets.cpp \
/*
- * Copyright 2017 Facebook, Inc.
+ * Copyright 2013-present Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
#pragma once
-#include <folly/Traits.h>
-#include <folly/functional/Invoke.h>
-
+#include <cassert>
+#include <cerrno>
#include <cstddef>
#include <cstdlib>
#include <exception>
#include <type_traits>
#include <utility>
+#include <folly/Traits.h>
+#include <folly/functional/Invoke.h>
+#include <folly/portability/Config.h>
+#include <folly/portability/Malloc.h>
+
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
* we have std::make_unique().
/*
- * Copyright 2017 Facebook, Inc.
+ * Copyright 2011-present Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (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");
* you may not use this file except in compliance with the License.
SimpleAllocator::~SimpleAllocator() {
std::lock_guard<std::mutex> g(m_);
for (auto& block : blocks_) {
- detail::aligned_free(block);
+ folly::aligned_free(block);
}
}
void* SimpleAllocator::allocateHard() {
// 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_) {
std::__throw_bad_alloc();
}
/*
- * Copyright 2017 Facebook, Inc.
+ * Copyright 2013-present Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
#include <folly/hash/Hash.h>
#include <folly/lang/Align.h>
#include <folly/portability/BitsFunctexcept.h>
-#include <folly/portability/Memory.h>
#include <folly/system/ThreadId.h>
namespace folly {
// Align to a cacheline
size = size + (hardware_destructive_interference_size - 1);
size &= ~size_t(hardware_destructive_interference_size - 1);
- void* mem = detail::aligned_malloc(
- size, hardware_destructive_interference_size);
+ void* mem =
+ aligned_malloc(size, hardware_destructive_interference_size);
if (!mem) {
std::__throw_bad_alloc();
}
auto allocator = *static_cast<SimpleAllocator**>(addr);
allocator->deallocate(mem);
} 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");
* 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");
* 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");
* 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");
* 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");
* 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");
* you may not use this file except in compliance with the License.
+++ /dev/null
-/*
- * 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");
* you may not use this file except in compliance with the License.
#pragma once
-#include <stdlib.h>
+#include <folly/Memory.h> // @shim
namespace folly {
namespace detail {
-void* aligned_malloc(size_t size, size_t align);
-void aligned_free(void* aligned_ptr);
+using folly::aligned_free;
+using folly::aligned_malloc;
} // namespace detail
} // namespace folly
/*
- * Copyright 2017 Facebook, Inc.
+ * Copyright 2013-present Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
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) {
// HACK: To enforce that `folly::` is imported here.
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");
* 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");
* you may not use this file except in compliance with the License.
#include <boost/algorithm/string/trim.hpp>
#include <boost/range/concepts.hpp>
+#include <folly/Memory.h>
#include <folly/portability/GMock.h>
#include <folly/portability/GTest.h>
-#include <folly/portability/Memory.h>
#include <folly/portability/SysMman.h>
using namespace folly;