Project

General

Profile

Statistics
| Branch: | Revision:

root / src / sim / parsim / ccommbufferbase.cc @ 81ad8b66

History | View | Annotate | Download (2.96 KB)

1 01873262 Georg Kunz
//=========================================================================
2
//  CCOMMBUFFERBASE.CC - part of
3
//
4
//                  OMNeT++/OMNEST
5
//           Discrete System Simulation in C++
6
//
7
//  Author: Andras Varga, 2003
8
//          Dept. of Electrical and Computer Systems Engineering,
9
//          Monash University, Melbourne, Australia
10
//
11
//=========================================================================
12
13
/*--------------------------------------------------------------*
14
  Copyright (C) 2003-2008 Andras Varga
15
  Copyright (C) 2006-2008 OpenSim Ltd.
16

17
  This file is distributed WITHOUT ANY WARRANTY. See the file
18
  `license' for details on this and other legal matters.
19
*--------------------------------------------------------------*/
20
21
22
#include "ccommbufferbase.h"
23
#include "cexception.h"
24
25
USING_NAMESPACE
26
27
28
cCommBufferBase::cCommBufferBase()
29
{
30
    mBuffer = 0;
31
    mBufferSize = 0;
32
    mPosition = 0;
33
    mMsgSize = 0;
34
}
35
36
cCommBufferBase::~cCommBufferBase ()
37
{
38
    delete [] mBuffer;
39
}
40
41
char *cCommBufferBase::getBuffer() const
42
{
43
    return mBuffer;
44
}
45
46
int cCommBufferBase::getBufferLength() const
47
{
48
    return mBufferSize;
49
}
50
51
void cCommBufferBase::allocateAtLeast(int size)
52
{
53
    size += 4; // allocate a bit more room, for sentry (used in cFileCommBuffer, etc.)
54
    if (mBufferSize < size)
55
    {
56
        delete [] mBuffer;
57
        mBuffer = new char[size];
58
        mBufferSize = size;
59
    }
60
}
61
62
void cCommBufferBase::setMessageSize(int size)
63
{
64
    mMsgSize = size;
65
    mPosition = 0;
66
}
67
68
int cCommBufferBase::getMessageSize() const
69
{
70
    return mMsgSize;
71
}
72
73
void cCommBufferBase::reset()
74
{
75
    mMsgSize = 0;
76
    mPosition = 0;
77
}
78
79
void cCommBufferBase::extendBufferFor(int dataSize)
80
{
81
    // TBD move reallocate+copy out of loop (more efficient)
82
    while (mMsgSize+dataSize >= mBufferSize)
83
    {
84
        // increase the size of the buffer while
85
        // retaining its own existing contents
86
        char *tempBuffer;
87
        int i, oldBufferSize = 0;
88
89
        oldBufferSize = mBufferSize;
90
        if (mBufferSize == 0)
91
            mBufferSize = 1000;
92
        else
93
            mBufferSize += mBufferSize;
94
95
        tempBuffer = new char[mBufferSize];
96
        for(i = 0; i < oldBufferSize; i++)
97
            tempBuffer[i] = mBuffer[i];
98
99
        delete [] mBuffer;
100
        mBuffer = tempBuffer;
101
    }
102
}
103
104
bool cCommBufferBase::isBufferEmpty() const
105
{
106
    return mPosition == mMsgSize;
107
}
108
109
void cCommBufferBase::assertBufferEmpty()
110
{
111
    if (mPosition == mMsgSize)
112
        return;
113
114
    if (mPosition > mMsgSize)
115
    {
116
        throw cRuntimeError("internal error: cCommBuffer pack/unpack mismatch: "
117
                             "read %d bytes past end of buffer while unpacking %d bytes",
118
                             mPosition-mMsgSize, mPosition);
119
    }
120
    else
121
    {
122
        throw cRuntimeError("internal error: cCommBuffer pack/unpack mismatch: "
123
                            "%d extra bytes remained in buffer after unpacking %d bytes",
124
                            mMsgSize-mPosition, mPosition);
125
    }
126
}
127