--- cyrusdb_skiplist.c 2009-06-19 09:30:50.412806000 -0400 +++ /afs/isis.unc.edu/home/b/a/baconm/cyrusdb_skiplist.c 2009-06-19 16:58:11.000001000 -0400 @@ -39,12 +39,12 @@ * AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. * - * $Id: cyrusdb_skiplist.c,v 1.64 2008/10/08 15:47:08 murch Exp $ + * $Id: cyrusdb_skiplist.c,v 1.66 2009/05/05 01:15:50 brong Exp $ */ /* xxx check retry_xxx for failure */ -/* xxx all offsets should be bit32s i think */ +/* xxx all offsets should be uint32_ts i think */ #include @@ -55,6 +55,7 @@ #include #include #include +#include #include #ifdef HAVE_UNISTD_H #include @@ -191,18 +192,6 @@ static time_t global_recovery = 0; static struct db_list *open_db = NULL; -#define BIT32_MAX 4294967295U - -#if UINT_MAX == BIT32_MAX -typedef unsigned int bit32; -#elif ULONG_MAX == BIT32_MAX -typedef unsigned long bit32; -#elif USHRT_MAX == BIT32_MAX -typedef unsigned short bit32; -#else -#error dont know what to use for bit32 -#endif - /* Perform an FSYNC/FDATASYNC if we are *not* operating in UNSAFE mode */ #define DO_FSYNC (!libcyrus_config_getswitch(CYRUSOPT_SKIPLIST_UNSAFE)) @@ -237,7 +226,7 @@ { char sfile[1024]; int fd, r = 0; - bit32 net32_time; + uint32_t net32_time; snprintf(sfile, sizeof(sfile), "%s/skipstamp", dbdir); @@ -250,7 +239,7 @@ if (fd == -1) r = -1; if (r != -1) r = ftruncate(fd, 0); - net32_time = htonl(global_recovery); + net32_time = htonl((uint32_t)global_recovery); if (r != -1) r = write(fd, &net32_time, 4); if (r != -1) r = close(fd); @@ -272,7 +261,7 @@ sfile); global_recovery = 0; } else { - global_recovery = ntohl(net32_time); + global_recovery = ntohl((uint32_t)net32_time); } } @@ -368,11 +357,11 @@ ... } struct dummy { - bit32 t = htonl(DUMMY); - bit32 ks = 0; - bit32 ds = 0; - bit32 forward[db->maxlevel]; - bit32 pad = -1; + uint32_t t = htonl(DUMMY); + uint32_t ks = 0; + uint32_t ds = 0; + uint32_t forward[db->maxlevel]; + uint32_t pad = -1; } */ #define DUMMY_OFFSET(db) (HEADER_SIZE) #define DUMMY_PTR(db) ((db)->map_base + HEADER_SIZE) @@ -381,11 +370,11 @@ /* bump to the next multiple of 4 bytes */ #define ROUNDUP(num) (((num) + 3) & 0xFFFFFFFC) -#define TYPE(ptr) (ntohl(*((bit32 *)(ptr)))) +#define TYPE(ptr) (ntohl(*((uint32_t *)(ptr)))) #define KEY(ptr) ((ptr) + 8) -#define KEYLEN(ptr) (ntohl(*((bit32 *)((ptr) + 4)))) +#define KEYLEN(ptr) (ntohl(*((uint32_t *)((ptr) + 4)))) #define DATA(ptr) ((ptr) + 8 + ROUNDUP(KEYLEN(ptr)) + 4) -#define DATALEN(ptr) (ntohl(*((bit32 *)((ptr) + 8 + ROUNDUP(KEYLEN(ptr)))))) +#define DATALEN(ptr) (ntohl(*((uint32_t *)((ptr) + 8 + ROUNDUP(KEYLEN(ptr)))))) #define FIRSTPTR(ptr) ((ptr) + 8 + ROUNDUP(KEYLEN(ptr)) + 4 + ROUNDUP(DATALEN(ptr))) /* return a pointer to the pointer */ @@ -395,16 +384,16 @@ * given a pointer to the start of the record, return the offset * corresponding to the xth pointer */ -#define FORWARD(ptr, x) (ntohl(*((bit32 *)(FIRSTPTR(ptr) + 4 * (x))))) +#define FORWARD(ptr, x) (ntohl(*((uint32_t *)(FIRSTPTR(ptr) + 4 * (x))))) /* how many levels does this record have? */ static unsigned LEVEL(const char *ptr) { - const bit32 *p, *q; + const uint32_t *p, *q; assert(TYPE(ptr) == DUMMY || TYPE(ptr) == INORDER || TYPE(ptr) == ADD); - p = q = (bit32 *) FIRSTPTR(ptr); - while (*p != (bit32)-1) p++; + p = q = (uint32_t *) FIRSTPTR(ptr); + while (*p != (uint32_t)-1) p++; return (p - q); } @@ -450,20 +439,20 @@ /* is it the beginning of the log? */ if (db->map_size == db->logstart) { - if (*((bit32 *)(db->map_base + db->map_size - 4)) != htonl(-1)) { + if (*((uint32_t *)(db->map_base + db->map_size - 4)) != htonl(-1)) { return 1; } } /* in the middle of the log somewhere */ else { - if (*((bit32 *)(db->map_base + db->map_size - 4)) != htonl(COMMIT)) { + if (*((uint32_t *)(db->map_base + db->map_size - 4)) != htonl(COMMIT)) { return 1; } /* if it's not an end of a record or a delete */ - if (!((*((bit32 *)(db->map_base + db->map_size - 8)) == htonl(-1)) || - (*((bit32 *)(db->map_base + db->map_size -12)) == htonl(DELETE)))) { + if (!((*((uint32_t *)(db->map_base + db->map_size - 8)) == htonl(-1)) || + (*((uint32_t *)(db->map_base + db->map_size -12)) == htonl(DELETE)))) { return 1; } } @@ -497,7 +486,7 @@ } -#define PADDING(ptr) (ntohl(*((bit32 *)((ptr) + RECSIZE(ptr) - 4)))) +#define PADDING(ptr) (ntohl(*((uint32_t *)((ptr) + RECSIZE(ptr) - 4)))) /* given an open, mapped db, read in the header information */ static int read_header(struct db *db) @@ -517,16 +506,16 @@ return CYRUSDB_IOERROR; } - db->version = ntohl(*((bit32 *)(db->map_base + OFFSET_VERSION))); + db->version = ntohl(*((uint32_t *)(db->map_base + OFFSET_VERSION))); db->version_minor = - ntohl(*((bit32 *)(db->map_base + OFFSET_VERSION_MINOR))); + ntohl(*((uint32_t *)(db->map_base + OFFSET_VERSION_MINOR))); if (db->version != SKIPLIST_VERSION) { syslog(LOG_ERR, "skiplist: version mismatch: %s has version %d.%d", db->fname, db->version, db->version_minor); return CYRUSDB_IOERROR; } - db->maxlevel = ntohl(*((bit32 *)(db->map_base + OFFSET_MAXLEVEL))); + db->maxlevel = ntohl(*((uint32_t *)(db->map_base + OFFSET_MAXLEVEL))); if(db->maxlevel > SKIPLIST_MAXLEVEL) { syslog(LOG_ERR, @@ -535,7 +524,7 @@ return CYRUSDB_IOERROR; } - db->curlevel = ntohl(*((bit32 *)(db->map_base + OFFSET_CURLEVEL))); + db->curlevel = ntohl(*((uint32_t *)(db->map_base + OFFSET_CURLEVEL))); if(db->curlevel > db->maxlevel) { syslog(LOG_ERR, @@ -544,10 +533,10 @@ return CYRUSDB_IOERROR; } - db->listsize = ntohl(*((bit32 *)(db->map_base + OFFSET_LISTSIZE))); - db->logstart = ntohl(*((bit32 *)(db->map_base + OFFSET_LOGSTART))); + db->listsize = ntohl(*((uint32_t *)(db->map_base + OFFSET_LISTSIZE))); + db->logstart = ntohl(*((uint32_t *)(db->map_base + OFFSET_LOGSTART))); db->last_recovery = - ntohl(*((bit32 *)(db->map_base + OFFSET_LASTRECOVERY))); + ntohl(*((uint32_t *)(db->map_base + OFFSET_LASTRECOVERY))); /* verify dummy node */ dptr = DUMMY_PTR(db); @@ -586,13 +575,13 @@ assert (db->lock_status == WRITELOCKED); memcpy(buf + 0, HEADER_MAGIC, HEADER_MAGIC_SIZE); - *((bit32 *)(buf + OFFSET_VERSION)) = htonl(db->version); - *((bit32 *)(buf + OFFSET_VERSION_MINOR)) = htonl(db->version_minor); - *((bit32 *)(buf + OFFSET_MAXLEVEL)) = htonl(db->maxlevel); - *((bit32 *)(buf + OFFSET_CURLEVEL)) = htonl(db->curlevel); - *((bit32 *)(buf + OFFSET_LISTSIZE)) = htonl(db->listsize); - *((bit32 *)(buf + OFFSET_LOGSTART)) = htonl(db->logstart); - *((bit32 *)(buf + OFFSET_LASTRECOVERY)) = htonl(db->last_recovery); + *((uint32_t *)(buf + OFFSET_VERSION)) = htonl((uint32_t)db->version); + *((uint32_t *)(buf + OFFSET_VERSION_MINOR)) = htonl((uint32_t)db->version_minor); + *((uint32_t *)(buf + OFFSET_MAXLEVEL)) = htonl((uint32_t)db->maxlevel); + *((uint32_t *)(buf + OFFSET_CURLEVEL)) = htonl((uint32_t)db->curlevel); + *((uint32_t *)(buf + OFFSET_LISTSIZE)) = htonl((uint32_t)db->listsize); + *((uint32_t *)(buf + OFFSET_LOGSTART)) = htonl((uint32_t)db->logstart); + *((uint32_t *)(buf + OFFSET_LASTRECOVERY)) = htonl((uint32_t)db->last_recovery); /* write it out */ lseek(db->fd, 0, SEEK_SET); @@ -755,7 +744,7 @@ static int dispose_db(struct db *db) { if (!db) return 0; if (db->lock_status) { syslog(LOG_ERR, "skiplist: closed while still locked"); unlock(db); @@ -855,7 +844,7 @@ if (!r) { int n; int dsize = DUMMY_SIZE(db); - bit32 *buf = (bit32 *) xzmalloc(dsize); + uint32_t *buf = (uint32_t *) xzmalloc(dsize); buf[0] = htonl(DUMMY); buf[(dsize / 4) - 1] = htonl(-1); @@ -1099,7 +1088,7 @@ while (ptr != db->map_base) { /* does it match prefix? */ - if (KEYLEN(ptr) < (bit32) prefixlen) break; + if (KEYLEN(ptr) < (uint32_t) prefixlen) break; if (prefixlen && db->compar(KEY(ptr), prefixlen, prefix, prefixlen)) break; if (!goodp || @@ -1194,19 +1183,19 @@ struct txn **tidptr, int overwrite) { const char *ptr; - bit32 klen, dlen; + uint32_t klen, dlen; struct iovec iov[50]; unsigned int lvl, i; int num_iov; struct txn *tid, *localtid = NULL; - bit32 endpadding = (bit32) htonl(-1); - bit32 zeropadding[4] = { 0, 0, 0, 0 }; + uint32_t endpadding = (uint32_t) htonl(-1); + uint32_t zeropadding[4] = { 0, 0, 0, 0 }; int updateoffsets[SKIPLIST_MAXLEVEL]; int newoffsets[SKIPLIST_MAXLEVEL]; int addrectype = htonl(ADD); int delrectype = htonl(DELETE); - bit32 todelete; - bit32 newoffset, netnewoffset; + uint32_t todelete; + uint32_t newoffset, netnewoffset; int r; assert(db != NULL); @@ -1245,7 +1234,7 @@ /* log a removal */ WRITEV_ADD_TO_IOVEC(iov, num_iov, (char *) &delrectype, 4); - todelete = htonl(ptr - db->map_base); + todelete = htonl((uint32_t)(ptr - db->map_base)); WRITEV_ADD_TO_IOVEC(iov, num_iov, (char *) &todelete, 4); /* now we write at newoffset */ @@ -1276,12 +1265,12 @@ for (i = 0; i < lvl; i++) { /* written in the iovec */ newoffsets[i] = - htonl(FORWARD(db->map_base + updateoffsets[i], i)); + htonl((uint32_t)FORWARD(db->map_base + updateoffsets[i], i)); } } - klen = htonl(keylen); - dlen = htonl(datalen); + klen = htonl((uint32_t)keylen); + dlen = htonl((uint32_t)datalen); netnewoffset = htonl(newoffset); @@ -1358,8 +1347,8 @@ const char *ptr; int delrectype = htonl(DELETE); int updateoffsets[SKIPLIST_MAXLEVEL]; - bit32 offset; - bit32 writebuf[2]; + uint32_t offset; + uint32_t writebuf[2]; struct txn *tid, *localtid = NULL; unsigned i; int r; @@ -1432,7 +1421,7 @@ int mycommit(struct db *db, struct txn *tid) { - bit32 commitrectype = htonl(COMMIT); + uint32_t commitrectype = htonl(COMMIT); int r = 0; assert(db && tid); @@ -1518,7 +1507,7 @@ { const char *ptr; int updateoffsets[SKIPLIST_MAXLEVEL]; - bit32 offset; + uint32_t offset; unsigned i; int r = 0; @@ -1533,7 +1522,7 @@ while (tid->logstart != tid->logend) { /* find the last log entry */ for (offset = tid->logstart, ptr = db->map_base + offset; - offset + RECSIZE(ptr) != (bit32) tid->logend; + offset + RECSIZE(ptr) != (uint32_t) tid->logend; offset += RECSIZE(ptr), ptr = db->map_base + offset) ; offset = ptr - db->map_base; @@ -1569,7 +1558,7 @@ const char *q; /* re-add this record. it can't exist right now. */ - newoffset = *((bit32 *)(ptr + 4)); + newoffset = *((uint32_t *)(ptr + 4)); q = db->map_base + ntohl(newoffset); lvl = LEVEL(q); (void) find_node(db, KEY(q), KEYLEN(q), updateoffsets); @@ -1627,7 +1616,7 @@ int num_iov; int updateoffsets[SKIPLIST_MAXLEVEL]; const char *ptr; - bit32 offset; + uint32_t offset; int r = 0; int iorectype = htonl(INORDER); unsigned i; @@ -1677,7 +1666,7 @@ /* write dummy record */ if (!r) { int dsize = DUMMY_SIZE(db); - bit32 *buf = (bit32 *) xzmalloc(dsize); + uint32_t *buf = (uint32_t *) xzmalloc(dsize); buf[0] = htonl(DUMMY); buf[(dsize / 4) - 1] = htonl(-1); @@ -1705,7 +1694,7 @@ db->listsize = 0; while (!r && offset != 0) { unsigned int lvl; - bit32 newoffset, newoffsetnet; + uint32_t newoffset, newoffsetnet; ptr = db->map_base + offset; lvl = LEVEL(ptr); @@ -1752,7 +1741,7 @@ /* set any dangling pointers to zero */ for (i = 0; !r && i < db->maxlevel; i++) { - bit32 newoffset = htonl(0); + uint32_t newoffset = htonl(0); r = lseek(db->fd, updateoffsets[i], SEEK_SET); if (r < 0) { @@ -1896,7 +1885,7 @@ break; case DELETE: - printf("offset=%04X\n", ntohl(*((bit32 *)(ptr + 4)))); + printf("offset=%04X\n", ntohl(*((uint32_t *)(ptr + 4)))); break; case COMMIT: @@ -1920,7 +1909,7 @@ static int myconsistent(struct db *db, struct txn *tid, int locked) { const char *ptr; - bit32 offset; + uint32_t offset; assert(db->current_txn == tid); /* could both be null */ @@ -1978,7 +1967,7 @@ { const char *ptr, *keyptr; int updateoffsets[SKIPLIST_MAXLEVEL]; - bit32 offset, offsetnet, myoff = 0; + uint32_t offset, offsetnet, myoff = 0; int r = 0, need_checkpoint = 0; time_t start = time(NULL); unsigned i; @@ -2084,7 +2073,7 @@ } /* check padding */ - if (!r && PADDING(ptr) != (bit32) -1) { + if (!r && PADDING(ptr) != (uint32_t) -1) { syslog(LOG_ERR, "DBERROR: %s: offset %04X padding not -1", db->fname, offset); r = CYRUSDB_IOERROR; @@ -2212,7 +2201,7 @@ } else { /* type == DELETE */ const char *p; - myoff = ntohl(*((bit32 *)(ptr + 4))); + myoff = ntohl(*((uint32_t *)(ptr + 4))); p = db->map_base + myoff; keyptr = find_node(db, KEY(p), KEYLEN(p), updateoffsets); if (keyptr == db->map_base || @@ -2249,7 +2238,7 @@ /* otherwise insert it */ } else if (TYPE(ptr) == ADD) { unsigned int lvl; - bit32 newoffsets[SKIPLIST_MAXLEVEL]; + uint32_t newoffsets[SKIPLIST_MAXLEVEL]; if (keyptr) { syslog(LOG_ERR, @@ -2310,7 +2299,7 @@ retry_write(db->fd, (char *) newoffsets, 4 * lvl); if (keyptr && lvl < LEVEL(keyptr)) { - bit32 newoffsetnet; + uint32_t newoffsetnet; for (i = lvl; i < LEVEL(keyptr); i++) { newoffsetnet = htonl(FORWARD(keyptr, i)); /* replace 'updateoffsets' to point onwards */