From: Andre Noll Date: Sat, 31 May 2008 14:17:27 +0000 (+0200) Subject: Switch to the new error code handling. X-Git-Tag: v0.1.0~98 X-Git-Url: http://git.tuebingen.mpg.de/?p=osl.git;a=commitdiff_plain;h=f57b24f2f5b6d8ce5ca0d5ad67b5c086695d8eb2 Switch to the new error code handling. --- diff --git a/error.h b/error.h index e311807..5652e9a 100644 --- a/error.h +++ b/error.h @@ -1,6 +1,3 @@ -extern char *__errlist[]; -extern char *__error_txt; - /** * This bit indicates whether a number is considered a system error number * If yes, the system errno is just the result of clearing this bit from @@ -28,73 +25,3 @@ static inline int is_errno(int num, int _errno) assert(num > 0 && _errno > 0); return ERRNO_TO_ERROR(_errno) == num; } - -/** - * version of strerror(3). - * - * \param num The error number. - * - * \return The error text of \a num. - */ -static inline char *error_txt(int num) -{ - assert(num > 0); - if (IS_SYSTEM_ERROR(num)) - return strerror((num) & ((1 << SYSTEM_ERROR_BIT) - 1)); - else - return __errlist[num]; -} - -#define ALL_ERRORS \ - _ERROR(SUCCESS, "success") \ - _ERROR(BAD_DB_DIR, "invalid database directory") \ - _ERROR(NO_COLUMN_DESC, "missing column description") \ - _ERROR(BAD_NAME, "invalid name for a column/table") \ - _ERROR(BAD_STORAGE_TYPE, "invalid storage type") \ - _ERROR(BAD_STORAGE_FLAGS, "invalid storage flags") \ - _ERROR(NO_COLUMN_NAME, "missing column name") \ - _ERROR(NO_COLUMNS, "at least one column required") \ - _ERROR(BAD_COLUMN_NAME, "invalid name for a table column") \ - _ERROR(NO_UNIQUE_RBTREE_COLUMN, "need at least one mapped column with OSL_UNIQE and OSL_RBTREE") \ - _ERROR(NO_RBTREE_COL, "at least one column needs an rbtree") \ - _ERROR(DUPLICATE_COL_NAME, "column name given twice") \ - _ERROR(BAD_STORAGE_SIZE, "invalid storage size") \ - _ERROR(NO_COMPARE_FUNC, "missing compare function") \ - _ERROR(BAD_DATA_SIZE, "wrong data size for fixed-size column") \ - _ERROR(NOT_MAPPED, "file not mapped") \ - _ERROR(ALREADY_MAPPED, "file already mapped") \ - _ERROR(BAD_SIZE, "invalid size specified") \ - _ERROR(TRUNC, "failed to truncate file") \ - _ERROR(BAD_TABLE, "table not open") \ - _ERROR(BAD_TABLE_DESC, "invalid table description") \ - _ERROR(RB_KEY_EXISTS, "key already exists in rbtree") \ - _ERROR(RB_KEY_NOT_FOUND, "key not found in rbtree") \ - _ERROR(BAD_ROW_NUM, "invalid row number") \ - _ERROR(INDEX_CORRUPTION, "index corruption detected") \ - _ERROR(INVALID_OBJECT, "reference to invalid object") \ - _ERROR(STAT, "can not stat file") \ - _ERROR(WRITE, "write error") \ - _ERROR(LSEEK, "lseek error") \ - _ERROR(BUSY, "table is busy") \ - _ERROR(SHORT_TABLE, "table too short") \ - _ERROR(NO_MAGIC, "missing table header magic") \ - _ERROR(VERSION_MISMATCH, "table version not supported") \ - _ERROR(BAD_COLUMN_NUM, "invalid column number") \ - _ERROR(BAD_TABLE_FLAGS, "invalid flags in table description") \ - _ERROR(BAD_ROW, "invalid row") \ - _ERROR(EMPTY, "file empty") \ - _ERROR(MMAP, "mmap error") \ - - -/** - * This is temporarily defined to expand to its first argument (prefixed by - * 'E_') and gets later redefined to expand to the error text only - */ -#define _ERROR(err, msg) E_ ## err, - -enum error_codes { - ALL_ERRORS -}; -#undef _ERROR -#define _ERROR(err, msg) msg, -#define DEFINE_ERRLIST char *__errlist[] = {ALL_ERRORS} diff --git a/fd.c b/fd.c index b0089d3..13708ac 100644 --- a/fd.c +++ b/fd.c @@ -13,6 +13,7 @@ #include #include "log.h" +#include "osl.h" #include "error.h" /** @@ -197,14 +198,14 @@ int mmap_full_file(const char *path, int open_mode, void **map, goto out; } *size = file_status.st_size; - ret = -E_EMPTY; + ret = -E_OSL_EMPTY; DEBUG_LOG("%s: size %zu\n", path, *size); if (!*size) goto out; *map = mmap(NULL, *size, mmap_prot, mmap_flags, fd, 0); if (*map == MAP_FAILED) { *map = NULL; - ret = -E_MMAP; + ret = -E_OSL_MMAP; goto out; } ret = 1; diff --git a/osl.c b/osl.c index 957e9a7..cce01aa 100644 --- a/osl.c +++ b/osl.c @@ -10,6 +10,7 @@ #include "log.h" +#include "osl.h" #include "error.h" #include "fd.h" #include "list.h" @@ -60,6 +61,34 @@ static __must_check __printf_1_2 __malloc char *make_message(const char *fmt, .. return p; } +/* Taken from Drepper: How to write shared libraries, Appendix B. */ +#include +#define MSGSTRFIELD(line) MSGSTRFIELD1(line) +#define MSGSTRFIELD1(line) str##line +static const union msgstr_t { + struct { +#define _S(n, s) char MSGSTRFIELD(__LINE__)[sizeof(s)]; +#include "errtab.h" +#undef _S + }; + char str[0]; +} msgstr = { { +#define _S(n, s) s, +#include "errtab.h" +#undef _S +} }; +static const unsigned int errmsgidx[] = { +#define _S(n, s) [n] = offsetof(union msgstr_t, MSGSTRFIELD(__LINE__)), +#include "errtab.h" +#undef _S +}; +__export const char *osl_strerror(int num) +{ + if (IS_SYSTEM_ERROR(num)) + return strerror((num) & ((1 << SYSTEM_ERROR_BIT) - 1)); + return msgstr.str + errmsgidx[num]; +} + /** * The log function. * diff --git a/osl.h.in b/osl.h.in index 8c386c7..a586be2 100644 --- a/osl.h.in +++ b/osl.h.in @@ -178,3 +178,4 @@ int osl_get_nth_row(const struct osl_table *t, unsigned col_num, unsigned n, struct osl_row **result); int osl_get_rank(const struct osl_table *t, struct osl_row *r, unsigned col_num, unsigned *rank); +const char *osl_strerror(int nr); diff --git a/osl_core.h b/osl_core.h index b6d1f82..a6342f8 100644 --- a/osl_core.h +++ b/osl_core.h @@ -7,7 +7,6 @@ /** \file osl_core.h Object storage layer details, not visible to users. */ #include "rbtree.h" -#include "osl.h" #include "hash.h" static __must_check __printf_1_2 __malloc char *make_message(const char *fmt, ...); @@ -261,7 +260,7 @@ _static_inline_ int get_row_index(const struct osl_table *t, uint32_t row_num, index_offset = t->index_header_size + t->row_index_size * row_num; if (index_offset + 8 > t->index_map.size) { *row_index = NULL; - return -E_INDEX_CORRUPTION; + return -E_OSL_INDEX_CORRUPTION; } *row_index = (char *)(t->index_map.data) + index_offset; return 1;