Remove vp10's build dependency on vp9
[aom.git] / vpx_mem / vpx_mem.c
blobc6f501a6fcf95b5dd901c15e2711db93afad9262
1 /*
2 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
12 #define __VPX_MEM_C__
14 #include "vpx_mem.h"
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 #include "include/vpx_mem_intrnl.h"
19 #include "vpx/vpx_integer.h"
21 void *vpx_memalign(size_t align, size_t size) {
22 void *addr,
23 * x = NULL;
25 addr = malloc(size + align - 1 + ADDRESS_STORAGE_SIZE);
27 if (addr) {
28 x = align_addr((unsigned char *)addr + ADDRESS_STORAGE_SIZE, (int)align);
29 /* save the actual malloc address */
30 ((size_t *)x)[-1] = (size_t)addr;
33 return x;
36 void *vpx_malloc(size_t size) {
37 return vpx_memalign(DEFAULT_ALIGNMENT, size);
40 void *vpx_calloc(size_t num, size_t size) {
41 void *x;
43 x = vpx_memalign(DEFAULT_ALIGNMENT, num * size);
45 if (x)
46 memset(x, 0, num * size);
48 return x;
51 void *vpx_realloc(void *memblk, size_t size) {
52 void *addr,
53 * new_addr = NULL;
54 int align = DEFAULT_ALIGNMENT;
57 The realloc() function changes the size of the object pointed to by
58 ptr to the size specified by size, and returns a pointer to the
59 possibly moved block. The contents are unchanged up to the lesser
60 of the new and old sizes. If ptr is null, realloc() behaves like
61 malloc() for the specified size. If size is zero (0) and ptr is
62 not a null pointer, the object pointed to is freed.
64 if (!memblk)
65 new_addr = vpx_malloc(size);
66 else if (!size)
67 vpx_free(memblk);
68 else {
69 addr = (void *)(((size_t *)memblk)[-1]);
70 memblk = NULL;
72 new_addr = realloc(addr, size + align + ADDRESS_STORAGE_SIZE);
74 if (new_addr) {
75 addr = new_addr;
76 new_addr = (void *)(((size_t)
77 ((unsigned char *)new_addr + ADDRESS_STORAGE_SIZE) + (align - 1)) &
78 (size_t) - align);
79 /* save the actual malloc address */
80 ((size_t *)new_addr)[-1] = (size_t)addr;
84 return new_addr;
87 void vpx_free(void *memblk) {
88 if (memblk) {
89 void *addr = (void *)(((size_t *)memblk)[-1]);
90 free(addr);
94 #if CONFIG_VP9_HIGHBITDEPTH
95 void *vpx_memset16(void *dest, int val, size_t length) {
96 int i;
97 void *orig = dest;
98 uint16_t *dest16 = dest;
99 for (i = 0; i < length; i++)
100 *dest16++ = val;
101 return orig;
103 #endif // CONFIG_VP9_HIGHBITDEPTH