summaryrefslogtreecommitdiffstats
path: root/media/libvpx/libvpx/vpx_mem
diff options
context:
space:
mode:
Diffstat (limited to 'media/libvpx/libvpx/vpx_mem')
-rw-r--r--media/libvpx/libvpx/vpx_mem/include/vpx_mem_intrnl.h31
-rw-r--r--media/libvpx/libvpx/vpx_mem/vpx_mem.c86
-rw-r--r--media/libvpx/libvpx/vpx_mem/vpx_mem.h52
-rw-r--r--media/libvpx/libvpx/vpx_mem/vpx_mem.mk4
4 files changed, 173 insertions, 0 deletions
diff --git a/media/libvpx/libvpx/vpx_mem/include/vpx_mem_intrnl.h b/media/libvpx/libvpx/vpx_mem/include/vpx_mem_intrnl.h
new file mode 100644
index 0000000000..5631130243
--- /dev/null
+++ b/media/libvpx/libvpx/vpx_mem/include/vpx_mem_intrnl.h
@@ -0,0 +1,31 @@
+/*
+ * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
+ *
+ * Use of this source code is governed by a BSD-style license
+ * that can be found in the LICENSE file in the root of the source
+ * tree. An additional intellectual property rights grant can be found
+ * in the file PATENTS. All contributing project authors may
+ * be found in the AUTHORS file in the root of the source tree.
+ */
+
+#ifndef VPX_VPX_MEM_INCLUDE_VPX_MEM_INTRNL_H_
+#define VPX_VPX_MEM_INCLUDE_VPX_MEM_INTRNL_H_
+#include "./vpx_config.h"
+
+#define ADDRESS_STORAGE_SIZE sizeof(size_t)
+
+#ifndef DEFAULT_ALIGNMENT
+#if defined(VXWORKS)
+/*default addr alignment to use in calls to vpx_* functions other than
+ * vpx_memalign*/
+#define DEFAULT_ALIGNMENT 32
+#else
+#define DEFAULT_ALIGNMENT (2 * sizeof(void *)) /* NOLINT */
+#endif
+#endif
+
+/*returns an addr aligned to the byte boundary specified by align*/
+#define align_addr(addr, align) \
+ (void *)(((size_t)(addr) + ((align)-1)) & ~(size_t)((align)-1))
+
+#endif // VPX_VPX_MEM_INCLUDE_VPX_MEM_INTRNL_H_
diff --git a/media/libvpx/libvpx/vpx_mem/vpx_mem.c b/media/libvpx/libvpx/vpx_mem/vpx_mem.c
new file mode 100644
index 0000000000..18abf1158b
--- /dev/null
+++ b/media/libvpx/libvpx/vpx_mem/vpx_mem.c
@@ -0,0 +1,86 @@
+/*
+ * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
+ *
+ * Use of this source code is governed by a BSD-style license
+ * that can be found in the LICENSE file in the root of the source
+ * tree. An additional intellectual property rights grant can be found
+ * in the file PATENTS. All contributing project authors may
+ * be found in the AUTHORS file in the root of the source tree.
+ */
+
+#include "vpx_mem.h"
+#include <limits.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include "include/vpx_mem_intrnl.h"
+#include "vpx/vpx_integer.h"
+
+#if !defined(VPX_MAX_ALLOCABLE_MEMORY)
+#if SIZE_MAX > (1ULL << 40)
+#define VPX_MAX_ALLOCABLE_MEMORY (1ULL << 40)
+#else
+// For 32-bit targets keep this below INT_MAX to avoid valgrind warnings.
+#define VPX_MAX_ALLOCABLE_MEMORY ((1ULL << 31) - (1 << 16))
+#endif
+#endif
+
+// Returns 0 in case of overflow of nmemb * size.
+static int check_size_argument_overflow(uint64_t nmemb, uint64_t size) {
+ const uint64_t total_size = nmemb * size;
+ if (nmemb == 0) return 1;
+ if (size > VPX_MAX_ALLOCABLE_MEMORY / nmemb) return 0;
+ if (total_size != (size_t)total_size) return 0;
+
+ return 1;
+}
+
+static size_t *get_malloc_address_location(void *const mem) {
+ return ((size_t *)mem) - 1;
+}
+
+static uint64_t get_aligned_malloc_size(size_t size, size_t align) {
+ return (uint64_t)size + align - 1 + ADDRESS_STORAGE_SIZE;
+}
+
+static void set_actual_malloc_address(void *const mem,
+ const void *const malloc_addr) {
+ size_t *const malloc_addr_location = get_malloc_address_location(mem);
+ *malloc_addr_location = (size_t)malloc_addr;
+}
+
+static void *get_actual_malloc_address(void *const mem) {
+ size_t *const malloc_addr_location = get_malloc_address_location(mem);
+ return (void *)(*malloc_addr_location);
+}
+
+void *vpx_memalign(size_t align, size_t size) {
+ void *x = NULL, *addr;
+ const uint64_t aligned_size = get_aligned_malloc_size(size, align);
+ if (!check_size_argument_overflow(1, aligned_size)) return NULL;
+
+ addr = malloc((size_t)aligned_size);
+ if (addr) {
+ x = align_addr((unsigned char *)addr + ADDRESS_STORAGE_SIZE, align);
+ set_actual_malloc_address(x, addr);
+ }
+ return x;
+}
+
+void *vpx_malloc(size_t size) { return vpx_memalign(DEFAULT_ALIGNMENT, size); }
+
+void *vpx_calloc(size_t num, size_t size) {
+ void *x;
+ if (!check_size_argument_overflow(num, size)) return NULL;
+
+ x = vpx_malloc(num * size);
+ if (x) memset(x, 0, num * size);
+ return x;
+}
+
+void vpx_free(void *memblk) {
+ if (memblk) {
+ void *addr = get_actual_malloc_address(memblk);
+ free(addr);
+ }
+}
diff --git a/media/libvpx/libvpx/vpx_mem/vpx_mem.h b/media/libvpx/libvpx/vpx_mem/vpx_mem.h
new file mode 100644
index 0000000000..7689a05e6e
--- /dev/null
+++ b/media/libvpx/libvpx/vpx_mem/vpx_mem.h
@@ -0,0 +1,52 @@
+/*
+ * Copyright (c) 2010 The WebM project authors. All Rights Reserved.
+ *
+ * Use of this source code is governed by a BSD-style license
+ * that can be found in the LICENSE file in the root of the source
+ * tree. An additional intellectual property rights grant can be found
+ * in the file PATENTS. All contributing project authors may
+ * be found in the AUTHORS file in the root of the source tree.
+ */
+
+#ifndef VPX_VPX_MEM_VPX_MEM_H_
+#define VPX_VPX_MEM_VPX_MEM_H_
+
+#include "vpx_config.h"
+#if defined(__uClinux__)
+#include <lddk.h>
+#endif
+
+#include <stdlib.h>
+#include <stddef.h>
+
+#include "vpx/vpx_integer.h"
+
+#if defined(__cplusplus)
+extern "C" {
+#endif
+
+void *vpx_memalign(size_t align, size_t size);
+void *vpx_malloc(size_t size);
+void *vpx_calloc(size_t num, size_t size);
+void vpx_free(void *memblk);
+
+#if CONFIG_VP9_HIGHBITDEPTH
+static INLINE void *vpx_memset16(void *dest, int val, size_t length) {
+ size_t i;
+ uint16_t *dest16 = (uint16_t *)dest;
+ for (i = 0; i < length; i++) *dest16++ = val;
+ return dest;
+}
+#endif
+
+#include <string.h>
+
+#ifdef VPX_MEM_PLTFRM
+#include VPX_MEM_PLTFRM
+#endif
+
+#if defined(__cplusplus)
+}
+#endif
+
+#endif // VPX_VPX_MEM_VPX_MEM_H_
diff --git a/media/libvpx/libvpx/vpx_mem/vpx_mem.mk b/media/libvpx/libvpx/vpx_mem/vpx_mem.mk
new file mode 100644
index 0000000000..7f275eabf9
--- /dev/null
+++ b/media/libvpx/libvpx/vpx_mem/vpx_mem.mk
@@ -0,0 +1,4 @@
+MEM_SRCS-yes += vpx_mem.mk
+MEM_SRCS-yes += vpx_mem.c
+MEM_SRCS-yes += vpx_mem.h
+MEM_SRCS-yes += include/vpx_mem_intrnl.h