comparison open.cpp @ 592:cfa74bcc1249

Remove uses of mmap() from open.cpp (These are relatively easy to deal with; just replace them with an lseek()/read() pair. Of course, now we're destructively modifying the fd position; I'd have liked to use pread() but, surprise, that's not available on mingw.)
author mas01cr
date Tue, 11 Aug 2009 21:42:29 +0000
parents cc2b97d020b1
children 6ad0a6e67d4c
comparison
equal deleted inserted replaced
591:0242e5d1643c 592:cfa74bcc1249
26 size_t key_table_length = 0; 26 size_t key_table_length = 0;
27 27
28 if(adb->header->length > 0) { 28 if(adb->header->length > 0) {
29 unsigned nfiles = adb->header->numFiles; 29 unsigned nfiles = adb->header->numFiles;
30 key_table_length = align_page_up(nfiles * ADB_FILETABLE_ENTRY_SIZE); 30 key_table_length = align_page_up(nfiles * ADB_FILETABLE_ENTRY_SIZE);
31 mmap_or_goto_error(char *, key_table, adb->header->fileTableOffset, key_table_length); 31 key_table = (char *)malloc(key_table_length);
32 if(!key_table) {
33 goto error;
34 }
35 lseek_set_or_goto_error(adb->fd, adb->header->fileTableOffset);
36 read_or_goto_error(adb->fd, key_table, key_table_length);
32 for (unsigned int k = 0; k < nfiles; k++) { 37 for (unsigned int k = 0; k < nfiles; k++) {
33 adb->keys->push_back(key_table + k*ADB_FILETABLE_ENTRY_SIZE); 38 adb->keys->push_back(key_table + k*ADB_FILETABLE_ENTRY_SIZE);
34 (*adb->keymap)[(key_table + k*ADB_FILETABLE_ENTRY_SIZE)] = k; 39 (*adb->keymap)[(key_table + k*ADB_FILETABLE_ENTRY_SIZE)] = k;
35 } 40 }
36 munmap(key_table, key_table_length); 41 free(key_table);
37 } 42 }
38 43
39 return 0; 44 return 0;
40 45
41 error: 46 error:
42 maybe_munmap(key_table, key_table_length); 47 if(key_table) {
48 free(key_table);
49 }
43 return 1; 50 return 1;
44 } 51 }
45 52
46 static int audiodb_collect_track_lengths(adb_t *adb) { 53 static int audiodb_collect_track_lengths(adb_t *adb) {
47 uint32_t *track_table = 0; 54 uint32_t *track_table = 0;
48 size_t track_table_length = 0; 55 size_t track_table_length = 0;
49 if(adb->header->length > 0) { 56 if(adb->header->length > 0) {
50 unsigned nfiles = adb->header->numFiles; 57 unsigned nfiles = adb->header->numFiles;
51 track_table_length = align_page_up(nfiles * ADB_TRACKTABLE_ENTRY_SIZE); 58 track_table_length = align_page_up(nfiles * ADB_TRACKTABLE_ENTRY_SIZE);
52 mmap_or_goto_error(uint32_t *, track_table, adb->header->trackTableOffset, track_table_length); 59 track_table = (uint32_t *) malloc(track_table_length);
60 if (!track_table) {
61 goto error;
62 }
63 lseek_set_or_goto_error(adb->fd, adb->header->trackTableOffset);
64 read_or_goto_error(adb->fd, track_table, track_table_length);
53 off_t offset = 0; 65 off_t offset = 0;
54 for (unsigned int k = 0; k < nfiles; k++) { 66 for (unsigned int k = 0; k < nfiles; k++) {
55 uint32_t track_length = track_table[k]; 67 uint32_t track_length = track_table[k];
56 adb->track_lengths->push_back(track_length); 68 adb->track_lengths->push_back(track_length);
57 adb->track_offsets->push_back(offset); 69 adb->track_offsets->push_back(offset);
58 offset += track_length * adb->header->dim * sizeof(double); 70 offset += track_length * adb->header->dim * sizeof(double);
59 } 71 }
60 munmap(track_table, track_table_length); 72 free(track_table);
61 } 73 }
62 74
63 return 0; 75 return 0;
64 76
65 error: 77 error:
66 maybe_munmap(track_table, track_table_length); 78 if(track_table) {
79 free(track_table);
80 }
67 return 1; 81 return 1;
68 } 82 }
69 83
70 adb_t *audiodb_open(const char *path, int flags) { 84 adb_t *audiodb_open(const char *path, int flags) {
71 adb_t *adb = 0; 85 adb_t *adb = 0;