|
| 1 | +#include <pthread.h> |
| 2 | +#include "../git-compat-util.h" |
| 3 | +/* We need original mmap/munmap here. */ |
| 4 | +#undef mmap |
| 5 | +#undef munmap |
| 6 | + |
| 7 | +/* |
| 8 | + * OSX doesn't have any specific setting like Linux's vm.max_map_count, |
| 9 | + * so COUNT_MAX can be any large number. We here set it to the default |
| 10 | + * value of Linux's vm.max_map_count. |
| 11 | + */ |
| 12 | +#define COUNT_MAX (65530) |
| 13 | + |
| 14 | +struct munmap_queue { |
| 15 | + void *start; |
| 16 | + size_t length; |
| 17 | +}; |
| 18 | + |
| 19 | +void *git_mmap(void *start, size_t length, int prot, int flags, int fd, off_t offset) |
| 20 | +{ |
| 21 | + /* |
| 22 | + * We can simply discard munmap operations in the queue by |
| 23 | + * restricting mmap arguments. |
| 24 | + */ |
| 25 | + if (start != NULL || flags != MAP_PRIVATE || prot != PROT_READ) |
| 26 | + die("invalid usage of mmap"); |
| 27 | + return mmap(start, length, prot, flags, fd, offset); |
| 28 | +} |
| 29 | + |
| 30 | +int git_munmap(void *start, size_t length) |
| 31 | +{ |
| 32 | + static pthread_mutex_t mutex; |
| 33 | + static struct munmap_queue *queue; |
| 34 | + static int count; |
| 35 | + int i; |
| 36 | + |
| 37 | + pthread_mutex_lock(&mutex); |
| 38 | + if (!queue) |
| 39 | + queue = xmalloc(COUNT_MAX * sizeof(struct munmap_queue)); |
| 40 | + queue[count].start = start; |
| 41 | + queue[count].length = length; |
| 42 | + if (++count == COUNT_MAX) { |
| 43 | + for (i = 0; i < COUNT_MAX; i++) |
| 44 | + munmap(queue[i].start, queue[i].length); |
| 45 | + count = 0; |
| 46 | + } |
| 47 | + pthread_mutex_unlock(&mutex); |
| 48 | + return 0; |
| 49 | +} |
0 commit comments