Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d8fcafb

Browse files
carlosmnEdward Thomson
authored and
Edward Thomson
committed
Split the page size from the mmap alignment
While often similar, these are not the same on Windows. We want to use the page size on Windows for the pools, but for mmap we need to use the allocation granularity as the alignment. On the other platforms these values remain the same.
1 parent 9a668ab commit d8fcafb

File tree

5 files changed

+41
-9
lines changed

5 files changed

+41
-9
lines changed

src/indexer.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@ static void hash_partially(git_indexer *idx, const uint8_t *data, size_t size)
449449
static int write_at(git_indexer *idx, const void *data, git_off_t offset, size_t size)
450450
{
451451
git_file fd = idx->pack->mwf.fd;
452-
size_t page_size;
452+
size_t mmap_alignment;
453453
size_t page_offset;
454454
git_off_t page_start;
455455
unsigned char *map_data;
@@ -458,11 +458,11 @@ static int write_at(git_indexer *idx, const void *data, git_off_t offset, size_t
458458

459459
assert(data && size);
460460

461-
if ((error = git__page_size(&page_size)) < 0)
461+
if ((error = git__mmap_alignment(&mmap_alignment)) < 0)
462462
return error;
463463

464-
/* the offset needs to be at the beginning of the a page boundary */
465-
page_offset = offset % page_size;
464+
/* the offset needs to be at the mmap boundary for the platform */
465+
page_offset = offset % mmap_alignment;
466466
page_start = offset - page_offset;
467467

468468
if ((error = p_mmap(&map, page_offset + size, GIT_PROT_WRITE, GIT_MAP_SHARED, fd, page_start)) < 0)

src/posix.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,13 @@ int git__page_size(size_t *page_size)
224224
return 0;
225225
}
226226

227+
int git__mmap_alignment(size_t *alignment)
228+
{
229+
/* dummy; here we don't need any alignment anyway */
230+
*alignment = 4096;
231+
return 0;
232+
}
233+
227234

228235
int p_mmap(git_map *out, size_t len, int prot, int flags, int fd, git_off_t offset)
229236
{

src/posix.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ extern int p_getcwd(char *buffer_out, size_t size);
109109
extern int p_rename(const char *from, const char *to);
110110

111111
extern int git__page_size(size_t *page_size);
112+
extern int git__mmap_alignment(size_t *page_size);
112113

113114
/**
114115
* Platform-dependent methods

src/unix/map.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@ int git__page_size(size_t *page_size)
2424
return 0;
2525
}
2626

27+
int git__mmap_alignment(size_t *alignment)
28+
{
29+
return git__page_size(alignment);
30+
}
31+
2732
int p_mmap(git_map *out, size_t len, int prot, int flags, int fd, git_off_t offset)
2833
{
2934
int mprot = PROT_READ;

src/win32/map.c

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,22 +17,41 @@ static DWORD get_page_size(void)
1717

1818
if (!page_size) {
1919
GetSystemInfo(&sys);
20-
page_size = sys.dwAllocationGranularity;
20+
page_size = sys.dwPageSize;
2121
}
2222

2323
return page_size;
2424
}
2525

26+
static DWORD get_allocation_granularity(void)
27+
{
28+
static DWORD granularity;
29+
SYSTEM_INFO sys;
30+
31+
if (!granularity) {
32+
GetSystemInfo(&sys);
33+
granularity = sys.dwAllocationGranularity;
34+
}
35+
36+
return granularity;
37+
}
38+
2639
int git__page_size(size_t *page_size)
2740
{
2841
*page_size = get_page_size();
2942
return 0;
3043
}
3144

45+
int git__mmap_alignment(size_t *page_size)
46+
{
47+
*page_size = get_allocation_granularity();
48+
return 0;
49+
}
50+
3251
int p_mmap(git_map *out, size_t len, int prot, int flags, int fd, git_off_t offset)
3352
{
3453
HANDLE fh = (HANDLE)_get_osfhandle(fd);
35-
DWORD page_size = get_page_size();
54+
DWORD alignment = get_allocation_granularity();
3655
DWORD fmap_prot = 0;
3756
DWORD view_prot = 0;
3857
DWORD off_low = 0;
@@ -62,12 +81,12 @@ int p_mmap(git_map *out, size_t len, int prot, int flags, int fd, git_off_t offs
6281
if (prot & GIT_PROT_READ)
6382
view_prot |= FILE_MAP_READ;
6483

65-
page_start = (offset / page_size) * page_size;
84+
page_start = (offset / alignment) * alignment;
6685
page_offset = offset - page_start;
6786

68-
if (page_offset != 0) { /* offset must be multiple of page size */
87+
if (page_offset != 0) { /* offset must be multiple of the allocation granularity */
6988
errno = EINVAL;
70-
giterr_set(GITERR_OS, "Failed to mmap. Offset must be multiple of page size");
89+
giterr_set(GITERR_OS, "Failed to mmap. Offset must be multiple of allocation granularity");
7190
return -1;
7291
}
7392

0 commit comments

Comments
 (0)