summaryrefslogtreecommitdiff
path: root/cppe/src/IceE/Buffer.cpp
blob: bbe06c08d49c78d7710e813dc795e3008b2332eb (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
// **********************************************************************
//
// Copyright (c) 2003-2007 ZeroC, Inc. All rights reserved.
//
// This copy of Ice-E is licensed to you under the terms described in the
// ICEE_LICENSE file included in this distribution.
//
// **********************************************************************

#include <IceE/Buffer.h>
#include <IceE/LocalException.h>

using namespace std;
using namespace Ice;
using namespace IceInternal;

void
IceInternal::Buffer::swap(Buffer& other)
{
#ifdef ICE_SMALL_MESSAGE_BUFFER_OPTIMIZATION
    Container::difference_type pos = i - b.begin();
    Container::difference_type otherPos = other.i - other.b.begin();
    b.swap(other.b);
    i = b.begin() + otherPos;
    other.i = other.b.begin() + pos;
#else
    b.swap(other.b);
    std::swap(i, other.i); 
#endif
}

void
IceInternal::Buffer::Container::swap(Container& other)
{
#ifdef ICE_SMALL_MESSAGE_BUFFER_OPTIMIZATION
    if(_buf == _fixed)
    {
	if(other._buf == other._fixed)
	{
	    value_type tmp[ICE_BUFFER_FIXED_SIZE];
	    memcpy(tmp, _fixed, _size);
	    memcpy(_fixed, other._fixed, other._size);
	    memcpy(other._fixed, tmp, _size);
	}
	else
	{
	    _buf = other._buf;
	    memcpy(other._fixed, _fixed, _size);
	    other._buf = other._fixed;
	}
    }
    else
    {
	if(other._buf == other._fixed)
	{
	    other._buf = _buf;
	    memcpy(_fixed, other._fixed, other._size);
	    _buf = _fixed;
	}
	else
	{
	    std::swap(_buf, other._buf);
	}
    }
#else
    std::swap(_buf, other._buf);
#endif

    std::swap(_size, other._size);
    std::swap(_capacity, other._capacity);
    std::swap(_shrinkCounter, other._shrinkCounter);
}

void
IceInternal::Buffer::Container::clear()
{
#ifdef ICE_SMALL_MESSAGE_BUFFER_OPTIMIZATION
    if(_buf != _fixed)
    {
	free(_buf);
	_buf = _fixed;
    }
    _size = 0;
    _capacity = ICE_BUFFER_FIXED_SIZE;
#else
    free(_buf);
    _buf = 0;
    _size = 0;
    _capacity = 0;
#endif
}

void
IceInternal::Buffer::Container::reserve(size_type n)
{
    if(n > _capacity)
    {
	_capacity = std::max<size_type>(n, std::min(2 * _capacity, _maxCapacity));
	_capacity = std::max<size_type>(static_cast<size_type>(240), _capacity);
    }
    else if(n < _capacity)
    {
	_capacity = n;
    }
    else
    {
	return;
    }
    
#ifdef ICE_SMALL_MESSAGE_BUFFER_OPTIMIZATION
    if(_buf != _fixed)
    {
	_buf = reinterpret_cast<pointer>(realloc(_buf, _capacity));
    }
    else if(_capacity > ICE_BUFFER_FIXED_SIZE)
    {
	_buf = reinterpret_cast<pointer>(malloc(_capacity));
	memcpy(_buf, _fixed, _size);
    }
#else
    if(_buf)
    {
	_buf = reinterpret_cast<pointer>(realloc(_buf, _capacity));
    }
    else
    {
	_buf = reinterpret_cast<pointer>(malloc(_capacity));
    }
#endif
	
    if(!_buf)
    {
	SyscallException ex(__FILE__, __LINE__);
	ex.error = getSystemErrno();
	throw ex;
    }
}