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

Side by Side Diff: src/utils.h

Issue 306473004: Reland 21502 - "Move OS::MemCopy and OS::MemMove out of platform to utils" (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Created 6 years, 6 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 | Annotate | Revision Log
« no previous file with comments | « src/unicode-inl.h ('k') | src/utils.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 2012 the V8 project authors. All rights reserved. 1 // Copyright 2012 the V8 project 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 #ifndef V8_UTILS_H_ 5 #ifndef V8_UTILS_H_
6 #define V8_UTILS_H_ 6 #define V8_UTILS_H_
7 7
8 #include <limits.h> 8 #include <limits.h>
9 #include <stdlib.h> 9 #include <stdlib.h>
10 #include <string.h> 10 #include <string.h>
(...skipping 304 matching lines...) Expand 10 before | Expand all | Expand 10 after
315 315
316 316
317 inline uint32_t ComputePointerHash(void* ptr) { 317 inline uint32_t ComputePointerHash(void* ptr) {
318 return ComputeIntegerHash( 318 return ComputeIntegerHash(
319 static_cast<uint32_t>(reinterpret_cast<intptr_t>(ptr)), 319 static_cast<uint32_t>(reinterpret_cast<intptr_t>(ptr)),
320 v8::internal::kZeroHashSeed); 320 v8::internal::kZeroHashSeed);
321 } 321 }
322 322
323 323
324 // ---------------------------------------------------------------------------- 324 // ----------------------------------------------------------------------------
325 // Generated memcpy/memmove
326
327 // Initializes the codegen support that depends on CPU features. This is
328 // called after CPU initialization.
329 void init_memcopy_functions();
330
331 #if defined(V8_TARGET_ARCH_IA32) || defined(V8_TARGET_ARCH_X87)
332 // Limit below which the extra overhead of the MemCopy function is likely
333 // to outweigh the benefits of faster copying.
334 const int kMinComplexMemCopy = 64;
335
336 // Copy memory area. No restrictions.
337 void MemMove(void* dest, const void* src, size_t size);
338 typedef void (*MemMoveFunction)(void* dest, const void* src, size_t size);
339
340 // Keep the distinction of "move" vs. "copy" for the benefit of other
341 // architectures.
342 V8_INLINE void MemCopy(void* dest, const void* src, size_t size) {
343 MemMove(dest, src, size);
344 }
345 #elif defined(V8_HOST_ARCH_ARM)
346 typedef void (*MemCopyUint8Function)(uint8_t* dest, const uint8_t* src,
347 size_t size);
348 extern MemCopyUint8Function memcopy_uint8_function;
349 V8_INLINE void MemCopyUint8Wrapper(uint8_t* dest, const uint8_t* src,
350 size_t chars) {
351 memcpy(dest, src, chars);
352 }
353 // For values < 16, the assembler function is slower than the inlined C code.
354 const int kMinComplexMemCopy = 16;
355 V8_INLINE void MemCopy(void* dest, const void* src, size_t size) {
356 (*memcopy_uint8_function)(reinterpret_cast<uint8_t*>(dest),
357 reinterpret_cast<const uint8_t*>(src), size);
358 }
359 V8_INLINE void MemMove(void* dest, const void* src, size_t size) {
360 memmove(dest, src, size);
361 }
362
363 typedef void (*MemCopyUint16Uint8Function)(uint16_t* dest, const uint8_t* src,
364 size_t size);
365 extern MemCopyUint16Uint8Function memcopy_uint16_uint8_function;
366 void MemCopyUint16Uint8Wrapper(uint16_t* dest, const uint8_t* src,
367 size_t chars);
368 // For values < 12, the assembler function is slower than the inlined C code.
369 const int kMinComplexConvertMemCopy = 12;
370 V8_INLINE void MemCopyUint16Uint8(uint16_t* dest, const uint8_t* src,
371 size_t size) {
372 (*memcopy_uint16_uint8_function)(dest, src, size);
373 }
374 #elif defined(V8_HOST_ARCH_MIPS)
375 typedef void (*MemCopyUint8Function)(uint8_t* dest, const uint8_t* src,
376 size_t size);
377 extern MemCopyUint8Function memcopy_uint8_function;
378 V8_INLINE void MemCopyUint8Wrapper(uint8_t* dest, const uint8_t* src,
379 size_t chars) {
380 memcpy(dest, src, chars);
381 }
382 // For values < 16, the assembler function is slower than the inlined C code.
383 const int kMinComplexMemCopy = 16;
384 V8_INLINE void MemCopy(void* dest, const void* src, size_t size) {
385 (*memcopy_uint8_function)(reinterpret_cast<uint8_t*>(dest),
386 reinterpret_cast<const uint8_t*>(src), size);
387 }
388 V8_INLINE void MemMove(void* dest, const void* src, size_t size) {
389 memmove(dest, src, size);
390 }
391 #else
392 // Copy memory area to disjoint memory area.
393 V8_INLINE void MemCopy(void* dest, const void* src, size_t size) {
394 memcpy(dest, src, size);
395 }
396 V8_INLINE void MemMove(void* dest, const void* src, size_t size) {
397 memmove(dest, src, size);
398 }
399 const int kMinComplexMemCopy = 16 * kPointerSize;
400 #endif // V8_TARGET_ARCH_IA32
401
402
403 // ----------------------------------------------------------------------------
325 // Miscellaneous 404 // Miscellaneous
326 405
327 // A static resource holds a static instance that can be reserved in 406 // A static resource holds a static instance that can be reserved in
328 // a local scope using an instance of Access. Attempts to re-reserve 407 // a local scope using an instance of Access. Attempts to re-reserve
329 // the instance will cause an error. 408 // the instance will cause an error.
330 template <typename T> 409 template <typename T>
331 class StaticResource { 410 class StaticResource {
332 public: 411 public:
333 StaticResource() : is_reserved_(false) {} 412 StaticResource() : is_reserved_(false) {}
334 413
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
395 474
396 explicit EmbeddedVector(T initial_value) : Vector<T>(buffer_, kSize) { 475 explicit EmbeddedVector(T initial_value) : Vector<T>(buffer_, kSize) {
397 for (int i = 0; i < kSize; ++i) { 476 for (int i = 0; i < kSize; ++i) {
398 buffer_[i] = initial_value; 477 buffer_[i] = initial_value;
399 } 478 }
400 } 479 }
401 480
402 // When copying, make underlying Vector to reference our buffer. 481 // When copying, make underlying Vector to reference our buffer.
403 EmbeddedVector(const EmbeddedVector& rhs) 482 EmbeddedVector(const EmbeddedVector& rhs)
404 : Vector<T>(rhs) { 483 : Vector<T>(rhs) {
405 OS::MemCopy(buffer_, rhs.buffer_, sizeof(T) * kSize); 484 MemCopy(buffer_, rhs.buffer_, sizeof(T) * kSize);
406 set_start(buffer_); 485 set_start(buffer_);
407 } 486 }
408 487
409 EmbeddedVector& operator=(const EmbeddedVector& rhs) { 488 EmbeddedVector& operator=(const EmbeddedVector& rhs) {
410 if (this == &rhs) return *this; 489 if (this == &rhs) return *this;
411 Vector<T>::operator=(rhs); 490 Vector<T>::operator=(rhs);
412 OS::MemCopy(buffer_, rhs.buffer_, sizeof(T) * kSize); 491 MemCopy(buffer_, rhs.buffer_, sizeof(T) * kSize);
413 this->set_start(buffer_); 492 this->set_start(buffer_);
414 return *this; 493 return *this;
415 } 494 }
416 495
417 private: 496 private:
418 T buffer_[kSize]; 497 T buffer_[kSize];
419 }; 498 };
420 499
421 500
422 /* 501 /*
(...skipping 710 matching lines...) Expand 10 before | Expand all | Expand 10 after
1133 template <typename T> 1212 template <typename T>
1134 inline void CopyWords(T* dst, const T* src, size_t num_words) { 1213 inline void CopyWords(T* dst, const T* src, size_t num_words) {
1135 STATIC_ASSERT(sizeof(T) == kPointerSize); 1214 STATIC_ASSERT(sizeof(T) == kPointerSize);
1136 // TODO(mvstanton): disabled because mac builds are bogus failing on this 1215 // TODO(mvstanton): disabled because mac builds are bogus failing on this
1137 // assert. They are doing a signed comparison. Investigate in 1216 // assert. They are doing a signed comparison. Investigate in
1138 // the morning. 1217 // the morning.
1139 // ASSERT(Min(dst, const_cast<T*>(src)) + num_words <= 1218 // ASSERT(Min(dst, const_cast<T*>(src)) + num_words <=
1140 // Max(dst, const_cast<T*>(src))); 1219 // Max(dst, const_cast<T*>(src)));
1141 ASSERT(num_words > 0); 1220 ASSERT(num_words > 0);
1142 1221
1143 // Use block copying OS::MemCopy if the segment we're copying is 1222 // Use block copying MemCopy if the segment we're copying is
1144 // enough to justify the extra call/setup overhead. 1223 // enough to justify the extra call/setup overhead.
1145 static const size_t kBlockCopyLimit = 16; 1224 static const size_t kBlockCopyLimit = 16;
1146 1225
1147 if (num_words < kBlockCopyLimit) { 1226 if (num_words < kBlockCopyLimit) {
1148 do { 1227 do {
1149 num_words--; 1228 num_words--;
1150 *dst++ = *src++; 1229 *dst++ = *src++;
1151 } while (num_words > 0); 1230 } while (num_words > 0);
1152 } else { 1231 } else {
1153 OS::MemCopy(dst, src, num_words * kPointerSize); 1232 MemCopy(dst, src, num_words * kPointerSize);
1154 } 1233 }
1155 } 1234 }
1156 1235
1157 1236
1158 // Copies words from |src| to |dst|. No restrictions. 1237 // Copies words from |src| to |dst|. No restrictions.
1159 template <typename T> 1238 template <typename T>
1160 inline void MoveWords(T* dst, const T* src, size_t num_words) { 1239 inline void MoveWords(T* dst, const T* src, size_t num_words) {
1161 STATIC_ASSERT(sizeof(T) == kPointerSize); 1240 STATIC_ASSERT(sizeof(T) == kPointerSize);
1162 ASSERT(num_words > 0); 1241 ASSERT(num_words > 0);
1163 1242
1164 // Use block copying OS::MemCopy if the segment we're copying is 1243 // Use block copying MemCopy if the segment we're copying is
1165 // enough to justify the extra call/setup overhead. 1244 // enough to justify the extra call/setup overhead.
1166 static const size_t kBlockCopyLimit = 16; 1245 static const size_t kBlockCopyLimit = 16;
1167 1246
1168 if (num_words < kBlockCopyLimit && 1247 if (num_words < kBlockCopyLimit &&
1169 ((dst < src) || (dst >= (src + num_words * kPointerSize)))) { 1248 ((dst < src) || (dst >= (src + num_words * kPointerSize)))) {
1170 T* end = dst + num_words; 1249 T* end = dst + num_words;
1171 do { 1250 do {
1172 num_words--; 1251 num_words--;
1173 *dst++ = *src++; 1252 *dst++ = *src++;
1174 } while (num_words > 0); 1253 } while (num_words > 0);
1175 } else { 1254 } else {
1176 OS::MemMove(dst, src, num_words * kPointerSize); 1255 MemMove(dst, src, num_words * kPointerSize);
1177 } 1256 }
1178 } 1257 }
1179 1258
1180 1259
1181 // Copies data from |src| to |dst|. The data spans must not overlap. 1260 // Copies data from |src| to |dst|. The data spans must not overlap.
1182 template <typename T> 1261 template <typename T>
1183 inline void CopyBytes(T* dst, const T* src, size_t num_bytes) { 1262 inline void CopyBytes(T* dst, const T* src, size_t num_bytes) {
1184 STATIC_ASSERT(sizeof(T) == 1); 1263 STATIC_ASSERT(sizeof(T) == 1);
1185 ASSERT(Min(dst, const_cast<T*>(src)) + num_bytes <= 1264 ASSERT(Min(dst, const_cast<T*>(src)) + num_bytes <=
1186 Max(dst, const_cast<T*>(src))); 1265 Max(dst, const_cast<T*>(src)));
1187 if (num_bytes == 0) return; 1266 if (num_bytes == 0) return;
1188 1267
1189 // Use block copying OS::MemCopy if the segment we're copying is 1268 // Use block copying MemCopy if the segment we're copying is
1190 // enough to justify the extra call/setup overhead. 1269 // enough to justify the extra call/setup overhead.
1191 static const int kBlockCopyLimit = OS::kMinComplexMemCopy; 1270 static const int kBlockCopyLimit = kMinComplexMemCopy;
1192 1271
1193 if (num_bytes < static_cast<size_t>(kBlockCopyLimit)) { 1272 if (num_bytes < static_cast<size_t>(kBlockCopyLimit)) {
1194 do { 1273 do {
1195 num_bytes--; 1274 num_bytes--;
1196 *dst++ = *src++; 1275 *dst++ = *src++;
1197 } while (num_bytes > 0); 1276 } while (num_bytes > 0);
1198 } else { 1277 } else {
1199 OS::MemCopy(dst, src, num_bytes); 1278 MemCopy(dst, src, num_bytes);
1200 } 1279 }
1201 } 1280 }
1202 1281
1203 1282
1204 template <typename T, typename U> 1283 template <typename T, typename U>
1205 inline void MemsetPointer(T** dest, U* value, int counter) { 1284 inline void MemsetPointer(T** dest, U* value, int counter) {
1206 #ifdef DEBUG 1285 #ifdef DEBUG
1207 T* a = NULL; 1286 T* a = NULL;
1208 U* b = NULL; 1287 U* b = NULL;
1209 a = b; // Fake assignment to check assignability. 1288 a = b; // Fake assignment to check assignability.
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
1312 chars); 1391 chars);
1313 } 1392 }
1314 } 1393 }
1315 } 1394 }
1316 1395
1317 template <typename sourcechar, typename sinkchar> 1396 template <typename sourcechar, typename sinkchar>
1318 void CopyCharsUnsigned(sinkchar* dest, const sourcechar* src, int chars) { 1397 void CopyCharsUnsigned(sinkchar* dest, const sourcechar* src, int chars) {
1319 sinkchar* limit = dest + chars; 1398 sinkchar* limit = dest + chars;
1320 #ifdef V8_HOST_CAN_READ_UNALIGNED 1399 #ifdef V8_HOST_CAN_READ_UNALIGNED
1321 if (sizeof(*dest) == sizeof(*src)) { 1400 if (sizeof(*dest) == sizeof(*src)) {
1322 if (chars >= static_cast<int>(OS::kMinComplexMemCopy / sizeof(*dest))) { 1401 if (chars >= static_cast<int>(kMinComplexMemCopy / sizeof(*dest))) {
1323 OS::MemCopy(dest, src, chars * sizeof(*dest)); 1402 MemCopy(dest, src, chars * sizeof(*dest));
1324 return; 1403 return;
1325 } 1404 }
1326 // Number of characters in a uintptr_t. 1405 // Number of characters in a uintptr_t.
1327 static const int kStepSize = sizeof(uintptr_t) / sizeof(*dest); // NOLINT 1406 static const int kStepSize = sizeof(uintptr_t) / sizeof(*dest); // NOLINT
1328 ASSERT(dest + kStepSize > dest); // Check for overflow. 1407 ASSERT(dest + kStepSize > dest); // Check for overflow.
1329 while (dest + kStepSize <= limit) { 1408 while (dest + kStepSize <= limit) {
1330 *reinterpret_cast<uintptr_t*>(dest) = 1409 *reinterpret_cast<uintptr_t*>(dest) =
1331 *reinterpret_cast<const uintptr_t*>(src); 1410 *reinterpret_cast<const uintptr_t*>(src);
1332 dest += kStepSize; 1411 dest += kStepSize;
1333 src += kStepSize; 1412 src += kStepSize;
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
1384 case 13: 1463 case 13:
1385 memcpy(dest, src, 13); 1464 memcpy(dest, src, 13);
1386 break; 1465 break;
1387 case 14: 1466 case 14:
1388 memcpy(dest, src, 14); 1467 memcpy(dest, src, 14);
1389 break; 1468 break;
1390 case 15: 1469 case 15:
1391 memcpy(dest, src, 15); 1470 memcpy(dest, src, 15);
1392 break; 1471 break;
1393 default: 1472 default:
1394 OS::MemCopy(dest, src, chars); 1473 MemCopy(dest, src, chars);
1395 break; 1474 break;
1396 } 1475 }
1397 } 1476 }
1398 1477
1399 1478
1400 void CopyCharsUnsigned(uint16_t* dest, const uint8_t* src, int chars) { 1479 void CopyCharsUnsigned(uint16_t* dest, const uint8_t* src, int chars) {
1401 if (chars >= OS::kMinComplexConvertMemCopy) { 1480 if (chars >= kMinComplexConvertMemCopy) {
1402 OS::MemCopyUint16Uint8(dest, src, chars); 1481 MemCopyUint16Uint8(dest, src, chars);
1403 } else { 1482 } else {
1404 OS::MemCopyUint16Uint8Wrapper(dest, src, chars); 1483 MemCopyUint16Uint8Wrapper(dest, src, chars);
1405 } 1484 }
1406 } 1485 }
1407 1486
1408 1487
1409 void CopyCharsUnsigned(uint16_t* dest, const uint16_t* src, int chars) { 1488 void CopyCharsUnsigned(uint16_t* dest, const uint16_t* src, int chars) {
1410 switch (static_cast<unsigned>(chars)) { 1489 switch (static_cast<unsigned>(chars)) {
1411 case 0: 1490 case 0:
1412 break; 1491 break;
1413 case 1: 1492 case 1:
1414 *dest = *src; 1493 *dest = *src;
(...skipping 10 matching lines...) Expand all
1425 case 5: 1504 case 5:
1426 memcpy(dest, src, 10); 1505 memcpy(dest, src, 10);
1427 break; 1506 break;
1428 case 6: 1507 case 6:
1429 memcpy(dest, src, 12); 1508 memcpy(dest, src, 12);
1430 break; 1509 break;
1431 case 7: 1510 case 7:
1432 memcpy(dest, src, 14); 1511 memcpy(dest, src, 14);
1433 break; 1512 break;
1434 default: 1513 default:
1435 OS::MemCopy(dest, src, chars * sizeof(*dest)); 1514 MemCopy(dest, src, chars * sizeof(*dest));
1436 break; 1515 break;
1437 } 1516 }
1438 } 1517 }
1439 1518
1440 1519
1441 #elif defined(V8_HOST_ARCH_MIPS) 1520 #elif defined(V8_HOST_ARCH_MIPS)
1442 void CopyCharsUnsigned(uint8_t* dest, const uint8_t* src, int chars) { 1521 void CopyCharsUnsigned(uint8_t* dest, const uint8_t* src, int chars) {
1443 if (chars < OS::kMinComplexMemCopy) { 1522 if (chars < kMinComplexMemCopy) {
1444 memcpy(dest, src, chars); 1523 memcpy(dest, src, chars);
1445 } else { 1524 } else {
1446 OS::MemCopy(dest, src, chars); 1525 MemCopy(dest, src, chars);
1447 } 1526 }
1448 } 1527 }
1449 1528
1450 void CopyCharsUnsigned(uint16_t* dest, const uint16_t* src, int chars) { 1529 void CopyCharsUnsigned(uint16_t* dest, const uint16_t* src, int chars) {
1451 if (chars < OS::kMinComplexMemCopy) { 1530 if (chars < kMinComplexMemCopy) {
1452 memcpy(dest, src, chars * sizeof(*dest)); 1531 memcpy(dest, src, chars * sizeof(*dest));
1453 } else { 1532 } else {
1454 OS::MemCopy(dest, src, chars * sizeof(*dest)); 1533 MemCopy(dest, src, chars * sizeof(*dest));
1455 } 1534 }
1456 } 1535 }
1457 #endif 1536 #endif
1458 1537
1459 1538
1460 class StringBuilder : public SimpleStringBuilder { 1539 class StringBuilder : public SimpleStringBuilder {
1461 public: 1540 public:
1462 explicit StringBuilder(int size) : SimpleStringBuilder(size) { } 1541 explicit StringBuilder(int size) : SimpleStringBuilder(size) { }
1463 StringBuilder(char* buffer, int size) : SimpleStringBuilder(buffer, size) { } 1542 StringBuilder(char* buffer, int size) : SimpleStringBuilder(buffer, size) { }
1464 1543
1465 // Add formatted contents to the builder just like printf(). 1544 // Add formatted contents to the builder just like printf().
1466 void AddFormatted(const char* format, ...); 1545 void AddFormatted(const char* format, ...);
1467 1546
1468 // Add formatted contents like printf based on a va_list. 1547 // Add formatted contents like printf based on a va_list.
1469 void AddFormattedList(const char* format, va_list list); 1548 void AddFormattedList(const char* format, va_list list);
1470 private: 1549 private:
1471 DISALLOW_IMPLICIT_CONSTRUCTORS(StringBuilder); 1550 DISALLOW_IMPLICIT_CONSTRUCTORS(StringBuilder);
1472 }; 1551 };
1473 1552
1474 1553
1475 } } // namespace v8::internal 1554 } } // namespace v8::internal
1476 1555
1477 #endif // V8_UTILS_H_ 1556 #endif // V8_UTILS_H_
OLDNEW
« no previous file with comments | « src/unicode-inl.h ('k') | src/utils.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698