1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
|
/*
* Copyright (c) 1996-1998 University of Utah and the Flux Group.
* All rights reserved.
*
* This file is part of the Flux OSKit. The OSKit is free software, also known
* as "open source;" you can redistribute it and/or modify it under the terms
* of the GNU General Public License (GPL), version 2, as published by the Free
* Software Foundation (FSF). To explore alternate licensing terms, contact
* the University of Utah at csl-dist@cs.utah.edu or +1-801-585-3271.
*
* The OSKit is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GPL for more details. You should have
* received a copy of the GPL along with the OSKit; see the file COPYING. If
* not, write to the FSF, 59 Temple Place #330, Boston, MA 02111-1307, USA.
*/
/*
* Blkio wrapper.
*/
#include <stdlib.h>
#include <string.h>
#include <oskit/com.h>
#include <oskit/com/wrapper.h>
#include <oskit/io/blkio.h>
struct gbio {
oskit_blkio_t blkioi; /* COM blkio interface */
int count;
oskit_blkio_t *w_blkioi; /* Wrapped COM blk I/O interface */
void (*before)(void *);
void (*after)(void *);
void *cookie;
};
#define WRAPPERCALL(g, ops, iface, call, args...) ({ \
oskit_error_t __err; \
g->before(g->cookie); \
__err = oskit_##iface##_##call(g->w_##ops ,##args); \
DEBUGWRAPPER(iface, call, __err); \
g->after(g->cookie); \
__err; })
#define WRAPPERCALL_NOARGS(g, ops, iface, call) ({ \
oskit_error_t __err; \
g->before(g->cookie); \
__err = oskit_##iface##_##call(g->w_##ops); \
DEBUGWRAPPER(iface, call, __err); \
g->after(g->cookie); \
__err; })
/*
* oskit_blkio methods.
*/
static OSKIT_COMDECL
blkio_query(oskit_blkio_t *b, const struct oskit_guid *iid, void **out_ihandle)
{
struct gbio *bio = (struct gbio *) b;
if (!bio || !bio->count)
return OSKIT_E_INVALIDARG;
if (memcmp(iid, &oskit_iunknown_iid, sizeof(*iid)) == 0 ||
memcmp(iid, &oskit_blkio_iid, sizeof(*iid)) == 0) {
*out_ihandle = &bio->blkioi;
bio->before(bio->cookie);
++bio->count;
bio->after(bio->cookie);
return 0;
}
*out_ihandle = NULL;
return OSKIT_E_NOINTERFACE;
}
static OSKIT_COMDECL_U
blkio_addref(oskit_blkio_t *b)
{
struct gbio *bio = (struct gbio *) b;
unsigned newcount;
if (!bio || !bio->count)
return OSKIT_E_INVALIDARG;
bio->before(bio->cookie);
newcount = ++bio->count;
bio->after(bio->cookie);
return newcount;
}
static OSKIT_COMDECL_U
blkio_release(oskit_blkio_t *b)
{
struct gbio *bio = (struct gbio *) b;
unsigned newcount;
if (!bio || !bio->count)
return OSKIT_E_INVALIDARG;
bio->before(bio->cookie);
if ((newcount = --bio->count) == 0) {
if (bio->w_blkioi)
oskit_blkio_release(bio->w_blkioi);
bio->after(bio->cookie);
free(bio);
}
else
bio->after(bio->cookie);
return newcount;
}
/*
* Return the block size of this block I/O object.
*/
static OSKIT_COMDECL_U
blkio_getblocksize(oskit_blkio_t *b)
{
struct gbio *bio = (struct gbio *) b;
oskit_error_t err;
if (!bio || !bio->count)
return OSKIT_E_INVALIDARG;
err = WRAPPERCALL_NOARGS(bio, blkioi, blkio, getblocksize);
return err;
}
/*
* Read from the block I/O subset object,
* clipping accesses to the range of this subset.
*/
static OSKIT_COMDECL
blkio_read(oskit_blkio_t *b, void *buf,
oskit_off_t off, oskit_size_t count, oskit_size_t *amount)
{
struct gbio *bio = (struct gbio *) b;
oskit_error_t err;
if (!bio || !bio->count)
return OSKIT_E_INVALIDARG;
err = WRAPPERCALL(bio, blkioi, blkio, read, buf, off, count, amount);
return err;
}
/*
* Write data to the block I/O subset object,
* clipping accesses to the range of this subset.
*/
static OSKIT_COMDECL
blkio_write(oskit_blkio_t *b, const void *buf,
oskit_off_t off, oskit_size_t count, oskit_size_t *amount)
{
struct gbio *bio = (struct gbio *) b;
oskit_error_t err;
if (!bio || !bio->count)
return OSKIT_E_INVALIDARG;
err = WRAPPERCALL(bio, blkioi, blkio, write, buf, off, count, amount);
return err;
}
/*
* Return the size of this subset I/O object.
*/
static OSKIT_COMDECL
blkio_getsize(oskit_blkio_t *b, oskit_off_t *out_size)
{
struct gbio *bio = (struct gbio *) b;
oskit_error_t err;
if (!bio || !bio->count)
return OSKIT_E_INVALIDARG;
err = WRAPPERCALL(bio, blkioi, blkio, getsize, out_size);
return err;
}
/*
* Attempt to set the size of this object,
* which never succeeds in our case.
*/
static OSKIT_COMDECL
blkio_setsize(oskit_blkio_t *b, oskit_off_t new_size)
{
return OSKIT_E_NOTIMPL;
}
/*
* Operations vector for the subset block I/O interface.
*/
static struct oskit_blkio_ops blkio_ops = {
blkio_query,
blkio_addref,
blkio_release,
blkio_getblocksize,
blkio_read,
blkio_write,
blkio_getsize,
blkio_setsize
};
/*
* oskit_blkio wrapper constructor
*/
oskit_error_t
oskit_wrap_blkio(oskit_blkio_t *in,
void (*before)(),
void (*after)(),
void *cookie,
oskit_blkio_t **out)
{
struct gbio *newbio = malloc(sizeof(*newbio));
if (newbio == NULL)
return OSKIT_ENOMEM;
memset(newbio, 0, sizeof(*newbio));
newbio->count = 1;
newbio->blkioi.ops = &blkio_ops;
newbio->w_blkioi = in;
oskit_blkio_addref(in);
newbio->before = before;
newbio->after = after;
newbio->cookie = cookie;
*out = &newbio->blkioi;
return 0;
}
|