view audioDB-internals.h @ 401:a8a5f2ca5380 api-inversion

Invert audioDB::l2norm / audiodb_l2norm() We now have some functions that shouldn't be exported to the user of the library, but are used in more than one source file; case in point: audiodb_sync_header(). Make a new audioDB-internals.h file for them, and while we're at it put the scary mmap()-related macros in there too. We can't delete audioDB::unitNormAndInsertL2() quite yet, because it's used in insertion too, but we can delete the non-append branches. That's not very much code to lose, but every little helps.
author mas01cr
date Wed, 03 Dec 2008 14:53:20 +0000
parents
children 58b88ab69424
line wrap: on
line source
/* 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;
}