• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  *  Copyright (c) 2010 The WebM project authors. All Rights Reserved.
3  *
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  */
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"
19 #include "vpx/vpx_integer.h"
20 
vpx_memalign(size_t align,size_t size)21 void *vpx_memalign(size_t align, size_t size) {
22   void *addr,
23        * x = NULL;
24 
25   addr = malloc(size + align - 1 + ADDRESS_STORAGE_SIZE);
26 
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;
31   }
32 
33   return x;
34 }
35 
vpx_malloc(size_t size)36 void *vpx_malloc(size_t size) {
37   return vpx_memalign(DEFAULT_ALIGNMENT, size);
38 }
39 
vpx_calloc(size_t num,size_t size)40 void *vpx_calloc(size_t num, size_t size) {
41   void *x;
42 
43   x = vpx_memalign(DEFAULT_ALIGNMENT, num * size);
44 
45   if (x)
46     memset(x, 0, num * size);
47 
48   return x;
49 }
50 
vpx_realloc(void * memblk,size_t size)51 void *vpx_realloc(void *memblk, size_t size) {
52   void *addr,
53        * new_addr = NULL;
54   int align = DEFAULT_ALIGNMENT;
55 
56   /*
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;
71 
72     new_addr = realloc(addr, size + align + ADDRESS_STORAGE_SIZE);
73 
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;
81     }
82   }
83 
84   return new_addr;
85 }
86 
vpx_free(void * memblk)87 void vpx_free(void *memblk) {
88   if (memblk) {
89     void *addr = (void *)(((size_t *)memblk)[-1]);
90     free(addr);
91   }
92 }
93 
94 #if CONFIG_VP9_HIGHBITDEPTH
vpx_memset16(void * dest,int val,size_t length)95 void *vpx_memset16(void *dest, int val, size_t length) {
96   size_t i;
97   uint16_t *dest16 = (uint16_t *)dest;
98   for (i = 0; i < length; i++)
99     *dest16++ = val;
100   return dest;
101 }
102 #endif  // CONFIG_VP9_HIGHBITDEPTH
103