vpx_mem.c 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 !defined(VPX_MAX_ALLOCABLE_MEMORY)
  18. #if SIZE_MAX > (1ULL << 40)
  19. #define VPX_MAX_ALLOCABLE_MEMORY (1ULL << 40)
  20. #else
  21. // For 32-bit targets keep this below INT_MAX to avoid valgrind warnings.
  22. #define VPX_MAX_ALLOCABLE_MEMORY ((1ULL << 31) - (1 << 16))
  23. #endif
  24. #endif
  25. // Returns 0 in case of overflow of nmemb * size.
  26. static int check_size_argument_overflow(uint64_t nmemb, uint64_t size) {
  27. const uint64_t total_size = nmemb * size;
  28. if (nmemb == 0) return 1;
  29. if (size > VPX_MAX_ALLOCABLE_MEMORY / nmemb) return 0;
  30. if (total_size != (size_t)total_size) return 0;
  31. return 1;
  32. }
  33. static size_t *get_malloc_address_location(void *const mem) {
  34. return ((size_t *)mem) - 1;
  35. }
  36. static uint64_t get_aligned_malloc_size(size_t size, size_t align) {
  37. return (uint64_t)size + align - 1 + ADDRESS_STORAGE_SIZE;
  38. }
  39. static void set_actual_malloc_address(void *const mem,
  40. const void *const malloc_addr) {
  41. size_t *const malloc_addr_location = get_malloc_address_location(mem);
  42. *malloc_addr_location = (size_t)malloc_addr;
  43. }
  44. static void *get_actual_malloc_address(void *const mem) {
  45. size_t *const malloc_addr_location = get_malloc_address_location(mem);
  46. return (void *)(*malloc_addr_location);
  47. }
  48. void *vpx_memalign(size_t align, size_t size) {
  49. void *x = NULL, *addr;
  50. const uint64_t aligned_size = get_aligned_malloc_size(size, align);
  51. if (!check_size_argument_overflow(1, aligned_size)) return NULL;
  52. addr = malloc((size_t)aligned_size);
  53. if (addr) {
  54. x = align_addr((unsigned char *)addr + ADDRESS_STORAGE_SIZE, align);
  55. set_actual_malloc_address(x, addr);
  56. }
  57. return x;
  58. }
  59. void *vpx_malloc(size_t size) { return vpx_memalign(DEFAULT_ALIGNMENT, size); }
  60. void *vpx_calloc(size_t num, size_t size) {
  61. void *x;
  62. if (!check_size_argument_overflow(num, size)) return NULL;
  63. x = vpx_malloc(num * size);
  64. if (x) memset(x, 0, num * size);
  65. return x;
  66. }
  67. void vpx_free(void *memblk) {
  68. if (memblk) {
  69. void *addr = get_actual_malloc_address(memblk);
  70. free(addr);
  71. }
  72. }