• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "android/utils/system.h"
13 #include <stdlib.h>
14 #include <stdio.h>
15 #ifdef _WIN32
16 #  define WIN32_LEAN_AND_MEAN
17 #  include <windows.h>  /* for Sleep */
18 #else
19 #  include <unistd.h>  /* for usleep */
20 #endif
21 
22 void*
android_alloc(size_t size)23 android_alloc( size_t  size )
24 {
25     void*   block;
26 
27     if (size == 0)
28         return NULL;
29 
30     block = malloc(size);
31     if (block != NULL)
32         return block;
33 
34     fprintf(stderr, "PANIC: not enough memory\n");
35     exit(1);
36     return NULL;
37 }
38 
39 void*
android_alloc0(size_t size)40 android_alloc0( size_t  size )
41 {
42     void*   block;
43 
44     if (size == 0)
45         return NULL;
46 
47     block = calloc(1, size);
48     if (block != NULL)
49         return block;
50 
51     fprintf(stderr, "PANIC: not enough memory\n");
52     exit(1);
53     return NULL;
54 }
55 
56 void*
android_realloc(void * block,size_t size)57 android_realloc( void*  block, size_t  size )
58 {
59     void*   block2;
60 
61     if (size == 0) {
62         free(block);
63         return NULL;
64     }
65     block2 = realloc(block, size);
66     if (block2 != NULL)
67         return block2;
68 
69     fprintf(stderr, "PANIC: not enough memory to reallocate %d bytes\n", size);
70     exit(1);
71     return NULL;
72 }
73 
74 void
android_free(void * block)75 android_free( void*  block )
76 {
77     if (block)
78         free(block);
79 }
80 
81 char*
android_strdup(const char * str)82 android_strdup( const char*  str )
83 {
84     int    len;
85     char*  copy;
86 
87     if (str == NULL)
88         return NULL;
89 
90     len  = strlen(str);
91     copy = malloc(len+1);
92     memcpy(copy, str, len);
93     copy[len] = 0;
94 
95     return copy;
96 }
97 
98 #ifdef _WIN32
99 char*
win32_strsep(char ** pline,const char * delim)100 win32_strsep(char**  pline, const char*  delim)
101 {
102     char*  line = *pline;
103     char*  p    = line;
104 
105     if (p == NULL)
106         return NULL;
107 
108     for (;;) {
109         int          c = *p++;
110         const char*  q = delim;
111 
112         if (c == 0) {
113             p = NULL;
114             break;
115         }
116 
117         while (*q) {
118             if (*q == c) {
119                 p[-1] = 0;
120                 goto Exit;
121             }
122             q++;
123         }
124     }
125 Exit:
126     *pline = p;
127     return line;
128 }
129 #endif
130 
131 
132 void
disable_sigalrm(signal_state_t * state)133 disable_sigalrm( signal_state_t  *state )
134 {
135 #ifdef _WIN32
136     (void)state;
137 #else
138     sigset_t  set;
139 
140     sigemptyset(&set);
141     sigaddset(&set, SIGALRM);
142     pthread_sigmask (SIG_BLOCK, &set, &state->old);
143 #endif
144 }
145 
146 void
restore_sigalrm(signal_state_t * state)147 restore_sigalrm( signal_state_t  *state )
148 {
149 #ifdef _WIN32
150     (void)state;
151 #else
152     pthread_sigmask (SIG_SETMASK, &state->old, NULL);
153 #endif
154 }
155 
156 void
sleep_ms(int timeout_ms)157 sleep_ms( int  timeout_ms )
158 {
159 #ifdef _WIN32
160     if (timeout_ms <= 0)
161         return;
162 
163     Sleep( timeout_ms );
164 #else
165     if (timeout_ms <= 0)
166         return;
167 
168     BEGIN_NOSIGALRM
169     usleep( timeout_ms*1000 );
170     END_NOSIGALRM
171 #endif
172 }
173