File: memory.h

package info (click to toggle)
libqcow 20160123-2
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 10,600 kB
  • ctags: 4,375
  • sloc: ansic: 142,933; sh: 12,887; makefile: 1,198; python: 549; sed: 134
file content (124 lines) | stat: -rw-r--r-- 3,073 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/*
 * Memory functions
 *
 * Copyright (C) 2006-2016, Joachim Metz <joachim.metz@gmail.com>
 *
 * Refer to AUTHORS for acknowledgements.
 *
 * This software 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 3 of the License, or
 * (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this software.  If not, see <http://www.gnu.org/licenses/>.
 */

#if !defined( _MEMORY_H )
#define _MEMORY_H

#include "common.h"

#if defined( HAVE_GLIB_H )
#include <glib.h>
#endif

#if defined( HAVE_STDLIB_H ) || defined( WINAPI )
#include <stdlib.h>
#endif

#if defined( HAVE_STRING_H ) || defined( WINAPI )
#include <string.h>
#endif

#if defined( __cplusplus )
extern "C" {
#endif

/* Memory allocation
 */
#if defined( HAVE_GLIB_H )
#define memory_allocate( size ) \
	g_malloc( (gsize) size )

#elif defined( WINAPI )
#define memory_allocate( size ) \
	HeapAlloc( GetProcessHeap(), 0, (SIZE_T) size )

#elif defined( HAVE_MALLOC )
#define memory_allocate( size ) \
	malloc( size )
#endif

#define memory_allocate_structure( type ) \
	(type *) memory_allocate( sizeof( type ) )

#define memory_allocate_structure_as_value( type ) \
	(intptr_t *) memory_allocate( sizeof( type ) )

/* Memory reallocation
 */
#if defined( HAVE_GLIB_H )
#define memory_reallocate( buffer, size ) \
	g_realloc( (gpointer) buffer, (gsize) size )

#elif defined( WINAPI )
/* HeapReAlloc does not allocate empty (NULL) buffers as realloc does
 */
#define memory_reallocate( buffer, size ) \
	( buffer == NULL ) ? \
	HeapAlloc( GetProcessHeap(), 0, (SIZE_T) size ) : \
	HeapReAlloc( GetProcessHeap(), 0, (LPVOID) buffer, (SIZE_T) size )

#elif defined( HAVE_REALLOC )
#define memory_reallocate( buffer, size ) \
	realloc( (void *) buffer, size )
#endif

/* Memory free
 */
#if defined( HAVE_GLIB_H )
#define memory_free( buffer ) \
	g_free( (gpointer) buffer )

#elif defined( WINAPI )
#define memory_free( buffer ) \
	HeapFree( GetProcessHeap(), 0, (LPVOID) buffer )

#elif defined( HAVE_FREE )
#define memory_free( buffer ) \
	free( (void *) buffer )
#endif

/* Memory compare
 */
#if defined( HAVE_MEMCMP ) || defined( WINAPI )
#define memory_compare( buffer1, buffer2, size ) \
	memcmp( (const void *) buffer1, (const void *) buffer2, size )
#endif

/* Memory copy
 */
#if defined( HAVE_MEMCPY ) || defined( WINAPI )
#define memory_copy( destination, source, count ) \
	memcpy( (void *) destination, (void *) source, count )
#endif

/* Memory set
 */
#if defined( HAVE_MEMSET ) || defined( WINAPI )
#define memory_set( buffer, value, count ) \
	memset( (void *) buffer, (int) value, count )
#endif

#if defined( __cplusplus )
}
#endif

#endif