Skip to content

Commit fba70a9

Browse files
authored
Merge pull request libgit2#4919 from pks-t/pks/shutdown-cb-count
Shutdown callback count
2 parents 9084712 + b46c359 commit fba70a9

File tree

7 files changed

+49
-20
lines changed

7 files changed

+49
-20
lines changed

src/global.c

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,23 @@
2626

2727
git_mutex git__mwindow_mutex;
2828

29-
#define MAX_SHUTDOWN_CB 10
29+
typedef int (*git_global_init_fn)(void);
30+
31+
static git_global_init_fn git__init_callbacks[] = {
32+
git_allocator_global_init,
33+
git_hash_global_init,
34+
git_sysdir_global_init,
35+
git_filter_global_init,
36+
git_merge_driver_global_init,
37+
git_transport_ssh_global_init,
38+
git_stream_registry_global_init,
39+
git_openssl_stream_global_init,
40+
git_mbedtls_stream_global_init,
41+
git_mwindow_global_init
42+
};
43+
44+
static git_global_shutdown_fn git__shutdown_callbacks[ARRAY_SIZE(git__init_callbacks)];
3045

31-
static git_global_shutdown_fn git__shutdown_callbacks[MAX_SHUTDOWN_CB];
3246
static git_atomic git__n_shutdown_callbacks;
3347
static git_atomic git__n_inits;
3448
char *git__user_agent;
@@ -37,7 +51,7 @@ char *git__ssl_ciphers;
3751
void git__on_shutdown(git_global_shutdown_fn callback)
3852
{
3953
int count = git_atomic_inc(&git__n_shutdown_callbacks);
40-
assert(count <= MAX_SHUTDOWN_CB && count > 0);
54+
assert(count <= (int) ARRAY_SIZE(git__shutdown_callbacks) && count > 0);
4155
git__shutdown_callbacks[count - 1] = callback;
4256
}
4357

@@ -52,6 +66,7 @@ static void git__global_state_cleanup(git_global_st *st)
5266

5367
static int init_common(void)
5468
{
69+
size_t i;
5570
int ret;
5671

5772
/* Initialize the CRT debug allocator first, before our first malloc */
@@ -60,17 +75,10 @@ static int init_common(void)
6075
git_win32__stack_init();
6176
#endif
6277

63-
/* Initialize any other subsystems that have global state */
64-
if ((ret = git_allocator_global_init()) == 0 &&
65-
(ret = git_hash_global_init()) == 0 &&
66-
(ret = git_sysdir_global_init()) == 0 &&
67-
(ret = git_filter_global_init()) == 0 &&
68-
(ret = git_merge_driver_global_init()) == 0 &&
69-
(ret = git_transport_ssh_global_init()) == 0 &&
70-
(ret = git_stream_registry_global_init()) == 0 &&
71-
(ret = git_openssl_stream_global_init()) == 0 &&
72-
(ret = git_mbedtls_stream_global_init()) == 0)
73-
ret = git_mwindow_global_init();
78+
/* Initialize subsystems that have global state */
79+
for (i = 0; i < ARRAY_SIZE(git__init_callbacks); i++)
80+
if ((ret = git__init_callbacks[i]()) != 0)
81+
break;
7482

7583
GIT_MEMORY_BARRIER;
7684

src/hash.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
typedef struct git_hash_prov git_hash_prov;
1515
typedef struct git_hash_ctx git_hash_ctx;
1616

17-
int git_hash_global_init(void);
1817
int git_hash_ctx_init(git_hash_ctx *ctx);
1918
void git_hash_ctx_cleanup(git_hash_ctx *ctx);
2019

@@ -32,6 +31,8 @@ void git_hash_ctx_cleanup(git_hash_ctx *ctx);
3231
# include "hash/hash_generic.h"
3332
#endif
3433

34+
int git_hash_global_init(void);
35+
3536
typedef struct {
3637
void *data;
3738
size_t len;

src/hash/hash_collisiondetect.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,14 @@ struct git_hash_ctx {
1515
SHA1_CTX c;
1616
};
1717

18-
#define git_hash_global_init() 0
1918
#define git_hash_ctx_init(ctx) git_hash_init(ctx)
2019
#define git_hash_ctx_cleanup(ctx)
2120

21+
GIT_INLINE(int) git_hash_global_init(void)
22+
{
23+
return 0;
24+
}
25+
2226
GIT_INLINE(int) git_hash_init(git_hash_ctx *ctx)
2327
{
2428
assert(ctx);

src/hash/hash_common_crypto.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,14 @@ struct git_hash_ctx {
1818

1919
#define CC_LONG_MAX ((CC_LONG)-1)
2020

21-
#define git_hash_global_init() 0
2221
#define git_hash_ctx_init(ctx) git_hash_init(ctx)
2322
#define git_hash_ctx_cleanup(ctx)
2423

24+
GIT_INLINE(int) git_hash_global_init(void)
25+
{
26+
return 0;
27+
}
28+
2529
GIT_INLINE(int) git_hash_init(git_hash_ctx *ctx)
2630
{
2731
assert(ctx);

src/hash/hash_generic.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,12 @@ struct git_hash_ctx {
1818
unsigned int W[16];
1919
};
2020

21-
#define git_hash_global_init() 0
2221
#define git_hash_ctx_init(ctx) git_hash_init(ctx)
2322
#define git_hash_ctx_cleanup(ctx)
2423

24+
GIT_INLINE(int) git_hash_global_init(void)
25+
{
26+
return 0;
27+
}
28+
2529
#endif

src/hash/hash_mbedtls.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ struct git_hash_ctx {
1414
mbedtls_sha1_context c;
1515
};
1616

17-
#define git_hash_global_init() 0
1817
#define git_hash_ctx_init(ctx) git_hash_init(ctx)
1918

19+
GIT_INLINE(int) git_hash_global_init(void)
20+
{
21+
return 0;
22+
}
23+
2024
#endif /* INCLUDE_hash_mbedtld_h__ */

src/hash/hash_openssl.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,14 @@ struct git_hash_ctx {
1616
SHA_CTX c;
1717
};
1818

19-
#define git_hash_global_init() 0
2019
#define git_hash_ctx_init(ctx) git_hash_init(ctx)
2120
#define git_hash_ctx_cleanup(ctx)
2221

22+
GIT_INLINE(int) git_hash_global_init(void)
23+
{
24+
return 0;
25+
}
26+
2327
GIT_INLINE(int) git_hash_init(git_hash_ctx *ctx)
2428
{
2529
assert(ctx);

0 commit comments

Comments
 (0)