win32: Add routines to abstract memory-mapped file functions In particular, the git__mmap() and git__munmap() routines provide the interface to platform specific memory-mapped file facilities. We provide implementations for unix and win32, which can be found in their own sub-directories. Signed-off-by: Ramsay Jones <ramsay@ramsay1.demon.co.uk> Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
diff --git a/Makefile b/Makefile
index ca88eb6..6d681ba 100644
--- a/Makefile
+++ b/Makefile
@@ -11,6 +11,7 @@ all::
DOXYGEN = doxygen
INSTALL = install
RANLIB = ranlib
+AR = ar cr
prefix=/usr/local
libdir=$(prefix)/lib
@@ -21,6 +22,8 @@ CFLAGS = -g -O2 -Wall
LIBS = -L. -lgit2 -lz
OS = unix
+CRYPTO_LIB = -lcrypto
+
EXTRA_LIBS =
EXTRA_SRC =
EXTRA_OBJ =
@@ -32,6 +35,8 @@ ifneq (,$(findstring CYGWIN,$(uname_S)))
endif
SRC_C = $(wildcard src/*.c)
+OS_SRC = $(wildcard src/$(OS)/*.c)
+SRC_C += $(OS_SRC)
OBJS = $(patsubst %.c,%.o,$(SRC_C))
HDRS = $(wildcard src/*.h)
PUBLIC_HEADERS = $(wildcard src/git/*.h)
@@ -46,7 +51,7 @@ TEST_RUN = $(patsubst %.exe,%.run,$(TEST_EXE))
ifndef NO_OPENSSL
SHA1_HEADER = <openssl/sha.h>
- EXTRA_LIBS += -lcrypto
+ EXTRA_LIBS += $(CRYPTO_LIB)
else
SHA1_HEADER = "sha1/sha1.h"
EXTRA_SRC += src/sha1/sha1.c
@@ -68,8 +73,7 @@ all:: $(GIT_LIB)
clean:
rm -f $(GIT_LIB)
rm -f libgit2.pc
- rm -f src/*.o
- rm -f src/sha1/*.o
+ rm -f src/*.o src/sha1/*.o src/unix/*.o
rm -f tests/*.o tests/*.exe tests/*.toc
rm -rf trash-*.exe
rm -rf apidocs
@@ -111,7 +115,7 @@ uninstall:
$(OBJS): $(HDRS)
$(GIT_LIB): $(OBJS)
rm -f $(GIT_LIB)
- $(AR) cr $(GIT_LIB) $(OBJS)
+ $(AR) $(GIT_LIB) $(OBJS)
$(RANLIB) $(GIT_LIB)
T_HDR = tests/test_lib.h tests/test_helpers.h
diff --git a/src/common.h b/src/common.h
index 14dc301..221c73c 100644
--- a/src/common.h
+++ b/src/common.h
@@ -23,14 +23,15 @@
#ifdef GIT_WIN32
# include <io.h>
-# include <winsock2.h>
+# include <windows.h>
+
+#define snprintf _snprintf
typedef int ssize_t;
#else
# include <unistd.h>
-# include <sys/mman.h>
# include <arpa/inet.h>
#endif
diff --git a/src/fileops.c b/src/fileops.c
index e2ec615..09e1e12 100644
--- a/src/fileops.c
+++ b/src/fileops.c
@@ -106,18 +106,16 @@ void gitfo_free_buf(gitfo_buf *obj)
obj->data = NULL;
}
-int gitfo_map_ro(gitfo_map *out, git_file fd, off_t begin, size_t len)
+int gitfo_map_ro(git_map *out, git_file fd, off_t begin, size_t len)
{
- out->data = mmap(NULL, len, PROT_READ, MAP_SHARED, fd, begin);
- if (out->data == (void*)-1)
+ if (git__mmap(out, len, GIT_PROT_READ, GIT_MAP_SHARED, fd, begin) < 0)
return git_os_error();
- out->len = len;
return GIT_SUCCESS;
}
-void gitfo_free_map(gitfo_map *out)
+void gitfo_free_map(git_map *out)
{
- munmap(out->data, out->len);
+ git__munmap(out);
}
/* cached diskio */
diff --git a/src/fileops.h b/src/fileops.h
index 3cc1e16..39e0181 100644
--- a/src/fileops.h
+++ b/src/fileops.h
@@ -10,6 +10,7 @@
#define _FILE_OFFSET_BITS 64
#include "common.h"
+#include "map.h"
#include <sys/stat.h>
#include <fcntl.h>
#include <time.h>
@@ -29,11 +30,6 @@ typedef struct { /* file io buffer */
size_t len; /* data length */
} gitfo_buf;
-typedef struct { /* memory mapped buffer */
- void *data; /* data bytes */
- size_t len; /* data length */
-} gitfo_map;
-
extern int gitfo_exists(const char *path);
extern int gitfo_open(const char *path, int flags);
extern int gitfo_creat(const char *path, int mode);
@@ -66,7 +62,7 @@ extern void gitfo_free_buf(gitfo_buf *obj);
* - GIT_EOSERR on an unspecified OS related error.
*/
extern int gitfo_map_ro(
- gitfo_map *out,
+ git_map *out,
git_file fd,
off_t begin,
size_t len);
@@ -75,7 +71,7 @@ extern int gitfo_map_ro(
* Release the memory associated with a previous memory mapping.
* @param map the mapping description previously configured.
*/
-extern void gitfo_free_map(gitfo_map *map);
+extern void gitfo_free_map(git_map *map);
/**
* Walk each directory entry, except '.' and '..', calling fn(state).
diff --git a/src/map.h b/src/map.h
new file mode 100644
index 0000000..3188ffd
--- /dev/null
+++ b/src/map.h
@@ -0,0 +1,31 @@
+#ifndef INCLUDE_map_h__
+#define INCLUDE_map_h__
+
+#include "common.h"
+
+
+/* git__mmap() prot values */
+#define GIT_PROT_NONE 0x0
+#define GIT_PROT_READ 0x1
+#define GIT_PROT_WRITE 0x2
+#define GIT_PROT_EXEC 0x4
+
+/* git__mmmap() flags values */
+#define GIT_MAP_FILE 0
+#define GIT_MAP_SHARED 1
+#define GIT_MAP_PRIVATE 2
+#define GIT_MAP_TYPE 0xf
+#define GIT_MAP_FIXED 0x10
+
+typedef struct { /* memory mapped buffer */
+ void *data; /* data bytes */
+ size_t len; /* data length */
+#ifdef GIT_WIN32
+ HANDLE fmh; /* file mapping handle */
+#endif
+} git_map;
+
+extern int git__mmap(git_map *out, size_t len, int prot, int flags, int fd, off_t offset);
+extern int git__munmap(git_map *map);
+
+#endif /* INCLUDE_map_h__ */
diff --git a/src/odb.c b/src/odb.c
index 11fd982..bd65f47 100644
--- a/src/odb.c
+++ b/src/odb.c
@@ -43,7 +43,7 @@ struct git_pack {
/** The .idx file, mapped into memory. */
git_file idx_fd;
- gitfo_map idx_map;
+ git_map idx_map;
uint32_t *im_fanout;
unsigned char *im_oid;
uint32_t *im_crc;
diff --git a/src/unix/map.c b/src/unix/map.c
new file mode 100644
index 0000000..a41bae0
--- /dev/null
+++ b/src/unix/map.c
@@ -0,0 +1,61 @@
+
+#include "map.h"
+#include <sys/mman.h>
+#include <errno.h>
+
+
+int git__mmap(git_map *out, size_t len, int prot, int flags, int fd, off_t offset)
+{
+ int mprot = 0;
+ int mflag = 0;
+
+ assert((out != NULL) && (len > 0));
+
+ if ((out == NULL) || (len == 0)) {
+ errno = EINVAL;
+ return GIT_ERROR;
+ }
+
+ out->data = NULL;
+ out->len = 0;
+
+ if (prot & GIT_PROT_WRITE)
+ mprot = PROT_WRITE;
+ else if (prot & GIT_PROT_READ)
+ mprot = PROT_READ;
+ else {
+ errno = EINVAL;
+ return GIT_ERROR;
+ }
+
+ if ((flags & GIT_MAP_TYPE) == GIT_MAP_SHARED)
+ mflag = MAP_SHARED;
+ else if ((flags & GIT_MAP_TYPE) == GIT_MAP_PRIVATE)
+ mflag = MAP_PRIVATE;
+
+ if (flags & GIT_MAP_FIXED) {
+ errno = EINVAL;
+ return GIT_ERROR;
+ }
+
+ out->data = mmap(NULL, len, mprot, mflag, fd, offset);
+ if (!out->data || out->data == MAP_FAILED)
+ return git_os_error();
+ out->len = len;
+
+ return GIT_SUCCESS;
+}
+
+int git__munmap(git_map *map)
+{
+ assert(map != NULL);
+
+ if (!map)
+ return GIT_ERROR;
+
+ munmap(map->data, map->len);
+
+ return GIT_SUCCESS;
+}
+
+
diff --git a/src/win32/map.c b/src/win32/map.c
new file mode 100644
index 0000000..7d685c7
--- /dev/null
+++ b/src/win32/map.c
@@ -0,0 +1,125 @@
+
+#include "map.h"
+#include <errno.h>
+
+
+static DWORD get_page_size(void)
+{
+ static DWORD page_size;
+ SYSTEM_INFO sys;
+
+ if (!page_size) {
+ GetSystemInfo(&sys);
+ page_size = sys.dwAllocationGranularity;
+ }
+
+ return page_size;
+}
+
+int git__mmap(git_map *out, size_t len, int prot, int flags, int fd, off_t offset)
+{
+ HANDLE fh = (HANDLE)_get_osfhandle(fd);
+ DWORD page_size = get_page_size();
+ DWORD fmap_prot = 0;
+ DWORD view_prot = 0;
+ DWORD off_low = 0;
+ DWORD off_hi = 0;
+ off_t page_start;
+ off_t page_offset;
+
+ assert((out != NULL) && (len > 0));
+
+ if ((out == NULL) || (len == 0)) {
+ errno = EINVAL;
+ return GIT_ERROR;
+ }
+
+ out->data = NULL;
+ out->len = 0;
+ out->fmh = NULL;
+
+ if (fh == INVALID_HANDLE_VALUE) {
+ errno = EBADF;
+ return GIT_ERROR;
+ }
+
+ if (prot & GIT_PROT_WRITE)
+ fmap_prot |= PAGE_READWRITE;
+ else if (prot & GIT_PROT_READ)
+ fmap_prot |= PAGE_READONLY;
+ else {
+ errno = EINVAL;
+ return GIT_ERROR;
+ }
+
+ if (prot & GIT_PROT_WRITE)
+ view_prot |= FILE_MAP_WRITE;
+ if (prot & GIT_PROT_READ)
+ view_prot |= FILE_MAP_READ;
+
+ if (flags & GIT_MAP_FIXED) {
+ errno = EINVAL;
+ return GIT_ERROR;
+ }
+
+ page_start = (offset / page_size) * page_size;
+ page_offset = offset - page_start;
+
+ if (page_offset != 0) { /* offset must be multiple of page size */
+ errno = EINVAL;
+ return GIT_ERROR;
+ }
+
+ out->fmh = CreateFileMapping(fh, NULL, fmap_prot, 0, 0, NULL);
+ if (!out->fmh || out->fmh == INVALID_HANDLE_VALUE) {
+ /* errno = ? */
+ out->fmh = NULL;
+ return GIT_ERROR;
+ }
+
+ off_low = (DWORD)(page_start);
+ if (sizeof(off_t) > 4)
+ off_hi = (DWORD)(page_start >> 32);
+ out->data = MapViewOfFile(out->fmh, view_prot, off_hi, off_low, len);
+ if (!out->data) {
+ /* errno = ? */
+ CloseHandle(out->fmh);
+ out->fmh = NULL;
+ return GIT_ERROR;
+ }
+ out->len = len;
+
+ return GIT_SUCCESS;
+}
+
+int git__munmap(git_map *map)
+{
+ assert(map != NULL);
+
+ if (!map)
+ return GIT_ERROR;
+
+ if (map->data) {
+ if (!UnmapViewOfFile(map->data)) {
+ /* errno = ? */
+ CloseHandle(map->fmh);
+ map->data = NULL;
+ map->fmh = NULL;
+ return GIT_ERROR;
+ }
+ map->data = NULL;
+ }
+
+ if (map->fmh) {
+ if (!CloseHandle(map->fmh)) {
+ /* errno = ? */
+ map->fmh = NULL;
+ return GIT_ERROR;
+ }
+ map->fmh = NULL;
+ }
+
+ return GIT_SUCCESS;
+}
+
+