Made the aligned malloc templated.
BUG=N/A Review URL: https://webrtc-codereview.appspot.com/865004 git-svn-id: http://webrtc.googlecode.com/svn/trunk@2866 4adac7df-926f-26a2-2b94-8c16560cd09d
This commit is contained in:
parent
2e5192fa6a
commit
46d4073a64
@ -34,7 +34,19 @@ void* GetRightAlign(const void* ptr, size_t alignment);
|
|||||||
// be de-allocated using AlignedFree.
|
// be de-allocated using AlignedFree.
|
||||||
void* AlignedMalloc(size_t size, size_t alignment);
|
void* AlignedMalloc(size_t size, size_t alignment);
|
||||||
// De-allocates memory created using the AlignedMalloc() API.
|
// De-allocates memory created using the AlignedMalloc() API.
|
||||||
void AlignedFree(void* memBlock);
|
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));
|
||||||
|
}
|
||||||
|
|
||||||
// Scoped pointer to AlignedMalloc-memory.
|
// Scoped pointer to AlignedMalloc-memory.
|
||||||
template<typename T>
|
template<typename T>
|
||||||
|
@ -27,14 +27,14 @@ namespace webrtc {
|
|||||||
// TODO(henrike): better to create just one memory block and interpret the
|
// TODO(henrike): better to create just one memory block and interpret the
|
||||||
// first sizeof(AlignedMemory) bytes as an AlignedMemory struct.
|
// first sizeof(AlignedMemory) bytes as an AlignedMemory struct.
|
||||||
struct AlignedMemory {
|
struct AlignedMemory {
|
||||||
void* alignedBuffer;
|
void* aligned_buffer;
|
||||||
void* memoryPointer;
|
void* memory_pointer;
|
||||||
};
|
};
|
||||||
|
|
||||||
uintptr_t GetRightAlign(uintptr_t startPos, size_t alignment) {
|
uintptr_t GetRightAlign(uintptr_t start_pos, size_t alignment) {
|
||||||
// The pointer should be aligned with |alignment| bytes. The - 1 guarantees
|
// The pointer should be aligned with |alignment| bytes. The - 1 guarantees
|
||||||
// that it is aligned towards the closest higher (right) address.
|
// that it is aligned towards the closest higher (right) address.
|
||||||
return (startPos + alignment - 1) & ~(alignment - 1);
|
return (start_pos + alignment - 1) & ~(alignment - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Alignment must be an integer power of two.
|
// Alignment must be an integer power of two.
|
||||||
@ -52,8 +52,8 @@ void* GetRightAlign(const void* ptr, size_t alignment) {
|
|||||||
if (!ValidAlignment(alignment)) {
|
if (!ValidAlignment(alignment)) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
uintptr_t startPos = reinterpret_cast<uintptr_t>(ptr);
|
uintptr_t start_pos = reinterpret_cast<uintptr_t>(ptr);
|
||||||
return reinterpret_cast<void*>(GetRightAlign(startPos, alignment));
|
return reinterpret_cast<void*>(GetRightAlign(start_pos, alignment));
|
||||||
}
|
}
|
||||||
|
|
||||||
void* AlignedMalloc(size_t size, size_t alignment) {
|
void* AlignedMalloc(size_t size, size_t alignment) {
|
||||||
@ -64,8 +64,8 @@ void* AlignedMalloc(size_t size, size_t alignment) {
|
|||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
AlignedMemory* returnValue = new AlignedMemory();
|
AlignedMemory* return_value = new AlignedMemory();
|
||||||
if (returnValue == NULL) {
|
if (return_value == NULL) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -73,46 +73,46 @@ void* AlignedMalloc(size_t size, size_t alignment) {
|
|||||||
// alignment - 1 bytes needs to be allocated.
|
// alignment - 1 bytes needs to be allocated.
|
||||||
// A pointer to AlignedMemory must be stored so that it can be retreived for
|
// A pointer to AlignedMemory must be stored so that it can be retreived for
|
||||||
// deletion, ergo the sizeof(uintptr_t).
|
// deletion, ergo the sizeof(uintptr_t).
|
||||||
returnValue->memoryPointer = malloc(size + sizeof(uintptr_t) +
|
return_value->memory_pointer = malloc(size + sizeof(uintptr_t) +
|
||||||
alignment - 1);
|
alignment - 1);
|
||||||
if (returnValue->memoryPointer == NULL) {
|
if (return_value->memory_pointer == NULL) {
|
||||||
delete returnValue;
|
delete return_value;
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Aligning after the sizeof(header) bytes will leave room for the header
|
// Aligning after the sizeof(header) bytes will leave room for the header
|
||||||
// in the same memory block.
|
// in the same memory block.
|
||||||
uintptr_t alignStartPos =
|
uintptr_t align_start_pos =
|
||||||
reinterpret_cast<uintptr_t>(returnValue->memoryPointer);
|
reinterpret_cast<uintptr_t>(return_value->memory_pointer);
|
||||||
alignStartPos += sizeof(uintptr_t);
|
align_start_pos += sizeof(uintptr_t);
|
||||||
uintptr_t alignedPos = GetRightAlign(alignStartPos, alignment);
|
uintptr_t aligned_pos = GetRightAlign(align_start_pos, alignment);
|
||||||
returnValue->alignedBuffer = reinterpret_cast<void*>(alignedPos);
|
return_value->aligned_buffer = reinterpret_cast<void*>(aligned_pos);
|
||||||
|
|
||||||
// Store the address to the AlignedMemory struct in the header so that a
|
// Store the address to the AlignedMemory struct in the header so that a
|
||||||
// it's possible to reclaim all memory.
|
// it's possible to reclaim all memory.
|
||||||
uintptr_t headerPos = alignedPos;
|
uintptr_t header_pos = aligned_pos;
|
||||||
headerPos -= sizeof(uintptr_t);
|
header_pos -= sizeof(uintptr_t);
|
||||||
void* headerPtr = reinterpret_cast<void*>(headerPos);
|
void* header_ptr = reinterpret_cast<void*>(header_pos);
|
||||||
uintptr_t headerValue = reinterpret_cast<uintptr_t>(returnValue);
|
uintptr_t header_value = reinterpret_cast<uintptr_t>(return_value);
|
||||||
memcpy(headerPtr,&headerValue,sizeof(uintptr_t));
|
memcpy(header_ptr, &header_value, sizeof(uintptr_t));
|
||||||
return returnValue->alignedBuffer;
|
return return_value->aligned_buffer;
|
||||||
}
|
}
|
||||||
|
|
||||||
void AlignedFree(void* memBlock) {
|
void AlignedFree(void* mem_block) {
|
||||||
if (memBlock == NULL) {
|
if (mem_block == NULL) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
uintptr_t alignedPos = reinterpret_cast<uintptr_t>(memBlock);
|
uintptr_t aligned_pos = reinterpret_cast<uintptr_t>(mem_block);
|
||||||
uintptr_t headerPos = alignedPos - sizeof(uintptr_t);
|
uintptr_t header_pos = aligned_pos - sizeof(uintptr_t);
|
||||||
|
|
||||||
// Read out the address of the AlignedMemory struct from the header.
|
// Read out the address of the AlignedMemory struct from the header.
|
||||||
uintptr_t* headerPtr = reinterpret_cast<uintptr_t*>(headerPos);
|
uintptr_t* header_ptr = reinterpret_cast<uintptr_t*>(header_pos);
|
||||||
AlignedMemory* deleteMemory = reinterpret_cast<AlignedMemory*>(*headerPtr);
|
AlignedMemory* delete_memory = reinterpret_cast<AlignedMemory*>(*header_ptr);
|
||||||
|
|
||||||
if (deleteMemory->memoryPointer != NULL) {
|
if (delete_memory->memory_pointer != NULL) {
|
||||||
free(deleteMemory->memoryPointer);
|
free(delete_memory->memory_pointer);
|
||||||
}
|
}
|
||||||
delete deleteMemory;
|
delete delete_memory;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace webrtc
|
} // namespace webrtc
|
||||||
|
@ -23,7 +23,7 @@
|
|||||||
// Returns true if |size| and |alignment| are valid combinations.
|
// Returns true if |size| and |alignment| are valid combinations.
|
||||||
bool CorrectUsage(size_t size, size_t alignment) {
|
bool CorrectUsage(size_t size, size_t alignment) {
|
||||||
webrtc::Allocator<char>::scoped_ptr_aligned scoped(
|
webrtc::Allocator<char>::scoped_ptr_aligned scoped(
|
||||||
static_cast<char*> (webrtc::AlignedMalloc(size, alignment)));
|
webrtc::AlignedMalloc<char>(size, alignment));
|
||||||
if (scoped.get() == NULL) {
|
if (scoped.get() == NULL) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -36,14 +36,15 @@ TEST(AlignedMalloc, GetRightAlign) {
|
|||||||
const size_t alignment = 32;
|
const size_t alignment = 32;
|
||||||
const size_t left_missalignment = 8;
|
const size_t left_missalignment = 8;
|
||||||
webrtc::Allocator<char>::scoped_ptr_aligned scoped(
|
webrtc::Allocator<char>::scoped_ptr_aligned scoped(
|
||||||
static_cast<char*> (webrtc::AlignedMalloc(size, alignment)));
|
webrtc::AlignedMalloc<char>(size, alignment));
|
||||||
EXPECT_TRUE(scoped.get() != NULL);
|
EXPECT_TRUE(scoped.get() != NULL);
|
||||||
const uintptr_t aligned_address = reinterpret_cast<uintptr_t> (scoped.get());
|
const uintptr_t aligned_address = reinterpret_cast<uintptr_t> (scoped.get());
|
||||||
const uintptr_t missaligned_address = aligned_address - left_missalignment;
|
const uintptr_t missaligned_address = aligned_address - left_missalignment;
|
||||||
const void* missaligned_ptr = reinterpret_cast<void*> (missaligned_address);
|
const char* missaligned_ptr = reinterpret_cast<const char*>(
|
||||||
const void* realignedPtr = webrtc::GetRightAlign(
|
missaligned_address);
|
||||||
missaligned_ptr, alignment);
|
const char* realigned_ptr = webrtc::GetRightAlign(
|
||||||
EXPECT_EQ(scoped.get(), realignedPtr);
|
missaligned_ptr, alignment);
|
||||||
|
EXPECT_EQ(scoped.get(), realigned_ptr);
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST(AlignedMalloc, IncorrectSize) {
|
TEST(AlignedMalloc, IncorrectSize) {
|
||||||
|
Loading…
Reference in New Issue
Block a user