view audioDB-internals.h @ 402:58b88ab69424 api-inversion

Move the struct adb definition from the auidioDB_API.h into the audioDB-internals.h header file, leaving only the typedef behind. Thus a user of the API sees only an incomplete type, which cannot be instantiated (but /pointers/ to it can); there's then less temptation to break the abstraction barrier by using structure fields in client code. Not only that, but we can now safely put C++ stuff in the structure. Take advantage of this by putting a std::set<std::string> in there, to hold all the keys currently in the database; populate this field on audiodb_open() (and delete it on audiodb_close). This will be useful when we come to implement variants of audiodb_insert().
author mas01cr
date Wed, 03 Dec 2008 17:40:15 +0000
parents a8a5f2ca5380
children f0a69693eaef
line wrap: on
line source
struct adb {
  char *path;
  int fd;
  int flags;
  adb_header_t *header;
  std::set<std::string> *keys;
};

/* We could go gcc-specific here and use typeof() instead of passing
 * in an explicit type.  Answers on a postcard as to whether that's a
 * good plan or not. */
#define mmap_or_goto_error(type, var, start, length) \
  { void *tmp = mmap(0, length, PROT_READ, MAP_SHARED, adb->fd, (start)); \
    if(tmp == (void *) -1) { \
      goto error; \
    } \
    var = (type) tmp; \
  }

#define maybe_munmap(table, length) \
  { if(table) { \
      munmap(table, length); \
    } \
  }

static inline int audiodb_sync_header(adb_t *adb) {
  off_t pos;
  pos = lseek(adb->fd, (off_t) 0, SEEK_CUR);
  if(pos == (off_t) -1) {
    goto error;
  }
  if(lseek(adb->fd, (off_t) 0, SEEK_SET) == (off_t) -1) {
    goto error;
  }
  if(write(adb->fd, adb->header, O2_HEADERSIZE) != O2_HEADERSIZE) {
    goto error;
  }

  /* can be fsync() if fdatasync() is racily exciting and new */
  fdatasync(adb->fd);
  if(lseek(adb->fd, pos, SEEK_SET) == (off_t) -1) {
    goto error;
  }
  return 0;

 error:
  return 1;
}