summaryrefslogtreecommitdiff
path: root/vpx_mem
diff options
context:
space:
mode:
authorUrvang Joshi <urvang@google.com>2016-07-25 17:12:14 -0700
committerJames Zern <jzern@google.com>2016-08-24 19:22:41 -0700
commit69c5ba1910f314984fce725ecef4aa3607dba967 (patch)
tree7b7a626b52077ca5d49eb925d41b559f0f8eb875 /vpx_mem
parenta6efe6d43765756db2702bae3cc9f9ea0f26b69e (diff)
downloadlibvpx-69c5ba1910f314984fce725ecef4aa3607dba967.tar
libvpx-69c5ba1910f314984fce725ecef4aa3607dba967.tar.gz
libvpx-69c5ba1910f314984fce725ecef4aa3607dba967.tar.bz2
libvpx-69c5ba1910f314984fce725ecef4aa3607dba967.zip
vpx_mem: Refactor code
Change-Id: I2da9cd5da48ae97e770bccfd1233bcc70b484688 (cherry picked from aom: 83c95f5 aom_mem: Refactor code)
Diffstat (limited to 'vpx_mem')
-rw-r--r--vpx_mem/vpx_mem.c55
1 files changed, 28 insertions, 27 deletions
diff --git a/vpx_mem/vpx_mem.c b/vpx_mem/vpx_mem.c
index cfced2f7d..f17e4a023 100644
--- a/vpx_mem/vpx_mem.c
+++ b/vpx_mem/vpx_mem.c
@@ -15,35 +15,42 @@
#include "include/vpx_mem_intrnl.h"
#include "vpx/vpx_integer.h"
-void *vpx_memalign(size_t align, size_t size) {
- void *addr, *x = NULL;
+static INLINE size_t *GetMallocAddressLocation(void *const mem) {
+ return ((size_t *)mem) - 1;
+}
+
+static INLINE void SetActualMallocAddress(void *const mem,
+ const void *const malloc_addr) {
+ size_t *const malloc_addr_location = GetMallocAddressLocation(mem);
+ *malloc_addr_location = (size_t)malloc_addr;
+}
- addr = malloc(size + align - 1 + ADDRESS_STORAGE_SIZE);
+static INLINE void *GetActualMallocAddress(void *const mem) {
+ size_t *const malloc_addr_location = GetMallocAddressLocation(mem);
+ return (void *)(*malloc_addr_location);
+}
+void *vpx_memalign(size_t align, size_t size) {
+ void *x = NULL;
+ void *const addr = malloc(size + align - 1 + ADDRESS_STORAGE_SIZE);
if (addr) {
x = align_addr((unsigned char *)addr + ADDRESS_STORAGE_SIZE, (int)align);
- /* save the actual malloc address */
- ((size_t *)x)[-1] = (size_t)addr;
+ SetActualMallocAddress(x, addr);
}
-
return x;
}
void *vpx_malloc(size_t size) { return vpx_memalign(DEFAULT_ALIGNMENT, size); }
void *vpx_calloc(size_t num, size_t size) {
- void *x;
-
- x = vpx_memalign(DEFAULT_ALIGNMENT, num * size);
-
- if (x) memset(x, 0, num * size);
-
+ const size_t total_size = num * size;
+ void *const x = vpx_malloc(total_size);
+ if (x) memset(x, 0, total_size);
return x;
}
void *vpx_realloc(void *memblk, size_t size) {
- void *addr, *new_addr = NULL;
- int align = DEFAULT_ALIGNMENT;
+ void *new_addr = NULL;
/*
The realloc() function changes the size of the object pointed to by
@@ -58,19 +65,13 @@ void *vpx_realloc(void *memblk, size_t size) {
else if (!size)
vpx_free(memblk);
else {
- addr = (void *)(((size_t *)memblk)[-1]);
+ void *addr = GetActualMallocAddress(memblk);
memblk = NULL;
-
- new_addr = realloc(addr, size + align + ADDRESS_STORAGE_SIZE);
-
- if (new_addr) {
- addr = new_addr;
- new_addr =
- (void *)(((size_t)((unsigned char *)new_addr + ADDRESS_STORAGE_SIZE) +
- (align - 1)) &
- (size_t)-align);
- /* save the actual malloc address */
- ((size_t *)new_addr)[-1] = (size_t)addr;
+ addr = realloc(addr, size + DEFAULT_ALIGNMENT + ADDRESS_STORAGE_SIZE);
+ if (addr) {
+ new_addr = align_addr((unsigned char *)addr + ADDRESS_STORAGE_SIZE,
+ DEFAULT_ALIGNMENT);
+ SetActualMallocAddress(new_addr, addr);
}
}
@@ -79,7 +80,7 @@ void *vpx_realloc(void *memblk, size_t size) {
void vpx_free(void *memblk) {
if (memblk) {
- void *addr = (void *)(((size_t *)memblk)[-1]);
+ void *addr = GetActualMallocAddress(memblk);
free(addr);
}
}