Updates according to style guide.

BUG=N/A

Review URL: https://webrtc-codereview.appspot.com/864004

git-svn-id: http://webrtc.googlecode.com/svn/trunk@2861 4adac7df-926f-26a2-2b94-8c16560cd09d
This commit is contained in:
henrike@webrtc.org 2012-10-02 15:38:35 +00:00
parent 4743f938d6
commit 28625c1af3

View File

@ -10,135 +10,109 @@
#include "aligned_malloc.h" #include "aligned_malloc.h"
#include <assert.h>
#include <memory.h> #include <memory.h>
#ifdef WEBRTC_ANDROID
#include <stdlib.h> #include <stdlib.h>
#endif
#if WEBRTC_MAC
#include <malloc/malloc.h>
#else
#include <malloc.h>
#endif
#if _WIN32 #if _WIN32
#include <windows.h> #include <windows.h>
#else #else
#include <stdint.h> #include <stdint.h>
#endif #endif
#include "typedefs.h" #include "typedefs.h"
// Ok reference on memory alignment: // Reference on memory alignment:
// http://stackoverflow.com/questions/227897/solve-the-memory-alignment-in-c-interview-question-that-stumped-me // http://stackoverflow.com/questions/227897/solve-the-memory-alignment-in-c-interview-question-that-stumped-me
namespace webrtc {
namespace webrtc // TODO(henrike): better to create just one memory block and interpret the
{ // first sizeof(AlignedMemory) bytes as an AlignedMemory struct.
// TODO (hellner) better to create just one memory block and struct AlignedMemory {
// interpret the first sizeof(AlignedMemory) bytes as
// an AlignedMemory struct.
struct AlignedMemory
{
void* alignedBuffer; void* alignedBuffer;
void* memoryPointer; void* memoryPointer;
}; };
uintptr_t GetRightAlign(uintptr_t startPos, size_t alignment) uintptr_t GetRightAlign(uintptr_t startPos, 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 (startPos + alignment - 1) & ~(alignment - 1);
} }
// Alignment must be an integer power of two. // Alignment must be an integer power of two.
bool ValidAlignment(size_t alignment) bool ValidAlignment(size_t alignment) {
{ if (!alignment) {
if (!alignment) return false;
{ }
return false; return (alignment & (alignment - 1)) == 0;
}
return (alignment & (alignment - 1)) == 0;
} }
void* GetRightAlign(const void* ptr, size_t alignment) void* GetRightAlign(const void* ptr, size_t alignment) {
{ if (!ptr) {
if (!ptr) return NULL;
{ }
return NULL; if (!ValidAlignment(alignment)) {
} return NULL;
if (!ValidAlignment(alignment)) }
{ uintptr_t startPos = reinterpret_cast<uintptr_t>(ptr);
return NULL; return reinterpret_cast<void*>(GetRightAlign(startPos, alignment));
}
uintptr_t startPos = reinterpret_cast<uintptr_t> (ptr);
return reinterpret_cast<void*> (GetRightAlign(startPos, alignment));
} }
void* AlignedMalloc(size_t size, size_t alignment) void* AlignedMalloc(size_t size, size_t alignment) {
{ if (size == 0) {
if (size == 0) { return NULL;
return NULL; }
} if (!ValidAlignment(alignment)) {
if (!ValidAlignment(alignment)) return NULL;
{ }
return NULL;
}
AlignedMemory* returnValue = new AlignedMemory(); AlignedMemory* returnValue = new AlignedMemory();
if(returnValue == NULL) if (returnValue == NULL) {
{ return NULL;
return NULL; }
}
// The memory is aligned towards the lowest address that so only // The memory is aligned towards the lowest address that so only
// 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) + returnValue->memoryPointer = malloc(size + sizeof(uintptr_t) +
alignment - 1); alignment - 1);
if(returnValue->memoryPointer == NULL) if (returnValue->memoryPointer == NULL) {
{ delete returnValue;
delete returnValue; return NULL;
return NULL; }
}
// Alligning 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)returnValue->memoryPointer; uintptr_t alignStartPos =
alignStartPos += sizeof(uintptr_t); reinterpret_cast<uintptr_t>(returnValue->memoryPointer);
uintptr_t alignedPos = GetRightAlign(alignStartPos, alignment); alignStartPos += sizeof(uintptr_t);
returnValue->alignedBuffer = reinterpret_cast<void*> (alignedPos); uintptr_t alignedPos = GetRightAlign(alignStartPos, alignment);
returnValue->alignedBuffer = reinterpret_cast<void*>(alignedPos);
// 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 headerPos = alignedPos;
headerPos -= sizeof(uintptr_t); headerPos -= sizeof(uintptr_t);
void* headerPtr = reinterpret_cast<void*> (headerPos); void* headerPtr = reinterpret_cast<void*>(headerPos);
uintptr_t headerValue = (uintptr_t)returnValue; uintptr_t headerValue = reinterpret_cast<uintptr_t>(returnValue);
memcpy(headerPtr,&headerValue,sizeof(uintptr_t)); memcpy(headerPtr,&headerValue,sizeof(uintptr_t));
return returnValue->alignedBuffer;
return returnValue->alignedBuffer;
} }
void AlignedFree(void* memBlock) void AlignedFree(void* memBlock) {
{ if (memBlock == NULL) {
if(memBlock == NULL) return;
{ }
return; uintptr_t alignedPos = reinterpret_cast<uintptr_t>(memBlock);
} uintptr_t headerPos = alignedPos - sizeof(uintptr_t);
uintptr_t alignedPos = (uintptr_t)memBlock;
uintptr_t headerPos = alignedPos - 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 = (uintptr_t*)headerPos; uintptr_t* headerPtr = reinterpret_cast<uintptr_t*>(headerPos);
AlignedMemory* deleteMemory = (AlignedMemory*) *headerPtr; AlignedMemory* deleteMemory = reinterpret_cast<AlignedMemory*>(*headerPtr);
if(deleteMemory->memoryPointer != NULL) if (deleteMemory->memoryPointer != NULL) {
{ free(deleteMemory->memoryPointer);
free(deleteMemory->memoryPointer); }
} delete deleteMemory;
delete deleteMemory;
} }
} // namespace webrtc } // namespace webrtc