vpx_mem.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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. #define __VPX_MEM_C__
  11. #include "vpx_mem.h"
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <string.h>
  15. #include "include/vpx_mem_intrnl.h"
  16. #include "vpx/vpx_integer.h"
  17. void *vpx_memalign(size_t align, size_t size) {
  18. void *addr,
  19. * x = NULL;
  20. addr = malloc(size + align - 1 + ADDRESS_STORAGE_SIZE);
  21. if (addr) {
  22. x = align_addr((unsigned char *)addr + ADDRESS_STORAGE_SIZE, (int)align);
  23. /* save the actual malloc address */
  24. ((size_t *)x)[-1] = (size_t)addr;
  25. }
  26. return x;
  27. }
  28. void *vpx_malloc(size_t size) {
  29. return vpx_memalign(DEFAULT_ALIGNMENT, size);
  30. }
  31. void *vpx_calloc(size_t num, size_t size) {
  32. void *x;
  33. x = vpx_memalign(DEFAULT_ALIGNMENT, num * size);
  34. if (x)
  35. memset(x, 0, num * size);
  36. return x;
  37. }
  38. void *vpx_realloc(void *memblk, size_t size) {
  39. void *addr,
  40. * new_addr = NULL;
  41. int align = DEFAULT_ALIGNMENT;
  42. /*
  43. The realloc() function changes the size of the object pointed to by
  44. ptr to the size specified by size, and returns a pointer to the
  45. possibly moved block. The contents are unchanged up to the lesser
  46. of the new and old sizes. If ptr is null, realloc() behaves like
  47. malloc() for the specified size. If size is zero (0) and ptr is
  48. not a null pointer, the object pointed to is freed.
  49. */
  50. if (!memblk)
  51. new_addr = vpx_malloc(size);
  52. else if (!size)
  53. vpx_free(memblk);
  54. else {
  55. addr = (void *)(((size_t *)memblk)[-1]);
  56. memblk = NULL;
  57. new_addr = realloc(addr, size + align + ADDRESS_STORAGE_SIZE);
  58. if (new_addr) {
  59. addr = new_addr;
  60. new_addr = (void *)(((size_t)
  61. ((unsigned char *)new_addr + ADDRESS_STORAGE_SIZE) + (align - 1)) &
  62. (size_t) - align);
  63. /* save the actual malloc address */
  64. ((size_t *)new_addr)[-1] = (size_t)addr;
  65. }
  66. }
  67. return new_addr;
  68. }
  69. void vpx_free(void *memblk) {
  70. if (memblk) {
  71. void *addr = (void *)(((size_t *)memblk)[-1]);
  72. free(addr);
  73. }
  74. }
  75. #if CONFIG_VP9 && CONFIG_VP9_HIGHBITDEPTH
  76. void *vpx_memset16(void *dest, int val, size_t length) {
  77. int i;
  78. void *orig = dest;
  79. uint16_t *dest16 = dest;
  80. for (i = 0; i < length; i++)
  81. *dest16++ = val;
  82. return orig;
  83. }
  84. #endif // CONFIG_VP9 && CONFIG_VP9_HIGHBITDEPTH