/*1* default memory allocator for libavutil2* Copyright (c) 2002 Fabrice Bellard3*4* This file is part of FFmpeg.5*6* FFmpeg is free software; you can redistribute it and/or7* modify it under the terms of the GNU Lesser General Public8* License as published by the Free Software Foundation; either9* version 2.1 of the License, or (at your option) any later version.10*11* FFmpeg is distributed in the hope that it will be useful,12* but WITHOUT ANY WARRANTY; without even the implied warranty of13* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU14* Lesser General Public License for more details.15*16* You should have received a copy of the GNU Lesser General Public17* License along with FFmpeg; if not, write to the Free Software18* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA19*/2021/**22* @file23* default memory allocator for libavutil24*/2526#define _XOPEN_SOURCE 6002728#include <limits.h>29#include <stdint.h>30#include <stdlib.h>31#include <string.h>3233#include "Common/MemoryUtil.h"3435#include "compat.h"36#include "intreadwrite.h"37#include "mem.h"3839/**40* Multiply two size_t values checking for overflow.41* @return 0 if success, AVERROR(EINVAL) if overflow.42*/43static inline int av_size_mult(size_t a, size_t b, size_t *r) {44size_t t = a * b;45/* Hack inspired from glibc: only try the division if nelem and elsize46* are both greater than sqrt(SIZE_MAX). */47if ((a | b) >= ((size_t)1 << (sizeof(size_t) * 4)) && a && t / a != b)48return AVERROR(EINVAL);49*r = t;50return 0;51}5253void *av_malloc(size_t size) {54void *ptr = NULL;5556// Some code requires av malloc to have an alignment of 32 at least. See #2015557ptr = AllocateAlignedMemory(size, 32);58if (!ptr && !size) {59// Compensate for platforms that don't allow zero-size allocations (not sure if this is actually an issue)60return av_malloc(1);61}62return ptr;63}6465void av_free(void *ptr) {66FreeAlignedMemory(ptr);67}6869void av_freep(void *arg) {70void *val;71memcpy(&val, arg, sizeof(val));72memset(arg, 0, sizeof(val));73av_free(val);74}7576void *av_mallocz(size_t size) {77void *ptr = av_malloc(size);78if (ptr)79memset(ptr, 0, size);80return ptr;81}828384