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.
|
||||
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<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.
|
||||
template<typename T>
|
||||
|
@ -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<uintptr_t>(ptr);
|
||||
return reinterpret_cast<void*>(GetRightAlign(startPos, alignment));
|
||||
uintptr_t start_pos = reinterpret_cast<uintptr_t>(ptr);
|
||||
return reinterpret_cast<void*>(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<uintptr_t>(returnValue->memoryPointer);
|
||||
alignStartPos += sizeof(uintptr_t);
|
||||
uintptr_t alignedPos = GetRightAlign(alignStartPos, alignment);
|
||||
returnValue->alignedBuffer = reinterpret_cast<void*>(alignedPos);
|
||||
uintptr_t align_start_pos =
|
||||
reinterpret_cast<uintptr_t>(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<void*>(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<void*>(headerPos);
|
||||
uintptr_t headerValue = reinterpret_cast<uintptr_t>(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<void*>(header_pos);
|
||||
uintptr_t header_value = reinterpret_cast<uintptr_t>(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<uintptr_t>(memBlock);
|
||||
uintptr_t headerPos = alignedPos - sizeof(uintptr_t);
|
||||
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* headerPtr = reinterpret_cast<uintptr_t*>(headerPos);
|
||||
AlignedMemory* deleteMemory = reinterpret_cast<AlignedMemory*>(*headerPtr);
|
||||
uintptr_t* header_ptr = reinterpret_cast<uintptr_t*>(header_pos);
|
||||
AlignedMemory* delete_memory = reinterpret_cast<AlignedMemory*>(*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
|
||||
|
@ -23,7 +23,7 @@
|
||||
// Returns true if |size| and |alignment| are valid combinations.
|
||||
bool CorrectUsage(size_t size, size_t alignment) {
|
||||
webrtc::Allocator<char>::scoped_ptr_aligned scoped(
|
||||
static_cast<char*> (webrtc::AlignedMalloc(size, alignment)));
|
||||
webrtc::AlignedMalloc<char>(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<char>::scoped_ptr_aligned scoped(
|
||||
static_cast<char*> (webrtc::AlignedMalloc(size, alignment)));
|
||||
webrtc::AlignedMalloc<char>(size, alignment));
|
||||
EXPECT_TRUE(scoped.get() != NULL);
|
||||
const uintptr_t aligned_address = reinterpret_cast<uintptr_t> (scoped.get());
|
||||
const uintptr_t missaligned_address = aligned_address - left_missalignment;
|
||||
const void* missaligned_ptr = reinterpret_cast<void*> (missaligned_address);
|
||||
const void* realignedPtr = webrtc::GetRightAlign(
|
||||
missaligned_ptr, alignment);
|
||||
EXPECT_EQ(scoped.get(), realignedPtr);
|
||||
const char* missaligned_ptr = reinterpret_cast<const char*>(
|
||||
missaligned_address);
|
||||
const char* realigned_ptr = webrtc::GetRightAlign(
|
||||
missaligned_ptr, alignment);
|
||||
EXPECT_EQ(scoped.get(), realigned_ptr);
|
||||
}
|
||||
|
||||
TEST(AlignedMalloc, IncorrectSize) {
|
||||
|
Loading…
Reference in New Issue
Block a user