blob: b261fc0da11787570dd4c0b7bc7a448004feb5be [file] [log] [blame]
John Koleszar0ea50ce2010-05-18 11:58:33 -04001/*
John Koleszarc2140b82010-09-09 08:16:39 -04002 * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
John Koleszar0ea50ce2010-05-18 11:58:33 -04003 *
John Koleszar94c52e42010-06-18 12:39:21 -04004 * Use of this source code is governed by a BSD-style license
John Koleszar09202d82010-06-04 16:19:40 -04005 * 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
John Koleszar94c52e42010-06-18 12:39:21 -04007 * in the file PATENTS. All contributing project authors may
John Koleszar09202d82010-06-04 16:19:40 -04008 * be found in the AUTHORS file in the root of the source tree.
John Koleszar0ea50ce2010-05-18 11:58:33 -04009 */
10
11
John Koleszar0ea50ce2010-05-18 11:58:33 -040012#include "vpx_mem.h"
13#include <stdio.h>
14#include <stdlib.h>
15#include <string.h>
16#include "include/vpx_mem_intrnl.h"
Deb Mukherjee5acfafb2014-08-26 12:35:15 -070017#include "vpx/vpx_integer.h"
John Koleszar0ea50ce2010-05-18 11:58:33 -040018
John Koleszarc6b90392012-07-13 15:21:29 -070019void *vpx_memalign(size_t align, size_t size) {
20 void *addr,
21 * x = NULL;
John Koleszar0ea50ce2010-05-18 11:58:33 -040022
James Zern4feae672015-04-16 15:44:18 -070023 addr = malloc(size + align - 1 + ADDRESS_STORAGE_SIZE);
John Koleszar0ea50ce2010-05-18 11:58:33 -040024
John Koleszarc6b90392012-07-13 15:21:29 -070025 if (addr) {
26 x = align_addr((unsigned char *)addr + ADDRESS_STORAGE_SIZE, (int)align);
27 /* save the actual malloc address */
28 ((size_t *)x)[-1] = (size_t)addr;
29 }
John Koleszar0ea50ce2010-05-18 11:58:33 -040030
John Koleszarc6b90392012-07-13 15:21:29 -070031 return x;
John Koleszar0ea50ce2010-05-18 11:58:33 -040032}
33
John Koleszarc6b90392012-07-13 15:21:29 -070034void *vpx_malloc(size_t size) {
35 return vpx_memalign(DEFAULT_ALIGNMENT, size);
John Koleszar0ea50ce2010-05-18 11:58:33 -040036}
37
John Koleszarc6b90392012-07-13 15:21:29 -070038void *vpx_calloc(size_t num, size_t size) {
39 void *x;
John Koleszar0ea50ce2010-05-18 11:58:33 -040040
John Koleszarc6b90392012-07-13 15:21:29 -070041 x = vpx_memalign(DEFAULT_ALIGNMENT, num * size);
John Koleszar0ea50ce2010-05-18 11:58:33 -040042
John Koleszarc6b90392012-07-13 15:21:29 -070043 if (x)
James Zern4feae672015-04-16 15:44:18 -070044 memset(x, 0, num * size);
John Koleszar0ea50ce2010-05-18 11:58:33 -040045
John Koleszarc6b90392012-07-13 15:21:29 -070046 return x;
John Koleszar0ea50ce2010-05-18 11:58:33 -040047}
48
John Koleszarc6b90392012-07-13 15:21:29 -070049void *vpx_realloc(void *memblk, size_t size) {
50 void *addr,
51 * new_addr = NULL;
52 int align = DEFAULT_ALIGNMENT;
John Koleszar0ea50ce2010-05-18 11:58:33 -040053
John Koleszarc6b90392012-07-13 15:21:29 -070054 /*
55 The realloc() function changes the size of the object pointed to by
56 ptr to the size specified by size, and returns a pointer to the
57 possibly moved block. The contents are unchanged up to the lesser
58 of the new and old sizes. If ptr is null, realloc() behaves like
59 malloc() for the specified size. If size is zero (0) and ptr is
60 not a null pointer, the object pointed to is freed.
61 */
62 if (!memblk)
63 new_addr = vpx_malloc(size);
64 else if (!size)
65 vpx_free(memblk);
66 else {
67 addr = (void *)(((size_t *)memblk)[-1]);
68 memblk = NULL;
John Koleszar0ea50ce2010-05-18 11:58:33 -040069
James Zern4feae672015-04-16 15:44:18 -070070 new_addr = realloc(addr, size + align + ADDRESS_STORAGE_SIZE);
John Koleszar0ea50ce2010-05-18 11:58:33 -040071
John Koleszarc6b90392012-07-13 15:21:29 -070072 if (new_addr) {
73 addr = new_addr;
74 new_addr = (void *)(((size_t)
75 ((unsigned char *)new_addr + ADDRESS_STORAGE_SIZE) + (align - 1)) &
76 (size_t) - align);
77 /* save the actual malloc address */
78 ((size_t *)new_addr)[-1] = (size_t)addr;
John Koleszar0ea50ce2010-05-18 11:58:33 -040079 }
John Koleszarc6b90392012-07-13 15:21:29 -070080 }
John Koleszar0ea50ce2010-05-18 11:58:33 -040081
John Koleszarc6b90392012-07-13 15:21:29 -070082 return new_addr;
John Koleszar0ea50ce2010-05-18 11:58:33 -040083}
84
John Koleszarc6b90392012-07-13 15:21:29 -070085void vpx_free(void *memblk) {
86 if (memblk) {
87 void *addr = (void *)(((size_t *)memblk)[-1]);
James Zern4feae672015-04-16 15:44:18 -070088 free(addr);
John Koleszarc6b90392012-07-13 15:21:29 -070089 }
John Koleszar0ea50ce2010-05-18 11:58:33 -040090}
91
Yaowu Xu72889a22015-08-14 12:16:07 -070092#if CONFIG_VP9_HIGHBITDEPTH
Deb Mukherjee5acfafb2014-08-26 12:35:15 -070093void *vpx_memset16(void *dest, int val, size_t length) {
James Zern35540892015-10-06 22:48:18 -070094 size_t i;
James Zernd0f40632015-10-06 22:51:35 -070095 uint16_t *dest16 = (uint16_t *)dest;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -070096 for (i = 0; i < length; i++)
97 *dest16++ = val;
James Zernd0f40632015-10-06 22:51:35 -070098 return dest;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -070099}
Yaowu Xu72889a22015-08-14 12:16:07 -0700100#endif // CONFIG_VP9_HIGHBITDEPTH