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