]> git.ozlabs.org Git - ccan/commitdiff
tdb2: Internal error helpers.
authorRusty Russell <rusty@rustcorp.com.au>
Tue, 1 Mar 2011 12:49:19 +0000 (23:19 +1030)
committerRusty Russell <rusty@rustcorp.com.au>
Tue, 1 Mar 2011 12:49:19 +0000 (23:19 +1030)
I use the "high pointers hold error numbers" trick, and also make
tdb_logerr return the error code, which enables the common case of
"return tdb_logerr(...)".

ccan/tdb2/private.h
ccan/tdb2/tdb.c
ccan/tdb2/tdb2.h

index 3366aea2d16c09c91d11ec4b115dab1b3eaca0a3..ae3ffb2a6b089819bbe6bbfbf26dc4f80dfb6be9 100644 (file)
@@ -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_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
 
 /* Prevent others from opening the file. */
 #define TDB_OPEN_LOCK 0
@@ -551,10 +561,10 @@ int tdb_transaction_recover(struct tdb_context *tdb);
 bool tdb_needs_recovery(struct tdb_context *tdb);
 
 /* tdb.c: */
 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);
 
 #ifdef TDB_TRACE
 void tdb_trace(struct tdb_context *tdb, const char *op);
index 38124582d125625766922e4dab802ca22de2ce82..fa8e88769bc5505a6836e546b0143aa1292b4f82 100644 (file)
@@ -706,10 +706,10 @@ const char *tdb_errorstr(const struct tdb_context *tdb)
        return "Invalid error code";
 }
 
        return "Invalid error code";
 }
 
-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, ...)
 {
        char *message;
        va_list ap;
 {
        char *message;
        va_list ap;
@@ -720,7 +720,7 @@ void COLD tdb_logerr(struct tdb_context *tdb,
        tdb->ecode = ecode;
 
        if (!tdb->logfn)
        tdb->ecode = ecode;
 
        if (!tdb->logfn)
-               return;
+               return ecode;
 
        /* FIXME: Doesn't assume asprintf. */
        va_start(ap, fmt);
 
        /* FIXME: Doesn't assume asprintf. */
        va_start(ap, fmt);
@@ -732,7 +732,7 @@ void COLD tdb_logerr(struct tdb_context *tdb,
                tdb->logfn(tdb, TDB_LOG_ERROR, tdb->log_private,
                           "out of memory formatting message:");
                tdb->logfn(tdb, level, tdb->log_private, fmt);
                tdb->logfn(tdb, TDB_LOG_ERROR, tdb->log_private,
                           "out of memory formatting message:");
                tdb->logfn(tdb, level, tdb->log_private, fmt);
-               return;
+               return ecode;
        }
        va_start(ap, fmt);
        len = vsprintf(message, fmt, ap);
        }
        va_start(ap, fmt);
        len = vsprintf(message, fmt, ap);
@@ -740,4 +740,5 @@ void COLD tdb_logerr(struct tdb_context *tdb,
        tdb->logfn(tdb, level, tdb->log_private, message);
        free(message);
        errno = saved_errno;
        tdb->logfn(tdb, level, tdb->log_private, message);
        free(message);
        errno = saved_errno;
+       return ecode;
 }
 }
index 3e02c8037e16a65b89ba1254e7d8752fb5babdc8..4fd34e8b5574d0557abe7e5c9fae5b95cead990b 100644 (file)
@@ -111,7 +111,8 @@ enum TDB_ERROR {
        TDB_ERR_EXISTS  = -5,   /* The key already exists. */
        TDB_ERR_NOEXIST = -6,   /* The key does not exist. */
        TDB_ERR_EINVAL  = -7,   /* You're using it wrong. */
        TDB_ERR_EXISTS  = -5,   /* The key already exists. */
        TDB_ERR_NOEXIST = -6,   /* The key does not exist. */
        TDB_ERR_EINVAL  = -7,   /* You're using it wrong. */
-       TDB_ERR_RDONLY  = -8    /* The database is read-only. */
+       TDB_ERR_RDONLY  = -8,   /* The database is read-only. */
+       TDB_ERR_LAST = TDB_ERR_RDONLY
 };
 
 /**
 };
 
 /**