1 | /****************************************************************************** |
---|
2 | * QUANTA - A toolkit for High Performance Data Sharing |
---|
3 | * Copyright (C) 2003 Electronic Visualization Laboratory, |
---|
4 | * University of Illinois at Chicago |
---|
5 | * |
---|
6 | * All rights reserved. |
---|
7 | * |
---|
8 | * Redistribution and use in source and binary forms, with or without |
---|
9 | * modification, are permitted provided that the following conditions are met: |
---|
10 | * |
---|
11 | * * Redistributions of source code must retain the above copyright |
---|
12 | * notice, this list of conditions and the following disclaimer. |
---|
13 | * * Redistributions in binary form must reproduce the above |
---|
14 | * copyright notice, this list of conditions and the following disclaimer |
---|
15 | * in the documentation and/or other materials provided with the distribution. |
---|
16 | * * Neither the name of the University of Illinois at Chicago nor |
---|
17 | * the names of its contributors may be used to endorse or promote |
---|
18 | * products derived from this software without specific prior written permission. |
---|
19 | * |
---|
20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
---|
21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
---|
22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
---|
23 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
---|
24 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
---|
25 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
---|
26 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
---|
27 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
---|
28 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
---|
29 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
---|
30 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
31 | * |
---|
32 | * Direct questions, comments etc about Quanta to cavern@evl.uic.edu |
---|
33 | *****************************************************************************/ |
---|
34 | |
---|
35 | /* |
---|
36 | * File name: QUANTAnet_iovec.cxx |
---|
37 | * |
---|
38 | * Description: Wrapper class for the iovec structure |
---|
39 | * |
---|
40 | * Comments: |
---|
41 | * |
---|
42 | * Change Log: |
---|
43 | */ |
---|
44 | |
---|
45 | #include "QUANTAnet_iovec_c.hxx" |
---|
46 | |
---|
47 | //constructor |
---|
48 | QUANTAnet_iovec_c :: QUANTAnet_iovec_c(int size) |
---|
49 | { |
---|
50 | num_of_elements = 0; |
---|
51 | nalloc = size; |
---|
52 | bufList = NULL; |
---|
53 | } |
---|
54 | |
---|
55 | //destructor |
---|
56 | QUANTAnet_iovec_c :: ~QUANTAnet_iovec_c() |
---|
57 | { |
---|
58 | |
---|
59 | if (bufList) { |
---|
60 | free(bufList); |
---|
61 | bufList = NULL; |
---|
62 | } |
---|
63 | |
---|
64 | } |
---|
65 | |
---|
66 | //add pointer and its length to the array |
---|
67 | bool QUANTAnet_iovec_c :: add(void* buff, int length) |
---|
68 | { |
---|
69 | if(bufList == NULL) |
---|
70 | { |
---|
71 | #ifdef WIN32 |
---|
72 | bufList = (WSABUF *) malloc (sizeof (WSABUF) * nalloc); |
---|
73 | #else |
---|
74 | bufList = (struct iovec*) malloc (sizeof (iovec) * nalloc); |
---|
75 | #endif |
---|
76 | |
---|
77 | //if malloc can not allocate memory |
---|
78 | if(bufList == NULL) |
---|
79 | { |
---|
80 | printf("QUANTAnet_iovec_c :: add() :: malloc - out of memory"); |
---|
81 | return false; |
---|
82 | } |
---|
83 | } |
---|
84 | |
---|
85 | else if(num_of_elements >= nalloc) |
---|
86 | { |
---|
87 | nalloc = nalloc*2; //nalloc is doubled to reallocate twice number of elements |
---|
88 | #ifdef WIN32 |
---|
89 | bufList = (WSABUF *)realloc((WSABUF *)bufList, nalloc * sizeof(WSABUF)); |
---|
90 | #else |
---|
91 | bufList = (struct iovec*)realloc((iovec *)bufList, nalloc * sizeof(iovec)); |
---|
92 | #endif |
---|
93 | |
---|
94 | //if realloc can not allocate memory |
---|
95 | if(bufList == NULL) |
---|
96 | { |
---|
97 | printf("QUANTAnet_iovec_c :: add() :: realloc - out of memory"); |
---|
98 | return false; |
---|
99 | } |
---|
100 | } |
---|
101 | |
---|
102 | #ifdef WIN32 |
---|
103 | bufList[num_of_elements].buf = (char*)buff; |
---|
104 | bufList[num_of_elements].len = length; |
---|
105 | #else |
---|
106 | bufList[num_of_elements].iov_base = (char *)buff; |
---|
107 | bufList[num_of_elements].iov_len = length; |
---|
108 | #endif |
---|
109 | |
---|
110 | ++num_of_elements; |
---|
111 | return true; |
---|
112 | } |
---|
113 | |
---|
114 | //remove the entry at index |
---|
115 | bool QUANTAnet_iovec_c :: removeAt(int index) |
---|
116 | { |
---|
117 | --num_of_elements; |
---|
118 | |
---|
119 | if(index > num_of_elements) |
---|
120 | return false; |
---|
121 | else |
---|
122 | { |
---|
123 | for(int i = index; i < num_of_elements; i++) |
---|
124 | bufList[i] = bufList[i + 1]; |
---|
125 | return true; |
---|
126 | } |
---|
127 | } |
---|
128 | |
---|
129 | //clear the array |
---|
130 | void QUANTAnet_iovec_c :: removeAll() |
---|
131 | { |
---|
132 | num_of_elements = 0; |
---|
133 | bufList = NULL; |
---|
134 | |
---|
135 | } |
---|
136 | //returns the size fo teh array |
---|
137 | int QUANTAnet_iovec_c :: size() |
---|
138 | { |
---|
139 | return num_of_elements; |
---|
140 | } |
---|
141 | |
---|
142 | |
---|
143 | |
---|
144 | |
---|