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

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') | base/os_compat_android_unittest.cc » ('j') | 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 //
89 // For any changes in the mkdtemp function, you should manually run the unittest
90 // OsCompatAndroidTest.DISABLED_TestMkdTemp in your local machine to check if it
91 // passes. Please don't enable it, since it creates a directory and may be
92 // source of flakyness.
93 char* mkdtemp(char* path) {
94 if (path == NULL) {
95 errno = EINVAL;
96 return NULL;
97 }
98
99 const int path_len = strlen(path);
100
101 // The last six characters of 'path' must be XXXXXX.
102 const base::StringPiece kSuffix("XXXXXX");
103 const int kSuffixLen = kSuffix.length();
104 if (!base::StringPiece(path, path_len).ends_with(kSuffix)) {
105 errno = EINVAL;
106 return NULL;
107 }
108
109 // If the path contains a directory, as in /tmp/foo/XXXXXXXX, make sure
110 // that /tmp/foo exists, otherwise we're going to loop a really long
111 // time for nothing below
112 char* dirsep = strrchr(path, '/');
113 if (dirsep != NULL) {
114 struct stat st;
115 int ret;
116
117 *dirsep = '\0'; // Terminating directory path temporarily
118
119 ret = stat(path, &st);
120
121 *dirsep = '/'; // Restoring directory separator
122 if (ret < 0) // Directory probably does not exist
123 return NULL;
124 if (!S_ISDIR(st.st_mode)) { // Not a directory
125 errno = ENOTDIR;
126 return NULL;
127 }
128 }
129
130 // Max number of tries using different random suffixes.
131 const int kMaxTries = 100;
132
133 // Now loop until we CAN create a directory by that name or we reach the max
134 // number of tries.
135 for (int i = 0; i < kMaxTries; ++i) {
136 // Fill the suffix XXXXXX with a random string composed of a-z chars.
137 for (int pos = 0; pos < kSuffixLen; ++pos) {
138 char rand_char = static_cast<char>(base::RandInt('a', 'z'));
139 path[path_len - kSuffixLen + pos] = rand_char;
140 }
141 if (mkdir(path, 0700) == 0) {
142 // We just created the directory succesfully.
143 return path;
144 }
145 if (errno != EEXIST) {
146 // The directory doesn't exist, but an error occured
147 return NULL;
148 }
149 }
150
151 // We reached the max number of tries.
152 return NULL;
153 }
154
32 } // extern "C" 155 } // extern "C"
OLDNEW
« no previous file with comments | « base/os_compat_android.h ('k') | base/os_compat_android_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698