mingw_support.c 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. /*
  2. * Copyright 2008 Extreme Engineering Solutions, Inc.
  3. *
  4. * mmap/munmap implementation derived from:
  5. * Clamav Native Windows Port : mmap win32 compatibility layer
  6. * Copyright (c) 2005-2006 Gianluigi Tiesi <sherpya@netfarm.it>
  7. * Parts by Kees Zeelenberg <kzlg@users.sourceforge.net> (LibGW32C)
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Library General Public
  11. * License as published by the Free Software Foundation; either
  12. * version 2 of the License, or (at your option) any later version.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Library General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Library General Public
  20. * License along with this software; if not, write to the
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. */
  23. #include "mingw_support.h"
  24. #include <stdio.h>
  25. #include <stdint.h>
  26. #include <errno.h>
  27. #include <io.h>
  28. int fsync(int fd)
  29. {
  30. return _commit(fd);
  31. }
  32. void *mmap(void *addr, size_t len, int prot, int flags, int fd, int offset)
  33. {
  34. void *map = NULL;
  35. HANDLE handle = INVALID_HANDLE_VALUE;
  36. DWORD cfm_flags = 0, mvf_flags = 0;
  37. switch (prot) {
  38. case PROT_READ | PROT_WRITE:
  39. cfm_flags = PAGE_READWRITE;
  40. mvf_flags = FILE_MAP_ALL_ACCESS;
  41. break;
  42. case PROT_WRITE:
  43. cfm_flags = PAGE_READWRITE;
  44. mvf_flags = FILE_MAP_WRITE;
  45. break;
  46. case PROT_READ:
  47. cfm_flags = PAGE_READONLY;
  48. mvf_flags = FILE_MAP_READ;
  49. break;
  50. default:
  51. return MAP_FAILED;
  52. }
  53. handle = CreateFileMappingA((HANDLE) _get_osfhandle(fd), NULL,
  54. cfm_flags, HIDWORD(len), LODWORD(len), NULL);
  55. if (!handle)
  56. return MAP_FAILED;
  57. map = MapViewOfFile(handle, mvf_flags, HIDWORD(offset),
  58. LODWORD(offset), len);
  59. CloseHandle(handle);
  60. if (!map)
  61. return MAP_FAILED;
  62. return map;
  63. }
  64. int munmap(void *addr, size_t len)
  65. {
  66. if (!UnmapViewOfFile(addr))
  67. return -1;
  68. return 0;
  69. }