| OLD | NEW |
| 1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 191 // Simple support to read a file into a 0-terminated C-string. | 191 // Simple support to read a file into a 0-terminated C-string. |
| 192 // The returned buffer must be freed by the caller. | 192 // The returned buffer must be freed by the caller. |
| 193 // On return, *exits tells whether the file existed. | 193 // On return, *exits tells whether the file existed. |
| 194 Vector<const char> ReadFile(const char* filename, | 194 Vector<const char> ReadFile(const char* filename, |
| 195 bool* exists, | 195 bool* exists, |
| 196 bool verbose = true); | 196 bool verbose = true); |
| 197 Vector<const char> ReadFile(FILE* file, | 197 Vector<const char> ReadFile(FILE* file, |
| 198 bool* exists, | 198 bool* exists, |
| 199 bool verbose = true); | 199 bool verbose = true); |
| 200 | 200 |
| 201 template<typename sourcechar, typename sinkchar> |
| 202 static inline void AlignedCharCopy(sinkchar* dest, |
| 203 const sourcechar* src, int chars) { |
| 204 #define COPY(offset) \ |
| 205 *(dest+offset) = static_cast<sinkchar>(*(src+offset)); |
| 201 | 206 |
| 207 int div = chars >> 4; |
| 208 while ( div ) { |
| 209 COPY(0) COPY(1) COPY(2) COPY(3) |
| 210 COPY(4) COPY(5) COPY(6) COPY(7) |
| 211 COPY(8) COPY(9) COPY(10) COPY(11) |
| 212 COPY(12) COPY(13) COPY(14) COPY(15) |
| 213 dest += 16; |
| 214 src += 16; |
| 215 div--; |
| 216 } |
| 217 |
| 218 #define CASE_COPY(N) \ |
| 219 case N : COPY(N-1) |
| 220 |
| 221 int mod = chars & 0xF; |
| 222 switch ( mod ) { |
| 223 CASE_COPY(15) CASE_COPY(14) CASE_COPY(13) CASE_COPY(12) |
| 224 CASE_COPY(11) CASE_COPY(10) CASE_COPY(9) CASE_COPY(8) |
| 225 CASE_COPY(7) CASE_COPY(6) CASE_COPY(5) CASE_COPY(4) |
| 226 CASE_COPY(3) CASE_COPY(2) CASE_COPY(1) |
| 227 break; |
| 228 } |
| 229 |
| 230 #undef CASE_COPY |
| 231 #undef COPY |
| 232 } |
| 233 |
| 234 #ifdef V8_HOST_CAN_READ_UNALIGNED |
| 235 template<typename Char> |
| 236 static inline void UnalignedCharCopy(Char* dest, const Char* src, int chars) { |
| 237 int size = static_cast<int>(chars * sizeof(Char)); |
| 238 if ( size >= OS::kMinComplexMemCopy ) { |
| 239 OS::MemCopy(dest, src, size); |
| 240 return; |
| 241 } |
| 242 |
| 243 unsigned int* dest_ = reinterpret_cast<unsigned int*>(dest); |
| 244 const unsigned int* src_ = reinterpret_cast<const unsigned int*>(src); |
| 245 const int cell = size / sizeof(unsigned int); |
| 246 |
| 247 #define COPY(offset) \ |
| 248 *(dest_+offset) = *(src_+offset); |
| 249 |
| 250 // copy patch cells by 16 |
| 251 int div = cell >> 4; |
| 252 while ( div ) { |
| 253 COPY(0) COPY(1) COPY(2) COPY(3) |
| 254 COPY(4) COPY(5) COPY(6) COPY(7) |
| 255 COPY(8) COPY(9) COPY(10) COPY(11) |
| 256 COPY(12) COPY(13) COPY(14) COPY(15) |
| 257 dest_ += 16; |
| 258 src_ += 16; |
| 259 div--; |
| 260 } |
| 261 |
| 262 #define CASE_COPY(N) \ |
| 263 case N : COPY(N-1) |
| 264 |
| 265 // copy left cells |
| 266 int mod = cell & 0xF; |
| 267 switch ( mod ) { |
| 268 CASE_COPY(15) CASE_COPY(14) CASE_COPY(13) CASE_COPY(12) |
| 269 CASE_COPY(11) CASE_COPY(10) CASE_COPY(9) CASE_COPY(8) |
| 270 CASE_COPY(7) CASE_COPY(6) CASE_COPY(5) CASE_COPY(4) |
| 271 CASE_COPY(3) CASE_COPY(2) CASE_COPY(1) |
| 272 dest_ += mod; |
| 273 src_ += mod; |
| 274 break; |
| 275 } |
| 276 |
| 277 // copy left chars |
| 278 Char* limit = dest + chars; |
| 279 dest = reinterpret_cast<Char*>(dest_); |
| 280 src = reinterpret_cast<const Char*>(src_); |
| 281 while ( dest < limit ) { |
| 282 *dest++ = *src++; |
| 283 } |
| 284 |
| 285 #undef CASE_COPY |
| 286 #undef COPY |
| 287 } |
| 288 #endif |
| 289 |
| 290 template<typename sourcechar, typename sinkchar> |
| 291 class CharCopier { |
| 292 public: |
| 293 static void Copy(sinkchar* dest, const sourcechar* src, int chars) { |
| 294 AlignedCharCopy(dest, src, chars); |
| 295 } |
| 296 }; |
| 297 |
| 298 template<> |
| 299 class CharCopier<char, char> { |
| 300 public: |
| 301 static void Copy(char* dest, const char* src, int chars) { |
| 302 #ifdef V8_HOST_CAN_READ_UNALIGNED |
| 303 UnalignedCharCopy(dest, src, chars); |
| 304 #else |
| 305 AlignedCharCopy(dest, src, chars); |
| 306 #endif |
| 307 } |
| 308 }; |
| 309 |
| 310 template<> |
| 311 class CharCopier<uc16, uc16> { |
| 312 public: |
| 313 static void Copy(uc16* dest, const uc16* src, int chars) { |
| 314 #ifdef V8_HOST_CAN_READ_UNALIGNED |
| 315 UnalignedCharCopy(dest, src, chars); |
| 316 #else |
| 317 AlignedCharCopy(dest, src, chars); |
| 318 #endif |
| 319 } |
| 320 }; |
| 202 | 321 |
| 203 // Copy from ASCII/16bit chars to ASCII/16bit chars. | 322 // Copy from ASCII/16bit chars to ASCII/16bit chars. |
| 204 template <typename sourcechar, typename sinkchar> | 323 template <typename sourcechar, typename sinkchar> |
| 205 inline void CopyChars(sinkchar* dest, const sourcechar* src, int chars) { | 324 static inline void CopyChars(sinkchar* dest, const sourcechar* src, int chars) { |
| 206 sinkchar* limit = dest + chars; | 325 // return faster if chars is small |
| 207 #ifdef V8_HOST_CAN_READ_UNALIGNED | 326 switch ( chars ) { |
| 208 if (sizeof(*dest) == sizeof(*src)) { | 327 #define CASE_COPY(N) \ |
| 209 if (chars >= static_cast<int>(OS::kMinComplexMemCopy / sizeof(*dest))) { | 328 case N : *(dest+N-1) = static_cast<sinkchar>(*(src+N-1)); |
| 210 OS::MemCopy(dest, src, chars * sizeof(*dest)); | 329 CASE_COPY(8) CASE_COPY(7) |
| 211 return; | 330 CASE_COPY(6) CASE_COPY(5) |
| 212 } | 331 CASE_COPY(4) CASE_COPY(3) |
| 213 // Number of characters in a uintptr_t. | 332 CASE_COPY(2) CASE_COPY(1) |
| 214 static const int kStepSize = sizeof(uintptr_t) / sizeof(*dest); // NOLINT | 333 return; |
| 215 while (dest <= limit - kStepSize) { | 334 #undef CASE_COPY |
| 216 *reinterpret_cast<uintptr_t*>(dest) = | |
| 217 *reinterpret_cast<const uintptr_t*>(src); | |
| 218 dest += kStepSize; | |
| 219 src += kStepSize; | |
| 220 } | |
| 221 } | 335 } |
| 222 #endif | 336 |
| 223 while (dest < limit) { | 337 // fall to copier |
| 224 *dest++ = static_cast<sinkchar>(*src++); | 338 CharCopier<sourcechar, sinkchar>::Copy(dest, src, chars); |
| 225 } | |
| 226 } | 339 } |
| 227 | 340 |
| 228 | 341 |
| 229 // A resource for using mmapped files to back external strings that are read | 342 // A resource for using mmapped files to back external strings that are read |
| 230 // from files. | 343 // from files. |
| 231 class MemoryMappedExternalResource: public | 344 class MemoryMappedExternalResource: public |
| 232 v8::String::ExternalAsciiStringResource { | 345 v8::String::ExternalAsciiStringResource { |
| 233 public: | 346 public: |
| 234 explicit MemoryMappedExternalResource(const char* filename); | 347 explicit MemoryMappedExternalResource(const char* filename); |
| 235 MemoryMappedExternalResource(const char* filename, | 348 MemoryMappedExternalResource(const char* filename, |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 267 | 380 |
| 268 // Add formatted contents like printf based on a va_list. | 381 // Add formatted contents like printf based on a va_list. |
| 269 void AddFormattedList(const char* format, va_list list); | 382 void AddFormattedList(const char* format, va_list list); |
| 270 private: | 383 private: |
| 271 DISALLOW_IMPLICIT_CONSTRUCTORS(StringBuilder); | 384 DISALLOW_IMPLICIT_CONSTRUCTORS(StringBuilder); |
| 272 }; | 385 }; |
| 273 | 386 |
| 274 } } // namespace v8::internal | 387 } } // namespace v8::internal |
| 275 | 388 |
| 276 #endif // V8_V8UTILS_H_ | 389 #endif // V8_V8UTILS_H_ |
| OLD | NEW |