vpx/vpx_mem/vpx_mem.c
Urvang Joshi 28c6207bcd vpx_realloc correction.
vpx_realloc was allocating 1 byte more than needed every time.
Fixed this, and took this opportunity to do a small refactoring.

Change-Id: I38fcb62b698894acbbab43466c1decd12f906789
(cherry picked from aom: 2a876b4 aom_realloc correction.)
2016-08-24 19:22:52 -07:00

102 lines
3.0 KiB
C

/*
* Copyright (c) 2010 The WebM project authors. All Rights Reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "vpx_mem.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "include/vpx_mem_intrnl.h"
#include "vpx/vpx_integer.h"
static INLINE size_t *GetMallocAddressLocation(void *const mem) {
return ((size_t *)mem) - 1;
}
static INLINE size_t GetAlignedMallocSize(size_t size, size_t align) {
return size + align - 1 + ADDRESS_STORAGE_SIZE;
}
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;
}
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;
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);
}
return x;
}
void *vpx_malloc(size_t size) { return vpx_memalign(DEFAULT_ALIGNMENT, size); }
void *vpx_calloc(size_t num, size_t 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 *new_addr = NULL;
/*
The realloc() function changes the size of the object pointed to by
ptr to the size specified by size, and returns a pointer to the
possibly moved block. The contents are unchanged up to the lesser
of the new and old sizes. If ptr is null, realloc() behaves like
malloc() for the specified size. If size is zero (0) and ptr is
not a null pointer, the object pointed to is freed.
*/
if (!memblk)
new_addr = vpx_malloc(size);
else if (!size)
vpx_free(memblk);
else {
void *addr = GetActualMallocAddress(memblk);
const size_t aligned_size = GetAlignedMallocSize(size, DEFAULT_ALIGNMENT);
memblk = NULL;
addr = realloc(addr, aligned_size);
if (addr) {
new_addr = align_addr((unsigned char *)addr + ADDRESS_STORAGE_SIZE,
DEFAULT_ALIGNMENT);
SetActualMallocAddress(new_addr, addr);
}
}
return new_addr;
}
void vpx_free(void *memblk) {
if (memblk) {
void *addr = GetActualMallocAddress(memblk);
free(addr);
}
}
#if CONFIG_VP9_HIGHBITDEPTH
void *vpx_memset16(void *dest, int val, size_t length) {
size_t i;
uint16_t *dest16 = (uint16_t *)dest;
for (i = 0; i < length; i++) *dest16++ = val;
return dest;
}
#endif // CONFIG_VP9_HIGHBITDEPTH