summaryrefslogtreecommitdiff
path: root/base/res_list.hh
blob: 04c3b7cfa36223071e1298c911a4b69a60faa01d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
/*
 * Copyright (c) 2003 The Regents of The University of Michigan
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met: redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer;
 * redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution;
 * neither the name of the copyright holders nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef __RES_LIST_HH__
#define __RES_LIST_HH__

#include "base/cprintf.hh"
#include <assert.h>

#define DEBUG_REMOVE 0

#define DEBUG_MEMORY 0
//#define DEBUG_MEMORY DEBUG

class res_list_base
{
#if DEBUG_MEMORY
  protected:
    static long long allocated_elements;
    static long long allocated_lists;

  public:
    long long get_elements(void) {
        return allocated_elements;
    }
    long long get_lists(void) {
        return allocated_lists;
    }

#endif
};

#if DEBUG_MEMORY
extern void what_the(void);
#endif

template<class T>
class res_list : public res_list_base
{
  public:
    class iterator;

    class res_element
    {
        res_element *next;
        res_element *prev;
        T *data;
        bool allocate_data;

      public:
        //  always adds to the END of the list
        res_element(res_element *_prev, bool allocate);
        ~res_element();
        void dump(void);

        friend class res_list<T>;
        friend class res_list<T>::iterator;
    };

    class iterator
    {
      private:
        res_element *p;

        friend class res_list<T>;

      public:
        // Constructors
        iterator(res_element *q) : p(q) {}
        iterator(void) { p=0; };

        void dump(void);
        T* data_ptr(void);
        res_element *res_el_ptr(void) { return p;}
        void point_to(T &d) { p->data = &d; }

        iterator next(void) { return iterator(p->next); }
        iterator prev(void) { return iterator(p->prev); }
        bool operator== (iterator x) { return (x.p == this->p); }
        bool operator != (iterator x) { return (x.p != this->p); }
        T &operator * (void) { return *(p->data); }
        T* operator -> (void) { return p->data; }
        bool isnull(void) { return (p==0); }
        bool notnull(void) { return (p!=0); }
    };

  private:
    iterator unused_elements;
    iterator head_ptr;
    iterator tail_ptr;

    unsigned base_elements;
    unsigned extra_elements;
    unsigned active_elements;
    bool     allocate_storage;
    unsigned build_size;

    int remove_count;

    //
    //  Allocate new elements, and assign them to the unused_elements
    //  list.
    //
    unsigned allocate_elements(unsigned num, bool allocate_storage);

  public:
    //
    //  List Constructor
    //
    res_list(unsigned size, bool alloc_storage = false,
             unsigned build_sz = 5);

    //
    //  List Destructor
    //
    ~res_list();

    iterator head(void) {return head_ptr;};
    iterator tail(void) {return tail_ptr;};

    unsigned num_free(void) { return size() - count(); }
    unsigned size(void) { return base_elements + extra_elements; }
    unsigned count(void) { return active_elements; }
    bool empty(void) { return count() == 0; }
    bool full(void);

    //
    //  Insert with data copy
    //
    iterator insert_after(iterator prev, T *d);
    iterator insert_after(iterator prev, T &d);
    iterator insert_before(iterator prev, T *d);
    iterator insert_before(iterator prev, T &d);

    //
    //  Insert new list element (no data copy)
    //
    iterator insert_after(iterator prev);
    iterator insert_before(iterator prev);

    iterator add_tail(T *d) { return insert_after(tail_ptr, d); }
    iterator add_tail(T &d) { return insert_after(tail_ptr, d); }
    iterator add_tail(void) { return insert_after(tail_ptr); }
    iterator add_head(T *d) { return insert_before(head_ptr, d); }
    iterator add_head(T &d) { return insert_before(head_ptr, d); }
    iterator add_head(void) { return insert_before(head_ptr); }

    iterator remove(iterator q);
    iterator remove_head(void) {return remove(head_ptr);}
    iterator remove_tail(void) {return remove(tail_ptr);}

    bool in_list(iterator j);
    void free_extras(void);
    void clear(void);
    void dump(void);
    void raw_dump(void);
};

template <class T>
inline
res_list<T>::res_element::res_element(res_element *_prev, bool allocate)
{
    allocate_data = allocate;
    prev = _prev;
    next = 0;

    if (prev)
        prev->next = this;

    if (allocate)
        data = new T;
    else
        data = 0;

#if DEBUG_MEMORY
    ++allocated_elements;
#endif
}

template <class T>
inline
res_list<T>::res_element::~res_element(void)
{
    if (prev)
        prev->next = next;

    if (next)
        next->prev = prev;

    if (allocate_data)
        delete data;

#if DEBUG_MEMORY
    --allocated_elements;
#endif
}

template <class T>
inline void
res_list<T>::res_element::dump(void)
{
    cprintf("  prev = %#x\n", prev);
    cprintf("  next = %#x\n", next);
    cprintf("  data = %#x\n", data);
}

template <class T>
inline void
res_list<T>::iterator::dump(void)
{
    if (p && p->data)
        p->data->dump();
    else {
        if (!p)
            cprintf("  Null Pointer\n");
        else
            cprintf("  Null 'data' Pointer\n");
    }
}

template <class T>
inline T *
res_list<T>::iterator::data_ptr(void)
{
    if (p)
        return p->data;
    else
        return 0;
}


//
//  Allocate new elements, and assign them to the unused_elements
//  list.
//
template <class T>
inline unsigned
res_list<T>::allocate_elements(unsigned num, bool allocate_storage)
{
    res_element *pnew, *plast = 0, *pfirst=0;

    for (int i=0; i<num; ++i) {
        pnew = new res_element(plast, allocate_storage);
        if (i==0)
            pfirst = pnew;
        plast = pnew;
    }

    if (unused_elements.notnull()) {
        //  Add these new elements to the front of the list
        plast->next = unused_elements.res_el_ptr();
        unused_elements.res_el_ptr()->prev = plast;
    }

    unused_elements = iterator(pfirst);

    return num;
}

template <class T>
inline
res_list<T>::res_list(unsigned size, bool alloc_storage, unsigned build_sz)
{
#if DEBUG_MEMORY
    ++allocated_lists;
#endif
    extra_elements = 0;
    active_elements = 0;
    build_size = build_sz;
    allocate_storage = alloc_storage;
    remove_count = 0;

    //  Create the new elements
    base_elements = allocate_elements(size, alloc_storage);

    //  The list of active elements
    head_ptr = iterator(0);
    tail_ptr = iterator(0);
}

//
//  List Destructor
//
template <class T>
inline
res_list<T>::~res_list(void)
{
    iterator n;

#if DEBUG_MEMORY
    --allocated_lists;
#endif

    //  put everything into the unused list
    clear();

    //  rudely delete all the res_elements
    for (iterator p = unused_elements;
         p.notnull();
         p = n) {

        n = p.next();

        //  delete the res_element
        //  (it will take care of deleting the data)
        delete p.res_el_ptr();
    }
}

template <class T>
inline bool
res_list<T>::full(void)
{
    if (build_size)
        return false;
    else
        return unused_elements.isnull();
}

//
//  Insert with data copy
//
template <class T>
inline typename res_list<T>::iterator
res_list<T>::insert_after(iterator prev, T *d)
{
    iterator p;

    if (!allocate_storage)
        panic("Can't copy data... not allocating storage");

    p = insert_after(prev);
    if (p.notnull())
        *p = *d;

    return p;
}


template <class T>
inline typename res_list<T>::iterator
res_list<T>::insert_after(iterator prev, T &d)
{
    iterator p;

    p = insert_after(prev);
    if (p.notnull()) {

        if (allocate_storage) {
            //  if we allocate storage, then copy the contents of the
            //  specified object to our object
            *p = d;
        }
        else {
            //  if we don't allocate storage, then we just want to
            //  point to the specified object
            p.point_to(d);
        }
    }

    return p;
}


template <class T>
inline typename res_list<T>::iterator
res_list<T>::insert_after(iterator prev)
{

#if DEBUG_MEMORY
    if (active_elements > 2*base_elements) {
        what_the();
    }
#endif

    //  If we have no unused elements, make some more
    if (unused_elements.isnull()) {

        if (build_size == 0) {
            return 0;   // No space left, and can't allocate more....
        }

        extra_elements += allocate_elements(build_size, allocate_storage);
    }

    //  grab the first unused element
    res_element *p = unused_elements.res_el_ptr();

    unused_elements = unused_elements.next();

    ++active_elements;

    //  Insert the new element
    if (head_ptr.isnull()) {
        //
        //  Special case #1: Empty List
        //
        head_ptr = p;
        tail_ptr = p;
        p->prev = 0;
        p->next = 0;
    }
    else if (prev.isnull()) {
        //
        //  Special case #2: Insert at head
        //

        // our next ptr points to old head element
        p->next = head_ptr.res_el_ptr();

        // our element becomes the new head element
        head_ptr = p;

        // no previous element for the head
        p->prev = 0;

        // old head element points back to this element
        p->next->prev = p;
    }
    else if (prev.next().isnull()) {
        //
        //  Special case #3 Insert at tail
        //

        // our prev pointer points to old tail element
        p->prev = tail_ptr.res_el_ptr();

        // our element becomes the new tail
        tail_ptr = p;

        // no next element for the tail
        p->next = 0;

        // old tail element point to this element
        p->prev->next = p;
    }
    else {
        //
        //  Normal insertion (after prev)
        //
        p->prev = prev.res_el_ptr();
        p->next = prev.next().res_el_ptr();

        prev.res_el_ptr()->next = p;
        p->next->prev = p;
    }

    return iterator(p);
}

template <class T>
inline typename res_list<T>::iterator
res_list<T>::insert_before(iterator next, T &d)
{
    iterator p;

    p = insert_before(next);
    if (p.notnull()) {

        if (allocate_storage) {
            //  if we allocate storage, then copy the contents of the
            //  specified object to our object
            *p = d;
        }
        else {
            //  if we don't allocate storage, then we just want to
            //  point to the specified object
            p.point_to(d);
        }
    }

    return p;
}


template <class T>
inline typename res_list<T>::iterator
res_list<T>::insert_before(iterator next)
{

#if DEBUG_MEMORY
    if (active_elements > 2*base_elements) {
        what_the();
    }
#endif

    //  If we have no unused elements, make some more
    if (unused_elements.isnull()) {

        if (build_size == 0) {
            return 0;   // No space left, and can't allocate more....
        }

        extra_elements += allocate_elements(build_size, allocate_storage);
    }

    //  grab the first unused element
    res_element *p = unused_elements.res_el_ptr();

    unused_elements = unused_elements.next();

    ++active_elements;

    //  Insert the new element
    if (head_ptr.isnull()) {
        //
        //  Special case #1: Empty List
        //
        head_ptr = p;
        tail_ptr = p;
        p->prev = 0;
        p->next = 0;
    }
    else if (next.isnull()) {
        //
        //  Special case #2 Insert at tail
        //

        // our prev pointer points to old tail element
        p->prev = tail_ptr.res_el_ptr();

        // our element becomes the new tail
        tail_ptr = p;

        // no next element for the tail
        p->next = 0;

        // old tail element point to this element
        p->prev->next = p;
    }
    else if (next.prev().isnull()) {
        //
        //  Special case #3: Insert at head
        //

        // our next ptr points to old head element
        p->next = head_ptr.res_el_ptr();

        // our element becomes the new head element
        head_ptr = p;

        // no previous element for the head
        p->prev = 0;

        // old head element points back to this element
        p->next->prev = p;
    }
    else {
        //
        //  Normal insertion (before next)
        //
        p->next = next.res_el_ptr();
        p->prev = next.prev().res_el_ptr();

        next.res_el_ptr()->prev = p;
        p->prev->next = p;
    }

    return iterator(p);
}


template <class T>
inline typename res_list<T>::iterator
res_list<T>::remove(iterator q)
{
    res_element *p = q.res_el_ptr();
    iterator n = 0;

    //  Handle the special cases
    if (active_elements == 1) {    // This is the only element
        head_ptr = 0;
        tail_ptr = 0;
    }
    else if (q == head_ptr) {      // This is the head element
        head_ptr = q.next();
        head_ptr.res_el_ptr()->prev = 0;

        n = head_ptr;
    }
    else if (q == tail_ptr) {      // This is the tail element
        tail_ptr = q.prev();
        tail_ptr.res_el_ptr()->next = 0;
    }
    else {                         // This is between two elements
        p->prev->next = p->next;
        p->next->prev = p->prev;

        // Get the "next" element for return
        n = p->next;
    }

    --active_elements;

    //  Put this element back onto the unused list
    p->next = unused_elements.res_el_ptr();
    p->prev = 0;
    if (p->next) {           // NULL if unused list is empty
        p->next->prev = p;
    }

    if (!allocate_storage) {
        p->data = 0;
    }

    unused_elements = q;

    //  A little "garbage collection"
    if (++remove_count > 10) {
        //	    free_extras();
        remove_count = 0;
    }

#if DEBUG_REMOVE
    unsigned unused_count = 0;
    for (iterator i=unused_elements;
         i.notnull();
         i = i.next()) {

        ++unused_count;
    }

    assert((active_elements+unused_count) == (base_elements+extra_elements));
#endif

    return iterator(n);
}


template <class T>
inline bool
res_list<T>::in_list(iterator j)
{
    iterator i;

    for (i=head(); i.notnull(); i=i.next()) {
        if (j.res_el_ptr() == i.res_el_ptr()) {
            return true;
        }
    }

    return false;
}

template <class T>
inline void
res_list<T>::free_extras(void)
{
    unsigned num_unused = base_elements + extra_elements - active_elements;
    unsigned to_free = extra_elements;
    res_element *p;


    if (extra_elements != 0) {
        //
        //  Free min(extra_elements, # unused elements)
        //
        if (extra_elements > num_unused) {
            to_free = num_unused;
        }

        p = unused_elements.res_el_ptr();
        for (int i=0; i<to_free; ++i) {
            res_element *q = p->next;

            delete p;

            p = q;
        }

        //  update the unused element pointer to point to the first
        //  element that wasn't deleted.
        unused_elements = iterator(p);

        //  Update the number of extra elements
        extra_elements -= to_free;
    }

    return;
}


template <class T>
inline void
res_list<T>::clear(void)
{
    iterator i,n;

    for (i=head_ptr; i.notnull(); i=n) {
        n = i.next();
        remove(i);
    }

    free_extras();
}

template <class T>
inline void
res_list<T>::dump(void)
{
    for (iterator i=head(); !i.isnull(); i=i.next())
        i->dump();
}

template <class T>
inline void
res_list<T>::raw_dump(void)
{
    int j = 0;
    res_element *p;
    for (iterator i=head(); !i.isnull(); i=i.next()) {
        cprintf("Element %d:\n", j);

        if (i.notnull()) {
            p = i.res_el_ptr();
            cprintf("  points to res_element @ %#x\n", p);
            p->dump();
            cprintf("  Data Element:\n");
            i->dump();
        }
        else {
            cprintf("  NULL iterator!\n");
        }

        ++j;
    }

}

#endif // __RES_LIST_HH__