Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(928)

Side by Side Diff: base/os_compat_android.cc

Issue 10696114: Upstreaming diffs in os_compat_android.cc (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/os_compat_android.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/os_compat_android.h" 5 #include "base/os_compat_android.h"
6 6
7 #include <errno.h>
8 #include <math.h>
9 #include <sys/stat.h>
7 #include <time64.h> 10 #include <time64.h>
8 11
12 #include "base/rand_util.h"
13 #include "base/string_piece.h"
9 #include "base/stringprintf.h" 14 #include "base/stringprintf.h"
10 15
11 // There is no futimes() avaiable in Bionic, so we provide our own 16 // There is no futimes() avaiable in Bionic, so we provide our own
12 // implementation until it is there. 17 // implementation until it is there.
13 extern "C" { 18 extern "C" {
14 19
15 int futimes(int fd, const struct timeval tv[2]) { 20 int futimes(int fd, const struct timeval tv[2]) {
16 const std::string fd_path = StringPrintf("/proc/self/fd/%d", fd); 21 const std::string fd_path = StringPrintf("/proc/self/fd/%d", fd);
17 return utimes(fd_path.c_str(), tv); 22 return utimes(fd_path.c_str(), tv);
18 } 23 }
19 24
20 // Android has only timegm64() and no timegm(). 25 // Android has only timegm64() and no timegm().
21 // We replicate the behaviour of timegm() when the result overflows time_t. 26 // We replicate the behaviour of timegm() when the result overflows time_t.
22 time_t timegm(struct tm* const t) { 27 time_t timegm(struct tm* const t) {
23 // time_t is signed on Android. 28 // time_t is signed on Android.
24 static const time_t kTimeMax = ~(1 << (sizeof(time_t) * CHAR_BIT - 1)); 29 static const time_t kTimeMax = ~(1 << (sizeof(time_t) * CHAR_BIT - 1));
25 static const time_t kTimeMin = (1 << (sizeof(time_t) * CHAR_BIT - 1)); 30 static const time_t kTimeMin = (1 << (sizeof(time_t) * CHAR_BIT - 1));
26 time64_t result = timegm64(t); 31 time64_t result = timegm64(t);
27 if (result < kTimeMin || result > kTimeMax) 32 if (result < kTimeMin || result > kTimeMax)
28 return -1; 33 return -1;
29 return result; 34 return result;
30 } 35 }
31 36
37 // The following is only needed when building with GCC 4.6 or higher
38 // (i.e. not with Android GCC 4.4.3, nor with Clang).
39 //
40 // GCC is now capable of optimizing successive calls to sin() and cos() into
41 // a single call to sincos(). This means that source code that looks like:
42 //
43 // double c, s;
44 // c = cos(angle);
45 // s = sin(angle);
46 //
47 // Will generate machine code that looks like:
48 //
49 // double c, s;
50 // sincos(angle, &s, &c);
51 //
52 // Unfortunately, sincos() and friends are not part of the Android libm.so
53 // library provided by the NDK for API level 9. When the optimization kicks
54 // in, it makes the final build fail with a puzzling message (puzzling
55 // because 'sincos' doesn't appear anywhere in the sources!).
56 //
57 // To solve this, we provide our own implementation of the sincos() function
58 // and related friends. Note that we must also explicitely tell GCC to disable
59 // optimizations when generating these. Otherwise, the generated machine code
60 // for each function would simply end up calling itself, resulting in a
61 // runtime crash due to stack overflow.
62 //
63 #if defined(__GNUC__) && !defined(__clang__)
64
65 // For the record, Clang does not support the 'optimize' attribute.
66 // In the unlikely event that it begins performing this optimization too,
67 // we'll have to find a different way to achieve this. NOTE: Tested with O1
68 // which still performs the optimization.
69 //
70 #define GCC_NO_OPTIMIZE __attribute__((optimize("O0")))
71
72 GCC_NO_OPTIMIZE
73 void sincos(double angle, double* s, double *c) {
74 *c = cos(angle);
75 *s = sin(angle);
76 }
77
78 GCC_NO_OPTIMIZE
79 void sincosf(float angle, float* s, float* c) {
80 *c = cosf(angle);
81 *s = sinf(angle);
82 }
83
84 #endif // __GNUC__ && !__clang__
85
86 // An implementation of mkdtemp, since it is not exposed by the NDK
87 // for native API level 9 that we target.
88 char* mkdtemp(char* path) {
89 if (path == NULL) {
90 errno = EINVAL;
91 return NULL;
92 }
93
94 const int path_len = strlen(path);
95
96 // The last six characters of 'path' must be XXXXXX.
97 const base::StringPiece kSuffix("XXXXXX");
98 const int kSuffixLen = kSuffix.length();
99 if (!base::StringPiece(path, path_len).ends_with(kSuffix)) {
100 errno = EINVAL;
101 return NULL;
102 }
103
104 // If the path contains a directory, as in /tmp/foo/XXXXXXXX, make sure
105 // that /tmp/foo exists, otherwise we're going to loop a really long
106 // time for nothing below
107 char* dirsep = strrchr(path, '/');
108 if (dirsep != NULL) {
109 struct stat st;
110 int ret;
111
112 *dirsep = '\0'; // Terminating directory path temporarily
113
114 ret = stat(path, &st);
115
116 *dirsep = '/'; // Restoring directory separator
117 if (ret < 0) // Directory probably does not exist
118 return NULL;
119 if (!S_ISDIR(st.st_mode)) { // Not a directory
120 errno = ENOTDIR;
121 return NULL;
122 }
123 }
124
125 // Max number of tries using different random suffixes.
126 const int kMaxTries = 100;
127
128 // Now loop until we CAN create a directory by that name or we reach the max
129 // number of tries.
130 for (int i = 0; i < kMaxTries; ++i) {
131 // Fill the suffix XXXXXX with a random string composed of a-z chars.
132 for (int pos = 0; pos < kSuffixLen; ++pos) {
brettw 2012/07/12 18:22:34 Style nit: you got 2 spaces.
felipeg 2012/07/13 10:17:47 Done.
133 char rand_char = static_cast<char>(base::RandInt('a', 'z'));
134 path[path_len - kSuffixLen + pos] = rand_char;
135 }
136 if (mkdir(path, 0700) == 0) {
137 // We just created the directory succesfully.
138 return path;
139 }
140 if (errno != EEXIST) {
141 // The directory doesn't exist, but an error occured
142 return NULL;
143 }
144 }
145
146 // We reached the max number of tries.
147 return NULL;
148 }
149
32 } // extern "C" 150 } // extern "C"
OLDNEW
« no previous file with comments | « base/os_compat_android.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698