2
0

vpx_mem.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. #include "vpx_mem.h"
  11. #include <limits.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. #if SIZE_MAX > (1ULL << 40)
  18. #define VPX_MAX_ALLOCABLE_MEMORY (1ULL << 40)
  19. #else
  20. // For 32-bit targets keep this below INT_MAX to avoid valgrind warnings.
  21. #define VPX_MAX_ALLOCABLE_MEMORY ((1ULL << 31) - (1 << 16))
  22. #endif
  23. // Returns 0 in case of overflow of nmemb * size.
  24. static int check_size_argument_overflow(uint64_t nmemb, uint64_t size) {
  25. const uint64_t total_size = nmemb * size;
  26. if (nmemb == 0) return 1;
  27. if (size > VPX_MAX_ALLOCABLE_MEMORY / nmemb) return 0;
  28. if (total_size != (size_t)total_size) return 0;
  29. return 1;
  30. }
  31. static size_t *get_malloc_address_location(void *const mem) {
  32. return ((size_t *)mem) - 1;
  33. }
  34. static uint64_t get_aligned_malloc_size(size_t size, size_t align) {
  35. return (uint64_t)size + align - 1 + ADDRESS_STORAGE_SIZE;
  36. }
  37. static void set_actual_malloc_address(void *const mem,
  38. const void *const malloc_addr) {
  39. size_t *const malloc_addr_location = get_malloc_address_location(mem);
  40. *malloc_addr_location = (size_t)malloc_addr;
  41. }
  42. static void *get_actual_malloc_address(void *const mem) {
  43. size_t *const malloc_addr_location = get_malloc_address_location(mem);
  44. return (void *)(*malloc_addr_location);
  45. }
  46. void *vpx_memalign(size_t align, size_t size) {
  47. void *x = NULL, *addr;
  48. const uint64_t aligned_size = get_aligned_malloc_size(size, align);
  49. if (!check_size_argument_overflow(1, aligned_size)) return NULL;
  50. addr = malloc((size_t)aligned_size);
  51. if (addr) {
  52. x = align_addr((unsigned char *)addr + ADDRESS_STORAGE_SIZE, align);
  53. set_actual_malloc_address(x, addr);
  54. }
  55. return x;
  56. }
  57. void *vpx_malloc(size_t size) { return vpx_memalign(DEFAULT_ALIGNMENT, size); }
  58. void *vpx_calloc(size_t num, size_t size) {
  59. void *x;
  60. if (!check_size_argument_overflow(num, size)) return NULL;
  61. x = vpx_malloc(num * size);
  62. if (x) memset(x, 0, num * size);
  63. return x;
  64. }
  65. void *vpx_realloc(void *memblk, size_t size) {
  66. void *new_addr = NULL;
  67. /*
  68. The realloc() function changes the size of the object pointed to by
  69. ptr to the size specified by size, and returns a pointer to the
  70. possibly moved block. The contents are unchanged up to the lesser
  71. of the new and old sizes. If ptr is null, realloc() behaves like
  72. malloc() for the specified size. If size is zero (0) and ptr is
  73. not a null pointer, the object pointed to is freed.
  74. */
  75. if (!memblk)
  76. new_addr = vpx_malloc(size);
  77. else if (!size)
  78. vpx_free(memblk);
  79. else {
  80. void *addr = get_actual_malloc_address(memblk);
  81. const uint64_t aligned_size =
  82. get_aligned_malloc_size(size, DEFAULT_ALIGNMENT);
  83. if (!check_size_argument_overflow(1, aligned_size)) return NULL;
  84. addr = realloc(addr, (size_t)aligned_size);
  85. if (addr) {
  86. new_addr = align_addr((unsigned char *)addr + ADDRESS_STORAGE_SIZE,
  87. DEFAULT_ALIGNMENT);
  88. set_actual_malloc_address(new_addr, addr);
  89. }
  90. }
  91. return new_addr;
  92. }
  93. void vpx_free(void *memblk) {
  94. if (memblk) {
  95. void *addr = get_actual_malloc_address(memblk);
  96. free(addr);
  97. }
  98. }
  99. #if CONFIG_VP9_HIGHBITDEPTH
  100. void *vpx_memset16(void *dest, int val, size_t length) {
  101. size_t i;
  102. uint16_t *dest16 = (uint16_t *)dest;
  103. for (i = 0; i < length; i++) *dest16++ = val;
  104. return dest;
  105. }
  106. #endif // CONFIG_VP9_HIGHBITDEPTH