[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[Libguestfs] [PATCH 5/5] Add lib/mmap.c file





--
Unix _IS_ user friendly - it's just
selective about who its friends are!
diff --git a/lib/mmap.c b/lib/mmap.c
index e69de29..fa2ff13 100644
--- a/lib/mmap.c
+++ b/lib/mmap.c
@@ -0,0 +1,64 @@
+/* mmap replacement for mingw.
+ *
+ * Copyright (C) 2011 by Daniel Gillen <gillen (dot) dan (at) pinguin (dot) lu>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation;
+ * version 2.1 of the License.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ */
+
+#include "mmap.h"
+
+#include <windows.h>
+#include <io.h>
+
+void *mmap (void *p_addr, size_t len, int prot, int flags, int fd, off_t offset,
+            void **pp_winmap)
+{
+  void *p_map;
+
+  // Check parameters for unsupported values
+  if (p_addr != NULL)
+    return MAP_FAILED;
+  if (prot != PROT_READ)
+    return MAP_FAILED;
+  if (flags != MAP_SHARED)
+    return MAP_FAILED;
+
+  // Create file mapping
+  *pp_winmap = CreateFileMapping ((HANDLE)_get_osfhandle(fd),
+                                  NULL, PAGE_READONLY, 0, 0, NULL);
+  if (*pp_winmap == NULL)
+    return MAP_FAILED;
+
+  // Create map view
+  p_map = MapViewOfFile (*pp_winmap, FILE_MAP_READ, 0, 0, len);
+  if (p_map == NULL) {
+    CloseHandle (*pp_winmap);
+    return MAP_FAILED;
+  }
+
+  return p_map;
+}
+
+int munmap (void *p_addr, size_t len, void **pp_winmap) {
+  if (p_addr == NULL || *pp_winmap == NULL)
+    return -1;
+
+  // Close map view
+  if (UnmapViewOfFile (p_addr) == 0)
+    return -1;
+
+  // Close file mapping
+  if (CloseHandle (*pp_winmap) == 0)
+    return -1;
+
+  *pp_winmap = NULL;
+  return 0;
+}

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]