From 46d4073a6436a1a6dd88b193876eb062b8357f04 Mon Sep 17 00:00:00 2001 From: "henrike@webrtc.org" Date: Wed, 3 Oct 2012 16:50:37 +0000 Subject: [PATCH] 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 --- .../interface/aligned_malloc.h | 14 +++- src/system_wrappers/source/aligned_malloc.cc | 64 +++++++++---------- .../source/aligned_malloc_unittest.cc | 13 ++-- 3 files changed, 52 insertions(+), 39 deletions(-) diff --git a/src/system_wrappers/interface/aligned_malloc.h b/src/system_wrappers/interface/aligned_malloc.h index 3fda6b660..662acabdf 100644 --- a/src/system_wrappers/interface/aligned_malloc.h +++ b/src/system_wrappers/interface/aligned_malloc.h @@ -34,7 +34,19 @@ void* GetRightAlign(const void* ptr, size_t alignment); // be de-allocated using AlignedFree. void* AlignedMalloc(size_t size, size_t alignment); // 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 +T* GetRightAlign(const T* ptr, size_t alignment) { + return reinterpret_cast(GetRightAlign(reinterpret_cast(ptr), + alignment)); +} +template +T* AlignedMalloc(size_t size, size_t alignment) { + return reinterpret_cast(AlignedMalloc(size, alignment)); +} // Scoped pointer to AlignedMalloc-memory. template diff --git a/src/system_wrappers/source/aligned_malloc.cc b/src/system_wrappers/source/aligned_malloc.cc index 3dd281f9b..c8efb42be 100644 --- a/src/system_wrappers/source/aligned_malloc.cc +++ b/src/system_wrappers/source/aligned_malloc.cc @@ -27,14 +27,14 @@ namespace webrtc { // TODO(henrike): better to create just one memory block and interpret the // first sizeof(AlignedMemory) bytes as an AlignedMemory struct. struct AlignedMemory { - void* alignedBuffer; - void* memoryPointer; + void* aligned_buffer; + 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 // 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. @@ -52,8 +52,8 @@ void* GetRightAlign(const void* ptr, size_t alignment) { if (!ValidAlignment(alignment)) { return NULL; } - uintptr_t startPos = reinterpret_cast(ptr); - return reinterpret_cast(GetRightAlign(startPos, alignment)); + uintptr_t start_pos = reinterpret_cast(ptr); + return reinterpret_cast(GetRightAlign(start_pos, alignment)); } void* AlignedMalloc(size_t size, size_t alignment) { @@ -64,8 +64,8 @@ void* AlignedMalloc(size_t size, size_t alignment) { return NULL; } - AlignedMemory* returnValue = new AlignedMemory(); - if (returnValue == NULL) { + AlignedMemory* return_value = new AlignedMemory(); + if (return_value == NULL) { return NULL; } @@ -73,46 +73,46 @@ void* AlignedMalloc(size_t size, size_t alignment) { // alignment - 1 bytes needs to be allocated. // A pointer to AlignedMemory must be stored so that it can be retreived for // deletion, ergo the sizeof(uintptr_t). - returnValue->memoryPointer = malloc(size + sizeof(uintptr_t) + - alignment - 1); - if (returnValue->memoryPointer == NULL) { - delete returnValue; + return_value->memory_pointer = malloc(size + sizeof(uintptr_t) + + alignment - 1); + if (return_value->memory_pointer == NULL) { + delete return_value; return NULL; } // Aligning after the sizeof(header) bytes will leave room for the header // in the same memory block. - uintptr_t alignStartPos = - reinterpret_cast(returnValue->memoryPointer); - alignStartPos += sizeof(uintptr_t); - uintptr_t alignedPos = GetRightAlign(alignStartPos, alignment); - returnValue->alignedBuffer = reinterpret_cast(alignedPos); + uintptr_t align_start_pos = + reinterpret_cast(return_value->memory_pointer); + align_start_pos += sizeof(uintptr_t); + uintptr_t aligned_pos = GetRightAlign(align_start_pos, alignment); + return_value->aligned_buffer = reinterpret_cast(aligned_pos); // Store the address to the AlignedMemory struct in the header so that a // it's possible to reclaim all memory. - uintptr_t headerPos = alignedPos; - headerPos -= sizeof(uintptr_t); - void* headerPtr = reinterpret_cast(headerPos); - uintptr_t headerValue = reinterpret_cast(returnValue); - memcpy(headerPtr,&headerValue,sizeof(uintptr_t)); - return returnValue->alignedBuffer; + uintptr_t header_pos = aligned_pos; + header_pos -= sizeof(uintptr_t); + void* header_ptr = reinterpret_cast(header_pos); + uintptr_t header_value = reinterpret_cast(return_value); + memcpy(header_ptr, &header_value, sizeof(uintptr_t)); + return return_value->aligned_buffer; } -void AlignedFree(void* memBlock) { - if (memBlock == NULL) { +void AlignedFree(void* mem_block) { + if (mem_block == NULL) { return; } - uintptr_t alignedPos = reinterpret_cast(memBlock); - uintptr_t headerPos = alignedPos - sizeof(uintptr_t); + uintptr_t aligned_pos = reinterpret_cast(mem_block); + uintptr_t header_pos = aligned_pos - sizeof(uintptr_t); // Read out the address of the AlignedMemory struct from the header. - uintptr_t* headerPtr = reinterpret_cast(headerPos); - AlignedMemory* deleteMemory = reinterpret_cast(*headerPtr); + uintptr_t* header_ptr = reinterpret_cast(header_pos); + AlignedMemory* delete_memory = reinterpret_cast(*header_ptr); - if (deleteMemory->memoryPointer != NULL) { - free(deleteMemory->memoryPointer); + if (delete_memory->memory_pointer != NULL) { + free(delete_memory->memory_pointer); } - delete deleteMemory; + delete delete_memory; } } // namespace webrtc diff --git a/src/system_wrappers/source/aligned_malloc_unittest.cc b/src/system_wrappers/source/aligned_malloc_unittest.cc index 9c6223c63..42e13c542 100644 --- a/src/system_wrappers/source/aligned_malloc_unittest.cc +++ b/src/system_wrappers/source/aligned_malloc_unittest.cc @@ -23,7 +23,7 @@ // Returns true if |size| and |alignment| are valid combinations. bool CorrectUsage(size_t size, size_t alignment) { webrtc::Allocator::scoped_ptr_aligned scoped( - static_cast (webrtc::AlignedMalloc(size, alignment))); + webrtc::AlignedMalloc(size, alignment)); if (scoped.get() == NULL) { return false; } @@ -36,14 +36,15 @@ TEST(AlignedMalloc, GetRightAlign) { const size_t alignment = 32; const size_t left_missalignment = 8; webrtc::Allocator::scoped_ptr_aligned scoped( - static_cast (webrtc::AlignedMalloc(size, alignment))); + webrtc::AlignedMalloc(size, alignment)); EXPECT_TRUE(scoped.get() != NULL); const uintptr_t aligned_address = reinterpret_cast (scoped.get()); const uintptr_t missaligned_address = aligned_address - left_missalignment; - const void* missaligned_ptr = reinterpret_cast (missaligned_address); - const void* realignedPtr = webrtc::GetRightAlign( - missaligned_ptr, alignment); - EXPECT_EQ(scoped.get(), realignedPtr); + const char* missaligned_ptr = reinterpret_cast( + missaligned_address); + const char* realigned_ptr = webrtc::GetRightAlign( + missaligned_ptr, alignment); + EXPECT_EQ(scoped.get(), realigned_ptr); } TEST(AlignedMalloc, IncorrectSize) {