2010-05-18 17:58:33 +02:00
|
|
|
/*
|
2010-09-09 14:16:39 +02:00
|
|
|
* Copyright (c) 2010 The WebM project authors. All Rights Reserved.
|
2010-05-18 17:58:33 +02:00
|
|
|
*
|
2010-06-18 18:39:21 +02:00
|
|
|
* Use of this source code is governed by a BSD-style license
|
2010-06-04 22:19:40 +02:00
|
|
|
* that can be found in the LICENSE file in the root of the source
|
|
|
|
* tree. An additional intellectual property rights grant can be found
|
2010-06-18 18:39:21 +02:00
|
|
|
* in the file PATENTS. All contributing project authors may
|
2010-06-04 22:19:40 +02:00
|
|
|
* be found in the AUTHORS file in the root of the source tree.
|
2010-05-18 17:58:33 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "vpx_mem.h"
|
2016-08-24 06:06:36 +02:00
|
|
|
#include <limits.h>
|
2010-05-18 17:58:33 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "include/vpx_mem_intrnl.h"
|
2014-08-26 21:35:15 +02:00
|
|
|
#include "vpx/vpx_integer.h"
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2016-08-24 06:06:36 +02:00
|
|
|
#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
|
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2016-09-07 19:51:33 +02:00
|
|
|
static size_t *get_malloc_address_location(void *const mem) {
|
2016-07-26 02:12:14 +02:00
|
|
|
return ((size_t *)mem) - 1;
|
|
|
|
}
|
|
|
|
|
2016-09-07 19:51:33 +02:00
|
|
|
static uint64_t get_aligned_malloc_size(size_t size, size_t align) {
|
2016-08-24 06:06:36 +02:00
|
|
|
return (uint64_t)size + align - 1 + ADDRESS_STORAGE_SIZE;
|
2016-07-26 21:02:37 +02:00
|
|
|
}
|
|
|
|
|
2016-09-07 19:51:33 +02:00
|
|
|
static void set_actual_malloc_address(void *const mem,
|
|
|
|
const void *const malloc_addr) {
|
2016-08-25 01:50:13 +02:00
|
|
|
size_t *const malloc_addr_location = get_malloc_address_location(mem);
|
2016-07-26 02:12:14 +02:00
|
|
|
*malloc_addr_location = (size_t)malloc_addr;
|
|
|
|
}
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2016-09-07 19:51:33 +02:00
|
|
|
static void *get_actual_malloc_address(void *const mem) {
|
2016-08-25 01:50:13 +02:00
|
|
|
size_t *const malloc_addr_location = get_malloc_address_location(mem);
|
2016-07-26 02:12:14 +02:00
|
|
|
return (void *)(*malloc_addr_location);
|
|
|
|
}
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2016-07-26 02:12:14 +02:00
|
|
|
void *vpx_memalign(size_t align, size_t size) {
|
2016-08-24 06:06:36 +02:00
|
|
|
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);
|
2012-07-14 00:21:29 +02:00
|
|
|
if (addr) {
|
2016-08-27 19:34:40 +02:00
|
|
|
x = align_addr((unsigned char *)addr + ADDRESS_STORAGE_SIZE, align);
|
2016-08-25 01:50:13 +02:00
|
|
|
set_actual_malloc_address(x, addr);
|
2012-07-14 00:21:29 +02:00
|
|
|
}
|
|
|
|
return x;
|
2010-05-18 17:58:33 +02:00
|
|
|
}
|
|
|
|
|
2016-07-25 23:16:28 +02:00
|
|
|
void *vpx_malloc(size_t size) { return vpx_memalign(DEFAULT_ALIGNMENT, size); }
|
2010-05-18 17:58:33 +02:00
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
void *vpx_calloc(size_t num, size_t size) {
|
2016-08-24 06:06:36 +02:00
|
|
|
void *x;
|
|
|
|
if (!check_size_argument_overflow(num, size)) return NULL;
|
|
|
|
|
|
|
|
x = vpx_malloc(num * size);
|
|
|
|
if (x) memset(x, 0, num * size);
|
2012-07-14 00:21:29 +02:00
|
|
|
return x;
|
2010-05-18 17:58:33 +02:00
|
|
|
}
|
|
|
|
|
2012-07-14 00:21:29 +02:00
|
|
|
void vpx_free(void *memblk) {
|
|
|
|
if (memblk) {
|
2016-08-25 01:50:13 +02:00
|
|
|
void *addr = get_actual_malloc_address(memblk);
|
2015-04-17 00:44:18 +02:00
|
|
|
free(addr);
|
2012-07-14 00:21:29 +02:00
|
|
|
}
|
2010-05-18 17:58:33 +02:00
|
|
|
}
|
|
|
|
|
2015-08-14 21:16:07 +02:00
|
|
|
#if CONFIG_VP9_HIGHBITDEPTH
|
2014-08-26 21:35:15 +02:00
|
|
|
void *vpx_memset16(void *dest, int val, size_t length) {
|
2015-10-07 07:48:18 +02:00
|
|
|
size_t i;
|
2015-10-07 07:51:35 +02:00
|
|
|
uint16_t *dest16 = (uint16_t *)dest;
|
2016-07-25 23:16:28 +02:00
|
|
|
for (i = 0; i < length; i++) *dest16++ = val;
|
2015-10-07 07:51:35 +02:00
|
|
|
return dest;
|
2014-08-26 21:35:15 +02:00
|
|
|
}
|
2015-08-14 21:16:07 +02:00
|
|
|
#endif // CONFIG_VP9_HIGHBITDEPTH
|