~ubuntu-branches/ubuntu/intrepid/raidutils/intrepid

« back to all changes in this revision

Viewing changes to lib/dpt_buff.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Barak Pearlmutter
  • Date: 2004-05-18 11:33:42 UTC
  • Revision ID: james.westby@ubuntu.com-20040518113342-tyqavmso5q351xi2
Tags: upstream-0.0.4
ImportĀ upstreamĀ versionĀ 0.0.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (c) 1996-2004, Adaptec Corporation
 
2
 * All rights reserved.
 
3
 *
 
4
 * Redistribution and use in source and binary forms, with or without
 
5
 * modification, are permitted provided that the following conditions are met:
 
6
 *
 
7
 * - Redistributions of source code must retain the above copyright notice, this
 
8
 *   list of conditions and the following disclaimer.
 
9
 * - Redistributions in binary form must reproduce the above copyright notice,
 
10
 *   this list of conditions and the following disclaimer in the documentation
 
11
 *   and/or other materials provided with the distribution.
 
12
 * - Neither the name of the Adaptec Corporation nor the names of its
 
13
 *   contributors may be used to endorse or promote products derived from this
 
14
 *   software without specific prior written permission.
 
15
 *
 
16
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 
17
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 
18
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 
19
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 
20
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 
21
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 
22
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 
23
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 
24
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 
25
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 
26
 * POSSIBILITY OF SUCH DAMAGE.
 
27
 */
 
28
 
 
29
//File - DPT_BUFF.CPP
 
30
//***************************************************************************
 
31
//
 
32
//Description:
 
33
//
 
34
//    This file contains function definitions for the dptBuffer_S
 
35
//structure.
 
36
//
 
37
//Author:       Doug Anderson
 
38
//Date:         11/16/92
 
39
//
 
40
//Editors:
 
41
//
 
42
//Remarks:
 
43
//
 
44
//
 
45
//***************************************************************************
 
46
 
 
47
 
 
48
//Include Files -------------------------------------------------------------
 
49
 
 
50
#include        "dpt_buff.h"
 
51
 
 
52
 
 
53
//Function - dptBuffer_S::extract() - start
 
54
//===========================================================================
 
55
//
 
56
//Description:
 
57
//
 
58
//    This function reads a block of data from the next available location
 
59
//in the DPT buffer.  This function can be called repetitively to read
 
60
//multiple data blocks from a single DPT buffer.
 
61
//
 
62
//Parameters:
 
63
//
 
64
//Return Value:
 
65
//
 
66
//   0 = Extraction truncated
 
67
//   1 = Success - all requested bytes were extracted
 
68
//
 
69
//Global Variables Affected:
 
70
//
 
71
//Remarks: (Side effects, Assumptions, Warnings...)
 
72
//
 
73
//
 
74
//---------------------------------------------------------------------------
 
75
 
 
76
uSHORT  dptBuffer_S::extract(void *inData_P,uLONG dataSize)
 
77
{
 
78
 
 
79
uLONG numToCopy = 0;
 
80
  // If there is any space left...
 
81
if (writeIndex>readIndex) {
 
82
     // Determine how many bytes have not been read...
 
83
   uLONG numLeft = writeIndex - readIndex;
 
84
     // Determine how many bytes to read from the buffer
 
85
   numToCopy     = (numLeft > dataSize) ? dataSize : numLeft;
 
86
     // Copy the data from the I/O buffer to the specified buffer
 
87
   memcpy(inData_P,data+readIndex,(uINT)numToCopy);
 
88
     // Increment the read index
 
89
   readIndex     += numToCopy;
 
90
}
 
91
 
 
92
return (numToCopy==dataSize);
 
93
 
 
94
}
 
95
//dptBuffer_S::extract() - end
 
96
 
 
97
 
 
98
//Function - dptBuffer_S::skip() - start
 
99
//===========================================================================
 
100
//
 
101
//Description:
 
102
//
 
103
//    This function increments the read index by the specified amount
 
104
//without extracting any information.
 
105
//
 
106
//Parameters:
 
107
//
 
108
//Return Value:
 
109
//
 
110
//   0 = Extraction truncated
 
111
//   1 = Success - all requested bytes were extracted
 
112
//
 
113
//Global Variables Affected:
 
114
//
 
115
//Remarks: (Side effects, Assumptions, Warnings...)
 
116
//
 
117
//
 
118
//---------------------------------------------------------------------------
 
119
 
 
120
uSHORT  dptBuffer_S::skip(uLONG dataSize)
 
121
{
 
122
 
 
123
uLONG numToCopy = 0;
 
124
  // If there is any space left...
 
125
if (writeIndex>readIndex) {
 
126
     // Determine how many bytes have not been read...
 
127
   uLONG numLeft = writeIndex - readIndex;
 
128
     // Determine how many bytes may be skipped
 
129
   numToCopy     = (numLeft>dataSize) ? dataSize : numLeft;
 
130
     // Increment the read index
 
131
   readIndex    += numToCopy;
 
132
}
 
133
 
 
134
return (numToCopy==dataSize);
 
135
 
 
136
}
 
137
//dptBuffer_S::skip() - end
 
138
 
 
139
 
 
140
//Function - dptBuffer_S::insert() - start
 
141
//===========================================================================
 
142
//
 
143
//Description:
 
144
//
 
145
//    This function writes a block of data to the next available location
 
146
//in the DPT buffer.  This function can be called repetitively to read
 
147
//multiple data blocks from a single DPT buffer.
 
148
//
 
149
//Parameters:
 
150
//
 
151
//Return Value:
 
152
//
 
153
//   0 = Insertion incomplete
 
154
//   1 = Success - all requested bytes were inserted
 
155
//
 
156
//Global Variables Affected:
 
157
//
 
158
//Remarks: (Side effects, Assumptions, Warnings...)
 
159
//
 
160
//
 
161
//---------------------------------------------------------------------------
 
162
 
 
163
uSHORT  dptBuffer_S::insert(void *inData_P,uLONG dataSize)
 
164
{
 
165
 
 
166
uLONG numToCopy = 0;
 
167
  // If there is any room left...
 
168
if (allocSize>writeIndex) {
 
169
     // Determine how many bytes are left in the data buffer
 
170
   uLONG numLeft = allocSize - writeIndex;
 
171
     // Determine how many bytes may be copied into the buffer
 
172
   numToCopy     = (numLeft < dataSize) ? numLeft : dataSize;
 
173
     // Copy the data into the buffer
 
174
   memcpy(data+writeIndex,inData_P,(uINT)numToCopy);
 
175
     // Increment the write index
 
176
   writeIndex    += numToCopy;
 
177
}
 
178
 
 
179
return (numToCopy==dataSize);
 
180
 
 
181
}
 
182
//dptBuffer_S::insert() - end
 
183
 
 
184
 
 
185
//Function - dptBuffer_S::setExtractSize() - start
 
186
//===========================================================================
 
187
//
 
188
//Description:
 
189
//
 
190
//    This function sets the number of bytes that can be read from
 
191
//the buffer.
 
192
//
 
193
//Parameters:
 
194
//
 
195
//Return Value:
 
196
//
 
197
//Global Variables Affected:
 
198
//
 
199
//Remarks: (Side effects, Assumptions, Warnings...)
 
200
//
 
201
//  1. This function should be used sparingly.  This function is
 
202
//     provided for use in situations where an the reset(),insert()
 
203
//     procedure was not used to initialize the buffer.
 
204
//
 
205
//---------------------------------------------------------------------------
 
206
 
 
207
void    dptBuffer_S::setExtractSize(uLONG inSize)
 
208
{
 
209
 
 
210
  // Take the minimum of the buffer size and the request size
 
211
writeIndex = (allocSize > inSize) ? inSize : allocSize;
 
212
 
 
213
}
 
214
//dptBuffer_S::setExtractSize() - end
 
215
 
 
216
 
 
217
//Function - dptBuffer_S::newBuffer() - start
 
218
//===========================================================================
 
219
//
 
220
//Description:
 
221
//
 
222
//      This function allocates a new DPT I/O buffer with the specified
 
223
//data buffer size and initializes the buffer header.
 
224
//
 
225
//Parameters:
 
226
//
 
227
//Return Value:
 
228
//
 
229
//Global Variables Affected:
 
230
//
 
231
//Remarks: (Side effects, Assumptions, Warnings...)
 
232
//
 
233
//
 
234
//---------------------------------------------------------------------------
 
235
 
 
236
dptBuffer_S *   dptBuffer_S::newBuffer(uLONG size)
 
237
{
 
238
 
 
239
dptBuffer_S *buff_P = NULL;
 
240
uLONG AllocSize;
 
241
 
 
242
  // If a non-zero data buffer has been requested...
 
243
if (size) {
 
244
 
 
245
   //
 
246
   // Set up the alloc size to include the buffer header
 
247
   //
 
248
   AllocSize = sizeof(dptBuffer_S)-1 + size;
 
249
 
 
250
   //
 
251
   // Now lets make sure the size is an even multiple of 16, and
 
252
   // just for good measure we will buffer it by 16 for all of those
 
253
   // environments that have a problem with alignment
 
254
   //
 
255
   AllocSize = (AllocSize & 0xfffffff0) + 0x10;
 
256
 
 
257
     // Allocate a new I/O buffer of the specified size
 
258
   buff_P = (dptBuffer_S *) new uCHAR[AllocSize];
 
259
     // If the buffer was allocated...
 
260
   if (buff_P != NULL) {
 
261
        // Initialize the buffer header
 
262
      buff_P->commID            = 0;
 
263
      buff_P->allocSize         = size;
 
264
      buff_P->readIndex         = 0;
 
265
      buff_P->writeIndex        = 0;
 
266
 
 
267
        // Clear the data buffer
 
268
      memset(buff_P->data,0,(uINT)size);
 
269
   }
 
270
}
 
271
 
 
272
return (buff_P);
 
273
 
 
274
}
 
275
//dptBuffer_S::newBuffer() - end
 
276
 
 
277
 
 
278
//Function - dptBuffer_S::delBuffer() - start
 
279
//===========================================================================
 
280
//
 
281
//Description:
 
282
//
 
283
//      This function frees an I/O buffer that was allocated with'
 
284
//newBuffer().
 
285
//
 
286
//Parameters:
 
287
//
 
288
//Return Value:
 
289
//
 
290
//Global Variables Affected:
 
291
//
 
292
//Remarks: (Side effects, Assumptions, Warnings...)
 
293
//
 
294
//
 
295
//---------------------------------------------------------------------------
 
296
 
 
297
void    dptBuffer_S::delBuffer(dptBuffer_S *buff_P)
 
298
{
 
299
 
 
300
  // If a valid I/O buffer was specified...
 
301
if (buff_P!=NULL)
 
302
     // Delete the buffer as a uCHAR array
 
303
   delete[] ((uCHAR *)buff_P);
 
304
 
 
305
}
 
306
//dptBuffer_S::delBuffer() - end
 
307
 
 
308
//Function - dptBuffer_S::netInsert() - start
 
309
//===========================================================================
 
310
//
 
311
//Description:
 
312
//
 
313
//    This function is used to place inData into DPT network order 
 
314
//(byte swapping if necessary) and then call insert() to place the
 
315
//value in the DPT buffer.  Apart from the possible byte swapping, this
 
316
//function should behave identically to insert().
 
317
//
 
318
//Parameters:
 
319
//
 
320
//Return Value:
 
321
//
 
322
//   0 = Extraction truncated
 
323
//   1 = Success - all requested bytes were extracted
 
324
//
 
325
//Global Variables Affected:
 
326
//
 
327
//Remarks: (Side effects, Assumptions, Warnings...)
 
328
//
 
329
//
 
330
//---------------------------------------------------------------------------
 
331
 
 
332
uSHORT dptBuffer_S::netInsert(uLONG inData)
 
333
{
 
334
        uLONG           temp = NET_SWAP_4(inData);
 
335
 
 
336
        return  (insert(&temp, sizeof(uLONG)));
 
337
}
 
338
 
 
339
uSHORT dptBuffer_S::netInsert(uSHORT inData)
 
340
{
 
341
        uSHORT  temp = NET_SWAP_2(inData);
 
342
 
 
343
        return  (insert(&temp, sizeof(uSHORT)));
 
344
}
 
345
//dptBuffer_S::netInsert() - end
 
346
 
 
347
 
 
348
//Function - dptBuffer_S::netExtract() - start
 
349
//===========================================================================
 
350
//
 
351
//Description:
 
352
//
 
353
//    This function is used to call extract() to remove data from the DPT
 
354
//buffer (which is in DPT network order) and then return the data in host 
 
355
//byte order (byte swapping if necessary).  Apart from the possible byte 
 
356
//swapping, this function should behave identically to extract().
 
357
//
 
358
//Parameters:
 
359
//
 
360
//Return Value:
 
361
//
 
362
//   0 = Extraction truncated
 
363
//   1 = Success - all requested bytes were extracted
 
364
//
 
365
//Global Variables Affected:
 
366
//
 
367
//Remarks: (Side effects, Assumptions, Warnings...)
 
368
//
 
369
//
 
370
//---------------------------------------------------------------------------
 
371
 
 
372
uSHORT dptBuffer_S::netExtract(uLONG &inData)
 
373
{
 
374
        uSHORT  retVal = extract(&inData, sizeof(uLONG));
 
375
 
 
376
        inData = NET_SWAP_4(inData);
 
377
 
 
378
        // return the result of the call to extract()
 
379
        return  retVal;
 
380
}
 
381
 
 
382
uSHORT dptBuffer_S::netExtract(long &inData)
 
383
{
 
384
        uSHORT  retVal = extract(&inData, sizeof(long));
 
385
 
 
386
        inData = NET_SWAP_4(inData);
 
387
 
 
388
        // return the result of the call to extract()
 
389
        return  retVal;
 
390
}
 
391
 
 
392
uSHORT dptBuffer_S::netExtract(uSHORT &inData)
 
393
{
 
394
        uSHORT  retVal = extract(&inData, sizeof(uSHORT));
 
395
 
 
396
        inData = NET_SWAP_2(inData);
 
397
 
 
398
        // return the result of the call to extract()
 
399
        return  retVal;
 
400
}
 
401
 
 
402
uSHORT dptBuffer_S::netExtract(short &inData)
 
403
{
 
404
        uSHORT  retVal = extract(&inData, sizeof(short));
 
405
 
 
406
        inData = NET_SWAP_2(inData);
 
407
 
 
408
        // return the result of the call to extract()
 
409
        return  retVal;
 
410
}
 
411
//dptBuffer_S::netExtract() - end
 
412
 
 
413
 
 
414