Statistics
| Branch: | Revision:

root / src / sim / ctaskring.cc @ e1750c09

History | View | Annotate | Download (3.02 KB)

1
//=========================================================================
2
//  CTASKRING.CC - part of
3
//
4
//                  OMNeT++/OMNEST
5
//           Discrete System Simulation in C++
6
//
7
//   Member functions of
8
//    cTaskRing : future event set, implemented as ring buffer
9
//
10
//  Author: Andras Varga, based on the code from Gabor Lencse
11
//          (the original is taken from G. H. Gonnet's book pp. 273-274)
12
//          Georg Kunz
13
//
14
//=========================================================================
15

    
16
/*--------------------------------------------------------------*
17
  Copyright (C) 1992-2005 Andras Varga
18
  Copyright (C) 2009      Georg Kunz
19

20
  This file is distributed WITHOUT ANY WARRANTY. See the file
21
  `license' for details on this and other legal matters.
22

23
  Note: This class is used in the parallel task queue.
24

25
*--------------------------------------------------------------*/
26

    
27
#include <stdio.h>           // sprintf
28
#include <string.h>          // strlen
29
#include <sstream>
30
#include "regmacros.h"
31
#include "ctaskring.h"
32
#include "cmessage.h"
33

    
34
//=== Registration
35
Register_Class(cTaskRing);
36

    
37

    
38
cTaskRing::cTaskRing(const char *name, int siz) : cOwnedObject(name)
39
{
40
    insertcntr = 0L;
41
    n = 0;
42
    size = siz;
43
    front = back = 0;
44
    h = new cMessage *[size];
45
}
46

    
47

    
48
cTaskRing::cTaskRing(const cTaskRing& ring) : cOwnedObject()
49
{
50
    h = NULL;
51
    n = 0;
52
    setName( ring.getName() );
53
    operator=(ring);
54
}
55

    
56

    
57
cTaskRing::~cTaskRing()
58
{
59
    clear();
60
    delete [] h;
61
}
62

    
63

    
64
std::string cTaskRing::info() const
65
{
66
    if (n==0)
67
        return std::string("empty");
68
    std::stringstream out;
69
    out << "length=" << n;
70
    return out.str();
71
}
72

    
73
/*
74
void cTaskRing::forEachChild(cVisitor *v)
75
{
76
    for (int i=back; i!=front-1; i=(i+1)%size)
77
        if (h[i])
78
            v->visit(h[i]);
79
}
80
*/
81

    
82
void cTaskRing::clear()
83
{
84
    for (int i=back; i!=front-1; i=(i+1)%size)
85
    {
86
        ignoreOwnerAndDelete(h[i]);
87
    }
88
    n = 0;
89
    front = back = 0;
90
}
91

    
92

    
93
void cTaskRing::clearNoDelete()
94
{
95
    for (int i=0; i<n; i++)
96
    {
97
        getFirst();
98
    }
99
    n = 0;
100
    front = back = 0;
101
}
102

    
103

    
104
cTaskRing& cTaskRing::operator=(const cTaskRing& ring)
105
{
106
    if (this==&ring) return *this;
107

    
108
    clear();
109

    
110
    cObject::operator=(ring);
111

    
112
    n = ring.n;
113
    size = ring.size;
114
    front = ring.front;
115
    back = ring.back;
116

    
117
    delete [] h;
118
    h = new cMessage *[size];
119

    
120
    for (int i=back; i!=front-1; i=(i+1)%size)
121
        h[i]=(cMessage *)ring.h[i]->dup();
122

    
123
    return *this;
124
}
125

    
126

    
127
void cTaskRing::insert(cMessage *event)
128
{
129
    if (++n > size)
130
    {
131
        int old_front = front;
132
        int old_back = back;
133
        int old_size = size;
134

    
135
        size *= 2;
136
        cMessage **hnew = new cMessage *[size];
137

    
138
        front = back = 0;
139
        for (int i=old_back; i!=old_front-1; i=(i+1)%old_size)
140
        {
141
            hnew[front++] = h[i];
142
        }
143
        h = hnew;
144
    }
145

    
146
    h[front] = event;
147
    front = (front+1) % size;
148
}
149

    
150

    
151
cMessage *cTaskRing::getFirst()
152
{
153
    if (n>0)
154
    {
155
      int i = back;
156
      n--;
157
      back = (back+1) % size;
158
      return h[i];
159
    }
160
    return NULL;
161
}