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

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

Powered by Google App Engine
This is Rietveld 408576698