Skip to content

Commit 03c0938

Browse files
committed
Avoid using atomics in pool.c
Instead, globally initialize the system page size.
1 parent cc1d7f5 commit 03c0938

File tree

4 files changed

+27
-17
lines changed

4 files changed

+27
-17
lines changed

src/global.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "sysdir.h"
1313
#include "filter.h"
1414
#include "merge_driver.h"
15+
#include "pool.h"
1516
#include "streams/registry.h"
1617
#include "streams/mbedtls.h"
1718
#include "streams/openssl.h"
@@ -38,7 +39,8 @@ static git_global_init_fn git__init_callbacks[] = {
3839
git_stream_registry_global_init,
3940
git_openssl_stream_global_init,
4041
git_mbedtls_stream_global_init,
41-
git_mwindow_global_init
42+
git_mwindow_global_init,
43+
git_pool_global_init
4244
};
4345

4446
static git_global_shutdown_fn git__shutdown_callbacks[ARRAY_SIZE(git__init_callbacks)];

src/pool.c

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -21,31 +21,27 @@ struct git_pool_page {
2121

2222
static void *pool_alloc_page(git_pool *pool, size_t size);
2323

24-
static size_t pool_system_page_size(void)
25-
{
26-
static git_atomic_ssize cached_size = {0};
27-
size_t page_size = 0;
24+
#ifndef GIT_DEBUG_POOL
2825

29-
if ((page_size = (size_t)git_atomic_ssize_get(&cached_size)) == 0) {
30-
if (git__page_size(&page_size) < 0)
31-
page_size = 4096;
32-
/* allow space for malloc overhead */
33-
page_size -= (2 * sizeof(void *)) + sizeof(git_pool_page);
34-
git_atomic_ssize_set(&cached_size, (int64_t)page_size);
35-
}
26+
static size_t system_page_size = 0;
3627

37-
return page_size;
28+
int git_pool_global_init(void)
29+
{
30+
if (git__page_size(&system_page_size) < 0)
31+
system_page_size = 4096;
32+
/* allow space for malloc overhead */
33+
system_page_size -= (2 * sizeof(void *)) + sizeof(git_pool_page);
34+
return 0;
3835
}
3936

40-
#ifndef GIT_DEBUG_POOL
4137
int git_pool_init(git_pool *pool, size_t item_size)
4238
{
4339
assert(pool);
4440
assert(item_size >= 1);
4541

4642
memset(pool, 0, sizeof(git_pool));
4743
pool->item_size = item_size;
48-
pool->page_size = pool_system_page_size();
44+
pool->page_size = system_page_size;
4945

5046
return 0;
5147
}
@@ -115,6 +111,11 @@ bool git_pool__ptr_in_pool(git_pool *pool, void *ptr)
115111

116112
#else
117113

114+
int git_pool_global_init(void)
115+
{
116+
return 0;
117+
}
118+
118119
static int git_pool__ptr_cmp(const void * a, const void * b)
119120
{
120121
if(a > b) {

src/pool.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,4 +135,12 @@ extern uint32_t git_pool__open_pages(git_pool *pool);
135135
#endif
136136
extern bool git_pool__ptr_in_pool(git_pool *pool, void *ptr);
137137

138+
/**
139+
* This function is being called by our global setup routines to
140+
* initialize the system pool size.
141+
*
142+
* @return 0 on success, <0 on failure
143+
*/
144+
extern int git_pool_global_init(void);
145+
138146
#endif

src/thread-utils.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@
2121

2222
# if (__GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 1))
2323
# error Atomic primitives do not exist on this version of gcc; configure libgit2 with -DTHREADSAFE=OFF
24-
# endif
25-
# if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7))
24+
# elif (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 7))
2625
# define GIT_BUILTIN_ATOMIC
2726
# else
2827
# define GIT_BUILTIN_SYNC

0 commit comments

Comments
 (0)