~ubuntu-branches/ubuntu/precise/ncbi-tools6/precise

« back to all changes in this revision

Viewing changes to connect/ncbi_memory_connector.c

  • Committer: Bazaar Package Importer
  • Author(s): Aaron M. Ucko
  • Date: 2005-03-27 12:00:15 UTC
  • mfrom: (2.1.2 hoary)
  • Revision ID: james.westby@ubuntu.com-20050327120015-embhesp32nj73p9r
Tags: 6.1.20041020-3
* Fix FTBFS under GCC 4.0 caused by inconsistent use of "static" on
  functions.  (Closes: #295110.)
* Add a watch file, now that we can.  (Upstream's layout needs version=3.)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*  $Id: ncbi_memory_connector.c,v 6.5 2003/05/31 05:15:39 lavr Exp $
 
2
 * ===========================================================================
 
3
 *
 
4
 *                            PUBLIC DOMAIN NOTICE
 
5
 *               National Center for Biotechnology Information
 
6
 *
 
7
 *  This software/database is a "United States Government Work" under the
 
8
 *  terms of the United States Copyright Act.  It was written as part of
 
9
 *  the author's official duties as a United States Government employee and
 
10
 *  thus cannot be copyrighted.  This software/database is freely available
 
11
 *  to the public for use. The National Library of Medicine and the U.S.
 
12
 *  Government have not placed any restriction on its use or reproduction.
 
13
 *
 
14
 *  Although all reasonable efforts have been taken to ensure the accuracy
 
15
 *  and reliability of the software and data, the NLM and the U.S.
 
16
 *  Government do not and cannot warrant the performance or results that
 
17
 *  may be obtained by using this software or data. The NLM and the U.S.
 
18
 *  Government disclaim all warranties, express or implied, including
 
19
 *  warranties of performance, merchantability or fitness for any particular
 
20
 *  purpose.
 
21
 *
 
22
 *  Please cite the author in any work or product based on this material.
 
23
 *
 
24
 * ===========================================================================
 
25
 *
 
26
 * Author:  Anton Lavrentiev
 
27
 *
 
28
 * File Description:
 
29
 *   In-memory CONNECTOR
 
30
 *
 
31
 *   See <connect/ncbi_connector.h> for the detailed specification of
 
32
 *   the connector's methods and structures.
 
33
 *
 
34
 */
 
35
 
 
36
#include <connect/ncbi_buffer.h>
 
37
#include <connect/ncbi_memory_connector.h>
 
38
#include <stdlib.h>
 
39
 
 
40
 
 
41
/***********************************************************************
 
42
 *  INTERNAL -- Auxiliary types and static functions
 
43
 ***********************************************************************/
 
44
 
 
45
/* All internal data necessary to perform the (re)connect and i/o
 
46
 */
 
47
typedef struct {
 
48
    BUF        buf;
 
49
    MT_LOCK    lock;
 
50
    EIO_Status r_status;
 
51
    EIO_Status w_status;
 
52
} SMemoryConnector;
 
53
 
 
54
 
 
55
/***********************************************************************
 
56
 *  INTERNAL -- "s_VT_*" functions for the "virt. table" of connector methods
 
57
 ***********************************************************************/
 
58
 
 
59
#ifdef __cplusplus
 
60
extern "C" {
 
61
#endif /* __cplusplus */
 
62
    static const char* s_VT_GetType (CONNECTOR       connector);
 
63
    static EIO_Status  s_VT_Open    (CONNECTOR       connector,
 
64
                                     const STimeout* timeout);
 
65
    static EIO_Status  s_VT_Wait    (CONNECTOR       connector,
 
66
                                     EIO_Event       event,
 
67
                                     const STimeout* timeout);
 
68
    static EIO_Status  s_VT_Write   (CONNECTOR       connector,
 
69
                                     const void*     buf,
 
70
                                     size_t          size,
 
71
                                     size_t*         n_written,
 
72
                                     const STimeout* timeout);
 
73
    static EIO_Status  s_VT_Read    (CONNECTOR       connector,
 
74
                                     void*           buf,
 
75
                                     size_t          size,
 
76
                                     size_t*         n_read,
 
77
                                     const STimeout* timeout);
 
78
    static EIO_Status  s_VT_Status  (CONNECTOR       connector,
 
79
                                     EIO_Event       dir);
 
80
    static EIO_Status  s_VT_Close   (CONNECTOR       connector,
 
81
                                     const STimeout* timeout);
 
82
    static void        s_Setup      (SMetaConnector* meta,
 
83
                                     CONNECTOR       connector);
 
84
    static void        s_Destroy    (CONNECTOR       connector);
 
85
#  ifdef IMPLEMENTED__CONN_WaitAsync
 
86
    static EIO_Status s_VT_WaitAsync(void*                   connector,
 
87
                                     FConnectorAsyncHandler  func,
 
88
                                     SConnectorAsyncHandler* data);
 
89
#  endif
 
90
#ifdef __cplusplus
 
91
} /* extern "C" */
 
92
#endif /* __cplusplus */
 
93
 
 
94
 
 
95
/*ARGSUSED*/
 
96
static const char* s_VT_GetType
 
97
(CONNECTOR connector)
 
98
{
 
99
    return "MEMORY";
 
100
}
 
101
 
 
102
 
 
103
/*ARGSUSED*/
 
104
static EIO_Status s_VT_Open
 
105
(CONNECTOR       connector,
 
106
 const STimeout* timeout)
 
107
{
 
108
    SMemoryConnector* xxx = (SMemoryConnector*) connector->handle;
 
109
    xxx->buf = 0;
 
110
    xxx->r_status = eIO_Success;
 
111
    xxx->w_status = eIO_Success;
 
112
    return eIO_Success;
 
113
}
 
114
 
 
115
 
 
116
/*ARGSUSED*/
 
117
static EIO_Status s_VT_Write
 
118
(CONNECTOR       connector,
 
119
 const void*     buf,
 
120
 size_t          size,
 
121
 size_t*         n_written,
 
122
 const STimeout* timeout)
 
123
{
 
124
    SMemoryConnector* xxx = (SMemoryConnector*) connector->handle;
 
125
    int written;
 
126
 
 
127
    if ( !size )
 
128
        return eIO_Success;
 
129
 
 
130
    MT_LOCK_Do(xxx->lock, eMT_Lock);
 
131
    written = BUF_Write(&xxx->buf, buf, size);
 
132
    MT_LOCK_Do(xxx->lock, eMT_Unlock);
 
133
    if ( !written ) {
 
134
        xxx->w_status = eIO_Unknown;
 
135
        return eIO_Unknown;
 
136
    }
 
137
 
 
138
    *n_written = size;
 
139
    xxx->w_status = eIO_Success;
 
140
    return eIO_Success;
 
141
}
 
142
 
 
143
 
 
144
/*ARGSUSED*/
 
145
static EIO_Status s_VT_Read
 
146
(CONNECTOR       connector,
 
147
 void*           buf,
 
148
 size_t          size,
 
149
 size_t*         n_read,
 
150
 const STimeout* timeout)
 
151
{
 
152
    SMemoryConnector* xxx = (SMemoryConnector*) connector->handle;
 
153
 
 
154
    if ( !size )
 
155
        return eIO_Success;
 
156
 
 
157
    MT_LOCK_Do(xxx->lock, eMT_Lock);
 
158
    *n_read = BUF_Read(xxx->buf, buf, size);
 
159
    MT_LOCK_Do(xxx->lock, eMT_Unlock);
 
160
    if ( !*n_read ) {
 
161
        xxx->r_status = eIO_Closed;
 
162
        return eIO_Closed;
 
163
    }
 
164
    xxx->r_status = eIO_Success;
 
165
 
 
166
    return eIO_Success;
 
167
}
 
168
 
 
169
 
 
170
/*ARGSUSED*/
 
171
static EIO_Status s_VT_Wait
 
172
(CONNECTOR       connector,
 
173
 EIO_Event       event,
 
174
 const STimeout* timeout)
 
175
{
 
176
    return eIO_Success;
 
177
}
 
178
 
 
179
 
 
180
static EIO_Status s_VT_Status
 
181
(CONNECTOR connector,
 
182
 EIO_Event dir)
 
183
{
 
184
    SMemoryConnector* xxx = (SMemoryConnector*) connector->handle;
 
185
 
 
186
    switch (dir) {
 
187
    case eIO_Read:
 
188
        return xxx->r_status;
 
189
    case eIO_Write:
 
190
        return xxx->w_status;
 
191
    default:
 
192
        assert(0); /* should never happen as checked by connection */
 
193
        return eIO_InvalidArg;
 
194
    }
 
195
}
 
196
 
 
197
 
 
198
/*ARGSUSED*/
 
199
static EIO_Status s_VT_Close
 
200
(CONNECTOR       connector,
 
201
 const STimeout* timeout)
 
202
{
 
203
    SMemoryConnector* xxx = (SMemoryConnector*) connector->handle;
 
204
    BUF_Destroy(xxx->buf);
 
205
    return eIO_Success;
 
206
}
 
207
 
 
208
 
 
209
static void s_Setup
 
210
(SMetaConnector* meta,
 
211
 CONNECTOR       connector)
 
212
{
 
213
    /* initialize virtual table */
 
214
    CONN_SET_METHOD(meta, get_type,   s_VT_GetType,   connector);
 
215
    CONN_SET_METHOD(meta, open,       s_VT_Open,      connector);
 
216
    CONN_SET_METHOD(meta, wait,       s_VT_Wait,      connector);
 
217
    CONN_SET_METHOD(meta, write,      s_VT_Write,     connector);
 
218
    CONN_SET_METHOD(meta, flush,      0,              0);
 
219
    CONN_SET_METHOD(meta, read,       s_VT_Read,      connector);
 
220
    CONN_SET_METHOD(meta, status,     s_VT_Status,    connector);
 
221
    CONN_SET_METHOD(meta, close,      s_VT_Close,     connector);
 
222
#ifdef IMPLEMENTED__CONN_WaitAsync
 
223
    CONN_SET_METHOD(meta, wait_async, s_VT_WaitAsync, connector);
 
224
#endif
 
225
    meta->default_timeout = 0/*infinite*/;
 
226
}
 
227
 
 
228
 
 
229
static void s_Destroy
 
230
(CONNECTOR connector)
 
231
{
 
232
    SMemoryConnector* xxx = (SMemoryConnector*) connector->handle;
 
233
 
 
234
    free(xxx);
 
235
    connector->handle = 0;
 
236
    free(connector);
 
237
}
 
238
 
 
239
 
 
240
/***********************************************************************
 
241
 *  EXTERNAL -- the connector's "constructors"
 
242
 ***********************************************************************/
 
243
 
 
244
extern CONNECTOR MEMORY_CreateConnector(MT_LOCK lock)
 
245
{
 
246
    CONNECTOR         ccc = (SConnector*) malloc(sizeof(SConnector));
 
247
    SMemoryConnector* xxx = (SMemoryConnector*) malloc(sizeof(*xxx));
 
248
 
 
249
    /* initialize internal data structures */
 
250
    xxx->lock    = lock;
 
251
 
 
252
    /* initialize connector data */
 
253
    ccc->handle  = xxx;
 
254
    ccc->next    = 0;
 
255
    ccc->meta    = 0;
 
256
    ccc->setup   = s_Setup;
 
257
    ccc->destroy = s_Destroy;
 
258
 
 
259
    return ccc;
 
260
}
 
261
 
 
262
 
 
263
/*
 
264
 * --------------------------------------------------------------------------
 
265
 * $Log: ncbi_memory_connector.c,v $
 
266
 * Revision 6.5  2003/05/31 05:15:39  lavr
 
267
 * Add ARGSUSED where args are meant to be unused, remove Flush
 
268
 *
 
269
 * Revision 6.4  2003/05/14 03:53:13  lavr
 
270
 * Log moved to end
 
271
 *
 
272
 * Revision 6.3  2002/10/22 15:11:24  lavr
 
273
 * Zero connector's handle to crash if revisited
 
274
 *
 
275
 * Revision 6.2  2002/04/26 16:32:49  lavr
 
276
 * Added setting of default timeout in meta-connector's setup routine
 
277
 *
 
278
 * Revision 6.1  2002/02/20 19:14:08  lavr
 
279
 * Initial revision
 
280
 *
 
281
 * ==========================================================================
 
282
 */