reimplement mingw mmap
authorDave Murphy <davem@devkitpro.org>
Wed, 16 May 2012 21:15:38 +0000 (22:15 +0100)
committerDave Murphy <davem@devkitpro.org>
Wed, 16 May 2012 21:15:38 +0000 (22:15 +0100)
configure.ac
src/mmap.c
src/mmap.h
src/pread.c [deleted file]

index 866d8bbd5830ba9e8068383f688a60df61cdc23e..f7feed2cc49aef62e054ffdebc4048859343ff4a 100644 (file)
@@ -18,8 +18,7 @@ AM_PROG_CC_C_O
 
 AC_CHECK_HEADERS(sys/mman.h)
 AC_CHECK_HEADERS(sys/poll.h)
-AC_CHECK_FUNCS(mmap)
-AC_REPLACE_FUNCS(mmap pread)
+AC_REPLACE_FUNCS(mmap)
 
 # Checks for libraries.
 PKG_CHECK_MODULES(USB, libusb-1.0 >= 1.0.0,,
index 7316389b99ec6eca4f9768d01030b86861aa582b..8a0a2e361dcebef27f0098ff84c57382b346d788 100644 (file)
@@ -1,69 +1,33 @@
-/* mmap.c -- version of mmap for gold.  */
-
-/* Copyright 2009 Free Software Foundation, Inc.
-   Written by Vladimir Simonov <sv@sw.ru>.
-
-   This file is part of gold.
-
-   This program is free software; you can redistribute it and/or modify
-   it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 3 of the License, or
-   (at your option) any later version.
-
-   This program 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 General Public License for more details.
-
-   You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
-   MA 02110-1301, USA.  */
-
-//#include "config.h"
-//#include "ansidecl.h"
-
 #include <string.h>
 #include <sys/types.h>
-#ifdef HAVE_STDLIB_H
 #include <stdlib.h>
-#endif
+#include <unistd.h>
 
 #include "mmap.h"
 
-extern ssize_t pread (int, void *, size_t, off_t);
-
-void *
-mmap (void *__addr, size_t __len, int __prot,
-       int __flags, int __fd, long long  __offset)
-{
-  void *ret;
-  ssize_t count;
+void *mmap (void *addr, size_t len, int prot, int flags, int fd, long long  offset) {
 
-  if (__addr || (__fd != -1 && (__prot & PROT_WRITE))
-             || (__flags & MAP_SHARED))
-    return MAP_FAILED;
+       void *buf;
+       ssize_t count;
 
-  ret = malloc (__len);
-  if (!ret)
-    return MAP_FAILED;
+       if ( addr || fd == -1 || (prot & PROT_WRITE)) return MAP_FAILED;
 
-  if (__fd == -1)
-    return ret;
+       buf = malloc(len);
+       if ( NULL == buf ) return MAP_FAILED;
 
-  count = pread (__fd, ret, __len, __offset);
-  if ((size_t) count != __len)
-    {
-    free (ret);
-    return MAP_FAILED;
-    }
+       if (lseek(fd,offset,SEEK_SET) != offset) return MAP_FAILED;
+       
+       count = read(fd, buf, len);
+       
+       if (count != len) {
+               free (buf);
+               return MAP_FAILED;
+       }
 
-  return ret;
+       return buf;
 }
 
-int
-munmap (void *__addr, size_t __len)
-{
-  free (__addr);
-  return 0;
+int munmap (void *addr, size_t len) {
+       free (addr);
+       return 0;
 }
index 375eafb7b57947f3456605da94ca1f01d5870916..bff766c4ac8cd3f0a7472a527e929af1714a14f2 100644 (file)
@@ -1,62 +1,31 @@
-/* mmap.h -- mmap family functions prototypes for gold.  */
-
-/* Copyright 2009 Free Software Foundation, Inc.
-   Written by Vladimir Simonov <sv@sw.ru>
-
-   This file is part of gold.
-
-   This program is free software; you can redistribute it and/or modify
-   it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 3 of the License, or
-   (at your option) any later version.
-
-   This program 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 General Public License for more details.
-
-   You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
-   MA 02110-1301, USA.  */
-
-#ifndef GOLD_MMAP_H
-#define GOLD_MMAP_H
+#ifndef STLINK_MMAP_H
+#define STLINK_MMAP_H
 
 #ifdef HAVE_SYS_MMAN_H
-
 #include <sys/mman.h>
-
-/* Some BSD systems still use MAP_ANON instead of MAP_ANONYMOUS.  */
-
-#ifndef MAP_ANONYMOUS
-# define MAP_ANONYMOUS MAP_ANON
-#endif
-
 #else
 
-#define PROT_READ 0x1
-#define PROT_WRITE 0x2
+#define PROT_READ      (1<<0)
+#define PROT_WRITE     (1<<1)
 
-#define MAP_SHARED 0x01
-#define MAP_PRIVATE 0x02
+#define MAP_SHARED     (1<<0)
+#define MAP_PRIVATE    (1<<1)
 
-#define MAP_ANONYMOUS 0x20
+#define MAP_ANONYMOUS (1<<5)
 
-#define MAP_FAILED ((void *) -1)
-#endif /* HAVE_SYS_MMAN_H */
+#define MAP_FAILED ((void *)-1)
 
-#ifndef HAVE_MMAP
 #ifdef __cplusplus
 extern "C" {
 #endif
-extern void *mmap (void *__addr, size_t __len, int __prot,
-                    int __flags, int __fd, long long __offset);
-extern int munmap (void *__addr, size_t __len);
+
+void *mmap(void *addr, size_t len, int prot, int flags, int fd, long long offset);
+int munmap(void *addr, size_t len);
+
 #ifdef __cplusplus
 }
 #endif
 
-#endif /* HAVE_MMAP */
+#endif /* HAVE_SYS_MMAN_H */
 
-#endif /* GOLD_MMAP_H */
+#endif /* STLINK_MMAP_H */
diff --git a/src/pread.c b/src/pread.c
deleted file mode 100644 (file)
index 4ac6115..0000000
+++ /dev/null
@@ -1,42 +0,0 @@
-/* pread.c -- version of pread for gold.  */
-
-/* Copyright 2006, 2007, 2009 Free Software Foundation, Inc.
-   Written by Ian Lance Taylor <iant@google.com>.
-
-   This file is part of gold.
-
-   This program is free software; you can redistribute it and/or modify
-   it under the terms of the GNU General Public License as published by
-   the Free Software Foundation; either version 3 of the License, or
-   (at your option) any later version.
-
-   This program 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 General Public License for more details.
-
-   You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software
-   Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
-   MA 02110-1301, USA.  */
-
-/* This file implements pread for systems which don't have it.  This
-   file is only compiled if pread is not present on the system.  This
-   is not an exact version of pread, as it does not preserve the
-   current file offset.  */
-
-//#include "config.h"
-
-#include <stdio.h>
-#include <sys/types.h>
-#include <unistd.h>
-
-extern ssize_t pread (int, void *, size_t, off_t);
-
-ssize_t
-pread (int fd, void *buf, size_t count, off_t offset)
-{
-  if (lseek(fd, offset, SEEK_SET) != offset)
-    return -1;
-  return read(fd, buf, count);
-}