@@ -144,14 +144,47 @@ void AddToLRUList(BufferDesc *buf)
144
144
145
145
/*
146
146
* RemoveFromLRUList()
147
+ * Remove item on list depending on where they are since nodes before them weren't removed
148
+ * due to being pinned
147
149
*/
148
- void RemoveFromLRUList ()
150
+ void RemoveFromLRUList (int trycounter )
149
151
{
150
152
LRUListNode * node ;
153
+ int loop ;
151
154
Assert (LRUList != NULL );
155
+
152
156
node = StrategyControl -> LRUListHead ;
153
- StrategyControl -> LRUListHead = node -> next ;
154
- StrategyControl -> LRUListHead -> prev = NULL ;
157
+ loop = NBuffers - trycounter ;
158
+
159
+ while (loop > 0 ) {
160
+ node = node -> next ;
161
+ loop -- ;
162
+ }
163
+
164
+ // delete the only element
165
+ if (node -> prev == NULL && node -> next == NULL )
166
+ {
167
+ StrategyControl -> LRUListHead = NULL ;
168
+ StrategyControl -> LRUListTail = NULL ;
169
+ }
170
+ // delete head
171
+ else if (node -> prev == NULL && node -> next != NULL )
172
+ {
173
+ StrategyControl -> LRUListHead = node -> next ;
174
+ StrategyControl -> LRUListHead -> prev = NULL ;
175
+ }
176
+ // delete tail
177
+ else if (node -> prev != NULL && node -> next == NULL )
178
+ {
179
+ StrategyControl -> LRUListTail = node -> prev ;
180
+ StrategyControl -> LRUListTail -> next = NULL ;
181
+ }
182
+ // middle elements
183
+ else
184
+ {
185
+ node -> prev -> next = node -> next ;
186
+ node -> next -> prev = node -> prev ;
187
+ }
155
188
}
156
189
157
190
/*
@@ -198,10 +231,22 @@ Random(void)
198
231
return GetBufferDescriptor (victim );
199
232
}
200
233
234
+ /*
235
+ * LRU - Helper routine for StrategyGetBuffer()
236
+ * Return in the order they are on the list but don't remove yet because it might be pinned
237
+ */
201
238
static inline BufferDesc *
202
- LRU (void )
239
+ LRU (int trycounter )
203
240
{
241
+ int loop = NBuffers - trycounter ;
204
242
LRUListNode * node = StrategyControl -> LRUListHead ;
243
+
244
+ while (loop > 0 )
245
+ {
246
+ node = node -> next ;
247
+ loop -- ;
248
+ }
249
+
205
250
return node -> nodeId ;
206
251
}
207
252
@@ -294,7 +339,7 @@ BufferDesc *doEviction(uint32 local_buf_state, BufferAccessStrategy strategy, ui
294
339
295
340
for (;;)
296
341
{
297
- buf = LRU ();
342
+ buf = LRU (trycounter );
298
343
299
344
/*
300
345
* If the buffer is pinned or has a nonzero usage_count, we cannot use
@@ -328,7 +373,7 @@ BufferDesc *doEviction(uint32 local_buf_state, BufferAccessStrategy strategy, ui
328
373
AddBufferToRing (strategy , buf );
329
374
if (LRU_EVICTION )
330
375
{
331
- RemoveFromLRUList ();
376
+ RemoveFromLRUList (trycounter );
332
377
}
333
378
}
334
379
* buf_state = local_buf_state ;
0 commit comments