OLD | NEW |
---|---|
1 // Copyright 2012 the V8 project authors. All rights reserved. | 1 // Copyright 2012 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 1121 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1132 | 1132 |
1133 // Every FunctionState starts with this id. | 1133 // Every FunctionState starts with this id. |
1134 static const int kFirstUsableId = 4; | 1134 static const int kFirstUsableId = 4; |
1135 | 1135 |
1136 // Every compiled stub starts with this id. | 1136 // Every compiled stub starts with this id. |
1137 static const int kStubEntryId = 5; | 1137 static const int kStubEntryId = 5; |
1138 | 1138 |
1139 int id_; | 1139 int id_; |
1140 }; | 1140 }; |
1141 | 1141 |
1142 | |
1143 template <typename T> | |
1144 class CircularBuffer { | |
titzer
2013/10/10 09:41:57
This class needs some unit tests.
| |
1145 public: | |
1146 explicit CircularBuffer(int capacity) : capacity_(capacity), cursor_(0) { | |
1147 ASSERT_LT(0, capacity); | |
1148 buffer_ = NewArray<T>(capacity_); | |
1149 for (int i = 0; i < capacity_; i++) buffer_[i] = NULL; | |
1150 } | |
1151 | |
1152 ~CircularBuffer() { | |
1153 #ifdef DEBUG | |
1154 for (int i = 0; i < capacity_; i++) CHECK_EQ(NULL, buffer_[i]); | |
1155 #endif | |
1156 DeleteArray(buffer_); | |
1157 buffer_ = NULL; | |
1158 } | |
1159 | |
1160 T Get(int i) { | |
1161 ASSERT_LE(0, i); | |
1162 ASSERT_LT(i, capacity_); | |
1163 return buffer_[i]; | |
1164 } | |
1165 | |
1166 T Remove(int i) { | |
1167 T evicted = buffer_[i]; | |
1168 buffer_[i] = NULL; | |
1169 return evicted; | |
1170 } | |
1171 | |
1172 // Add an item to the cursor position and advance the cursor. Return | |
1173 // the object pointer being overwritten. | |
1174 T Add(T item) { | |
1175 T evicted = buffer_[cursor_]; | |
1176 buffer_[cursor_] = item; | |
1177 AdvanceCursor(); | |
1178 return evicted; | |
1179 } | |
1180 | |
1181 T Current() { | |
1182 return buffer_[cursor_]; | |
1183 } | |
1184 | |
1185 void AdvanceCursor() { | |
1186 cursor_ = (cursor_ + 1) % capacity_; | |
1187 } | |
1188 | |
1189 int capacity() { return capacity_; } | |
1190 | |
1191 private: | |
1192 int capacity_; | |
1193 int cursor_; | |
1194 T* buffer_; | |
1195 }; | |
1196 | |
1197 | |
1198 template <typename T> | |
1199 class CircularQueue { | |
titzer
2013/10/10 09:41:57
And this one too.
| |
1200 public: | |
1201 explicit CircularQueue(int capacity) | |
1202 : capacity_(capacity), length_(0), shift_(0) { | |
1203 ASSERT_LT(0, capacity); | |
1204 buffer_ = NewArray<T>(capacity_); | |
1205 #ifdef DEBUG | |
1206 for (int i = 0; i < capacity_; i++) buffer_[i] = NULL; | |
1207 #endif // DEBUG | |
1208 } | |
1209 | |
1210 ~CircularQueue() { | |
1211 ASSERT_EQ(0, length_); | |
1212 DeleteArray(buffer_); | |
1213 buffer_ = NULL; | |
1214 } | |
1215 | |
1216 int length() { return length_; } | |
1217 bool available() { return length_ < capacity_; } | |
1218 | |
1219 void Enqueue(const T& item) { | |
1220 ASSERT(available()); | |
1221 int index = ConvertIndex(length_); | |
1222 buffer_[index] = item; | |
1223 length_++; | |
1224 } | |
1225 | |
1226 void Prepend(const T& item) { | |
1227 ASSERT(available()); | |
1228 int index = ConvertIndex(capacity_ - 1); // Move shift_ back by one. | |
1229 shift_ = index; | |
1230 buffer_[index] = item; | |
1231 length_++; | |
1232 } | |
1233 | |
1234 T Dequeue() { | |
1235 ASSERT_LT(0, length_); | |
1236 int index = ConvertIndex(0); | |
1237 shift_ = ConvertIndex(1); | |
1238 length_--; | |
1239 T result = buffer_[index]; | |
1240 #ifdef DEBUG | |
1241 ASSERT_NE(NULL, result); | |
1242 buffer_[index] = NULL; | |
1243 #endif // DEBUG | |
1244 return result; | |
1245 } | |
1246 | |
1247 private: | |
1248 // Convert access index to backing store index. | |
1249 int ConvertIndex(int i) { | |
1250 int result = (i + shift_) % capacity_; | |
1251 ASSERT_LE(0, result); | |
1252 ASSERT_LT(result, capacity_); | |
1253 return result; | |
1254 } | |
1255 | |
1256 int capacity_; | |
1257 int length_; | |
1258 int shift_; | |
1259 T* buffer_; | |
1260 }; | |
1261 | |
1142 } } // namespace v8::internal | 1262 } } // namespace v8::internal |
1143 | 1263 |
1144 #endif // V8_UTILS_H_ | 1264 #endif // V8_UTILS_H_ |
OLD | NEW |