# HG changeset patch # User jbm@humility # Date 1254206348 25200 # Node ID 21a6c17f4e3e9c20f66abec2556cd91c128c8d20 # Parent 2e44bc2caefe751130d68137d90692b6d2c4cb01 jbm: it compiles... but bombs in unit tests diff -r 2e44bc2caefe -r 21a6c17f4e3e include/Base.h --- a/include/Base.h Sat Sep 26 12:45:03 2009 -0700 +++ b/include/Base.h Mon Sep 28 23:39:08 2009 -0700 @@ -12,9 +12,20 @@ #include #include #include + +#if OSX +/* OS X specific bits */ #include #include +#else +/* OS X fixup kludge bits */ + +#include +#include + +#endif + namespace Mooseyard { /** A 32-bit file position/offset. Supports files of up to 4GB. */ @@ -110,4 +121,4 @@ } -#endif _MOOSEYARD_BASE_ +#endif /* _MOOSEYARD_BASE_ */ diff -r 2e44bc2caefe -r 21a6c17f4e3e include/File.h --- a/include/File.h Sat Sep 26 12:45:03 2009 -0700 +++ b/include/File.h Mon Sep 28 23:39:08 2009 -0700 @@ -31,7 +31,7 @@ File (const char *filename, bool writeable =false, bool create =false) throw(Error); - File (const char *filename, int oflag) throw(Error); + File (const char *filename, int oflag, bool locked) throw(Error); ~File(); off_t length() const throw(Error); @@ -89,9 +89,11 @@ private: static int _open (const char *filename, int posixMode) throw(Error); + static int _check (int result) throw(Error); +#ifdef OSX /* On linux, these are the same type signature to gcc */ static ssize_t _check (ssize_t result) throw(Error) {_check((int)result); return result;} static off_t _check (off_t result) throw(Error) {_check((int)result); return result;} - static int _check (int result) throw(Error); +#endif static void _checkRead (ssize_t result, size_t expectedSize) throw(Error); bool _lock (bool block); void _unlock(); @@ -106,4 +108,4 @@ } -#endif _MOOSEYARD_FILE_ +#endif /* _MOOSEYARD_FILE_ */ diff -r 2e44bc2caefe -r 21a6c17f4e3e include/cf_fixup.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/include/cf_fixup.h Mon Sep 28 23:39:08 2009 -0700 @@ -0,0 +1,9 @@ +typedef double CFSwappedFloat64; + +static inline CFSwappedFloat64 CFConvertDoubleHostToSwapped(double d) { return d; }; +static inline double CFConvertDoubleSwappedToHost(CFSwappedFloat64 d) { return d; }; + +#define OSSwapHostToLittleInt32(x) (x) +#define OSSwapHostToLittleInt16(x) (x) +#define OSSwapLittleToHostInt32(x) (x) +#define OSSwapLittleToHostInt16(x) (x) diff -r 2e44bc2caefe -r 21a6c17f4e3e src/Base.cpp --- a/src/Base.cpp Sat Sep 26 12:45:03 2009 -0700 +++ b/src/Base.cpp Mon Sep 28 23:39:08 2009 -0700 @@ -11,7 +11,10 @@ #include "File.h" #include #include + +#if USE_REMOVED_UUID #include +#endif namespace Mooseyard { @@ -39,7 +42,7 @@ #pragma mark - #pragma mark UUID: -#if 0 +#if USE_REMOVED_UUID /** Typical 128-bit universally-unique identifier. */ class UUID { public: diff -r 2e44bc2caefe -r 21a6c17f4e3e src/File.cpp --- a/src/File.cpp Sat Sep 26 12:45:03 2009 -0700 +++ b/src/File.cpp Mon Sep 28 23:39:08 2009 -0700 @@ -10,6 +10,7 @@ #include "File.h" #include "MemoryMap.h" +#include #include #include #include @@ -26,11 +27,19 @@ _locked(0) { } - File::File (const char *filename, int oflag) throw(Error) + File::File (const char *filename, int oflag, bool locked) throw(Error) +#if BSD + :_fd(_check( ::open(filename, oflag | (locked ? O_EXLOCK : 0), 0644) )), +#else :_fd(_check( ::open(filename, oflag, 0644) )), +#endif _memoryMap(NULL), - _locked( (oflag | O_EXLOCK) ?1 :0) - { } + _locked( locked ? 1 : 0) + { +#if !BSDISH + _check( ::flock(_fd, LOCK_EX) ); +#endif + } File::~File() { delete _memoryMap; @@ -102,7 +111,11 @@ } void File::flushDisk() throw(Error) { +#if OSX _check( ::fcntl(_fd,F_FULLFSYNC) ); +#else + _check( fsync(_fd) ); +#endif } bool File::hasPath (const char *path) const throw(Error) { diff -r 2e44bc2caefe -r 21a6c17f4e3e src/Hash.cpp --- a/src/Hash.cpp Sat Sep 26 12:45:03 2009 -0700 +++ b/src/Hash.cpp Mon Sep 28 23:39:08 2009 -0700 @@ -9,6 +9,7 @@ #include "Hash.h" #include "Dictionary.h" +#include #include namespace Mooseyard { @@ -110,7 +111,7 @@ for (int index=0; index<_tableSize; index++) if (_table[index].hasValue()) { const Key &key = _table[index].key(); - printf("%3i: %*s -> %p\n", + ::printf("%3i: %*s -> %p\n", index, (int)key.length,(const char*)key.bytes, _table[index].value()); } } diff -r 2e44bc2caefe -r 21a6c17f4e3e src/Index.cpp --- a/src/Index.cpp Sat Sep 26 12:45:03 2009 -0700 +++ b/src/Index.cpp Mon Sep 28 23:39:08 2009 -0700 @@ -16,6 +16,12 @@ #include #include +#define __STDC_LIMIT_MACROS /* I <3 you, C99 */ +# define UINT32_MAX (4294967295U) + +#include + + namespace Mooseyard { // Quadratic probing results in fewer bucket tests on lookup, but makes the hash tables diff -r 2e44bc2caefe -r 21a6c17f4e3e src/Ottoman.cpp --- a/src/Ottoman.cpp Sat Sep 26 12:45:03 2009 -0700 +++ b/src/Ottoman.cpp Mon Sep 28 23:39:08 2009 -0700 @@ -42,7 +42,6 @@ Ottoman::Ottoman() :_writeable(true), - _file(NULL), _filename(NULL), _lastVersion(NULL), _current( new OverlayDictionary(&Dictionary::kEmpty) ) @@ -97,9 +96,6 @@ delete _file; _file = curFile; } - - if (changed) - versionsChanged(); return changed; } @@ -200,12 +196,11 @@ if (!lastVersion) lastVersion = new VersionDictionary(dstFile); VersionDictionary *saved = lastVersion->_appendAndOpen(_current->overlay(), - dstFile, - _current->baseReplaced()); + dstFile, + _current->baseReplaced()); // (don't delete _lastVersion: saved->_previousVersion now points to it.) _lastVersion = saved; _current->revertTo(_lastVersion); - versionsChanged(); } bool Ottoman::save() { @@ -221,10 +216,10 @@ } File* Ottoman::_writeTo (const char *dstFileName, bool overwriteAllowed) { - int mode = O_RDWR | O_CREAT | O_TRUNC | O_EXLOCK; + int mode = O_RDWR | O_CREAT | O_TRUNC; if (!overwriteAllowed) mode |= O_EXCL; - File *dstFile = new File(dstFileName, mode); + File *dstFile = new File(dstFileName, mode, true); try { dstFile->write(DictionaryFileHeader()); _append(dstFile); @@ -240,7 +235,6 @@ File *dstFile = _writeTo(dstFileName, overwriteAllowed); free(_filename); _filename = strdup(dstFileName); - delete _file; _file = dstFile; } @@ -248,8 +242,8 @@ if (!_file) return false; char tempFileName[1024]; - strlcpy(tempFileName, _filename, sizeof(tempFileName)-1); - strlcat(tempFileName, "~", sizeof(tempFileName)); + ::strncpy(tempFileName, _filename, sizeof(tempFileName)-1); + ::strncat(tempFileName, "~", sizeof(tempFileName)); File *tempFile; { // Check for conflict in existing file: diff -r 2e44bc2caefe -r 21a6c17f4e3e src/VersionDictionary.cpp --- a/src/VersionDictionary.cpp Sat Sep 26 12:45:03 2009 -0700 +++ b/src/VersionDictionary.cpp Mon Sep 28 23:39:08 2009 -0700 @@ -15,6 +15,10 @@ #include #include #include +#include + +# define UINT32_MAX (4294967295U) + namespace Mooseyard { diff -r 2e44bc2caefe -r 21a6c17f4e3e test/TestUtils.cpp --- a/test/TestUtils.cpp Sat Sep 26 12:45:03 2009 -0700 +++ b/test/TestUtils.cpp Mon Sep 28 23:39:08 2009 -0700 @@ -11,6 +11,7 @@ #include "File.h" #include #include +#include namespace Mooseyard { @@ -24,7 +25,9 @@ void shuffle(int a[], int n, unsigned seed) { if (seed==0) { +#ifdef BSD srandomdev(); +#endif seed = random(); } fprintf(stderr,"shuffle(n=%i, seed=%u)\n", n,seed); @@ -46,15 +49,18 @@ sNWords = 0; FILE *in = fopen("/usr/share/dict/words", "r"); - const char *word; + char word[4096]; size_t wordLen; - while (NULL != (word=fgetln(in,&wordLen))) { - if (word[wordLen-1]=='\n') - wordLen--; - char *str = new char[wordLen+1]; - memcpy(str, word, wordLen); - str[wordLen] = 0; - sWords[sNWords] = str; + while (NULL != ::fgets(word, 4096, in)) { + wordLen = ::strlen(word); + if (word[wordLen-1]=='\n') { + word[wordLen-1] = '\0'; + wordLen--; + } + + if (wordLen == 0) continue; + + sWords[sNWords] = strdup(word); //if( sNWords % 10000 == 0) // printf("'%s' ... ", sWords[sNWords]->string()); sNWords++; @@ -93,7 +99,9 @@ using namespace Mooseyard; int main(int argc, char **argv) { +#if BSD srandomdev(); +#endif try { ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); diff -r 2e44bc2caefe -r 21a6c17f4e3e test/VersionDictionary_test.cpp --- a/test/VersionDictionary_test.cpp Sat Sep 26 12:45:03 2009 -0700 +++ b/test/VersionDictionary_test.cpp Mon Sep 28 23:39:08 2009 -0700 @@ -46,16 +46,16 @@ TEST(File,HasPath) { { - File f("/tmp/jubba", O_RDWR | O_CREAT | O_TRUNC); + File f("/tmp/jubba", O_RDWR | O_CREAT | O_TRUNC, true); f.write("howdy"); } { - File f("/tmp/jubba", O_RDWR); + File f("/tmp/jubba", O_RDWR, true); EXPECT_TRUE(f.hasPath("/tmp/jubba")); File::unlink("/tmp/jubba"); EXPECT_FALSE(f.hasPath("/tmp/jubba")); - - File f2("/tmp/jubba", O_RDWR | O_CREAT | O_TRUNC); + + File f2("/tmp/jubba", O_RDWR | O_CREAT | O_TRUNC, true); f2.write("howdy"); f2.flush(); @@ -108,7 +108,7 @@ time_t startTime = ::time(NULL); time_t createTime; { - File file("/tmp/hashfiletest", O_RDWR | O_CREAT | O_TRUNC); + File file("/tmp/hashfiletest", O_RDWR | O_CREAT | O_TRUNC, true); VersionDictionary *hf; { Timer t("Creating & writing VersionDictionary", nWords); @@ -152,7 +152,7 @@ } { printf("Opening OverlayVersionDictionary...\n"); - File file("/tmp/hashfiletest", O_RDWR); + File file("/tmp/hashfiletest", O_RDWR, true); OverlayVersionDictionary hf(&file); EXPECT_EQ( nWords , hf.count() ); EXPECT_TRUE(hf.get("abatement").equals("abatement")); @@ -225,7 +225,7 @@ File oldFile("/tmp/hashfiletest"); OverlayVersionDictionary oldhf(&oldFile); - File newFile("/tmp/hashfiletest2", O_RDWR | O_CREAT | O_TRUNC); + File newFile("/tmp/hashfiletest2", O_RDWR | O_CREAT | O_TRUNC, true); newFile.write("Ha5h", 4); // VersionDictionary won't write to an empty file oldhf.saveAs(&newFile); printf("File size: %llu bytes\n", newFile.length());