X-Git-Url: https://git.ozlabs.org/?a=blobdiff_plain;ds=inline;f=ccan%2Ftdb2%2Fprivate.h;h=4ecda6a5f529729518b051a53db711f582fde6db;hb=850c5cfed46cd4f38df79783791969c7b30ad9da;hp=69fd40fce91fd295d2e868d569d60e6e804ecf4a;hpb=52c4843176ce73484dd405261d13048bcd1b85e5;p=ccan diff --git a/ccan/tdb2/private.h b/ccan/tdb2/private.h index 69fd40fc..4ecda6a5 100644 --- a/ccan/tdb2/private.h +++ b/ccan/tdb2/private.h @@ -74,6 +74,16 @@ typedef uint64_t tdb_off_t; #define TDB_RECOVERY_INVALID_MAGIC (0x0ULL) #define TDB_OFF_ERR ((tdb_off_t)-1) +#define TDB_OFF_IS_ERR(off) unlikely(off >= (tdb_off_t)TDB_ERR_LAST) + +/* Packing errors into pointers and v.v. */ +#define TDB_PTR_IS_ERR(ptr) \ + unlikely((void *)(ptr) >= (void *)(long)TDB_ERR_LAST) +#define TDB_PTR_ERR(p) ((enum TDB_ERROR)(long)(p)) +#define TDB_ERR_PTR(err) ((void *)(long)(err)) + +/* Common case of returning true, false or -ve error. */ +typedef int tdb_bool_err; /* Prevent others from opening the file. */ #define TDB_OPEN_LOCK 0 @@ -382,6 +392,14 @@ struct tdb_methods { /* hash.c: */ void tdb_hash_init(struct tdb_context *tdb); +int first_in_hash(struct tdb_context *tdb, + struct traverse_info *tinfo, + TDB_DATA *kbuf, size_t *dlen); + +int next_in_hash(struct tdb_context *tdb, + struct traverse_info *tinfo, + TDB_DATA *kbuf, size_t *dlen); + /* Hash random memory. */ uint64_t tdb_hash(struct tdb_context *tdb, const void *ptr, size_t len); @@ -500,61 +518,54 @@ void add_stat_(struct tdb_context *tdb, uint64_t *stat, size_t val); void tdb_lock_init(struct tdb_context *tdb); /* Lock/unlock a range of hashes. */ -int tdb_lock_hashes(struct tdb_context *tdb, - tdb_off_t hash_lock, tdb_len_t hash_range, - int ltype, enum tdb_lock_flags waitflag); -int tdb_unlock_hashes(struct tdb_context *tdb, - tdb_off_t hash_lock, - tdb_len_t hash_range, int ltype); +enum TDB_ERROR tdb_lock_hashes(struct tdb_context *tdb, + tdb_off_t hash_lock, tdb_len_t hash_range, + int ltype, enum tdb_lock_flags waitflag); +enum TDB_ERROR tdb_unlock_hashes(struct tdb_context *tdb, + tdb_off_t hash_lock, + tdb_len_t hash_range, int ltype); /* Lock/unlock a particular free bucket. */ -int tdb_lock_free_bucket(struct tdb_context *tdb, tdb_off_t b_off, - enum tdb_lock_flags waitflag); +enum TDB_ERROR tdb_lock_free_bucket(struct tdb_context *tdb, tdb_off_t b_off, + enum tdb_lock_flags waitflag); void tdb_unlock_free_bucket(struct tdb_context *tdb, tdb_off_t b_off); /* Serialize transaction start. */ -int tdb_transaction_lock(struct tdb_context *tdb, int ltype); -int tdb_transaction_unlock(struct tdb_context *tdb, int ltype); +enum TDB_ERROR tdb_transaction_lock(struct tdb_context *tdb, int ltype); +void tdb_transaction_unlock(struct tdb_context *tdb, int ltype); /* Do we have any hash locks (ie. via tdb_chainlock) ? */ bool tdb_has_hash_locks(struct tdb_context *tdb); /* Lock entire database. */ -int tdb_allrecord_lock(struct tdb_context *tdb, int ltype, - enum tdb_lock_flags flags, bool upgradable); -int tdb_allrecord_unlock(struct tdb_context *tdb, int ltype); -int tdb_allrecord_upgrade(struct tdb_context *tdb); +enum TDB_ERROR tdb_allrecord_lock(struct tdb_context *tdb, int ltype, + enum tdb_lock_flags flags, bool upgradable); +void tdb_allrecord_unlock(struct tdb_context *tdb, int ltype); +enum TDB_ERROR tdb_allrecord_upgrade(struct tdb_context *tdb); /* Serialize db open. */ -int tdb_lock_open(struct tdb_context *tdb, enum tdb_lock_flags flags); +enum TDB_ERROR tdb_lock_open(struct tdb_context *tdb, + enum tdb_lock_flags flags); void tdb_unlock_open(struct tdb_context *tdb); bool tdb_has_open_lock(struct tdb_context *tdb); /* Serialize db expand. */ -int tdb_lock_expand(struct tdb_context *tdb, int ltype); +enum TDB_ERROR tdb_lock_expand(struct tdb_context *tdb, int ltype); void tdb_unlock_expand(struct tdb_context *tdb, int ltype); bool tdb_has_expansion_lock(struct tdb_context *tdb); /* If it needs recovery, grab all the locks and do it. */ -int tdb_lock_and_recover(struct tdb_context *tdb); - -/* traverse.c: */ -int first_in_hash(struct tdb_context *tdb, int ltype, - struct traverse_info *tinfo, - TDB_DATA *kbuf, size_t *dlen); -int next_in_hash(struct tdb_context *tdb, int ltype, - struct traverse_info *tinfo, - TDB_DATA *kbuf, size_t *dlen); +enum TDB_ERROR tdb_lock_and_recover(struct tdb_context *tdb); /* transaction.c: */ int tdb_transaction_recover(struct tdb_context *tdb); bool tdb_needs_recovery(struct tdb_context *tdb); /* tdb.c: */ -void COLD tdb_logerr(struct tdb_context *tdb, - enum TDB_ERROR ecode, - enum tdb_log_level level, - const char *fmt, ...); +enum TDB_ERROR COLD tdb_logerr(struct tdb_context *tdb, + enum TDB_ERROR ecode, + enum tdb_log_level level, + const char *fmt, ...); #ifdef TDB_TRACE void tdb_trace(struct tdb_context *tdb, const char *op);