]> git.sesse.net Git - vlc/blob - include/vlc_memory.h
Introduce realloc_or_free(), which does what realloc() does but will free the argumen...
[vlc] / include / vlc_memory.h
1 /*****************************************************************************
2  * vlc_memory.h: Memory functions
3  *****************************************************************************
4  * Copyright (C) 2009 the VideoLAN team
5  *
6  * Authors: JP Dinger <jpd at videolan dot org>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License along
19  * with this program; if not, write to the Free Software Foundation, Inc.,
20  * 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21  *****************************************************************************/
22
23 #ifndef VLC_MEMORY_H
24 #define VLC_MEMORY_H 1
25
26 /**
27  * \file
28  * This file deals with memory fixups
29  */
30
31 /**
32  * \defgroup memory Memory
33  * @{
34  */
35
36 /**
37  * This wrapper around realloc() will free the input pointer when
38  * realloc() returns NULL. The use case ptr = realloc(ptr, newsize) will
39  * cause a memory leak when ptr pointed to a heap allocation before,
40  * leaving the buffer allocated but unreferenced. vlc_realloc() is a
41  * drop-in replacement for that use case (and only that use case).
42  */
43 static inline void *realloc_or_free( void *p, size_t sz )
44 {
45     void *n = realloc(p,sz);
46     if( !n )
47         free(p);
48     return n;
49 }
50
51 /**
52  * @}
53  */
54
55 #endif