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