source:
XIOS/trunk/src/buffer_server.cpp
@
1007
Last change on this file since 1007 was 885, checked in by rlacroix, 8 years ago | |
---|---|
|
|
File size: 2.8 KB |
Rev | Line | |
---|---|---|
[591] | 1 | #include "xios_spl.hpp" |
[300] | 2 | #include "exception.hpp" |
3 | #include "buffer_server.hpp" | |
4 | ||
[509] | 5 | |
[335] | 6 | namespace xios |
[300] | 7 | { |
8 | ||
[509] | 9 | CServerBuffer::CServerBuffer(StdSize buffSize) |
[300] | 10 | { |
[885] | 11 | size = 3 * buffSize; |
[717] | 12 | first = 0; |
13 | current = 1; | |
14 | end = size; | |
15 | buffer = new char[size]; // use MPI_ALLOC_MEM later? | |
[300] | 16 | } |
17 | ||
18 | CServerBuffer::~CServerBuffer() | |
19 | { | |
20 | delete [] buffer ; | |
21 | } | |
22 | ||
[509] | 23 | |
[300] | 24 | bool CServerBuffer::isBufferFree(size_t count) |
25 | { | |
26 | bool ret ; | |
[509] | 27 | |
[300] | 28 | if (count==0) return true ; |
[509] | 29 | |
[300] | 30 | if (current>first) |
31 | { | |
32 | if (current+count<size) | |
33 | { | |
34 | ret=true ; | |
35 | } | |
36 | else if (current+count==size) | |
37 | { | |
38 | if (first>0) | |
39 | { | |
40 | ret=true ; | |
41 | } | |
42 | else | |
43 | { | |
44 | ret=false ; | |
45 | } | |
46 | } | |
47 | else | |
48 | { | |
49 | if (count<first) | |
50 | { | |
51 | ret=true ; | |
52 | } | |
53 | else | |
54 | { | |
55 | ret=false ; | |
56 | } | |
57 | } | |
58 | } | |
59 | else | |
60 | { | |
61 | if (current+count<first) | |
62 | { | |
63 | ret=true ; | |
64 | } | |
65 | else | |
66 | { | |
67 | ret=false ; | |
68 | } | |
69 | } | |
70 | ||
71 | return ret ; | |
72 | } | |
73 | ||
[509] | 74 | |
[300] | 75 | void* CServerBuffer::getBuffer(size_t count) |
76 | { | |
77 | char* ret ; | |
[509] | 78 | |
[300] | 79 | if (count==0) return buffer+current ; |
[509] | 80 | |
[300] | 81 | if (current>first) |
82 | { | |
83 | if (current+count<size) | |
84 | { | |
85 | ret=buffer+current ; | |
86 | current+=count ; | |
87 | } | |
88 | else if (current+count==size) | |
89 | { | |
90 | if (first>0) | |
91 | { | |
92 | ret=buffer+current ; | |
93 | current=0 ; | |
94 | } | |
95 | else | |
96 | { | |
97 | ERROR("void* CServerBuffer::getBuffer(size_t count)", | |
98 | <<"cannot allocate required size in buffer") ; | |
99 | } | |
100 | } | |
101 | else | |
102 | { | |
103 | end=current ; | |
104 | if (count<first) | |
105 | { | |
106 | ret=buffer ; | |
107 | current=count ; | |
108 | } | |
109 | else | |
110 | { | |
111 | ERROR("void* CServerBuffer::getBuffer(size_t count)", | |
112 | <<"cannot allocate required size in buffer") ; | |
113 | } | |
114 | } | |
115 | } | |
116 | else | |
117 | { | |
118 | if (current+count<first) | |
119 | { | |
120 | ret=buffer+current ; | |
121 | current+=count ; | |
122 | } | |
123 | else | |
124 | { | |
125 | ERROR("void* CServerBuffer::getBuffer(size_t count)", | |
126 | <<"cannot allocate required size in buffer") ; | |
127 | } | |
128 | } | |
129 | ||
130 | return ret ; | |
131 | } | |
[509] | 132 | |
[300] | 133 | void CServerBuffer::freeBuffer(size_t count) |
134 | { | |
135 | if (count==0) return ; | |
136 | ||
[509] | 137 | if (first==end-1) |
[300] | 138 | { |
139 | first=0 ; | |
140 | count-- ; | |
141 | end=size ; | |
142 | } | |
143 | ||
144 | if (first<=current) | |
145 | { | |
[509] | 146 | if (first+count <current) |
[300] | 147 | { |
148 | first+=count ; | |
149 | } | |
150 | else | |
151 | { | |
152 | ERROR("void CServerBuffer::freeBuffer(size_t count)", | |
153 | <<"cannot free required size in buffer") ; | |
154 | } | |
[509] | 155 | |
[300] | 156 | } |
157 | else | |
158 | { | |
159 | if (first+count<end) | |
160 | { | |
161 | first+=count ; | |
162 | } | |
163 | else | |
164 | { | |
165 | ERROR("void CServerBuffer::freeBuffer(size_t count)", | |
166 | <<"cannot free required size in buffer") ; | |
167 | } | |
168 | } | |
[509] | 169 | } |
170 | ||
[300] | 171 | } |
Note: See TracBrowser
for help on using the repository browser.