diff options
author | Edward Thomson <ethomson@edwardthomson.com> | 2020-04-06 12:28:59 +0100 |
---|---|---|
committer | Edward Thomson <ethomson@edwardthomson.com> | 2020-06-05 07:14:25 +0100 |
commit | 6ad20febdbccf9b04d3c73ac506eeef70d3b684a (patch) | |
tree | f6fac441bcd600fde37602470a2e48788ad0d0ef | |
parent | d89310caf6cdaa4300f1f571450906f18eba09b2 (diff) | |
download | libgit2-6ad20febdbccf9b04d3c73ac506eeef70d3b684a.tar.gz |
cli: add global state
As we consume parts of the libgit2 utility functions (like `git_buf`),
we will inevitably need to allocate. Since we re-use the libgit2
allocation functions - but linked into our application - we'll need
to configure our allocation strategy ahead of time.
-rw-r--r-- | src/cli/cli.h | 1 | ||||
-rw-r--r-- | src/cli/global.c | 56 | ||||
-rw-r--r-- | src/cli/global.h | 7 | ||||
-rw-r--r-- | src/cli/main.c | 5 |
4 files changed, 67 insertions, 2 deletions
diff --git a/src/cli/cli.h b/src/cli/cli.h index 3fddae3b0..60924f0aa 100644 --- a/src/cli/cli.h +++ b/src/cli/cli.h @@ -10,6 +10,7 @@ #include "git2_util.h" +#include "global.h" #include "opt.h" #define PROGRAM_NAME "git2" diff --git a/src/cli/global.c b/src/cli/global.c new file mode 100644 index 000000000..3131937c3 --- /dev/null +++ b/src/cli/global.c @@ -0,0 +1,56 @@ +/* + * Copyright (C) the libgit2 contributors. All rights reserved. + * + * This file is part of libgit2, distributed under the GNU GPL v2 with + * a Linking Exception. For full terms see the included COPYING file. + */ + +#include <stdio.h> +#include <git2.h> +#include <git2client.h> +#include "git2_util.h" +#include "cli.h" +#include "hash.h" +#include "runtime.h" + +#ifdef GIT_WIN32 +# include "win32/w32_crtdbg_stacktrace.h" +# include "win32/w32_stack.h" +#endif + +static void client_shutdown(void); + +static int client_init(void) +{ + if (git_client_init() < 0) + return -1; + + return git_runtime_shutdown_register(client_shutdown); +} + +static void client_shutdown(void) +{ + git_client_shutdown(); +} + +int cli_global_init() +{ + static git_runtime_init_fn init_fns[] = { + client_init, + +#if defined(GIT_MSVC_CRTDBG) + git_win32__crtdbg_stacktrace_init, + git_win32__stack_init, +#endif + git_allocator_global_init, + git_hash_global_init, + git_threads_global_init, + }; + + return git_runtime_init(init_fns, ARRAY_SIZE(init_fns)); +} + +int cli_global_shutdown() +{ + return git_runtime_shutdown(); +} diff --git a/src/cli/global.h b/src/cli/global.h new file mode 100644 index 000000000..13761c00e --- /dev/null +++ b/src/cli/global.h @@ -0,0 +1,7 @@ +#ifndef CLI_global_h__ +#define CLI_global_h__ + +extern int cli_global_init(void); +extern int cli_global_shutdown(void); + +#endif /* CLI_global_h__ */ diff --git a/src/cli/main.c b/src/cli/main.c index 20152e6cf..930cccf31 100644 --- a/src/cli/main.c +++ b/src/cli/main.c @@ -7,6 +7,7 @@ #include <stdio.h> #include <git2.h> +#include <git2client.h> #include "cli.h" static int show_version = 0; @@ -28,7 +29,7 @@ int main(int argc, char **argv) int args_len = 0; int error = 0; - if (git_libgit2_init() < 0) { + if (cli_global_init() < 0) { fprintf(stderr, "error: failed to initialize libgit2\n"); exit(1); } @@ -60,6 +61,6 @@ int main(int argc, char **argv) } done: - git_libgit2_shutdown(); + cli_global_shutdown(); return error; } |