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