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

Side by Side Diff: runtime/vm/object.h

Issue 9195031: Add ByteArray interface and provide internal and external implementations. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: address remaining review comments Created 8 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 | « runtime/vm/dart_api_impl_test.cc ('k') | runtime/vm/object.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) 2012, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #ifndef VM_OBJECT_H_ 5 #ifndef VM_OBJECT_H_
6 #define VM_OBJECT_H_ 6 #define VM_OBJECT_H_
7 7
8 #include "include/dart_api.h" 8 #include "include/dart_api.h"
9 #include "platform/assert.h" 9 #include "platform/assert.h"
10 #include "platform/utils.h" 10 #include "platform/utils.h"
(...skipping 1694 matching lines...) Expand 10 before | Expand all | Expand 10 after
1705 intptr_t token_index) const; 1705 intptr_t token_index) const;
1706 1706
1707 // Resolving native methods for script loaded in the library. 1707 // Resolving native methods for script loaded in the library.
1708 Dart_NativeEntryResolver native_entry_resolver() const { 1708 Dart_NativeEntryResolver native_entry_resolver() const {
1709 return raw_ptr()->native_entry_resolver_; 1709 return raw_ptr()->native_entry_resolver_;
1710 } 1710 }
1711 void set_native_entry_resolver(Dart_NativeEntryResolver value) const { 1711 void set_native_entry_resolver(Dart_NativeEntryResolver value) const {
1712 raw_ptr()->native_entry_resolver_ = value; 1712 raw_ptr()->native_entry_resolver_ = value;
1713 } 1713 }
1714 1714
1715 RawString* PrivateName(const char* name);
1716
1715 void Register() const; 1717 void Register() const;
1716 1718
1717 RawLibrary* next_registered() const { return raw_ptr()->next_registered_; } 1719 RawLibrary* next_registered() const { return raw_ptr()->next_registered_; }
1718 1720
1719 static RawLibrary* LookupLibrary(const String& url); 1721 static RawLibrary* LookupLibrary(const String& url);
1720 static RawString* CheckForDuplicateDefinition(); 1722 static RawString* CheckForDuplicateDefinition();
1721 static bool IsKeyUsed(intptr_t key); 1723 static bool IsKeyUsed(intptr_t key);
1722 1724
1723 static void InitCoreLibrary(Isolate* isolate); 1725 static void InitCoreLibrary(Isolate* isolate);
1724 static RawLibrary* CoreLibrary(); 1726 static RawLibrary* CoreLibrary();
(...skipping 1510 matching lines...) Expand 10 before | Expand all | Expand 10 after
3235 class ImmutableArray : public Array { 3237 class ImmutableArray : public Array {
3236 public: 3238 public:
3237 static RawImmutableArray* New(intptr_t len, Heap::Space space = Heap::kNew); 3239 static RawImmutableArray* New(intptr_t len, Heap::Space space = Heap::kNew);
3238 3240
3239 private: 3241 private:
3240 HEAP_OBJECT_IMPLEMENTATION(ImmutableArray, Array); 3242 HEAP_OBJECT_IMPLEMENTATION(ImmutableArray, Array);
3241 friend class Class; 3243 friend class Class;
3242 }; 3244 };
3243 3245
3244 3246
3245 class ByteBuffer : public Instance { 3247 class ByteArray : public Instance {
3248 public:
3249 virtual intptr_t Length() const;
3250
3251 private:
3252 HEAP_OBJECT_IMPLEMENTATION(ByteArray, Instance);
3253 friend class Class;
3254 };
3255
3256
3257 class InternalByteArray : public ByteArray {
3246 public: 3258 public:
3247 intptr_t Length() const { 3259 intptr_t Length() const {
3248 ASSERT(!IsNull()); 3260 ASSERT(!IsNull());
3249 return Smi::Value(raw_ptr()->length_); 3261 return Smi::Value(raw_ptr()->length_);
3250 } 3262 }
3251 3263
3264 static intptr_t length_offset() {
3265 return OFFSET_OF(RawInternalByteArray, length_);
3266 }
3267
3268 static intptr_t data_offset() {
3269 return length_offset() + kWordSize;
3270 }
3271
3252 template<typename T> 3272 template<typename T>
3253 T At(intptr_t byte_offset) const { 3273 T At(intptr_t byte_offset) const {
3254 T* addr = Addr<T>(byte_offset); 3274 T* addr = Addr<T>(byte_offset);
3255 ASSERT(Utils::IsAligned(reinterpret_cast<intptr_t>(addr), sizeof(T))); 3275 ASSERT(Utils::IsAligned(reinterpret_cast<intptr_t>(addr), sizeof(T)));
3256 return *addr; 3276 return *addr;
3257 } 3277 }
3278
3258 template<typename T> 3279 template<typename T>
3259 void SetAt(intptr_t byte_offset, T value) const { 3280 void SetAt(intptr_t byte_offset, T value) const {
3260 T* addr = Addr<T>(byte_offset); 3281 T* addr = Addr<T>(byte_offset);
3261 ASSERT(Utils::IsAligned(reinterpret_cast<intptr_t>(addr), sizeof(T))); 3282 ASSERT(Utils::IsAligned(reinterpret_cast<intptr_t>(addr), sizeof(T)));
3262 *addr = value; 3283 *addr = value;
3263 } 3284 }
3264 3285
3265 template<typename T> 3286 template<typename T>
3266 T UnalignedAt(intptr_t byte_offset) const { 3287 T UnalignedAt(intptr_t byte_offset) const {
3267 T result; 3288 T result;
3268 memmove(&result, Addr<T>(byte_offset), sizeof(T)); 3289 memmove(&result, Addr<T>(byte_offset), sizeof(T));
3269 return result; 3290 return result;
3270 } 3291 }
3292
3271 template<typename T> 3293 template<typename T>
3272 void SetUnalignedAt(intptr_t byte_offset, T value) const { 3294 void SetUnalignedAt(intptr_t byte_offset, T value) const {
3273 memmove(Addr<T>(byte_offset), &value, sizeof(T)); 3295 memmove(Addr<T>(byte_offset), &value, sizeof(T));
3274 } 3296 }
3275 3297
3276 virtual bool Equals(const Instance& other) const;
3277
3278 static intptr_t InstanceSize() { 3298 static intptr_t InstanceSize() {
3279 return RoundedAllocationSize(sizeof(RawByteBuffer)); 3299 ASSERT(sizeof(RawInternalByteArray) ==
3300 OFFSET_OF_RETURNED_VALUE(RawInternalByteArray, data));
3301 return 0;
3280 } 3302 }
3281 3303
3282 static RawByteBuffer* New(uint8_t* data, 3304 static intptr_t InstanceSize(intptr_t len) {
3283 intptr_t len, 3305 return RoundedAllocationSize(sizeof(RawInternalByteArray) + len);
3284 Heap::Space space = Heap::kNew); 3306 }
3307
3308 static RawInternalByteArray* New(intptr_t len,
3309 Heap::Space space = Heap::kNew);
3310 static RawInternalByteArray* New(const uint8_t* data,
3311 intptr_t len,
3312 Heap::Space space = Heap::kNew);
3285 3313
3286 private: 3314 private:
3287 template<typename T> 3315 template<typename T>
3316 T* Addr(intptr_t byte_offset) const {
3317 intptr_t limit = byte_offset + sizeof(T);
3318 // TODO(iposva): Determine if we should throw an exception here.
3319 ASSERT((byte_offset >= 0) && (limit <= Length()));
3320 uint8_t* addr = &raw_ptr()->data()[byte_offset];
3321 return reinterpret_cast<T*>(addr);
3322 }
3323
3324 void SetLength(intptr_t value) {
3325 raw_ptr()->length_ = Smi::New(value);
3326 }
3327
3328 HEAP_OBJECT_IMPLEMENTATION(InternalByteArray, ByteArray);
3329 friend class Class;
3330 };
3331
3332
3333 class ExternalByteArray : public ByteArray {
3334 public:
3335 intptr_t Length() const {
3336 ASSERT(!IsNull());
3337 return Smi::Value(raw_ptr()->length_);
3338 }
3339
3340 template<typename T>
3341 T At(intptr_t byte_offset) const {
3342 T* addr = Addr<T>(byte_offset);
3343 ASSERT(Utils::IsAligned(reinterpret_cast<intptr_t>(addr), sizeof(T)));
3344 return *addr;
3345 }
3346
3347 template<typename T>
3348 void SetAt(intptr_t byte_offset, T value) const {
3349 T* addr = Addr<T>(byte_offset);
3350 ASSERT(Utils::IsAligned(reinterpret_cast<intptr_t>(addr), sizeof(T)));
3351 *addr = value;
3352 }
3353
3354 template<typename T>
3355 T UnalignedAt(intptr_t byte_offset) const {
3356 T result;
3357 memmove(&result, Addr<T>(byte_offset), sizeof(T));
3358 return result;
3359 }
3360
3361 template<typename T>
3362 void SetUnalignedAt(intptr_t byte_offset, T value) const {
3363 memmove(Addr<T>(byte_offset), &value, sizeof(T));
3364 }
3365
3366 static intptr_t InstanceSize() {
3367 return RoundedAllocationSize(sizeof(RawExternalByteArray));
3368 }
3369
3370 static RawExternalByteArray* New(uint8_t* data,
3371 intptr_t len,
3372 Heap::Space space = Heap::kNew);
3373
3374 private:
3375 template<typename T>
3288 T* Addr(intptr_t byte_offset) const { 3376 T* Addr(intptr_t byte_offset) const {
3289 intptr_t limit = byte_offset + sizeof(T); 3377 intptr_t limit = byte_offset + sizeof(T);
3290 // TODO(iposva): Determine if we should throw an exception here. 3378 // TODO(iposva): Determine if we should throw an exception here.
3291 ASSERT((byte_offset >= 0) && (limit <= Length())); 3379 ASSERT((byte_offset >= 0) && (limit <= Length()));
3292 uint8_t* addr = &raw_ptr()->data_[byte_offset]; 3380 uint8_t* addr = &raw_ptr()->data_[byte_offset];
3293 return reinterpret_cast<T*>(addr); 3381 return reinterpret_cast<T*>(addr);
3294 } 3382 }
3295 3383
3296 void SetLength(intptr_t value) { 3384 void SetLength(intptr_t value) {
3297 raw_ptr()->length_ = Smi::New(value); 3385 raw_ptr()->length_ = Smi::New(value);
3298 } 3386 }
3299 3387
3300 void SetData(uint8_t* data) const { 3388 void SetData(uint8_t* data) const {
3301 raw_ptr()->data_ = data; 3389 raw_ptr()->data_ = data;
3302 } 3390 }
3303 3391
3304 HEAP_OBJECT_IMPLEMENTATION(ByteBuffer, Instance); 3392 HEAP_OBJECT_IMPLEMENTATION(ExternalByteArray, ByteArray);
3305 friend class Class; 3393 friend class Class;
3306 }; 3394 };
3307 3395
3308 3396
3309 class Closure : public Instance { 3397 class Closure : public Instance {
3310 public: 3398 public:
3311 RawFunction* function() const { return raw_ptr()->function_; } 3399 RawFunction* function() const { return raw_ptr()->function_; }
3312 static intptr_t function_offset() { 3400 static intptr_t function_offset() {
3313 return OFFSET_OF(RawClosure, function_); 3401 return OFFSET_OF(RawClosure, function_);
3314 } 3402 }
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after
3512 } 3600 }
3513 3601
3514 3602
3515 void Context::SetAt(intptr_t index, const Instance& value) const { 3603 void Context::SetAt(intptr_t index, const Instance& value) const {
3516 StorePointer(InstanceAddr(index), value.raw()); 3604 StorePointer(InstanceAddr(index), value.raw());
3517 } 3605 }
3518 3606
3519 } // namespace dart 3607 } // namespace dart
3520 3608
3521 #endif // VM_OBJECT_H_ 3609 #endif // VM_OBJECT_H_
OLDNEW
« no previous file with comments | « runtime/vm/dart_api_impl_test.cc ('k') | runtime/vm/object.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698