# HG changeset patch # User mas01cr # Date 1250026954 0 # Node ID 70acfcb5010a66ebcf45a340e47cf5a97adbfdfc # Parent cfa74bcc12490902dfe02afbd0707ce84c5e609a Remove mmap() from l2norm.cpp We use instead a similarly bad method for dealing with l2norming when there's already data in the database: malloc() a buffer the size of the data. This will likely fail in even more cases than the previous mmap()-the-buffer strategy. diff -r cfa74bcc1249 -r 70acfcb5010a l2norm.cpp --- a/l2norm.cpp Tue Aug 11 21:42:29 2009 +0000 +++ b/l2norm.cpp Tue Aug 11 21:42:34 2009 +0000 @@ -4,32 +4,37 @@ #include "audioDB-internals.h" static int audiodb_l2norm_existing(adb_t *adb) { - double *data_buffer, *l2norm_buffer; - adb_header_t *header = adb->header; - size_t data_buffer_size = align_page_up(header->length); - size_t nvectors = header->length / (sizeof(double) * header->dim); - /* FIXME: this map of the vector data will lose if we ever turn the - * l2norm flag on when we have already inserted a large number of - * vectors, as the mmap() will fail. "Don't do that, then" is one - * possible answer. */ - mmap_or_goto_error(double *, data_buffer, header->dataOffset, data_buffer_size); + double *data_buffer = 0, *l2norm_buffer = 0; + size_t data_buffer_size = align_page_up(adb->header->length); + size_t nvectors = adb->header->length / (sizeof(double) * adb->header->dim); + /* FIXME: this allocation of the vector data buffer will lose if we + * ever turn the l2norm flag on when we have already inserted a + * large number of vectors, as the malloc() will fail. "Don't do + * that, then" is one possible answer. */ + data_buffer = (double *) malloc(data_buffer_size); + if (!data_buffer) { + goto error; + } + lseek_set_or_goto_error(adb->fd, adb->header->dataOffset); + read_or_goto_error(adb->fd, data_buffer, data_buffer_size); + l2norm_buffer = (double *) malloc(nvectors * sizeof(double)); if(!l2norm_buffer) { goto error; } - audiodb_l2norm_buffer(data_buffer, header->dim, nvectors, l2norm_buffer); - if(lseek(adb->fd, adb->header->l2normTableOffset, SEEK_SET) == (off_t) -1) { - goto error; - } + audiodb_l2norm_buffer(data_buffer, adb->header->dim, nvectors, l2norm_buffer); + lseek_set_or_goto_error(adb->fd, adb->header->l2normTableOffset); write_or_goto_error(adb->fd, l2norm_buffer, nvectors * sizeof(double)); - munmap(data_buffer, data_buffer_size); free(l2norm_buffer); + free(data_buffer); return 0; error: - maybe_munmap(data_buffer, data_buffer_size); + if(data_buffer) { + free(data_buffer); + } if(l2norm_buffer) { free(l2norm_buffer); } @@ -37,16 +42,16 @@ } int audiodb_l2norm(adb_t *adb) { - adb_header_t *header = adb->header; if(!(adb->flags & O_RDWR)) { return 1; } - if(header->flags & ADB_HEADER_FLAG_L2NORM) { + if(adb->header->flags & ADB_HEADER_FLAG_L2NORM) { /* non-error code for forthcoming backwards-compatibility * reasons */ return 0; } - if((!(header->flags & ADB_HEADER_FLAG_REFERENCES)) && (header->length > 0)) { + if((!(adb->header->flags & ADB_HEADER_FLAG_REFERENCES)) && + (adb->header->length > 0)) { if(audiodb_l2norm_existing(adb)) { goto error; }