Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an embryo of a TLS-aware error handling system
This adds the per-thread global variable git_errno to the system, which callers can examine to get information about an error. Two helper functions are added to reduce LoC-count for the library code itself. Also, some exceptions are made for running sparse on GIT_TLS definitions, since it doesn't grok thread-local variables at all. Signed-off-by: Andreas Ericsson <ae@op5.se> Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
- Loading branch information
Showing
6 changed files
with
74 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#include "common.h" | ||
#include "thread-utils.h" /* for GIT_TLS */ | ||
|
||
/* compile-time constant initialization required */ | ||
GIT_TLS int git_errno = 0; | ||
|
||
static struct { | ||
int num; | ||
const char *str; | ||
} error_codes[] = { | ||
{ GIT_ENOTOID, "Not a git oid" }, | ||
{ GIT_ENOTFOUND, "Object does not exist in the scope searched" }, | ||
}; | ||
|
||
const char *git_strerror(int num) | ||
{ | ||
int i; | ||
for (i = 0; i < ARRAY_SIZE(error_codes); i++) | ||
if (num == error_codes[i].num) | ||
return error_codes[i].str; | ||
|
||
return "Unknown error"; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
#ifndef INCLUDE_errors_h__ | ||
#define INCLUDE_errors_h__ | ||
#include "git/errors.h" | ||
|
||
/* convenience functions */ | ||
static inline int git_int_error(int code) | ||
{ | ||
git_errno = code; | ||
return code; | ||
} | ||
|
||
static inline void *git_ptr_error(int code) | ||
{ | ||
git_errno = code; | ||
return NULL; | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#ifndef INCLUDE_git_errors_h__ | ||
#define INCLUDE_git_errors_h__ | ||
/** | ||
* @file git/errors.h | ||
* @brief Git error handling routines and variables | ||
* @ingroup Git | ||
* @{ | ||
*/ | ||
|
||
#include "common.h" | ||
#include "thread-utils.h" | ||
GIT_BEGIN_DECL | ||
|
||
/** The git errno. */ | ||
GIT_EXTERN(int) GIT_TLS git_errno; | ||
|
||
/** | ||
* strerror() for the Git library | ||
* @param num The error code to explain | ||
* @return a string explaining the error code | ||
*/ | ||
GIT_EXTERN(const char *) git_strerror(int num); | ||
/** @} */ | ||
GIT_END_DECL | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters