comparison libtests/0001/prog1.c @ 392:78fed0d4c108 api-inversion

Include some necessary information in struct adb. Now the struct adb contains a database fd, the flags used to open that fd (so that we can later tell if it was for write or not) and a database header pointer. audiodb_open() is now responsible for filling in all of that information. To do that, it needs to take an open(2) flag; that's good, because it means that the call to open(2) is no longer invoking undefined behaviour. (Also, the previous version of audiodb_open() leaked an fd). Unfortunately, that means we have broken ABI and API compatibility. (Fortunately, we have fewer than 12 users). Use audiodb_open() in audioDB::initDBHeader(). We've temporarily(?) put acquire_lock(int, bool) in the API header; that means we need to include <stdbool.h> and compile C files with -std=c99. Do so. Make audiodb_close() free resources allocated by audiodb_open(). Include a struct adb * field in the audioDB C++ object... ... which lets us actually implement memory-correctness, by audiodb_close()ing the database in audioDB::cleanup(). [ The lock is, I think, correctly disposed of; man fcntl(2) on Linux says that the locks are released once any file descriptor relating to the file is closed, and we close the fd in audiodb_close(). ]
author mas01cr
date Mon, 24 Nov 2008 15:42:15 +0000
parents 94c18f128ce8
children e072aa1611f5
comparison
equal deleted inserted replaced
391:69f8bf88c0ff 392:78fed0d4c108
31 clean_remove_db(databasename); 31 clean_remove_db(databasename);
32 32
33 /* create new db */ 33 /* create new db */
34 //# creation 34 //# creation
35 //${AUDIODB} -N -d testdb 35 //${AUDIODB} -N -d testdb
36 mydbp=audiodb_open(databasename); 36 mydbp=audiodb_open(databasename,O_RDWR);
37 37
38 38
39 /* open should fail (return NULL), so create a new db */ 39 /* open should fail (return NULL), so create a new db */
40 if (!mydbp){ 40 if (!mydbp){
41 mydbp=audiodb_create(databasename,0,0,0); 41 mydbp=audiodb_create(databasename,0,0,0);
67 } 67 }
68 68
69 69
70 /* should pass now - db exists */ 70 /* should pass now - db exists */
71 //expect_clean_error_exit ${AUDIODB} -N -d testdb 71 //expect_clean_error_exit ${AUDIODB} -N -d testdb
72 mydbp2=audiodb_open(databasename); 72 mydbp2=audiodb_open(databasename, O_RDONLY);
73 if (!mydbp2){ 73 if (!mydbp2){
74 returnval=-1; 74 returnval=-1;
75 } 75 }
76 76
77 //this test would fail at compile time because of the API interface 77 //this test would fail at compile time because of the API interface