vlc_memory.h 1.72 KB
Newer Older
1 2 3
/*****************************************************************************
 * vlc_memory.h: Memory functions
 *****************************************************************************
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
4
 * Copyright (C) 2009 VLC authors and VideoLAN
5 6 7
 *
 * Authors: JP Dinger <jpd at videolan dot org>
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
8 9 10
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
11 12 13 14
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
15 16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
17
 *
Jean-Baptiste Kempf's avatar
LGPL  
Jean-Baptiste Kempf committed
18 19 20
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 22 23 24 25
 *****************************************************************************/

#ifndef VLC_MEMORY_H
#define VLC_MEMORY_H 1

Pierre Ynard's avatar
Pierre Ynard committed
26 27
#include <stdlib.h>

28 29 30
/**
 * \defgroup memory Memory
 * @{
31 32
 * \file
 * Memory fixups
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
 */

/**
 * This wrapper around realloc() will free the input pointer when
 * realloc() returns NULL. The use case ptr = realloc(ptr, newsize) will
 * cause a memory leak when ptr pointed to a heap allocation before,
 * leaving the buffer allocated but unreferenced. vlc_realloc() is a
 * drop-in replacement for that use case (and only that use case).
 */
static inline void *realloc_or_free( void *p, size_t sz )
{
    void *n = realloc(p,sz);
    if( !n )
        free(p);
    return n;
}

/**
 * @}
 */

#endif