1 | /* |
---|
2 | * |
---|
3 | * Copyright (C) 1988, 1989 by the Massachusetts Institute of Technology |
---|
4 | * Developed by the MIT Student Information Processing Board (SIPB). |
---|
5 | * For copying information, see the file mit-copyright.h in this release. |
---|
6 | * |
---|
7 | */ |
---|
8 | /* |
---|
9 | * $Id: tmem.c,v 1.7 1999-01-22 23:10:02 ghudson Exp $ |
---|
10 | * |
---|
11 | * tfile module for ``memory'' tfiles. |
---|
12 | * |
---|
13 | */ |
---|
14 | |
---|
15 | #ifndef lint |
---|
16 | static char rcsid_tmem_c[] = |
---|
17 | "$Id: tmem.c,v 1.7 1999-01-22 23:10:02 ghudson Exp $"; |
---|
18 | #endif /* lint */ |
---|
19 | |
---|
20 | #include <errno.h> |
---|
21 | #include <stdlib.h> |
---|
22 | #include <sys/types.h> |
---|
23 | #include <sys/uio.h> |
---|
24 | #include <discuss/tfile.h> |
---|
25 | |
---|
26 | #define min(x,y) ((x)<(y)?(x):(y)) |
---|
27 | |
---|
28 | enum iovdir { SCATTER, GATHER }; |
---|
29 | |
---|
30 | static int iovmove (direct, iovpp, buf, len) |
---|
31 | register enum iovdir direct; |
---|
32 | struct iovec **iovpp; |
---|
33 | register char *buf; |
---|
34 | register int len; |
---|
35 | { |
---|
36 | register int moved = 0; |
---|
37 | register int count; |
---|
38 | register struct iovec *iovp; |
---|
39 | |
---|
40 | iovp = *iovpp; |
---|
41 | while (len > 0) { |
---|
42 | count = min (iovp->iov_len, len); |
---|
43 | if (direct == SCATTER) |
---|
44 | memcpy (iovp->iov_base, buf, count); |
---|
45 | else |
---|
46 | memcpy (buf, iovp->iov_base, count); |
---|
47 | len -= count; |
---|
48 | moved += count; |
---|
49 | buf += count; |
---|
50 | iovp->iov_base = (char *)iovp->iov_base + count; |
---|
51 | iovp->iov_len -= count; |
---|
52 | if (iovp->iov_len == 0) { |
---|
53 | iovp ++; |
---|
54 | if (iovp->iov_len == 0) |
---|
55 | break; |
---|
56 | } |
---|
57 | } |
---|
58 | *iovpp = iovp; |
---|
59 | return moved; |
---|
60 | } |
---|
61 | |
---|
62 | static int tmem(op, infop, info, argp, argn, result) |
---|
63 | int op, argn; |
---|
64 | register int *result, *info; |
---|
65 | char **infop, *argp; |
---|
66 | { |
---|
67 | *result = 0; /* optimist */ |
---|
68 | switch (op) { |
---|
69 | case TFOPEN: |
---|
70 | case TFCLOSE: |
---|
71 | return 0; |
---|
72 | case TFREAD: |
---|
73 | return iovmove (GATHER, (struct iovec **)infop, |
---|
74 | argp, argn); |
---|
75 | case TFWRITE: |
---|
76 | return iovmove (SCATTER, (struct iovec **)infop, |
---|
77 | argp, argn); |
---|
78 | case TFDESTROY: |
---|
79 | if (*info) |
---|
80 | free (*infop); |
---|
81 | return 0; |
---|
82 | default: |
---|
83 | *result = EINVAL; |
---|
84 | return -1; |
---|
85 | } |
---|
86 | } |
---|
87 | |
---|
88 | tfile mem_tfile (buffer, length) |
---|
89 | char *buffer; |
---|
90 | int length; |
---|
91 | { |
---|
92 | register struct iovec *ts = |
---|
93 | (struct iovec *) malloc (2 * sizeof (struct iovec)); |
---|
94 | ts[0].iov_base = buffer; |
---|
95 | ts[0].iov_len = length; |
---|
96 | ts[1].iov_len = 0; |
---|
97 | return tcreate (length, (char *) ts, 1, tmem); |
---|
98 | } |
---|
99 | |
---|
100 | tfile memv_tfile (vec) |
---|
101 | register struct iovec *vec; |
---|
102 | { |
---|
103 | register int i, len; |
---|
104 | for (i=0, len = 0; vec[i].iov_len; i++) |
---|
105 | len += vec[i].iov_len; |
---|
106 | |
---|
107 | return tcreate (len, (char *) vec, 0, tmem); |
---|
108 | } |
---|