| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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_ID_RING_H_ | 5 #ifndef VM_OBJECT_ID_RING_H_ |
| 6 #define VM_OBJECT_ID_RING_H_ | 6 #define VM_OBJECT_ID_RING_H_ |
| 7 | 7 |
| 8 namespace dart { | 8 namespace dart { |
| 9 | 9 |
| 10 // Forward declarations. | 10 // Forward declarations. |
| 11 class RawObject; | 11 class RawObject; |
| 12 class Isolate; | 12 class Isolate; |
| 13 class ObjectPointerVisitor; | 13 class ObjectPointerVisitor; |
| 14 class JSONStream; |
| 14 | 15 |
| 15 // A ring buffer of object pointers that have been given an id. An object | 16 // A ring buffer of object pointers that have been given an id. An object |
| 16 // may be pointed to by multiple ids. Objects contained in the ring will | 17 // may be pointed to by multiple ids. Objects contained in the ring will |
| 17 // be preserved across scavenges but not old space collections. | 18 // be preserved across scavenges but not old space collections. |
| 18 // When the ring buffer wraps around older objects will be replaced and their | 19 // When the ring buffer wraps around older objects will be replaced and their |
| 19 // ids will be invalidated. | 20 // ids will be invalidated. |
| 20 class ObjectIdRing { | 21 class ObjectIdRing { |
| 21 public: | 22 public: |
| 22 enum LookupResult { | 23 enum LookupResult { |
| 23 kValid = 0, | 24 kValid = 0, |
| 24 kInvalid, // Malformed ring id (used in service.cc). | 25 kInvalid, // Malformed ring id (used in service.cc). |
| 25 kCollected, // Entry was reclaimed due to a full GC (entries are weak). | 26 kCollected, // Entry was reclaimed due to a full GC (entries are weak). |
| 26 kExpired, // Entry was evicted during an insertion into a full ring. | 27 kExpired, // Entry was evicted during an insertion into a full ring. |
| 27 }; | 28 }; |
| 28 | 29 |
| 30 enum IdPolicy { |
| 31 kAllocateId, // Always allocate a new object id. |
| 32 kReuseId, // If the object is already in the ring, reuse id. |
| 33 // Otherwise allocate a new object id. |
| 34 kNumIdPolicy, |
| 35 }; |
| 36 |
| 29 static const int32_t kMaxId = 0x3FFFFFFF; | 37 static const int32_t kMaxId = 0x3FFFFFFF; |
| 30 static const int32_t kInvalidId = -1; | 38 static const int32_t kInvalidId = -1; |
| 31 static const int32_t kDefaultCapacity = 1024; | 39 static const int32_t kDefaultCapacity = 1024; |
| 32 | 40 |
| 33 static void Init(Isolate* isolate, int32_t capacity = kDefaultCapacity); | 41 static void Init(Isolate* isolate, int32_t capacity = kDefaultCapacity); |
| 34 | 42 |
| 35 ~ObjectIdRing(); | 43 ~ObjectIdRing(); |
| 36 | 44 |
| 37 // Adds the argument to the ring and returns its id. Note we do not allow | 45 // Adds the argument to the ring and returns its id. Note we do not allow |
| 38 // adding Object::null(). | 46 // adding Object::null(). |
| 39 int32_t GetIdForObject(RawObject* raw_obj); | 47 int32_t GetIdForObject(RawObject* raw_obj, IdPolicy policy = kAllocateId); |
| 40 | 48 |
| 41 // Returns Object::null() when the result is not kValid. | 49 // Returns Object::null() when the result is not kValid. |
| 42 RawObject* GetObjectForId(int32_t id, LookupResult* kind); | 50 RawObject* GetObjectForId(int32_t id, LookupResult* kind); |
| 43 | 51 |
| 44 void VisitPointers(ObjectPointerVisitor* visitor); | 52 void VisitPointers(ObjectPointerVisitor* visitor); |
| 45 | 53 |
| 54 void PrintJSON(JSONStream* js); |
| 55 |
| 46 private: | 56 private: |
| 47 friend class ObjectIdRingTestHelper; | 57 friend class ObjectIdRingTestHelper; |
| 48 | 58 |
| 49 void SetCapacityAndMaxSerial(int32_t capacity, int32_t max_serial); | 59 void SetCapacityAndMaxSerial(int32_t capacity, int32_t max_serial); |
| 60 int32_t FindExistingIdForObject(RawObject* raw_obj); |
| 50 | 61 |
| 51 ObjectIdRing(Isolate* isolate, int32_t capacity); | 62 ObjectIdRing(Isolate* isolate, int32_t capacity); |
| 52 Isolate* isolate_; | 63 Isolate* isolate_; |
| 53 RawObject** table_; | 64 RawObject** table_; |
| 54 int32_t max_serial_; | 65 int32_t max_serial_; |
| 55 int32_t capacity_; | 66 int32_t capacity_; |
| 56 int32_t serial_num_; | 67 int32_t serial_num_; |
| 57 bool wrapped_; | 68 bool wrapped_; |
| 58 | 69 |
| 59 RawObject** table() { | 70 RawObject** table() { |
| 60 return table_; | 71 return table_; |
| 61 } | 72 } |
| 62 int32_t table_size() { | 73 int32_t table_size() { |
| 63 return capacity_; | 74 return capacity_; |
| 64 } | 75 } |
| 65 | 76 |
| 66 int32_t NextSerial(); | 77 int32_t NextSerial(); |
| 67 int32_t AllocateNewId(RawObject* object); | 78 int32_t AllocateNewId(RawObject* object); |
| 68 int32_t IndexOfId(int32_t id); | 79 int32_t IndexOfId(int32_t id); |
| 80 int32_t IdOfIndex(int32_t index); |
| 69 bool IsValidContiguous(int32_t id); | 81 bool IsValidContiguous(int32_t id); |
| 70 bool IsValidId(int32_t id); | 82 bool IsValidId(int32_t id); |
| 71 | 83 |
| 72 DISALLOW_COPY_AND_ASSIGN(ObjectIdRing); | 84 DISALLOW_COPY_AND_ASSIGN(ObjectIdRing); |
| 73 }; | 85 }; |
| 74 | 86 |
| 75 } // namespace dart | 87 } // namespace dart |
| 76 | 88 |
| 77 #endif // VM_OBJECT_ID_RING_H_ | 89 #endif // VM_OBJECT_ID_RING_H_ |
| OLD | NEW |