aboutsummaryrefslogtreecommitdiffstats
path: root/wrapper.c
diff options
context:
space:
mode:
authorPatrick Steinhardt <ps@pks.im>2025-04-15 11:38:17 +0200
committerJunio C Hamano <gitster@pobox.com>2025-04-15 08:24:35 -0700
commit632b5e3ee274a2012a88bc32af2d9cf04c5bd363 (patch)
tree8f2e31d3b9f8dad801b3ac68d010b9207efa0259 /wrapper.c
parentobject-file: move `git_open_cloexec()` to "compat/open.c" (diff)
downloadgit-632b5e3ee274a2012a88bc32af2d9cf04c5bd363.tar.gz
git-632b5e3ee274a2012a88bc32af2d9cf04c5bd363.zip
object-file: move `xmmap()` into "wrapper.c"
The `xmmap()` function is provided by "object-file.c" even though its functionality has nothing to do with the object file subsystem. Move it into "wrapper.c", whose header already declares those functions. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Diffstat (limited to 'wrapper.c')
-rw-r--r--wrapper.c48
1 files changed, 48 insertions, 0 deletions
diff --git a/wrapper.c b/wrapper.c
index 8b98593149..3c79778055 100644
--- a/wrapper.c
+++ b/wrapper.c
@@ -829,3 +829,51 @@ uint32_t git_rand(unsigned flags)
return result;
}
+
+static void mmap_limit_check(size_t length)
+{
+ static size_t limit = 0;
+ if (!limit) {
+ limit = git_env_ulong("GIT_MMAP_LIMIT", 0);
+ if (!limit)
+ limit = SIZE_MAX;
+ }
+ if (length > limit)
+ die(_("attempting to mmap %"PRIuMAX" over limit %"PRIuMAX),
+ (uintmax_t)length, (uintmax_t)limit);
+}
+
+void *xmmap_gently(void *start, size_t length,
+ int prot, int flags, int fd, off_t offset)
+{
+ void *ret;
+
+ mmap_limit_check(length);
+ ret = mmap(start, length, prot, flags, fd, offset);
+ if (ret == MAP_FAILED && !length)
+ ret = NULL;
+ return ret;
+}
+
+const char *mmap_os_err(void)
+{
+ static const char blank[] = "";
+#if defined(__linux__)
+ if (errno == ENOMEM) {
+ /* this continues an existing error message: */
+ static const char enomem[] =
+", check sys.vm.max_map_count and/or RLIMIT_DATA";
+ return enomem;
+ }
+#endif /* OS-specific bits */
+ return blank;
+}
+
+void *xmmap(void *start, size_t length,
+ int prot, int flags, int fd, off_t offset)
+{
+ void *ret = xmmap_gently(start, length, prot, flags, fd, offset);
+ if (ret == MAP_FAILED)
+ die_errno(_("mmap failed%s"), mmap_os_err());
+ return ret;
+}