Commit 497f27ed authored by Urvang Joshi's avatar Urvang Joshi Committed by Yaowu Xu

aom_realloc correction.

aom_realloc was allocating 1 byte more than needed every time.
Fixed this, and took this opportunity to do a small refactoring.

Change-Id: I38fcb62b698894acbbab43466c1decd12f906789
parent 73a3fd47
...@@ -18,6 +18,10 @@ ...@@ -18,6 +18,10 @@
#include "include/aom_mem_intrnl.h" #include "include/aom_mem_intrnl.h"
#include "aom/aom_integer.h" #include "aom/aom_integer.h"
static inline size_t GetAlignedMallocSize(size_t size, size_t align) {
return size + align - 1 + ADDRESS_STORAGE_SIZE;
}
static inline size_t *GetMallocAddressLocation(void *const mem) { static inline size_t *GetMallocAddressLocation(void *const mem) {
return ((size_t *)mem) - 1; return ((size_t *)mem) - 1;
} }
...@@ -35,7 +39,8 @@ static inline void *GetActualMallocAddress(void *const mem) { ...@@ -35,7 +39,8 @@ static inline void *GetActualMallocAddress(void *const mem) {
void *aom_memalign(size_t align, size_t size) { void *aom_memalign(size_t align, size_t size) {
void *x = NULL; void *x = NULL;
void *const addr = malloc(size + align - 1 + ADDRESS_STORAGE_SIZE); const size_t aligned_size = GetAlignedMallocSize(size, align);
void *const addr = malloc(aligned_size);
if (addr) { if (addr) {
x = align_addr((unsigned char *)addr + ADDRESS_STORAGE_SIZE, (int)align); x = align_addr((unsigned char *)addr + ADDRESS_STORAGE_SIZE, (int)align);
SetActualMallocAddress(x, addr); SetActualMallocAddress(x, addr);
...@@ -69,8 +74,9 @@ void *aom_realloc(void *memblk, size_t size) { ...@@ -69,8 +74,9 @@ void *aom_realloc(void *memblk, size_t size) {
aom_free(memblk); aom_free(memblk);
else { else {
void *addr = GetActualMallocAddress(memblk); void *addr = GetActualMallocAddress(memblk);
const size_t aligned_size = GetAlignedMallocSize(size, DEFAULT_ALIGNMENT);
memblk = NULL; memblk = NULL;
addr = realloc(addr, size + DEFAULT_ALIGNMENT + ADDRESS_STORAGE_SIZE); addr = realloc(addr, aligned_size);
if (addr) { if (addr) {
new_addr = align_addr((unsigned char *)addr + ADDRESS_STORAGE_SIZE, new_addr = align_addr((unsigned char *)addr + ADDRESS_STORAGE_SIZE,
DEFAULT_ALIGNMENT); DEFAULT_ALIGNMENT);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment