Fixed memory leaks in test suite
[libgit2.git] / src / errors.c
blobf206b37de925edb8e36dce24fddb125e493d2cc9
1 #include "common.h"
2 #include "thread-utils.h" /* for GIT_TLS */
4 #if defined(GIT_TLS)
5 /* compile-time constant initialization required */
6 GIT_TLS int git_errno = 0;
8 #elif defined(GIT_HAS_PTHREAD)
10 static pthread_key_t errno_key;
12 static void init_errno(void) __attribute__((constructor));
13 static void init_errno(void)
15 pthread_key_create(&errno_key, free);
18 int *git__errno_storage(void)
20 int *e = pthread_getspecific(errno_key);
21 if (!e) {
22 #undef calloc
23 e = calloc(1, sizeof(*e));
24 #define calloc(a,b) GIT__FORBID_MALLOC
25 pthread_setspecific(errno_key, e);
27 return e;
30 #endif
32 static struct {
33 int num;
34 const char *str;
35 } error_codes[] = {
36 { GIT_ENOTOID, "Not a git oid" },
37 { GIT_ENOTFOUND, "Object does not exist in the scope searched" },
38 { GIT_ENOMEM, "Not enough space" },
41 const char *git_strerror(int num)
43 size_t i;
45 if (num == GIT_EOSERR)
46 return strerror(errno);
47 for (i = 0; i < ARRAY_SIZE(error_codes); i++)
48 if (num == error_codes[i].num)
49 return error_codes[i].str;
51 return "Unknown error";