1 /*
2  * Memory functions
3  *
4  * Copyright (C) 2011-2021, Joachim Metz <joachim.metz@gmail.com>
5  *
6  * Refer to AUTHORS for acknowledgements.
7  *
8  * This program is free software: you can redistribute it and/or modify
9  * it under the terms of the GNU Lesser General Public License as published by
10  * the Free Software Foundation, either version 3 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 Lesser General Public License
19  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
20  */
21 
22 #if !defined( _MEMORY_H )
23 #define _MEMORY_H
24 
25 #include "common.h"
26 
27 #if defined( HAVE_GLIB_H )
28 #include <glib.h>
29 #endif
30 
31 #if defined( HAVE_STDLIB_H ) || defined( WINAPI )
32 #include <stdlib.h>
33 #endif
34 
35 #if defined( HAVE_STRING_H ) || defined( WINAPI )
36 #include <string.h>
37 #endif
38 
39 #if defined( __cplusplus )
40 extern "C" {
41 #endif
42 
43 /* Note that 128 MiB is an arbitrary selected upper limit here
44  */
45 #define MEMORY_MAXIMUM_ALLOCATION_SIZE \
46 	( 128 * 1024 * 1024 )
47 
48 /* Memory allocation
49  */
50 #if defined( HAVE_GLIB_H )
51 #define memory_allocate( size ) \
52 	g_malloc( (gsize) size )
53 
54 #elif defined( WINAPI )
55 #define memory_allocate( size ) \
56 	HeapAlloc( GetProcessHeap(), 0, (SIZE_T) size )
57 
58 #elif defined( HAVE_MALLOC )
59 #define memory_allocate( size ) \
60 	malloc( size )
61 #endif
62 
63 #define memory_allocate_structure( type ) \
64 	(type *) memory_allocate( sizeof( type ) )
65 
66 #define memory_allocate_structure_as_value( type ) \
67 	(intptr_t *) memory_allocate( sizeof( type ) )
68 
69 /* Memory reallocation
70  */
71 #if defined( HAVE_GLIB_H )
72 #define memory_reallocate( buffer, size ) \
73 	g_realloc( (gpointer) buffer, (gsize) size )
74 
75 #elif defined( WINAPI )
76 /* HeapReAlloc does not allocate empty (NULL) buffers as realloc does
77  */
78 #define memory_reallocate( buffer, size ) \
79 	( buffer == NULL ) ? \
80 	HeapAlloc( GetProcessHeap(), 0, (SIZE_T) size ) : \
81 	HeapReAlloc( GetProcessHeap(), 0, (LPVOID) buffer, (SIZE_T) size )
82 
83 #elif defined( HAVE_REALLOC )
84 #define memory_reallocate( buffer, size ) \
85 	realloc( (void *) buffer, size )
86 #endif
87 
88 /* Memory free
89  */
90 #if defined( HAVE_GLIB_H )
91 #define memory_free( buffer ) \
92 	g_free( (gpointer) buffer )
93 
94 #elif defined( WINAPI )
95 #define memory_free( buffer ) \
96 	( buffer == NULL ) ? TRUE : HeapFree( GetProcessHeap(), 0, (LPVOID) buffer )
97 
98 #elif defined( HAVE_FREE )
99 #define memory_free( buffer ) \
100 	free( (void *) buffer )
101 #endif
102 
103 /* Memory compare
104  */
105 #if defined( HAVE_MEMCMP ) || defined( WINAPI )
106 #define memory_compare( buffer1, buffer2, size ) \
107 	memcmp( (const void *) buffer1, (const void *) buffer2, size )
108 #endif
109 
110 /* Memory copy
111  */
112 #if defined( HAVE_MEMCPY ) || defined( WINAPI )
113 #define memory_copy( destination, source, count ) \
114 	memcpy( (void *) destination, (void *) source, count )
115 #endif
116 
117 /* Memory set
118  */
119 #if defined( HAVE_MEMSET ) || defined( WINAPI )
120 #define memory_set( buffer, value, count ) \
121 	memset( (void *) buffer, (int) value, count )
122 #endif
123 
124 #if defined( __cplusplus )
125 }
126 #endif
127 
128 #endif /* !defined( _MEMORY_H ) */
129 
130