1 /*****************************************************************************
2  * alloc.c
3  *****************************************************************************
4  * Copyright (C) 2011-2017 L-SMASH project
5  *
6  * Authors: Yusuke Nakamura <muken.the.vfrmaniac@gmail.com>
7  *
8  * Permission to use, copy, modify, and/or distribute this software for any
9  * purpose with or without fee is hereby granted, provided that the above
10  * copyright notice and this permission notice appear in all copies.
11  *
12  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
13  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
14  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
15  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
16  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
17  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
18  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19  *****************************************************************************/
20 
21 /* This file is available under an ISC license. */
22 
23 #include "internal.h" /* must be placed first */
24 
25 #include <stdlib.h>
26 #include <string.h>
27 
lsmash_malloc(size_t size)28 void *lsmash_malloc( size_t size )
29 {
30     return malloc( size );
31 }
32 
lsmash_malloc_zero(size_t size)33 void *lsmash_malloc_zero( size_t size )
34 {
35     if( !size )
36         return NULL;
37     void *p = malloc( size );
38     if( !p )
39         return NULL;
40     memset( p, 0, size );
41     return p;
42 }
43 
lsmash_realloc(void * ptr,size_t size)44 void *lsmash_realloc( void *ptr, size_t size )
45 {
46     return realloc( ptr, size );
47 }
48 
lsmash_memdup(const void * ptr,size_t size)49 void *lsmash_memdup( const void *ptr, size_t size )
50 {
51     if( !ptr || size == 0 )
52         return NULL;
53     void *dst = malloc( size );
54     if( !dst )
55         return NULL;
56     memcpy( dst, ptr, size );
57     return dst;
58 }
59 
lsmash_free(void * ptr)60 void lsmash_free( void *ptr )
61 {
62     /* free() shall do nothing if a given address is NULL. */
63     free( ptr );
64 }
65 
lsmash_freep(void * ptrptr)66 void lsmash_freep( void *ptrptr )
67 {
68     if( !ptrptr )
69         return;
70     void **ptr = (void **)ptrptr;
71     free( *ptr );
72     *ptr = NULL;
73 }
74