Move aligned memory utilities to rtc_base/memory/
This moves them from an API directory (system_wrappers/include/) to a non-API directory, which is exactly what we want for utilities like this. BUG=webrtc:8445 Change-Id: I6dc34fe662f5d87b3b5288d33055345bc6bf91db Reviewed-on: https://webrtc-review.googlesource.com/21164 Commit-Queue: Karl Wiberg <kwiberg@webrtc.org> Reviewed-by: Danil Chapovalov <danilchap@webrtc.org> Cr-Commit-Position: refs/heads/master@{#22567}
This commit is contained in:
@ -1228,6 +1228,7 @@ if (rtc_include_tests) {
|
||||
"../system_wrappers:system_wrappers",
|
||||
"../test:fileutils",
|
||||
"../test:test_support",
|
||||
"memory:unittests",
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
47
rtc_base/memory/BUILD.gn
Normal file
47
rtc_base/memory/BUILD.gn
Normal file
@ -0,0 +1,47 @@
|
||||
# Copyright (c) 2018 The WebRTC project authors. All Rights Reserved.
|
||||
#
|
||||
# Use of this source code is governed by a BSD-style license
|
||||
# that can be found in the LICENSE file in the root of the source
|
||||
# tree. An additional intellectual property rights grant can be found
|
||||
# in the file PATENTS. All contributing project authors may
|
||||
# be found in the AUTHORS file in the root of the source tree.
|
||||
|
||||
import("../../webrtc.gni")
|
||||
if (is_android) {
|
||||
import("//build/config/android/config.gni")
|
||||
import("//build/config/android/rules.gni")
|
||||
}
|
||||
|
||||
rtc_source_set("aligned_array") {
|
||||
sources = [
|
||||
"aligned_array.h",
|
||||
]
|
||||
deps = [
|
||||
":aligned_malloc",
|
||||
"..:checks",
|
||||
]
|
||||
}
|
||||
|
||||
rtc_source_set("aligned_malloc") {
|
||||
sources = [
|
||||
"aligned_malloc.cc",
|
||||
"aligned_malloc.h",
|
||||
]
|
||||
deps = [
|
||||
"../..:typedefs",
|
||||
]
|
||||
}
|
||||
|
||||
rtc_source_set("unittests") {
|
||||
testonly = true
|
||||
sources = [
|
||||
"aligned_array_unittest.cc",
|
||||
"aligned_malloc_unittest.cc",
|
||||
]
|
||||
deps = [
|
||||
":aligned_array",
|
||||
":aligned_malloc",
|
||||
"../..:typedefs",
|
||||
"../../test:test_support",
|
||||
]
|
||||
}
|
||||
78
rtc_base/memory/aligned_array.h
Normal file
78
rtc_base/memory/aligned_array.h
Normal file
@ -0,0 +1,78 @@
|
||||
/*
|
||||
* Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by a BSD-style license
|
||||
* that can be found in the LICENSE file in the root of the source
|
||||
* tree. An additional intellectual property rights grant can be found
|
||||
* in the file PATENTS. All contributing project authors may
|
||||
* be found in the AUTHORS file in the root of the source tree.
|
||||
*/
|
||||
|
||||
#ifndef RTC_BASE_MEMORY_ALIGNED_ARRAY_H_
|
||||
#define RTC_BASE_MEMORY_ALIGNED_ARRAY_H_
|
||||
|
||||
#include "rtc_base/checks.h"
|
||||
#include "rtc_base/memory/aligned_malloc.h"
|
||||
|
||||
namespace webrtc {
|
||||
|
||||
// Wrapper class for aligned arrays. Every row (and the first dimension) are
|
||||
// aligned to the given byte alignment.
|
||||
template <typename T>
|
||||
class AlignedArray {
|
||||
public:
|
||||
AlignedArray(size_t rows, size_t cols, size_t alignment)
|
||||
: rows_(rows), cols_(cols) {
|
||||
RTC_CHECK_GT(alignment, 0);
|
||||
head_row_ =
|
||||
static_cast<T**>(AlignedMalloc(rows_ * sizeof(*head_row_), alignment));
|
||||
for (size_t i = 0; i < rows_; ++i) {
|
||||
head_row_[i] = static_cast<T*>(
|
||||
AlignedMalloc(cols_ * sizeof(**head_row_), alignment));
|
||||
}
|
||||
}
|
||||
|
||||
~AlignedArray() {
|
||||
for (size_t i = 0; i < rows_; ++i) {
|
||||
AlignedFree(head_row_[i]);
|
||||
}
|
||||
AlignedFree(head_row_);
|
||||
}
|
||||
|
||||
T* const* Array() { return head_row_; }
|
||||
|
||||
const T* const* Array() const { return head_row_; }
|
||||
|
||||
T* Row(size_t row) {
|
||||
RTC_CHECK_LE(row, rows_);
|
||||
return head_row_[row];
|
||||
}
|
||||
|
||||
const T* Row(size_t row) const {
|
||||
RTC_CHECK_LE(row, rows_);
|
||||
return head_row_[row];
|
||||
}
|
||||
|
||||
T& At(size_t row, size_t col) {
|
||||
RTC_CHECK_LE(col, cols_);
|
||||
return Row(row)[col];
|
||||
}
|
||||
|
||||
const T& At(size_t row, size_t col) const {
|
||||
RTC_CHECK_LE(col, cols_);
|
||||
return Row(row)[col];
|
||||
}
|
||||
|
||||
size_t rows() const { return rows_; }
|
||||
|
||||
size_t cols() const { return cols_; }
|
||||
|
||||
private:
|
||||
size_t rows_;
|
||||
size_t cols_;
|
||||
T** head_row_;
|
||||
};
|
||||
|
||||
} // namespace webrtc
|
||||
|
||||
#endif // RTC_BASE_MEMORY_ALIGNED_ARRAY_H_
|
||||
60
rtc_base/memory/aligned_array_unittest.cc
Normal file
60
rtc_base/memory/aligned_array_unittest.cc
Normal file
@ -0,0 +1,60 @@
|
||||
/*
|
||||
* Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by a BSD-style license
|
||||
* that can be found in the LICENSE file in the root of the source
|
||||
* tree. An additional intellectual property rights grant can be found
|
||||
* in the file PATENTS. All contributing project authors may
|
||||
* be found in the AUTHORS file in the root of the source tree.
|
||||
*/
|
||||
|
||||
#include "rtc_base/memory/aligned_array.h"
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include "test/gtest.h"
|
||||
|
||||
namespace {
|
||||
|
||||
bool IsAligned(const void* ptr, size_t alignment) {
|
||||
return reinterpret_cast<uintptr_t>(ptr) % alignment == 0;
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
namespace webrtc {
|
||||
|
||||
TEST(AlignedArrayTest, CheckAlignment) {
|
||||
AlignedArray<bool> arr(10, 7, 128);
|
||||
ASSERT_TRUE(IsAligned(arr.Array(), 128));
|
||||
for (size_t i = 0; i < 10; ++i) {
|
||||
ASSERT_TRUE(IsAligned(arr.Row(i), 128));
|
||||
ASSERT_EQ(arr.Row(i), arr.Array()[i]);
|
||||
}
|
||||
}
|
||||
|
||||
TEST(AlignedArrayTest, CheckOverlap) {
|
||||
AlignedArray<size_t> arr(10, 7, 128);
|
||||
|
||||
for (size_t i = 0; i < 10; ++i) {
|
||||
for (size_t j = 0; j < 7; ++j) {
|
||||
arr.At(i, j) = 20 * i + j;
|
||||
}
|
||||
}
|
||||
|
||||
for (size_t i = 0; i < 10; ++i) {
|
||||
for (size_t j = 0; j < 7; ++j) {
|
||||
ASSERT_EQ(arr.At(i, j), 20 * i + j);
|
||||
ASSERT_EQ(arr.Row(i)[j], 20 * i + j);
|
||||
ASSERT_EQ(arr.Array()[i][j], 20 * i + j);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST(AlignedArrayTest, CheckRowsCols) {
|
||||
AlignedArray<bool> arr(10, 7, 128);
|
||||
ASSERT_EQ(arr.rows(), 10u);
|
||||
ASSERT_EQ(arr.cols(), 7u);
|
||||
}
|
||||
|
||||
} // namespace webrtc
|
||||
100
rtc_base/memory/aligned_malloc.cc
Normal file
100
rtc_base/memory/aligned_malloc.cc
Normal file
@ -0,0 +1,100 @@
|
||||
/*
|
||||
* Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by a BSD-style license
|
||||
* that can be found in the LICENSE file in the root of the source
|
||||
* tree. An additional intellectual property rights grant can be found
|
||||
* in the file PATENTS. All contributing project authors may
|
||||
* be found in the AUTHORS file in the root of the source tree.
|
||||
*/
|
||||
|
||||
#include "rtc_base/memory/aligned_malloc.h"
|
||||
|
||||
#include <memory.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#else
|
||||
#include <stdint.h>
|
||||
#endif
|
||||
|
||||
#include "typedefs.h" // NOLINT(build/include)
|
||||
|
||||
// Reference on memory alignment:
|
||||
// http://stackoverflow.com/questions/227897/solve-the-memory-alignment-in-c-interview-question-that-stumped-me
|
||||
namespace webrtc {
|
||||
|
||||
uintptr_t GetRightAlign(uintptr_t start_pos, size_t alignment) {
|
||||
// The pointer should be aligned with |alignment| bytes. The - 1 guarantees
|
||||
// that it is aligned towards the closest higher (right) address.
|
||||
return (start_pos + alignment - 1) & ~(alignment - 1);
|
||||
}
|
||||
|
||||
// Alignment must be an integer power of two.
|
||||
bool ValidAlignment(size_t alignment) {
|
||||
if (!alignment) {
|
||||
return false;
|
||||
}
|
||||
return (alignment & (alignment - 1)) == 0;
|
||||
}
|
||||
|
||||
void* GetRightAlign(const void* pointer, size_t alignment) {
|
||||
if (!pointer) {
|
||||
return NULL;
|
||||
}
|
||||
if (!ValidAlignment(alignment)) {
|
||||
return NULL;
|
||||
}
|
||||
uintptr_t start_pos = reinterpret_cast<uintptr_t>(pointer);
|
||||
return reinterpret_cast<void*>(GetRightAlign(start_pos, alignment));
|
||||
}
|
||||
|
||||
void* AlignedMalloc(size_t size, size_t alignment) {
|
||||
if (size == 0) {
|
||||
return NULL;
|
||||
}
|
||||
if (!ValidAlignment(alignment)) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// The memory is aligned towards the lowest address that so only
|
||||
// alignment - 1 bytes needs to be allocated.
|
||||
// A pointer to the start of the memory must be stored so that it can be
|
||||
// retreived for deletion, ergo the sizeof(uintptr_t).
|
||||
void* memory_pointer = malloc(size + sizeof(uintptr_t) + alignment - 1);
|
||||
if (memory_pointer == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
// Aligning after the sizeof(uintptr_t) bytes will leave room for the header
|
||||
// in the same memory block.
|
||||
uintptr_t align_start_pos = reinterpret_cast<uintptr_t>(memory_pointer);
|
||||
align_start_pos += sizeof(uintptr_t);
|
||||
uintptr_t aligned_pos = GetRightAlign(align_start_pos, alignment);
|
||||
void* aligned_pointer = reinterpret_cast<void*>(aligned_pos);
|
||||
|
||||
// Store the address to the beginning of the memory just before the aligned
|
||||
// memory.
|
||||
uintptr_t header_pos = aligned_pos - sizeof(uintptr_t);
|
||||
void* header_pointer = reinterpret_cast<void*>(header_pos);
|
||||
uintptr_t memory_start = reinterpret_cast<uintptr_t>(memory_pointer);
|
||||
memcpy(header_pointer, &memory_start, sizeof(uintptr_t));
|
||||
|
||||
return aligned_pointer;
|
||||
}
|
||||
|
||||
void AlignedFree(void* mem_block) {
|
||||
if (mem_block == NULL) {
|
||||
return;
|
||||
}
|
||||
uintptr_t aligned_pos = reinterpret_cast<uintptr_t>(mem_block);
|
||||
uintptr_t header_pos = aligned_pos - sizeof(uintptr_t);
|
||||
|
||||
// Read out the address of the AlignedMemory struct from the header.
|
||||
uintptr_t memory_start_pos = *reinterpret_cast<uintptr_t*>(header_pos);
|
||||
void* memory_start = reinterpret_cast<void*>(memory_start_pos);
|
||||
free(memory_start);
|
||||
}
|
||||
|
||||
} // namespace webrtc
|
||||
57
rtc_base/memory/aligned_malloc.h
Normal file
57
rtc_base/memory/aligned_malloc.h
Normal file
@ -0,0 +1,57 @@
|
||||
/*
|
||||
* Copyright (c) 2011 The WebRTC project authors. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by a BSD-style license
|
||||
* that can be found in the LICENSE file in the root of the source
|
||||
* tree. An additional intellectual property rights grant can be found
|
||||
* in the file PATENTS. All contributing project authors may
|
||||
* be found in the AUTHORS file in the root of the source tree.
|
||||
*/
|
||||
|
||||
#ifndef RTC_BASE_MEMORY_ALIGNED_MALLOC_H_
|
||||
#define RTC_BASE_MEMORY_ALIGNED_MALLOC_H_
|
||||
|
||||
// The functions declared here
|
||||
// 1) Allocates block of aligned memory.
|
||||
// 2) Re-calculates a pointer such that it is aligned to a higher or equal
|
||||
// address.
|
||||
// Note: alignment must be a power of two. The alignment is in bytes.
|
||||
|
||||
#include <stddef.h>
|
||||
|
||||
namespace webrtc {
|
||||
|
||||
// Returns a pointer to the first boundry of |alignment| bytes following the
|
||||
// address of |ptr|.
|
||||
// Note that there is no guarantee that the memory in question is available.
|
||||
// |ptr| has no requirements other than it can't be NULL.
|
||||
void* GetRightAlign(const void* ptr, size_t alignment);
|
||||
|
||||
// Allocates memory of |size| bytes aligned on an |alignment| boundry.
|
||||
// The return value is a pointer to the memory. Note that the memory must
|
||||
// be de-allocated using AlignedFree.
|
||||
void* AlignedMalloc(size_t size, size_t alignment);
|
||||
// De-allocates memory created using the AlignedMalloc() API.
|
||||
void AlignedFree(void* mem_block);
|
||||
|
||||
// Templated versions to facilitate usage of aligned malloc without casting
|
||||
// to and from void*.
|
||||
template <typename T>
|
||||
T* GetRightAlign(const T* ptr, size_t alignment) {
|
||||
return reinterpret_cast<T*>(
|
||||
GetRightAlign(reinterpret_cast<const void*>(ptr), alignment));
|
||||
}
|
||||
template <typename T>
|
||||
T* AlignedMalloc(size_t size, size_t alignment) {
|
||||
return reinterpret_cast<T*>(AlignedMalloc(size, alignment));
|
||||
}
|
||||
|
||||
// Deleter for use with unique_ptr. E.g., use as
|
||||
// std::unique_ptr<Foo, AlignedFreeDeleter> foo;
|
||||
struct AlignedFreeDeleter {
|
||||
inline void operator()(void* ptr) const { AlignedFree(ptr); }
|
||||
};
|
||||
|
||||
} // namespace webrtc
|
||||
|
||||
#endif // RTC_BASE_MEMORY_ALIGNED_MALLOC_H_
|
||||
82
rtc_base/memory/aligned_malloc_unittest.cc
Normal file
82
rtc_base/memory/aligned_malloc_unittest.cc
Normal file
@ -0,0 +1,82 @@
|
||||
/*
|
||||
* Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
|
||||
*
|
||||
* Use of this source code is governed by a BSD-style license
|
||||
* that can be found in the LICENSE file in the root of the source
|
||||
* tree. An additional intellectual property rights grant can be found
|
||||
* in the file PATENTS. All contributing project authors may
|
||||
* be found in the AUTHORS file in the root of the source tree.
|
||||
*/
|
||||
|
||||
#include "rtc_base/memory/aligned_malloc.h"
|
||||
|
||||
#include <memory>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
#else
|
||||
#include <stdint.h>
|
||||
#endif
|
||||
|
||||
#include "test/gtest.h"
|
||||
#include "typedefs.h" // NOLINT(build/include)
|
||||
|
||||
namespace webrtc {
|
||||
|
||||
// Returns true if |size| and |alignment| are valid combinations.
|
||||
bool CorrectUsage(size_t size, size_t alignment) {
|
||||
std::unique_ptr<char, AlignedFreeDeleter> scoped(
|
||||
static_cast<char*>(AlignedMalloc(size, alignment)));
|
||||
if (scoped.get() == NULL) {
|
||||
return false;
|
||||
}
|
||||
const uintptr_t scoped_address = reinterpret_cast<uintptr_t>(scoped.get());
|
||||
return 0u == scoped_address % alignment;
|
||||
}
|
||||
|
||||
TEST(AlignedMalloc, GetRightAlign) {
|
||||
const size_t size = 100;
|
||||
const size_t alignment = 32;
|
||||
const size_t left_misalignment = 1;
|
||||
std::unique_ptr<char, AlignedFreeDeleter> scoped(
|
||||
static_cast<char*>(AlignedMalloc(size, alignment)));
|
||||
EXPECT_TRUE(scoped.get() != NULL);
|
||||
const uintptr_t aligned_address = reinterpret_cast<uintptr_t>(scoped.get());
|
||||
const uintptr_t misaligned_address = aligned_address - left_misalignment;
|
||||
const char* misaligned_ptr =
|
||||
reinterpret_cast<const char*>(misaligned_address);
|
||||
const char* realigned_ptr = GetRightAlign(misaligned_ptr, alignment);
|
||||
EXPECT_EQ(scoped.get(), realigned_ptr);
|
||||
}
|
||||
|
||||
TEST(AlignedMalloc, IncorrectSize) {
|
||||
const size_t incorrect_size = 0;
|
||||
const size_t alignment = 64;
|
||||
EXPECT_FALSE(CorrectUsage(incorrect_size, alignment));
|
||||
}
|
||||
|
||||
TEST(AlignedMalloc, IncorrectAlignment) {
|
||||
const size_t size = 100;
|
||||
const size_t incorrect_alignment = 63;
|
||||
EXPECT_FALSE(CorrectUsage(size, incorrect_alignment));
|
||||
}
|
||||
|
||||
TEST(AlignedMalloc, AlignTo2Bytes) {
|
||||
size_t size = 100;
|
||||
size_t alignment = 2;
|
||||
EXPECT_TRUE(CorrectUsage(size, alignment));
|
||||
}
|
||||
|
||||
TEST(AlignedMalloc, AlignTo32Bytes) {
|
||||
size_t size = 100;
|
||||
size_t alignment = 32;
|
||||
EXPECT_TRUE(CorrectUsage(size, alignment));
|
||||
}
|
||||
|
||||
TEST(AlignedMalloc, AlignTo128Bytes) {
|
||||
size_t size = 100;
|
||||
size_t alignment = 128;
|
||||
EXPECT_TRUE(CorrectUsage(size, alignment));
|
||||
}
|
||||
|
||||
} // namespace webrtc
|
||||
Reference in New Issue
Block a user