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

Side by Side Diff: include/v8.h

Issue 6119009: Wrap external pointers more carefully. (Closed) Base URL: https://v8.googlecode.com/svn/branches/bleeding_edge
Patch Set: Next round Created 9 years, 11 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 | « no previous file | src/api.cc » ('j') | test/cctest/test-api.cc » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2007-2009 the V8 project authors. All rights reserved. 1 // Copyright 2007-2009 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 3335 matching lines...) Expand 10 before | Expand all | Expand 10 after
3346 // Tag information for HeapObject. 3346 // Tag information for HeapObject.
3347 const int kHeapObjectTag = 1; 3347 const int kHeapObjectTag = 1;
3348 const int kHeapObjectTagSize = 2; 3348 const int kHeapObjectTagSize = 2;
3349 const intptr_t kHeapObjectTagMask = (1 << kHeapObjectTagSize) - 1; 3349 const intptr_t kHeapObjectTagMask = (1 << kHeapObjectTagSize) - 1;
3350 3350
3351 // Tag information for Smi. 3351 // Tag information for Smi.
3352 const int kSmiTag = 0; 3352 const int kSmiTag = 0;
3353 const int kSmiTagSize = 1; 3353 const int kSmiTagSize = 1;
3354 const intptr_t kSmiTagMask = (1 << kSmiTagSize) - 1; 3354 const intptr_t kSmiTagMask = (1 << kSmiTagSize) - 1;
3355 3355
3356 template <size_t ptr_size> struct SmiConstants; 3356 template <size_t ptr_size> struct SmiTraits;
Lasse Reichstein 2011/01/13 12:57:57 I think I would prefer "SmiTagging", but it's bett
antonm 2011/01/13 15:49:15 Done.
3357 3357
3358 // Smi constants for 32-bit systems. 3358 // Smi constants for 32-bit systems.
3359 template <> struct SmiConstants<4> { 3359 template <> struct SmiTraits<4> {
3360 static const int kSmiShiftSize = 0; 3360 static const int kSmiShiftSize = 0;
3361 static const int kSmiValueSize = 31; 3361 static const int kSmiValueSize = 31;
3362 static inline int SmiToInt(internal::Object* value) { 3362 static inline int SmiToInt(internal::Object* value) {
3363 int shift_bits = kSmiTagSize + kSmiShiftSize; 3363 int shift_bits = kSmiTagSize + kSmiShiftSize;
3364 // Throw away top 32 bits and shift down (requires >> to be sign extending). 3364 // Throw away top 32 bits and shift down (requires >> to be sign extending).
3365 return static_cast<int>(reinterpret_cast<intptr_t>(value)) >> shift_bits; 3365 return static_cast<int>(reinterpret_cast<intptr_t>(value)) >> shift_bits;
3366 } 3366 }
3367
3368 // For 32-bit systems any 2 bytes aligned pointer can be encoded as smi
3369 // with a plain reinterpret_cast.
3370 static const intptr_t kEncodablePointerMask = 0x1;
3371 static const int kPointerToSmiShift = 0;
3367 }; 3372 };
3368 3373
3369 // Smi constants for 64-bit systems. 3374 // Smi constants for 64-bit systems.
3370 template <> struct SmiConstants<8> { 3375 template <> struct SmiTraits<8> {
3371 static const int kSmiShiftSize = 31; 3376 static const int kSmiShiftSize = 31;
3372 static const int kSmiValueSize = 32; 3377 static const int kSmiValueSize = 32;
3373 static inline int SmiToInt(internal::Object* value) { 3378 static inline int SmiToInt(internal::Object* value) {
3374 int shift_bits = kSmiTagSize + kSmiShiftSize; 3379 int shift_bits = kSmiTagSize + kSmiShiftSize;
3375 // Shift down and throw away top 32 bits. 3380 // Shift down and throw away top 32 bits.
3376 return static_cast<int>(reinterpret_cast<intptr_t>(value) >> shift_bits); 3381 return static_cast<int>(reinterpret_cast<intptr_t>(value) >> shift_bits);
3377 } 3382 }
3383
3384 // This is a trade-off: pointer can be encoded if it fits into
3385 // 32 bits, so to cover bigger range we need to require better
3386 // alignment. Currently we require 8 bytes alignment (which
Lasse Reichstein 2011/01/13 12:57:57 Drop "better" alignment (the text should be stand-
antonm 2011/01/13 15:49:15 Done.
3387 // should be the most common case) and hence we have 2 ^ (32 + 3) =
3388 // 32G address space covered. Alas, it might be not enough to
3389 // cover stack allocated objects on some platforms.
3390 static const int kPointerAlignment = 3;
3391
3392 static const intptr_t kEncodablePointerMask =
3393 intptr_t(0xffffffff) << kPointerAlignment;
Lasse Reichstein 2011/01/13 12:57:57 Shouldn't this be inverted to mask the bits that m
antonm 2011/01/13 15:49:15 Thanks a lot, of course it should.
3394
3395 static const int kPointerToSmiShift =
3396 kSmiTagSize + kSmiShiftSize - kPointerAlignment;
3378 }; 3397 };
3379 3398
3380 const int kSmiShiftSize = SmiConstants<kApiPointerSize>::kSmiShiftSize; 3399 typedef SmiTraits<kApiPointerSize> PlatformSmiTraits;
3381 const int kSmiValueSize = SmiConstants<kApiPointerSize>::kSmiValueSize; 3400 const int kSmiShiftSize = PlatformSmiTraits::kSmiShiftSize;
3401 const int kSmiValueSize = PlatformSmiTraits::kSmiValueSize;
3402 const intptr_t kEncodablePointerMask = PlatformSmiTraits::kEncodablePointerMask;
3403 const int kPointerToSmiShift = PlatformSmiTraits::kPointerToSmiShift;
3382 3404
3383 template <size_t ptr_size> struct InternalConstants; 3405 template <size_t ptr_size> struct InternalConstants;
3384 3406
3385 // Internal constants for 32-bit systems. 3407 // Internal constants for 32-bit systems.
3386 template <> struct InternalConstants<4> { 3408 template <> struct InternalConstants<4> {
3387 static const int kStringResourceOffset = 3 * kApiPointerSize; 3409 static const int kStringResourceOffset = 3 * kApiPointerSize;
3388 }; 3410 };
3389 3411
3390 // Internal constants for 64-bit systems. 3412 // Internal constants for 64-bit systems.
3391 template <> struct InternalConstants<8> { 3413 template <> struct InternalConstants<8> {
(...skipping 27 matching lines...) Expand all
3419 static inline bool HasHeapObjectTag(internal::Object* value) { 3441 static inline bool HasHeapObjectTag(internal::Object* value) {
3420 return ((reinterpret_cast<intptr_t>(value) & kHeapObjectTagMask) == 3442 return ((reinterpret_cast<intptr_t>(value) & kHeapObjectTagMask) ==
3421 kHeapObjectTag); 3443 kHeapObjectTag);
3422 } 3444 }
3423 3445
3424 static inline bool HasSmiTag(internal::Object* value) { 3446 static inline bool HasSmiTag(internal::Object* value) {
3425 return ((reinterpret_cast<intptr_t>(value) & kSmiTagMask) == kSmiTag); 3447 return ((reinterpret_cast<intptr_t>(value) & kSmiTagMask) == kSmiTag);
3426 } 3448 }
3427 3449
3428 static inline int SmiValue(internal::Object* value) { 3450 static inline int SmiValue(internal::Object* value) {
3429 return SmiConstants<kApiPointerSize>::SmiToInt(value); 3451 return PlatformSmiTraits::SmiToInt(value);
3430 } 3452 }
3431 3453
3432 static inline int GetInstanceType(internal::Object* obj) { 3454 static inline int GetInstanceType(internal::Object* obj) {
3433 typedef internal::Object O; 3455 typedef internal::Object O;
3434 O* map = ReadField<O*>(obj, kHeapObjectMapOffset); 3456 O* map = ReadField<O*>(obj, kHeapObjectMapOffset);
3435 return ReadField<uint8_t>(map, kMapInstanceTypeOffset); 3457 return ReadField<uint8_t>(map, kMapInstanceTypeOffset);
3436 } 3458 }
3437 3459
3460 static inline void* GetExternalPointerFromSmi(internal::Object* value) {
3461 const intptr_t address = reinterpret_cast<intptr_t>(value);
3462 return reinterpret_cast<void*>(address >> kPointerToSmiShift);
3463 }
3464
3438 static inline void* GetExternalPointer(internal::Object* obj) { 3465 static inline void* GetExternalPointer(internal::Object* obj) {
3439 if (HasSmiTag(obj)) { 3466 if (HasSmiTag(obj)) {
3440 return obj; 3467 return GetExternalPointerFromSmi(obj);
3441 } else if (GetInstanceType(obj) == kProxyType) { 3468 } else if (GetInstanceType(obj) == kProxyType) {
3442 return ReadField<void*>(obj, kProxyProxyOffset); 3469 return ReadField<void*>(obj, kProxyProxyOffset);
3443 } else { 3470 } else {
3444 return NULL; 3471 return NULL;
3445 } 3472 }
3446 } 3473 }
3447 3474
3448 static inline bool IsExternalTwoByteString(int instance_type) { 3475 static inline bool IsExternalTwoByteString(int instance_type) {
3449 int representation = (instance_type & kFullStringRepresentationMask); 3476 int representation = (instance_type & kFullStringRepresentationMask);
3450 return representation == kExternalTwoByteRepresentationTag; 3477 return representation == kExternalTwoByteRepresentationTag;
(...skipping 346 matching lines...) Expand 10 before | Expand all | Expand 10 after
3797 3824
3798 3825
3799 } // namespace v8 3826 } // namespace v8
3800 3827
3801 3828
3802 #undef V8EXPORT 3829 #undef V8EXPORT
3803 #undef TYPE_CHECK 3830 #undef TYPE_CHECK
3804 3831
3805 3832
3806 #endif // V8_H_ 3833 #endif // V8_H_
OLDNEW
« no previous file with comments | « no previous file | src/api.cc » ('j') | test/cctest/test-api.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698