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