]> granicus.if.org Git - libvpx/commitdiff
aom_realloc correction.
authorUrvang Joshi <urvang@google.com>
Tue, 26 Jul 2016 19:02:37 +0000 (12:02 -0700)
committerYaowu Xu <yaowu@google.com>
Tue, 6 Sep 2016 21:27:20 +0000 (21:27 +0000)
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

aom_mem/aom_mem.c

index 35b286b47d14cc75e654d7756ef6e7094faa4579..38350c69e08071ac7a8ed4f8989eabdece59e703 100644 (file)
 #include "include/aom_mem_intrnl.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) {
   return ((size_t *)mem) - 1;
 }
@@ -35,7 +39,8 @@ static inline void *GetActualMallocAddress(void *const mem) {
 
 void *aom_memalign(size_t align, size_t size) {
   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) {
     x = align_addr((unsigned char *)addr + ADDRESS_STORAGE_SIZE, (int)align);
     SetActualMallocAddress(x, addr);
@@ -69,8 +74,9 @@ void *aom_realloc(void *memblk, size_t size) {
     aom_free(memblk);
   else {
     void *addr = GetActualMallocAddress(memblk);
+    const size_t aligned_size = GetAlignedMallocSize(size, DEFAULT_ALIGNMENT);
     memblk = NULL;
-    addr = realloc(addr, size + DEFAULT_ALIGNMENT + ADDRESS_STORAGE_SIZE);
+    addr = realloc(addr, aligned_size);
     if (addr) {
       new_addr = align_addr((unsigned char *)addr + ADDRESS_STORAGE_SIZE,
                             DEFAULT_ALIGNMENT);