annotate audioDB-internals.h @ 410:d7e590d58c85 api-inversion

Pavlovian response to compiler warnings... ... attempt to squash them. For now we can get most of the way by writing a simple write_or_goto_error() macro for write(), and the equivalent for read(). One of the warnings, for the return value of chdir(), is silly, because we're already in an error case, and we really can't do anything sensible if the chdir fails. Try to deal with it anyway.
author mas01cr
date Thu, 11 Dec 2008 08:54:01 +0000
parents f0a69693eaef
children 6e6f4c1cc14d
rev   line source
mas01cr@408 1 typedef struct adb_datum_internal {
mas01cr@408 2 uint32_t nvectors;
mas01cr@408 3 uint32_t dim;
mas01cr@408 4 const char *key;
mas01cr@408 5 void *data;
mas01cr@408 6 void *times;
mas01cr@408 7 void *power;
mas01cr@408 8 } adb_datum_internal_t;
mas01cr@408 9
mas01cr@402 10 struct adb {
mas01cr@402 11 char *path;
mas01cr@402 12 int fd;
mas01cr@402 13 int flags;
mas01cr@402 14 adb_header_t *header;
mas01cr@402 15 std::set<std::string> *keys;
mas01cr@402 16 };
mas01cr@402 17
mas01cr@401 18 /* We could go gcc-specific here and use typeof() instead of passing
mas01cr@401 19 * in an explicit type. Answers on a postcard as to whether that's a
mas01cr@401 20 * good plan or not. */
mas01cr@401 21 #define mmap_or_goto_error(type, var, start, length) \
mas01cr@401 22 { void *tmp = mmap(0, length, PROT_READ, MAP_SHARED, adb->fd, (start)); \
mas01cr@401 23 if(tmp == (void *) -1) { \
mas01cr@401 24 goto error; \
mas01cr@401 25 } \
mas01cr@401 26 var = (type) tmp; \
mas01cr@401 27 }
mas01cr@401 28
mas01cr@401 29 #define maybe_munmap(table, length) \
mas01cr@401 30 { if(table) { \
mas01cr@401 31 munmap(table, length); \
mas01cr@401 32 } \
mas01cr@401 33 }
mas01cr@401 34
mas01cr@410 35 #define write_or_goto_error(fd, buffer, size) \
mas01cr@410 36 { ssize_t tmp = size; \
mas01cr@410 37 if(write(fd, buffer, size) != tmp) { \
mas01cr@410 38 goto error; \
mas01cr@410 39 } \
mas01cr@410 40 }
mas01cr@410 41
mas01cr@410 42 #define read_or_goto_error(fd, buffer, size) \
mas01cr@410 43 { ssize_t tmp = size; \
mas01cr@410 44 if(read(fd, buffer, size) != tmp) { \
mas01cr@410 45 goto error; \
mas01cr@410 46 } \
mas01cr@410 47 }
mas01cr@410 48
mas01cr@401 49 static inline int audiodb_sync_header(adb_t *adb) {
mas01cr@401 50 off_t pos;
mas01cr@401 51 pos = lseek(adb->fd, (off_t) 0, SEEK_CUR);
mas01cr@401 52 if(pos == (off_t) -1) {
mas01cr@401 53 goto error;
mas01cr@401 54 }
mas01cr@401 55 if(lseek(adb->fd, (off_t) 0, SEEK_SET) == (off_t) -1) {
mas01cr@401 56 goto error;
mas01cr@401 57 }
mas01cr@401 58 if(write(adb->fd, adb->header, O2_HEADERSIZE) != O2_HEADERSIZE) {
mas01cr@401 59 goto error;
mas01cr@401 60 }
mas01cr@401 61
mas01cr@401 62 /* can be fsync() if fdatasync() is racily exciting and new */
mas01cr@401 63 fdatasync(adb->fd);
mas01cr@401 64 if(lseek(adb->fd, pos, SEEK_SET) == (off_t) -1) {
mas01cr@401 65 goto error;
mas01cr@401 66 }
mas01cr@401 67 return 0;
mas01cr@401 68
mas01cr@401 69 error:
mas01cr@401 70 return 1;
mas01cr@401 71 }