blob: b98fe83c05a27bf70cce148742e8cda1cff09d4e [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
12#define __VPX_MEM_C__
13
14#include "vpx_mem.h"
15#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
18#include "include/vpx_mem_intrnl.h"
Deb Mukherjee5acfafb2014-08-26 12:35:15 -070019#include "vpx/vpx_integer.h"
John Koleszar0ea50ce2010-05-18 11:58:33 -040020
John Koleszarc6b90392012-07-13 15:21:29 -070021void *vpx_memalign(size_t align, size_t size) {
22 void *addr,
23 * x = NULL;
John Koleszar0ea50ce2010-05-18 11:58:33 -040024
James Zern4feae672015-04-16 15:44:18 -070025 addr = malloc(size + align - 1 + ADDRESS_STORAGE_SIZE);
John Koleszar0ea50ce2010-05-18 11:58:33 -040026
John Koleszarc6b90392012-07-13 15:21:29 -070027 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;
31 }
John Koleszar0ea50ce2010-05-18 11:58:33 -040032
John Koleszarc6b90392012-07-13 15:21:29 -070033 return x;
John Koleszar0ea50ce2010-05-18 11:58:33 -040034}
35
John Koleszarc6b90392012-07-13 15:21:29 -070036void *vpx_malloc(size_t size) {
37 return vpx_memalign(DEFAULT_ALIGNMENT, size);
John Koleszar0ea50ce2010-05-18 11:58:33 -040038}
39
John Koleszarc6b90392012-07-13 15:21:29 -070040void *vpx_calloc(size_t num, size_t size) {
41 void *x;
John Koleszar0ea50ce2010-05-18 11:58:33 -040042
John Koleszarc6b90392012-07-13 15:21:29 -070043 x = vpx_memalign(DEFAULT_ALIGNMENT, num * size);
John Koleszar0ea50ce2010-05-18 11:58:33 -040044
John Koleszarc6b90392012-07-13 15:21:29 -070045 if (x)
James Zern4feae672015-04-16 15:44:18 -070046 memset(x, 0, num * size);
John Koleszar0ea50ce2010-05-18 11:58:33 -040047
John Koleszarc6b90392012-07-13 15:21:29 -070048 return x;
John Koleszar0ea50ce2010-05-18 11:58:33 -040049}
50
John Koleszarc6b90392012-07-13 15:21:29 -070051void *vpx_realloc(void *memblk, size_t size) {
52 void *addr,
53 * new_addr = NULL;
54 int align = DEFAULT_ALIGNMENT;
John Koleszar0ea50ce2010-05-18 11:58:33 -040055
John Koleszarc6b90392012-07-13 15:21:29 -070056 /*
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.
63 */
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;
John Koleszar0ea50ce2010-05-18 11:58:33 -040071
James Zern4feae672015-04-16 15:44:18 -070072 new_addr = realloc(addr, size + align + ADDRESS_STORAGE_SIZE);
John Koleszar0ea50ce2010-05-18 11:58:33 -040073
John Koleszarc6b90392012-07-13 15:21:29 -070074 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;
John Koleszar0ea50ce2010-05-18 11:58:33 -040081 }
John Koleszarc6b90392012-07-13 15:21:29 -070082 }
John Koleszar0ea50ce2010-05-18 11:58:33 -040083
John Koleszarc6b90392012-07-13 15:21:29 -070084 return new_addr;
John Koleszar0ea50ce2010-05-18 11:58:33 -040085}
86
John Koleszarc6b90392012-07-13 15:21:29 -070087void vpx_free(void *memblk) {
88 if (memblk) {
89 void *addr = (void *)(((size_t *)memblk)[-1]);
James Zern4feae672015-04-16 15:44:18 -070090 free(addr);
John Koleszarc6b90392012-07-13 15:21:29 -070091 }
John Koleszar0ea50ce2010-05-18 11:58:33 -040092}
93
Yaowu Xu72889a22015-08-14 12:16:07 -070094#if CONFIG_VP9_HIGHBITDEPTH
Deb Mukherjee5acfafb2014-08-26 12:35:15 -070095void *vpx_memset16(void *dest, int val, size_t length) {
James Zern35540892015-10-06 22:48:18 -070096 size_t i;
James Zernd0f40632015-10-06 22:51:35 -070097 uint16_t *dest16 = (uint16_t *)dest;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -070098 for (i = 0; i < length; i++)
99 *dest16++ = val;
James Zernd0f40632015-10-06 22:51:35 -0700100 return dest;
Deb Mukherjee5acfafb2014-08-26 12:35:15 -0700101}
Yaowu Xu72889a22015-08-14 12:16:07 -0700102#endif // CONFIG_VP9_HIGHBITDEPTH