error: store the error messages in a reusable buffer Instead of allocating a brand new buffer for each error string we want to store, we can use a per-thread buffer to store the error string and re-use the underlying storage. We already use the buffer to format the string, so this mostly makes that more direct.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
diff --git a/src/errors.c b/src/errors.c
index 7a26005..56fad00 100644
--- a/src/errors.c
+++ b/src/errors.c
@@ -18,19 +18,30 @@ static git_error g_git_oom_error = {
GITERR_NOMEMORY
};
-static void set_error(int error_class, char *string)
+static void set_error_from_buffer(int error_class)
{
git_error *error = &GIT_GLOBAL->error_t;
+ git_buf *buf = &GIT_GLOBAL->error_buf;
- if (error->message != string)
- git__free(error->message);
-
- error->message = string;
+ error->message = buf->ptr;
error->klass = error_class;
GIT_GLOBAL->last_error = error;
}
+static void set_error(int error_class, char *string)
+{
+ git_buf *buf = &GIT_GLOBAL->error_buf;
+
+ git_buf_clear(buf);
+ if (string) {
+ git_buf_puts(buf, string);
+ git__free(string);
+ }
+
+ set_error_from_buffer(error_class);
+}
+
void giterr_set_oom(void)
{
GIT_GLOBAL->last_error = &g_git_oom_error;
@@ -38,27 +49,28 @@ void giterr_set_oom(void)
void giterr_set(int error_class, const char *string, ...)
{
- git_buf buf = GIT_BUF_INIT;
va_list arglist;
#ifdef GIT_WIN32
DWORD win32_error_code = (error_class == GITERR_OS) ? GetLastError() : 0;
#endif
int error_code = (error_class == GITERR_OS) ? errno : 0;
+ git_buf *buf = &GIT_GLOBAL->error_buf;
+ git_buf_clear(buf);
if (string) {
va_start(arglist, string);
- git_buf_vprintf(&buf, string, arglist);
+ git_buf_vprintf(buf, string, arglist);
va_end(arglist);
if (error_class == GITERR_OS)
- git_buf_PUTS(&buf, ": ");
+ git_buf_PUTS(buf, ": ");
}
if (error_class == GITERR_OS) {
#ifdef GIT_WIN32
char * win32_error = git_win32_get_error_message(win32_error_code);
if (win32_error) {
- git_buf_puts(&buf, win32_error);
+ git_buf_puts(buf, win32_error);
git__free(win32_error);
SetLastError(0);
@@ -66,26 +78,29 @@ void giterr_set(int error_class, const char *string, ...)
else
#endif
if (error_code)
- git_buf_puts(&buf, strerror(error_code));
+ git_buf_puts(buf, strerror(error_code));
if (error_code)
errno = 0;
}
- if (!git_buf_oom(&buf))
- set_error(error_class, git_buf_detach(&buf));
+ if (!git_buf_oom(buf))
+ set_error_from_buffer(error_class);
}
void giterr_set_str(int error_class, const char *string)
{
- char *message;
+ git_buf *buf = &GIT_GLOBAL->error_buf;
assert(string);
- message = git__strdup(string);
+ if (!string)
+ return;
- if (message)
- set_error(error_class, message);
+ git_buf_clear(buf);
+ git_buf_puts(buf, string);
+ if (!git_buf_oom(buf))
+ set_error_from_buffer(error_class);
}
int giterr_set_regex(const regex_t *regex, int error_code)
@@ -119,13 +134,14 @@ void giterr_clear(void)
int giterr_detach(git_error *cpy)
{
git_error *error = GIT_GLOBAL->last_error;
+ git_buf *buf = &GIT_GLOBAL->error_buf;
assert(cpy);
if (!error)
return -1;
- cpy->message = error->message;
+ cpy->message = git_buf_detach(buf);
cpy->klass = error->klass;
error->message = NULL;
diff --git a/src/global.c b/src/global.c
index 37a47bd..3d37ee4 100644
--- a/src/global.c
+++ b/src/global.c
@@ -279,18 +279,19 @@ int git_libgit2_shutdown(void)
git_global_st *git__global_state(void)
{
- void *ptr;
+ git_global_st *ptr;
assert(git_atomic_get(&git__n_inits) > 0);
if ((ptr = TlsGetValue(_tls_index)) != NULL)
return ptr;
- ptr = git__malloc(sizeof(git_global_st));
+ ptr = git__calloc(1, sizeof(git_global_st));
if (!ptr)
return NULL;
- memset(ptr, 0x0, sizeof(git_global_st));
+ git_buf_init(&ptr->error_buf, 0);
+
TlsSetValue(_tls_index, ptr);
return ptr;
}
@@ -378,18 +379,18 @@ int git_libgit2_shutdown(void)
git_global_st *git__global_state(void)
{
- void *ptr;
+ git_global_st *ptr;
assert(git_atomic_get(&git__n_inits) > 0);
if ((ptr = pthread_getspecific(_tls_key)) != NULL)
return ptr;
- ptr = git__malloc(sizeof(git_global_st));
+ ptr = git__calloc(1, sizeof(git_global_st));
if (!ptr)
return NULL;
- memset(ptr, 0x0, sizeof(git_global_st));
+ git_buf_init(&ptr->error_buf, 0);
pthread_setspecific(_tls_key, ptr);
return ptr;
}
@@ -407,6 +408,7 @@ int git_libgit2_init(void)
ssl_inited = 1;
}
+ git_buf_init(&__state.error_buf, 0);
return git_atomic_inc(&git__n_inits);
}
diff --git a/src/global.h b/src/global.h
index fdad6ba..37e909a 100644
--- a/src/global.h
+++ b/src/global.h
@@ -14,6 +14,7 @@
typedef struct {
git_error *last_error;
git_error error_t;
+ git_buf error_buf;
char oid_fmt[GIT_OID_HEXSZ+1];
} git_global_st;