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>

Patrick Steinhardt committed Apr 15, 2025 at 11:38 UTC 632b5e3ee274a2012a88bc32af2d9cf04c5bd363
2 files changed +48 -48
object-file.c
-48
@@ -718,54 +718,6 @@ int has_loose_object(const struct object_id *oid)
718 return check_and_freshen(oid, 0);
719 }
720
721 -static void mmap_limit_check(size_t length)
722 -{
723 - static size_t limit = 0;
724 - if (!limit) {
725 - limit = git_env_ulong("GIT_MMAP_LIMIT", 0);
726 - if (!limit)
727 - limit = SIZE_MAX;
728 - }
729 - if (length > limit)
730 - die(_("attempting to mmap %"PRIuMAX" over limit %"PRIuMAX),
731 - (uintmax_t)length, (uintmax_t)limit);
732 -}
733 -
734 -void *xmmap_gently(void *start, size_t length,
735 - int prot, int flags, int fd, off_t offset)
736 -{
737 - void *ret;
738 -
739 - mmap_limit_check(length);
740 - ret = mmap(start, length, prot, flags, fd, offset);
741 - if (ret == MAP_FAILED && !length)
742 - ret = NULL;
743 - return ret;
744 -}
745 -
746 -const char *mmap_os_err(void)
747 -{
748 - static const char blank[] = "";
749 -#if defined(__linux__)
750 - if (errno == ENOMEM) {
751 - /* this continues an existing error message: */
752 - static const char enomem[] =
753 -", check sys.vm.max_map_count and/or RLIMIT_DATA";
754 - return enomem;
755 - }
756 -#endif /* OS-specific bits */
757 - return blank;
758 -}
759 -
760 -void *xmmap(void *start, size_t length,
761 - int prot, int flags, int fd, off_t offset)
762 -{
763 - void *ret = xmmap_gently(start, length, prot, flags, fd, offset);
764 - if (ret == MAP_FAILED)
765 - die_errno(_("mmap failed%s"), mmap_os_err());
766 - return ret;
767 -}
768 -
721 static int format_object_header_literally(char *str, size_t size,
722 const char *type, size_t objsize)
723 {
wrapper.c
+48
@@ -829,3 +829,51 @@ uint32_t git_rand(unsigned flags)
829
830 return result;
831 }
832 +
833 +static void mmap_limit_check(size_t length)
834 +{
835 + static size_t limit = 0;
836 + if (!limit) {
837 + limit = git_env_ulong("GIT_MMAP_LIMIT", 0);
838 + if (!limit)
839 + limit = SIZE_MAX;
840 + }
841 + if (length > limit)
842 + die(_("attempting to mmap %"PRIuMAX" over limit %"PRIuMAX),
843 + (uintmax_t)length, (uintmax_t)limit);
844 +}
845 +
846 +void *xmmap_gently(void *start, size_t length,
847 + int prot, int flags, int fd, off_t offset)
848 +{
849 + void *ret;
850 +
851 + mmap_limit_check(length);
852 + ret = mmap(start, length, prot, flags, fd, offset);
853 + if (ret == MAP_FAILED && !length)
854 + ret = NULL;
855 + return ret;
856 +}
857 +
858 +const char *mmap_os_err(void)
859 +{
860 + static const char blank[] = "";
861 +#if defined(__linux__)
862 + if (errno == ENOMEM) {
863 + /* this continues an existing error message: */
864 + static const char enomem[] =
865 +", check sys.vm.max_map_count and/or RLIMIT_DATA";
866 + return enomem;
867 + }
868 +#endif /* OS-specific bits */
869 + return blank;
870 +}
871 +
872 +void *xmmap(void *start, size_t length,
873 + int prot, int flags, int fd, off_t offset)
874 +{
875 + void *ret = xmmap_gently(start, length, prot, flags, fd, offset);
876 + if (ret == MAP_FAILED)
877 + die_errno(_("mmap failed%s"), mmap_os_err());
878 + return ret;
879 +}