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

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: fewer templates 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
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 3224 matching lines...) Expand 10 before | Expand all | Expand 10 after
3235 class ImmutableArray : public Array { 3235 class ImmutableArray : public Array {
3236 public: 3236 public:
3237 static RawImmutableArray* New(intptr_t len, Heap::Space space = Heap::kNew); 3237 static RawImmutableArray* New(intptr_t len, Heap::Space space = Heap::kNew);
3238 3238
3239 private: 3239 private:
3240 HEAP_OBJECT_IMPLEMENTATION(ImmutableArray, Array); 3240 HEAP_OBJECT_IMPLEMENTATION(ImmutableArray, Array);
3241 friend class Class; 3241 friend class Class;
3242 }; 3242 };
3243 3243
3244 3244
3245 class ByteBuffer : public Instance { 3245 class ByteArray : public Instance {
3246 public:
3247 virtual intptr_t Length() const;
3248
3249 private:
3250 HEAP_OBJECT_IMPLEMENTATION(ByteArray, Instance);
3251 friend class Class;
3252 };
3253
3254
3255 class InternalByteArray : public ByteArray {
3246 public: 3256 public:
3247 intptr_t Length() const { 3257 intptr_t Length() const {
3248 ASSERT(!IsNull()); 3258 ASSERT(!IsNull());
3249 return Smi::Value(raw_ptr()->length_); 3259 return Smi::Value(raw_ptr()->length_);
3250 } 3260 }
3251 3261
3262 static intptr_t length_offset() {
3263 return OFFSET_OF(RawInternalByteArray, length_);
3264 }
3265
3266 static intptr_t data_offset() {
3267 return length_offset() + kWordSize;
3268 }
3269
3252 template<typename T> 3270 template<typename T>
3253 T At(intptr_t byte_offset) const { 3271 T At(intptr_t byte_offset) const {
3254 T* addr = Addr<T>(byte_offset); 3272 T* addr = Addr<T>(byte_offset);
3255 ASSERT(Utils::IsAligned(reinterpret_cast<intptr_t>(addr), sizeof(T))); 3273 ASSERT(Utils::IsAligned(reinterpret_cast<intptr_t>(addr), sizeof(T)));
3256 return *addr; 3274 return *addr;
3257 } 3275 }
3276
3258 template<typename T> 3277 template<typename T>
3259 void SetAt(intptr_t byte_offset, T value) const { 3278 void SetAt(intptr_t byte_offset, T value) const {
3260 T* addr = Addr<T>(byte_offset); 3279 T* addr = Addr<T>(byte_offset);
3261 ASSERT(Utils::IsAligned(reinterpret_cast<intptr_t>(addr), sizeof(T))); 3280 ASSERT(Utils::IsAligned(reinterpret_cast<intptr_t>(addr), sizeof(T)));
3262 *addr = value; 3281 *addr = value;
3263 } 3282 }
3264 3283
3265 template<typename T> 3284 template<typename T>
3266 T UnalignedAt(intptr_t byte_offset) const { 3285 T UnalignedAt(intptr_t byte_offset) const {
3267 T result; 3286 T result;
3268 memmove(&result, Addr<T>(byte_offset), sizeof(T)); 3287 memmove(&result, Addr<T>(byte_offset), sizeof(T));
3269 return result; 3288 return result;
3270 } 3289 }
3290
3271 template<typename T> 3291 template<typename T>
3272 void SetUnalignedAt(intptr_t byte_offset, T value) const { 3292 void SetUnalignedAt(intptr_t byte_offset, T value) const {
3273 memmove(Addr<T>(byte_offset), &value, sizeof(T)); 3293 memmove(Addr<T>(byte_offset), &value, sizeof(T));
3274 } 3294 }
3275 3295
3276 virtual bool Equals(const Instance& other) const;
3277
3278 static intptr_t InstanceSize() { 3296 static intptr_t InstanceSize() {
3279 return RoundedAllocationSize(sizeof(RawByteBuffer)); 3297 ASSERT(sizeof(RawInternalByteArray) ==
3298 OFFSET_OF_RETURNED_VALUE(RawInternalByteArray, data));
3299 return 0;
3280 } 3300 }
3281 3301
3282 static RawByteBuffer* New(uint8_t* data, 3302 static intptr_t InstanceSize(intptr_t len) {
3283 intptr_t len, 3303 return RoundedAllocationSize(sizeof(RawInternalByteArray) + len);
3284 Heap::Space space = Heap::kNew); 3304 }
3305
3306 static RawInternalByteArray* New(intptr_t len,
3307 Heap::Space space = Heap::kNew);
3308 static RawInternalByteArray* New(const uint8_t* characters,
Ivan Posva 2012/01/25 23:49:57 characters?
cshapiro 2012/01/26 02:25:42 Well, they are characters in the C sense. That is
3309 intptr_t len,
3310 Heap::Space space = Heap::kNew);
3285 3311
3286 private: 3312 private:
3287 template<typename T> 3313 template<typename T>
3314 T* Addr(intptr_t byte_offset) const {
3315 intptr_t limit = byte_offset + sizeof(T);
3316 // TODO(iposva): Determine if we should throw an exception here.
3317 ASSERT((byte_offset >= 0) && (limit <= Length()));
3318 uint8_t* addr = &raw_ptr()->data()[byte_offset];
3319 return reinterpret_cast<T*>(addr);
3320 }
3321
3322 void SetLength(intptr_t value) {
3323 raw_ptr()->length_ = Smi::New(value);
3324 }
3325
3326 HEAP_OBJECT_IMPLEMENTATION(InternalByteArray, ByteArray);
3327 friend class Class;
3328 };
3329
3330
3331 class ExternalByteArray : public ByteArray {
3332 public:
3333 intptr_t Length() const {
3334 ASSERT(!IsNull());
3335 return Smi::Value(raw_ptr()->length_);
3336 }
3337
3338 template<typename T>
3339 T At(intptr_t byte_offset) const {
3340 T* addr = Addr<T>(byte_offset);
3341 ASSERT(Utils::IsAligned(reinterpret_cast<intptr_t>(addr), sizeof(T)));
3342 return *addr;
3343 }
3344
3345 template<typename T>
3346 void SetAt(intptr_t byte_offset, T value) const {
3347 T* addr = Addr<T>(byte_offset);
3348 ASSERT(Utils::IsAligned(reinterpret_cast<intptr_t>(addr), sizeof(T)));
3349 *addr = value;
3350 }
3351
3352 template<typename T>
3353 T UnalignedAt(intptr_t byte_offset) const {
3354 T result;
3355 memmove(&result, Addr<T>(byte_offset), sizeof(T));
3356 return result;
3357 }
3358
3359 template<typename T>
3360 void SetUnalignedAt(intptr_t byte_offset, T value) const {
3361 memmove(Addr<T>(byte_offset), &value, sizeof(T));
3362 }
3363
3364 static intptr_t InstanceSize() {
3365 return RoundedAllocationSize(sizeof(RawExternalByteArray));
3366 }
3367
3368 static RawExternalByteArray* New(uint8_t* data,
3369 intptr_t len,
3370 Heap::Space space = Heap::kNew);
3371
3372 private:
3373 template<typename T>
3288 T* Addr(intptr_t byte_offset) const { 3374 T* Addr(intptr_t byte_offset) const {
3289 intptr_t limit = byte_offset + sizeof(T); 3375 intptr_t limit = byte_offset + sizeof(T);
3290 // TODO(iposva): Determine if we should throw an exception here. 3376 // TODO(iposva): Determine if we should throw an exception here.
3291 ASSERT((byte_offset >= 0) && (limit <= Length())); 3377 ASSERT((byte_offset >= 0) && (limit <= Length()));
3292 uint8_t* addr = &raw_ptr()->data_[byte_offset]; 3378 uint8_t* addr = &raw_ptr()->data_[byte_offset];
3293 return reinterpret_cast<T*>(addr); 3379 return reinterpret_cast<T*>(addr);
3294 } 3380 }
3295 3381
3296 void SetLength(intptr_t value) { 3382 void SetLength(intptr_t value) {
3297 raw_ptr()->length_ = Smi::New(value); 3383 raw_ptr()->length_ = Smi::New(value);
3298 } 3384 }
3299 3385
3300 void SetData(uint8_t* data) const { 3386 void SetData(uint8_t* data) const {
3301 raw_ptr()->data_ = data; 3387 raw_ptr()->data_ = data;
3302 } 3388 }
3303 3389
3304 HEAP_OBJECT_IMPLEMENTATION(ByteBuffer, Instance); 3390 HEAP_OBJECT_IMPLEMENTATION(ExternalByteArray, ByteArray);
3305 friend class Class; 3391 friend class Class;
3306 }; 3392 };
3307 3393
3308 3394
3309 class Closure : public Instance { 3395 class Closure : public Instance {
3310 public: 3396 public:
3311 RawFunction* function() const { return raw_ptr()->function_; } 3397 RawFunction* function() const { return raw_ptr()->function_; }
3312 static intptr_t function_offset() { 3398 static intptr_t function_offset() {
3313 return OFFSET_OF(RawClosure, function_); 3399 return OFFSET_OF(RawClosure, function_);
3314 } 3400 }
(...skipping 197 matching lines...) Expand 10 before | Expand all | Expand 10 after
3512 } 3598 }
3513 3599
3514 3600
3515 void Context::SetAt(intptr_t index, const Instance& value) const { 3601 void Context::SetAt(intptr_t index, const Instance& value) const {
3516 StorePointer(InstanceAddr(index), value.raw()); 3602 StorePointer(InstanceAddr(index), value.raw());
3517 } 3603 }
3518 3604
3519 } // namespace dart 3605 } // namespace dart
3520 3606
3521 #endif // VM_OBJECT_H_ 3607 #endif // VM_OBJECT_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698