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 kNewId, // Always allocate a new object id. | |
turnidge
2015/05/11 19:48:45
kAllocateId?
Cutch
2015/05/12 14:59:57
Done.
| |
32 kExistingOrNewId, // If the object is already in the ring, reuse id. | |
turnidge
2015/05/11 19:48:45
kReuseId?
Cutch
2015/05/12 14:59:57
Done.
| |
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 = kNewId); |
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 PrintGetObjectRequestsToJSON(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_; |
65 int32_t* ids_; | |
turnidge
2015/05/11 19:48:45
I think we may be able to drop the ids_ array. Le
| |
54 int32_t max_serial_; | 66 int32_t max_serial_; |
55 int32_t capacity_; | 67 int32_t capacity_; |
56 int32_t serial_num_; | 68 int32_t serial_num_; |
57 bool wrapped_; | 69 bool wrapped_; |
58 | 70 |
59 RawObject** table() { | 71 RawObject** table() { |
60 return table_; | 72 return table_; |
61 } | 73 } |
62 int32_t table_size() { | 74 int32_t table_size() { |
63 return capacity_; | 75 return capacity_; |
64 } | 76 } |
65 | 77 |
66 int32_t NextSerial(); | 78 int32_t NextSerial(); |
67 int32_t AllocateNewId(RawObject* object); | 79 int32_t AllocateNewId(RawObject* object); |
68 int32_t IndexOfId(int32_t id); | 80 int32_t IndexOfId(int32_t id); |
81 int32_t IdOfIndex(int32_t index); | |
69 bool IsValidContiguous(int32_t id); | 82 bool IsValidContiguous(int32_t id); |
70 bool IsValidId(int32_t id); | 83 bool IsValidId(int32_t id); |
71 | 84 |
72 DISALLOW_COPY_AND_ASSIGN(ObjectIdRing); | 85 DISALLOW_COPY_AND_ASSIGN(ObjectIdRing); |
73 }; | 86 }; |
74 | 87 |
75 } // namespace dart | 88 } // namespace dart |
76 | 89 |
77 #endif // VM_OBJECT_ID_RING_H_ | 90 #endif // VM_OBJECT_ID_RING_H_ |
OLD | NEW |