1 /* 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> 17 #ifndef __STDC_FORMAT_MACROS 18 #define __STDC_FORMAT_MACROS 1 19 #endif 20 #include <inttypes.h> /* for PRId64 et al. */ 21 #include "android/utils/assert.h" 22 #include "android/utils/compiler.h" 23 24 ANDROID_BEGIN_HEADER 25 26 /* internal helpers */ 27 void* _android_array_alloc( size_t itemSize, size_t count ); 28 void* _android_array_alloc0( size_t itemSize, size_t count ); 29 void* _android_array_realloc( void* block, size_t itemSize, size_t count ); 30 31 /* the following functions perform 'checked allocations', i.e. 32 * they abort if there is not enough memory. 33 */ 34 35 /* checked malloc, only returns NULL if size is 0 */ 36 void* android_alloc( size_t size ); 37 38 /* checked calloc, only returns NULL if size is 0 */ 39 void* android_alloc0( size_t size ); 40 41 /* checked realloc, only returns NULL if size if 0 */ 42 void* android_realloc( void* block, size_t size ); 43 44 /* free memory block */ 45 void android_free( void* block ); 46 47 /* convenience macros */ 48 49 #define AZERO(p) memset((char*)(p),0,sizeof(*(p))) 50 #define ANEW(p) (p = android_alloc(sizeof(*p))) 51 #define ANEW0(p) (p = android_alloc0(sizeof(*p))) 52 #define AFREE(p) android_free(p) 53 54 #define AMEM_ZERO(dst,size) memset((char*)(dst), 0, (size_t)(size)) 55 #define AMEM_COPY(dst,src,size) memcpy((char*)(dst),(const char*)(src),(size_t)(size)) 56 #define AMEM_MOVE(dst,src,size) memmove((char*)(dst),(const char*)(src),(size_t)(size)) 57 58 #define AARRAY_NEW(p,count) (AASSERT_LOC(), (p) = _android_array_alloc(sizeof(*p),(count))) 59 #define AARRAY_NEW0(p,count) (AASSERT_LOC(), (p) = _android_array_alloc0(sizeof(*p),(count))) 60 #define AARRAY_RENEW(p,count) (AASSERT_LOC(), (p) = _android_array_realloc((p),sizeof(*(p)),(count))) 61 62 #define AARRAY_COPY(dst,src,count) AMEM_COPY(dst,src,(count)*sizeof((dst)[0])) 63 #define AARRAY_MOVE(dst,src,count) AMEM_MOVE(dst,src,(count)*sizeof((dst)[0])) 64 #define AARRAY_ZERO(dst,count) AMEM_ZERO(dst,(count)*sizeof((dst)[0])) 65 66 #define AARRAY_STATIC_LEN(a) (sizeof((a))/sizeof((a)[0])) 67 68 #define AINLINED static __inline__ 69 70 /* unlike strdup(), this accepts NULL as valid input (and will return NULL then) */ 71 char* android_strdup(const char* src); 72 73 #define ASTRDUP(str) android_strdup(str) 74 75 /* used for functions that return a Posix-style status code, i.e. 76 * 0 means success, -1 means failure with the error code in 'errno' 77 */ 78 typedef int APosixStatus; 79 80 /* used for functions that return or accept a boolean type */ 81 typedef int ABool; 82 83 /** Stringification macro 84 **/ 85 #ifndef STRINGIFY 86 #define _STRINGIFY(x) #x 87 #define STRINGIFY(x) _STRINGIFY(x) 88 #endif 89 90 /** Concatenation macros 91 **/ 92 #ifndef GLUE 93 #define _GLUE(x,y) x##y 94 #define GLUE(x,y) _GLUE(x,y) 95 96 #define _GLUE3(x,y,z) x##y##z 97 #define GLUE3(x,y,z) _GLUE3(x,y,z) 98 #endif 99 100 /** Handle strsep() on Win32 101 **/ 102 #ifdef _WIN32 103 # undef strsep 104 # define strsep win32_strsep 105 extern char* win32_strsep(char** pline, const char* delim); 106 #endif 107 108 /** Handle strcasecmp on Windows (and older Mingw32 toolchain) 109 **/ 110 #if defined(_WIN32) && !ANDROID_GCC_PREREQ(4,4) 111 # define strcasecmp stricmp 112 #endif 113 114 /** SIGNAL HANDLING 115 ** 116 ** the following can be used to block SIGALRM for a given period of time. 117 ** use with caution, the QEMU execution loop uses SIGALRM extensively 118 ** 119 **/ 120 #ifdef _WIN32 121 typedef struct { int dumy; } signal_state_t; 122 #else 123 #include <signal.h> 124 typedef struct { sigset_t old; } signal_state_t; 125 #endif 126 127 extern void disable_sigalrm( signal_state_t *state ); 128 extern void restore_sigalrm( signal_state_t *state ); 129 130 #ifdef _WIN32 131 132 #define BEGIN_NOSIGALRM \ 133 { 134 135 #define END_NOSIGALRM \ 136 } 137 138 #else /* !WIN32 */ 139 140 #define BEGIN_NOSIGALRM \ 141 { signal_state_t __sigalrm_state; \ 142 disable_sigalrm( &__sigalrm_state ); 143 144 #define END_NOSIGALRM \ 145 restore_sigalrm( &__sigalrm_state ); \ 146 } 147 148 #endif /* !WIN32 */ 149 150 /** TIME HANDLING 151 ** 152 ** sleep for a given time in milliseconds. note: this uses 153 ** disable_sigalrm()/restore_sigalrm() 154 **/ 155 156 extern void sleep_ms( int timeout ); 157 158 /** FORMATTING int64_t in printf() statements 159 ** 160 ** Normally defined in <inttypes.h> except on Windows and maybe others. 161 **/ 162 163 #ifndef PRId64 164 # define PRId64 "lld" 165 #endif 166 #ifndef PRIx64 167 # define PRIx64 "llx" 168 #endif 169 #ifndef PRIu64 170 # define PRIu64 "llu" 171 #endif 172 173 /* */ 174 175 ANDROID_END_HEADER 176 177 #endif /* _ANDROID_UTILS_SYSTEM_H */ 178