]> mj.ucw.cz Git - libucw.git/commitdiff
Added unmapping and writeable mappings.
authorMartin Mares <mj@ucw.cz>
Fri, 16 Feb 2001 17:56:06 +0000 (17:56 +0000)
committerMartin Mares <mj@ucw.cz>
Fri, 16 Feb 2001 17:56:06 +0000 (17:56 +0000)
lib/lib.h
lib/mmap.c

index b397c89b47c25152f799b00e21c8ea86b6bf12f2..9283e7aadb896473b5b6143fd726225ee2eb2b1c 100644 (file)
--- a/lib/lib.h
+++ b/lib/lib.h
@@ -178,6 +178,7 @@ uns random_max(uns);
 
 /* mmap.c */
 
-void *mmap_file(byte *name, unsigned *len);
+void *mmap_file(byte *name, unsigned *len, int writeable);
+void munmap_file(void *start, unsigned len);
 
 #endif
index 62101f8d66a210c16225765c48cfc48e83f13e4f..e461e1ee2838d48f0aded5e1b59765cf1a567a22 100644 (file)
 #include <sys/mman.h>
 
 #ifndef MAP_FAILED
+#warning System includes do not define MAP_FAILED.
 #define MAP_FAILED ((void *)-1L)
 #endif
 
 void *
-mmap_file(byte *name, unsigned *len)
+mmap_file(byte *name, unsigned *len, int writeable)
 {
-  int fd = open(name, O_RDONLY);
+  int fd = open(name, writeable ? O_RDWR : O_RDONLY);
   struct stat st;
   void *x;
 
@@ -31,10 +32,16 @@ mmap_file(byte *name, unsigned *len)
     {
       if (len)
        *len = st.st_size;
-      x = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
+      x = mmap(NULL, st.st_size, writeable ? (PROT_READ | PROT_WRITE) : PROT_READ, MAP_SHARED, fd, 0);
       if (x == MAP_FAILED)
        x = NULL;
     }
   close(fd);
   return x;
 }
+
+void
+munmap_file(void *start, unsigned len)
+{
+  munmap(start, len);
+}