includes: sort includes again
[gem5.git] / src / base / res_list.hh
1 /*
2 * Copyright (c) 2001-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Steve Raasch
29 * Nathan Binkert
30 */
31
32 #ifndef __RES_LIST_HH__
33 #define __RES_LIST_HH__
34
35 #include <cassert>
36
37 #include "base/cprintf.hh"
38
39 #define DEBUG_REMOVE 0
40
41 #define DEBUG_MEMORY 0
42 //#define DEBUG_MEMORY DEBUG
43
44 class res_list_base
45 {
46 #if DEBUG_MEMORY
47 protected:
48 static long long allocated_elements;
49 static long long allocated_lists;
50
51 public:
52 long long get_elements(void) {
53 return allocated_elements;
54 }
55 long long get_lists(void) {
56 return allocated_lists;
57 }
58
59 #endif
60 };
61
62 #if DEBUG_MEMORY
63 extern void what_the(void);
64 #endif
65
66 template<class T>
67 class res_list : public res_list_base
68 {
69 public:
70 class iterator;
71
72 class res_element
73 {
74 res_element *next;
75 res_element *prev;
76 T *data;
77 bool allocate_data;
78
79 public:
80 // always adds to the END of the list
81 res_element(res_element *_prev, bool allocate);
82 ~res_element();
83 void dump(void);
84
85 friend class res_list<T>;
86 friend class res_list<T>::iterator;
87 };
88
89 class iterator
90 {
91 private:
92 res_element *p;
93
94 friend class res_list<T>;
95
96 public:
97 // Constructors
98 iterator(res_element *q) : p(q) {}
99 iterator(void) { p=0; };
100
101 void dump(void);
102 T* data_ptr(void);
103 res_element *res_el_ptr(void) { return p;}
104 void point_to(T &d) { p->data = &d; }
105
106 iterator next(void) { return iterator(p->next); }
107 iterator prev(void) { return iterator(p->prev); }
108 bool operator== (iterator x) { return (x.p == this->p); }
109 bool operator != (iterator x) { return (x.p != this->p); }
110 T &operator * (void) { return *(p->data); }
111 T* operator -> (void) { return p->data; }
112 bool isnull(void) { return (p==0); }
113 bool notnull(void) { return (p!=0); }
114 };
115
116 private:
117 iterator unused_elements;
118 iterator head_ptr;
119 iterator tail_ptr;
120
121 unsigned base_elements;
122 unsigned extra_elements;
123 unsigned active_elements;
124 bool allocate_storage;
125 unsigned build_size;
126
127 int remove_count;
128
129 //
130 // Allocate new elements, and assign them to the unused_elements
131 // list.
132 //
133 unsigned allocate_elements(unsigned num, bool allocate_storage);
134
135 public:
136 //
137 // List Constructor
138 //
139 res_list(unsigned size, bool alloc_storage = false,
140 unsigned build_sz = 5);
141
142 //
143 // List Destructor
144 //
145 ~res_list();
146
147 iterator head(void) {return head_ptr;};
148 iterator tail(void) {return tail_ptr;};
149
150 unsigned num_free(void) { return size() - count(); }
151 unsigned size(void) { return base_elements + extra_elements; }
152 unsigned count(void) { return active_elements; }
153 bool empty(void) { return count() == 0; }
154 bool full(void);
155
156 //
157 // Insert with data copy
158 //
159 iterator insert_after(iterator prev, T *d);
160 iterator insert_after(iterator prev, T &d);
161 iterator insert_before(iterator prev, T *d);
162 iterator insert_before(iterator prev, T &d);
163
164 //
165 // Insert new list element (no data copy)
166 //
167 iterator insert_after(iterator prev);
168 iterator insert_before(iterator prev);
169
170 iterator add_tail(T *d) { return insert_after(tail_ptr, d); }
171 iterator add_tail(T &d) { return insert_after(tail_ptr, d); }
172 iterator add_tail(void) { return insert_after(tail_ptr); }
173 iterator add_head(T *d) { return insert_before(head_ptr, d); }
174 iterator add_head(T &d) { return insert_before(head_ptr, d); }
175 iterator add_head(void) { return insert_before(head_ptr); }
176
177 iterator remove(iterator q);
178 iterator remove_head(void) {return remove(head_ptr);}
179 iterator remove_tail(void) {return remove(tail_ptr);}
180
181 bool in_list(iterator j);
182 void free_extras(void);
183 void clear(void);
184 void dump(void);
185 void raw_dump(void);
186 };
187
188 template <class T>
189 inline
190 res_list<T>::res_element::res_element(res_element *_prev, bool allocate)
191 {
192 allocate_data = allocate;
193 prev = _prev;
194 next = 0;
195
196 if (prev)
197 prev->next = this;
198
199 if (allocate)
200 data = new T;
201 else
202 data = 0;
203
204 #if DEBUG_MEMORY
205 ++allocated_elements;
206 #endif
207 }
208
209 template <class T>
210 inline
211 res_list<T>::res_element::~res_element(void)
212 {
213 if (prev)
214 prev->next = next;
215
216 if (next)
217 next->prev = prev;
218
219 if (allocate_data)
220 delete data;
221
222 #if DEBUG_MEMORY
223 --allocated_elements;
224 #endif
225 }
226
227 template <class T>
228 inline void
229 res_list<T>::res_element::dump(void)
230 {
231 cprintf(" prev = %#x\n", prev);
232 cprintf(" next = %#x\n", next);
233 cprintf(" data = %#x\n", data);
234 }
235
236 template <class T>
237 inline void
238 res_list<T>::iterator::dump(void)
239 {
240 if (p && p->data)
241 p->data->dump();
242 else {
243 if (!p)
244 cprintf(" Null Pointer\n");
245 else
246 cprintf(" Null 'data' Pointer\n");
247 }
248 }
249
250 template <class T>
251 inline T *
252 res_list<T>::iterator::data_ptr(void)
253 {
254 if (p)
255 return p->data;
256 else
257 return 0;
258 }
259
260
261 //
262 // Allocate new elements, and assign them to the unused_elements
263 // list.
264 //
265 template <class T>
266 inline unsigned
267 res_list<T>::allocate_elements(unsigned num, bool allocate_storage)
268 {
269 res_element *pnew, *plast = 0, *pfirst=0;
270
271 for (int i=0; i<num; ++i) {
272 pnew = new res_element(plast, allocate_storage);
273 if (i==0)
274 pfirst = pnew;
275 plast = pnew;
276 }
277
278 if (unused_elements.notnull()) {
279 // Add these new elements to the front of the list
280 plast->next = unused_elements.res_el_ptr();
281 unused_elements.res_el_ptr()->prev = plast;
282 }
283
284 unused_elements = iterator(pfirst);
285
286 return num;
287 }
288
289 template <class T>
290 inline
291 res_list<T>::res_list(unsigned size, bool alloc_storage, unsigned build_sz)
292 {
293 #if DEBUG_MEMORY
294 ++allocated_lists;
295 #endif
296 extra_elements = 0;
297 active_elements = 0;
298 build_size = build_sz;
299 allocate_storage = alloc_storage;
300 remove_count = 0;
301
302 // Create the new elements
303 base_elements = allocate_elements(size, alloc_storage);
304
305 // The list of active elements
306 head_ptr = iterator(0);
307 tail_ptr = iterator(0);
308 }
309
310 //
311 // List Destructor
312 //
313 template <class T>
314 inline
315 res_list<T>::~res_list(void)
316 {
317 iterator n;
318
319 #if DEBUG_MEMORY
320 --allocated_lists;
321 #endif
322
323 // put everything into the unused list
324 clear();
325
326 // rudely delete all the res_elements
327 for (iterator p = unused_elements;
328 p.notnull();
329 p = n) {
330
331 n = p.next();
332
333 // delete the res_element
334 // (it will take care of deleting the data)
335 delete p.res_el_ptr();
336 }
337 }
338
339 template <class T>
340 inline bool
341 res_list<T>::full(void)
342 {
343 if (build_size)
344 return false;
345 else
346 return unused_elements.isnull();
347 }
348
349 //
350 // Insert with data copy
351 //
352 template <class T>
353 inline typename res_list<T>::iterator
354 res_list<T>::insert_after(iterator prev, T *d)
355 {
356 iterator p;
357
358 if (!allocate_storage)
359 this->panic("Can't copy data... not allocating storage");
360
361 p = insert_after(prev);
362 if (p.notnull())
363 *p = *d;
364
365 return p;
366 }
367
368
369 template <class T>
370 inline typename res_list<T>::iterator
371 res_list<T>::insert_after(iterator prev, T &d)
372 {
373 iterator p;
374
375 p = insert_after(prev);
376 if (p.notnull()) {
377
378 if (allocate_storage) {
379 // if we allocate storage, then copy the contents of the
380 // specified object to our object
381 *p = d;
382 }
383 else {
384 // if we don't allocate storage, then we just want to
385 // point to the specified object
386 p.point_to(d);
387 }
388 }
389
390 return p;
391 }
392
393
394 template <class T>
395 inline typename res_list<T>::iterator
396 res_list<T>::insert_after(iterator prev)
397 {
398
399 #if DEBUG_MEMORY
400 if (active_elements > 2*base_elements) {
401 what_the();
402 }
403 #endif
404
405 // If we have no unused elements, make some more
406 if (unused_elements.isnull()) {
407
408 if (build_size == 0) {
409 return 0; // No space left, and can't allocate more....
410 }
411
412 extra_elements += allocate_elements(build_size, allocate_storage);
413 }
414
415 // grab the first unused element
416 res_element *p = unused_elements.res_el_ptr();
417
418 unused_elements = unused_elements.next();
419
420 ++active_elements;
421
422 // Insert the new element
423 if (head_ptr.isnull()) {
424 //
425 // Special case #1: Empty List
426 //
427 head_ptr = p;
428 tail_ptr = p;
429 p->prev = 0;
430 p->next = 0;
431 }
432 else if (prev.isnull()) {
433 //
434 // Special case #2: Insert at head
435 //
436
437 // our next ptr points to old head element
438 p->next = head_ptr.res_el_ptr();
439
440 // our element becomes the new head element
441 head_ptr = p;
442
443 // no previous element for the head
444 p->prev = 0;
445
446 // old head element points back to this element
447 p->next->prev = p;
448 }
449 else if (prev.next().isnull()) {
450 //
451 // Special case #3 Insert at tail
452 //
453
454 // our prev pointer points to old tail element
455 p->prev = tail_ptr.res_el_ptr();
456
457 // our element becomes the new tail
458 tail_ptr = p;
459
460 // no next element for the tail
461 p->next = 0;
462
463 // old tail element point to this element
464 p->prev->next = p;
465 }
466 else {
467 //
468 // Normal insertion (after prev)
469 //
470 p->prev = prev.res_el_ptr();
471 p->next = prev.next().res_el_ptr();
472
473 prev.res_el_ptr()->next = p;
474 p->next->prev = p;
475 }
476
477 return iterator(p);
478 }
479
480 template <class T>
481 inline typename res_list<T>::iterator
482 res_list<T>::insert_before(iterator next, T &d)
483 {
484 iterator p;
485
486 p = insert_before(next);
487 if (p.notnull()) {
488
489 if (allocate_storage) {
490 // if we allocate storage, then copy the contents of the
491 // specified object to our object
492 *p = d;
493 }
494 else {
495 // if we don't allocate storage, then we just want to
496 // point to the specified object
497 p.point_to(d);
498 }
499 }
500
501 return p;
502 }
503
504
505 template <class T>
506 inline typename res_list<T>::iterator
507 res_list<T>::insert_before(iterator next)
508 {
509
510 #if DEBUG_MEMORY
511 if (active_elements > 2*base_elements) {
512 what_the();
513 }
514 #endif
515
516 // If we have no unused elements, make some more
517 if (unused_elements.isnull()) {
518
519 if (build_size == 0) {
520 return 0; // No space left, and can't allocate more....
521 }
522
523 extra_elements += allocate_elements(build_size, allocate_storage);
524 }
525
526 // grab the first unused element
527 res_element *p = unused_elements.res_el_ptr();
528
529 unused_elements = unused_elements.next();
530
531 ++active_elements;
532
533 // Insert the new element
534 if (head_ptr.isnull()) {
535 //
536 // Special case #1: Empty List
537 //
538 head_ptr = p;
539 tail_ptr = p;
540 p->prev = 0;
541 p->next = 0;
542 }
543 else if (next.isnull()) {
544 //
545 // Special case #2 Insert at tail
546 //
547
548 // our prev pointer points to old tail element
549 p->prev = tail_ptr.res_el_ptr();
550
551 // our element becomes the new tail
552 tail_ptr = p;
553
554 // no next element for the tail
555 p->next = 0;
556
557 // old tail element point to this element
558 p->prev->next = p;
559 }
560 else if (next.prev().isnull()) {
561 //
562 // Special case #3: Insert at head
563 //
564
565 // our next ptr points to old head element
566 p->next = head_ptr.res_el_ptr();
567
568 // our element becomes the new head element
569 head_ptr = p;
570
571 // no previous element for the head
572 p->prev = 0;
573
574 // old head element points back to this element
575 p->next->prev = p;
576 }
577 else {
578 //
579 // Normal insertion (before next)
580 //
581 p->next = next.res_el_ptr();
582 p->prev = next.prev().res_el_ptr();
583
584 next.res_el_ptr()->prev = p;
585 p->prev->next = p;
586 }
587
588 return iterator(p);
589 }
590
591
592 template <class T>
593 inline typename res_list<T>::iterator
594 res_list<T>::remove(iterator q)
595 {
596 res_element *p = q.res_el_ptr();
597 iterator n = 0;
598
599 // Handle the special cases
600 if (active_elements == 1) { // This is the only element
601 head_ptr = 0;
602 tail_ptr = 0;
603 }
604 else if (q == head_ptr) { // This is the head element
605 head_ptr = q.next();
606 head_ptr.res_el_ptr()->prev = 0;
607
608 n = head_ptr;
609 }
610 else if (q == tail_ptr) { // This is the tail element
611 tail_ptr = q.prev();
612 tail_ptr.res_el_ptr()->next = 0;
613 }
614 else { // This is between two elements
615 p->prev->next = p->next;
616 p->next->prev = p->prev;
617
618 // Get the "next" element for return
619 n = p->next;
620 }
621
622 --active_elements;
623
624 // Put this element back onto the unused list
625 p->next = unused_elements.res_el_ptr();
626 p->prev = 0;
627 if (p->next) { // NULL if unused list is empty
628 p->next->prev = p;
629 }
630
631 if (!allocate_storage) {
632 p->data = 0;
633 }
634
635 unused_elements = q;
636
637 // A little "garbage collection"
638 if (++remove_count > 10) {
639 // free_extras();
640 remove_count = 0;
641 }
642
643 #if DEBUG_REMOVE
644 unsigned unused_count = 0;
645 for (iterator i=unused_elements;
646 i.notnull();
647 i = i.next()) {
648
649 ++unused_count;
650 }
651
652 assert((active_elements+unused_count) == (base_elements+extra_elements));
653 #endif
654
655 return iterator(n);
656 }
657
658
659 template <class T>
660 inline bool
661 res_list<T>::in_list(iterator j)
662 {
663 iterator i;
664
665 for (i=head(); i.notnull(); i=i.next()) {
666 if (j.res_el_ptr() == i.res_el_ptr()) {
667 return true;
668 }
669 }
670
671 return false;
672 }
673
674 template <class T>
675 inline void
676 res_list<T>::free_extras(void)
677 {
678 unsigned num_unused = base_elements + extra_elements - active_elements;
679 unsigned to_free = extra_elements;
680 res_element *p;
681
682
683 if (extra_elements != 0) {
684 //
685 // Free min(extra_elements, # unused elements)
686 //
687 if (extra_elements > num_unused) {
688 to_free = num_unused;
689 }
690
691 p = unused_elements.res_el_ptr();
692 for (int i=0; i<to_free; ++i) {
693 res_element *q = p->next;
694
695 delete p;
696
697 p = q;
698 }
699
700 // update the unused element pointer to point to the first
701 // element that wasn't deleted.
702 unused_elements = iterator(p);
703
704 // Update the number of extra elements
705 extra_elements -= to_free;
706 }
707
708 return;
709 }
710
711
712 template <class T>
713 inline void
714 res_list<T>::clear(void)
715 {
716 iterator i,n;
717
718 for (i=head_ptr; i.notnull(); i=n) {
719 n = i.next();
720 remove(i);
721 }
722
723 free_extras();
724 }
725
726 template <class T>
727 inline void
728 res_list<T>::dump(void)
729 {
730 for (iterator i=head(); !i.isnull(); i=i.next())
731 i->dump();
732 }
733
734 template <class T>
735 inline void
736 res_list<T>::raw_dump(void)
737 {
738 int j = 0;
739 res_element *p;
740 for (iterator i=head(); !i.isnull(); i=i.next()) {
741 cprintf("Element %d:\n", j);
742
743 if (i.notnull()) {
744 p = i.res_el_ptr();
745 cprintf(" points to res_element @ %#x\n", p);
746 p->dump();
747 cprintf(" Data Element:\n");
748 i->dump();
749 }
750 else {
751 cprintf(" NULL iterator!\n");
752 }
753
754 ++j;
755 }
756
757 }
758
759 #endif // __RES_LIST_HH__