CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
CoCalc provides the best real-time collaborative environment for Jupyter Notebooks, LaTeX documents, and SageMath, scalable from individual users to large groups and classes!
Path: blob/master/ext/at3_standalone/mem.cpp
Views: 1401
/*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 "compat.h"34#include "intreadwrite.h"35#include "mem.h"3637/**38* Multiply two size_t values checking for overflow.39* @return 0 if success, AVERROR(EINVAL) if overflow.40*/41static inline int av_size_mult(size_t a, size_t b, size_t *r)42{43size_t t = a * b;44/* Hack inspired from glibc: only try the division if nelem and elsize45* are both greater than sqrt(SIZE_MAX). */46if ((a | b) >= ((size_t)1 << (sizeof(size_t) * 4)) && a && t / a != b)47return AVERROR(EINVAL);48*r = t;49return 0;50}5152#define ALIGN (HAVE_AVX ? 32 : 16)5354void *av_malloc(size_t size)55{56void *ptr = NULL;57ptr = malloc(size);58if(!ptr && !size) {59size = 1;60ptr= av_malloc(1);61}62return ptr;63}6465void *av_realloc(void *ptr, size_t size)66{67return realloc(ptr, size + !size);68}6970void *av_realloc_f(void *ptr, size_t nelem, size_t elsize)71{72size_t size;73void *r;7475if (av_size_mult(elsize, nelem, &size)) {76av_free(ptr);77return NULL;78}79r = av_realloc(ptr, size);80if (!r && size)81av_free(ptr);82return r;83}8485void av_free(void *ptr)86{87free(ptr);88}8990void av_freep(void *arg)91{92void *val;9394memcpy(&val, arg, sizeof(val));95memset(arg, 0, sizeof(val));96av_free(val);97}9899void *av_mallocz(size_t size)100{101void *ptr = av_malloc(size);102if (ptr)103memset(ptr, 0, size);104return ptr;105}106107108