blob: 5053786d27834706bd44d3465b33fe6a02ce9041 [file] [log] [blame]
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -08001/* Copyright (C) 2008 The Android Open Source Project
2**
3** This software is licensed under the terms of the GNU General Public
4** License version 2, as published by the Free Software Foundation, and
5** may be copied, distributed, and modified under those terms.
6**
7** This program is distributed in the hope that it will be useful,
8** but WITHOUT ANY WARRANTY; without even the implied warranty of
9** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10** GNU General Public License for more details.
11*/
12#ifndef _ANDROID_UTILS_SYSTEM_H
13#define _ANDROID_UTILS_SYSTEM_H
14
15#include <string.h>
16#include <stdint.h>
David 'Digit' Turner4c0f7452010-11-17 17:55:17 +010017#include "android/utils/assert.h"
18
19/* internal helpers */
20void* _android_array_alloc( size_t itemSize, size_t count );
21void* _android_array_alloc0( size_t itemSize, size_t count );
22void* _android_array_realloc( void* block, size_t itemSize, size_t count );
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080023
24/* the following functions perform 'checked allocations', i.e.
25 * they abort if there is not enough memory.
26 */
27
28/* checked malloc, only returns NULL if size is 0 */
29void* android_alloc( size_t size );
30
31/* checked calloc, only returns NULL if size is 0 */
32void* android_alloc0( size_t size );
33
34/* checked realloc, only returns NULL if size if 0 */
35void* android_realloc( void* block, size_t size );
36
37/* free memory block */
38void android_free( void* block );
39
40/* convenience macros */
41
42#define AZERO(p) memset((char*)(p),0,sizeof(*(p)))
43#define ANEW(p) (p = android_alloc(sizeof(*p)))
44#define ANEW0(p) (p = android_alloc0(sizeof(*p)))
45#define AFREE(p) android_free(p)
46
47#define AMEM_ZERO(dst,size) memset((char*)(dst), 0, (size_t)(size))
48#define AMEM_COPY(dst,src,size) memcpy((char*)(dst),(const char*)(src),(size_t)(size))
49#define AMEM_MOVE(dst,src,size) memmove((char*)(dst),(const char*)(src),(size_t)(size))
50
David 'Digit' Turner4c0f7452010-11-17 17:55:17 +010051#define AARRAY_NEW(p,count) (AASSERT_LOC(), (p) = _android_array_alloc(sizeof(*p),(count)))
52#define AARRAY_NEW0(p,count) (AASSERT_LOC(), (p) = _android_array_alloc0(sizeof(*p),(count)))
53#define AARRAY_RENEW(p,count) (AASSERT_LOC(), (p) = _android_array_realloc((p),sizeof(*(p)),(count)))
The Android Open Source Project8b23a6c2009-03-03 19:30:32 -080054
55#define AARRAY_COPY(dst,src,count) AMEM_COPY(dst,src,(count)*sizeof((dst)[0]))
56#define AARRAY_MOVE(dst,src,count) AMEM_MOVE(dst,src,(count)*sizeof((dst)[0]))
57#define AARRAY_ZERO(dst,count) AMEM_ZERO(dst,(count)*sizeof((dst)[0]))
58
59#define AARRAY_STATIC_LEN(a) (sizeof((a))/sizeof((a)[0]))
60
61#define AINLINED static __inline__
62
63/* unlike strdup(), this accepts NULL as valid input (and will return NULL then) */
64char* android_strdup(const char* src);
65
66#define ASTRDUP(str) android_strdup(str)
67
68/* used for functions that return a Posix-style status code, i.e.
69 * 0 means success, -1 means failure with the error code in 'errno'
70 */
71typedef int APosixStatus;
72
73/* used for functions that return or accept a boolean type */
74typedef int ABool;
75
76/** Stringification macro
77 **/
78#ifndef STRINGIFY
79#define _STRINGIFY(x) #x
80#define STRINGIFY(x) _STRINGIFY(x)
81#endif
82
83/** Concatenation macros
84 **/
85#ifndef GLUE
86#define _GLUE(x,y) x##y
87#define GLUE(x,y) _GLUE(x,y)
88
89#define _GLUE3(x,y,z) x##y##z
90#define GLUE3(x,y,z) _GLUE3(x,y,z)
91#endif
92
93/** Handle strsep() on Win32
94 **/
95#ifdef _WIN32
96# undef strsep
97# define strsep win32_strsep
98extern char* win32_strsep(char** pline, const char* delim);
99#endif
100
101/** Handle strcasecmp on Windows
102 **/
103#ifdef _WIN32
104# define strcasecmp stricmp
105#endif
106
107/** EINTR HANDLING
108 **
109 ** since QEMU uses SIGALRM pretty extensively, having a system call returning
110 ** EINTR on Unix happens very frequently. provide a simple macro to guard against
111 ** this.
112 **/
113
114#ifdef _WIN32
115# define CHECKED(ret, call) (ret) = (call)
116#else
117# define CHECKED(ret, call) do { (ret) = (call); } while ((ret) < 0 && errno == EINTR)
118#endif
119
120/** SIGNAL HANDLING
121 **
122 ** the following can be used to block SIGALRM for a given period of time.
123 ** use with caution, the QEMU execution loop uses SIGALRM extensively
124 **
125 **/
126#ifdef _WIN32
127typedef struct { int dumy; } signal_state_t;
128#else
129#include <signal.h>
130typedef struct { sigset_t old; } signal_state_t;
131#endif
132
133extern void disable_sigalrm( signal_state_t *state );
134extern void restore_sigalrm( signal_state_t *state );
135
136#ifdef _WIN32
137
138#define BEGIN_NOSIGALRM \
139 {
140
141#define END_NOSIGALRM \
142 }
143
144#else /* !WIN32 */
145
146#define BEGIN_NOSIGALRM \
147 { signal_state_t __sigalrm_state; \
148 disable_sigalrm( &__sigalrm_state );
149
150#define END_NOSIGALRM \
151 restore_sigalrm( &__sigalrm_state ); \
152 }
153
154#endif /* !WIN32 */
155
156/** TIME HANDLING
157 **
158 ** sleep for a given time in milliseconds. note: this uses
159 ** disable_sigalrm()/restore_sigalrm()
160 **/
161
162extern void sleep_ms( int timeout );
163
164/* */
165
166#endif /* _ANDROID_UTILS_SYSTEM_H */