2 * Copyright (c) 2012 The Native Client Authors. All rights reserved.
3 * Use of this source code is governed by a BSD-style license that can be
4 * found in the LICENSE file.
8 * NaCl Generic I/O interface.
11 #ifndef NATIVE_CLIENT_SRC_SHARED_GIO_GIO_H_
12 #define NATIVE_CLIENT_SRC_SHARED_GIO_GIO_H_
14 /* is this needed? - maybe for size_t */
15 #include "native_client/src/include/portability.h"
25 * In the generic I/O package, -1 is used consistently to indicate
26 * error. Check the return value for equality to -1 and do not check
29 * Here's the rationale. Note that the Read and Write virtual
30 * functions, like the read/write system calls, return ssize_t but
31 * takes a count argument that's of type size_t. Since
32 * sizeof(ssize_t) == sizeof(size_t), this means that we either never
33 * perform the full operation if the count argument would be negative
34 * if viewed as an ssize_t, or we just do it and expect the caller to
35 * do the right thing. The only negative value that we reserve as an
36 * error indication is -1, and this is also an unreasonable input
37 * value on any sane, von-Neumann machine: there must be at least one
38 * byte of code in the address space, and at least for Read, is
39 * read-only text, and for Write it is extremely unlikely that any
40 * application will want to write out (almost) the entire address
43 * When -1 is used to indicate an error, errno is set to indicate the
48 * Will implicitly close if not already closed, but no error
49 * reporting, other than possibly logging.
51 void (*Dtor)(struct Gio *vself);
54 * Read virtual fn. Like read syscall: returns number of bytes
55 * actually read, -1 on error, so 0 indcates EOF. Depending on
56 * subclass, there may be short reads even before EOF, but all short
57 * reads must return at least one byte, so that this is
58 * distinguishable from EOF.
60 ssize_t (*Read)(struct Gio *vself,
65 * Write virtual fn. Like write syscall: returns number of bytes
66 * actually written, -1 on error. Depending on subclass, there may
69 ssize_t (*Write)(struct Gio *vself,
74 * Seek virtual function. Like the lseek syscall. whence is one of
75 * SEEK_SET, SEEK_CUR, SEEK_END. There is no ftell -- use
76 * Seek(self, 0, SEEK_CUR) to obtain the current position. Whether
77 * seeking beyond the end of an Gio object and writing results in
78 * defined behavior depends on the subclass involved, i.e., some
79 * subclasses may grow/extend the object (e.g., file, shared
80 * memory), and others may not (e.g., in-memory snapshot).
82 off_t (*Seek)(struct Gio *vself,
86 /* Only used for write, 0 on success, -1 on error */
87 int (*Flush)(struct Gio *vself);
90 * Returns 0 on success, -1 on error. Implicitly Flush. If Flush
91 * succeeds, deallocate system-level resources. After Close, no
92 * other operations should be performed other than Dtor. Close
93 * might be merged with the Dtor, except that the Dtor cannot report
96 int (*Close)(struct Gio *vself);
100 struct GioVtbl const *vtbl;
108 int GioFileCtor(struct GioFile *self,
112 ssize_t GioFileRead(struct Gio *vself,
116 ssize_t GioFileWrite(struct Gio *vself,
120 off_t GioFileSeek(struct Gio *vself,
124 int GioFileFlush(struct Gio *vself);
126 int GioFileClose(struct Gio *vself);
128 void GioFileDtor(struct Gio *vself);
130 int GioFileRefCtor(struct GioFile *self,
134 size_t gprintf(struct Gio *gp,
138 size_t gvprintf(struct Gio *gp,
144 #endif /* NATIVE_CLIENT_SRC_SHARED_GIO_GIO_H_ */