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

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

Issue 2683633005: Add exception handler cache to isolate (Closed)
Patch Set: Add exception handler cache to isolate Created 3 years, 10 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
OLDNEW
(Empty)
1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file
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.
4
5 #ifndef RUNTIME_VM_FIXED_CACHE_H_
6 #define RUNTIME_VM_FIXED_CACHE_H_
7
8 #include <stddef.h>
9 #include <stdint.h>
10
11 namespace dart {
12
13 /*
14 A simple sorted fixed size Key-Value storage.
15
16 Assumes both Key and Value are POD-like objects.
17
18 Keys must be comparable with operator<.
19
20 Duplicates are no allowed - check with Lookup before insertion.
Florian Schneider 2017/02/08 21:34:05 s/no/not/
Dmitry Olshansky 2017/02/09 17:54:34 Done.
21
22 Optionally Values may have cleanup function to delete
23 any resources they point to.
24 */
25 template <class K, class V, intptr_t kSize>
26 class FixedCache {
27 public:
28 typedef void (*Deleter)(V*);
29
30 struct Entry {
31 K key;
32 V value;
33 };
34
35 explicit FixedCache(Deleter deleter = NULL) : deleter_(deleter), length_(0) {}
36
37 V* Lookup(K key) {
38 intptr_t i = LowerBound(key);
39 if (i != length_ && pairs_[i].key == key) return &pairs_[i].value;
40 return NULL;
41 }
42
43 void Insert(K key, V value) {
44 intptr_t i = LowerBound(key);
45
46 if (length_ == kSize) {
47 if (deleter_) deleter_(&pairs_[length_ - 1].value);
48 length_ = kSize - 1;
49 if (i == kSize) i = kSize - 1;
50 }
51
52 for (intptr_t j = length_; j-- > i;) {
Florian Schneider 2017/02/08 21:34:05 Can you rewrite this without the update in the tes
Dmitry Olshansky 2017/02/09 17:54:34 Done.
53 pairs_[j + 1] = pairs_[j];
54 }
55
56 length_ += 1;
57 pairs_[i].key = key;
58 pairs_[i].value = value;
59 }
60
61 void Clear() {
62 if (deleter_) {
63 for (intptr_t i = 0; i < length_; i++) {
64 deleter_(&pairs_[i].value);
65 }
66 }
67 length_ = 0;
68 }
69
70 ~FixedCache() { Clear(); }
71
72 private:
73 intptr_t LowerBound(K key) {
74 intptr_t low = 0, high = length_;
75 while (low != high) {
76 intptr_t mid = low + (high - low) / 2;
77 if (key < pairs_[mid].key) {
78 high = mid;
79 } else if (key > pairs_[mid].key) {
80 low = mid + 1;
81 } else {
82 low = high = mid;
83 }
84 }
85 return low;
86 }
87 Entry pairs_[kSize]; // Sorted array of pairs.
88 Deleter deleter_;
89 intptr_t length_;
90 };
91
92 } // namespace dart
93
94 #endif // RUNTIME_VM_FIXED_CACHE_H_
OLDNEW
« no previous file with comments | « runtime/vm/exceptions.h ('k') | runtime/vm/heap.cc » ('j') | runtime/vm/heap.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698