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

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

Issue 2683633005: Add exception handler cache to isolate (Closed)
Patch Set: s/HandlerInfo/ExceptionHandlerInfo 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
« no previous file with comments | « runtime/vm/exceptions.h ('k') | runtime/vm/fixed_cache_test.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 // A simple sorted fixed size Key-Value storage.
14 //
15 // Assumes both Key and Value are POD-like objects.
16 //
17 // Keys must be comparable with operator<.
18 //
19 // Duplicates are not allowed - check with Lookup before insertion.
20 //
21 // Optionally Values may have cleanup function to delete
22 // any resources they point to.
23 template <class K, class V, intptr_t kCapacity>
24 class FixedCache {
25 public:
26 typedef void (*Deleter)(V*);
27
28 struct Entry {
29 K key;
30 V value;
31 };
32
33 explicit FixedCache(Deleter deleter = NULL) : deleter_(deleter), length_(0) {}
34
35 ~FixedCache() { Clear(); }
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_ == kCapacity) {
47 if (deleter_) deleter_(&pairs_[length_ - 1].value);
48 length_ = kCapacity - 1;
49 if (i == kCapacity) i = kCapacity - 1;
50 }
51
52 for (intptr_t j = length_ - 1; j >= i; j--) {
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 private:
71 intptr_t LowerBound(K key) {
72 intptr_t low = 0, high = length_;
73 while (low != high) {
74 intptr_t mid = low + (high - low) / 2;
75 if (key < pairs_[mid].key) {
76 high = mid;
77 } else if (key > pairs_[mid].key) {
78 low = mid + 1;
79 } else {
80 low = high = mid;
81 }
82 }
83 return low;
84 }
85
86 Entry pairs_[kCapacity]; // Sorted array of pairs.
87 Deleter deleter_;
88 intptr_t length_;
89 };
90
91 } // namespace dart
92
93 #endif // RUNTIME_VM_FIXED_CACHE_H_
OLDNEW
« no previous file with comments | « runtime/vm/exceptions.h ('k') | runtime/vm/fixed_cache_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698