Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -1654,6 +1654,7 @@ ifeq ($(uname_S),Darwin)
COMPAT_CFLAGS += -DAPPLE_COMMON_CRYPTO
endif
PTHREAD_LIBS =
COMPAT_OBJS += compat/osxmmap.o
endif

ifdef NO_LIBGEN_H
Expand Down
49 changes: 49 additions & 0 deletions compat/osxmmap.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include <pthread.h>
#include "../git-compat-util.h"
/* We need original mmap/munmap here. */
#undef mmap
#undef munmap

/*
* OSX doesn't have any specific setting like Linux's vm.max_map_count,
* so COUNT_MAX can be any large number. We here set it to the default
* value of Linux's vm.max_map_count.
*/
#define COUNT_MAX (65530)

struct munmap_queue {
void *start;
size_t length;
};

void *git_mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset)
{
/*
* We can simply discard munmap operations in the queue by
* restricting mmap arguments.
*/
if (start != NULL || flags != MAP_PRIVATE || prot != PROT_READ)
die("invalid usage of mmap");
return mmap(start, length, prot, flags, fd, offset);
}

int git_munmap(void *start, size_t length)
{
static pthread_mutex_t mutex;
static struct munmap_queue *queue;
static int count;
int i;

pthread_mutex_lock(&mutex);
if (!queue)
queue = xmalloc(COUNT_MAX * sizeof(struct munmap_queue));
queue[count].start = start;
queue[count].length = length;
if (++count == COUNT_MAX) {
for (i = 0; i < COUNT_MAX; i++)
munmap(queue[i].start, queue[i].length);
count = 0;
}
pthread_mutex_unlock(&mutex);
return 0;
}
7 changes: 7 additions & 0 deletions compat/posix.h
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,13 @@ int git_munmap(void *start, size_t length);

#include <sys/mman.h>

#if defined(__APPLE__)
#define mmap git_mmap
#define munmap git_munmap
void *git_mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset);
int git_munmap(void *start, size_t length);
#endif

#endif /* NO_MMAP || USE_WIN32_MMAP */

#ifndef MAP_FAILED
Expand Down
4 changes: 4 additions & 0 deletions contrib/buildsystems/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,10 @@ if(CMAKE_SYSTEM_NAME STREQUAL "Windows")
compat/strdup.c)
set(NO_UNIX_SOCKETS 1)

elseif(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
list(APPEND compat_SOURCES
compat/osxmmap.c)

elseif(CMAKE_SYSTEM_NAME STREQUAL "Linux")
add_compile_definitions(PROCFS_EXECUTABLE_PATH="/proc/self/exe" HAVE_DEV_TTY )
list(APPEND compat_SOURCES unix-socket.c unix-stream-server.c compat/linux/procinfo.c)
Expand Down
2 changes: 2 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -1275,6 +1275,8 @@ elif host_machine.system() == 'windows'
else
libgit_sources += 'compat/mingw.c'
endif
elif host_machine.system() == 'darwin'
libgit_sources += 'compat/osxmmap.c'
endif

if host_machine.system() == 'linux'
Expand Down
Loading