aboutsummaryrefslogtreecommitdiff
path: root/winsup/cygwin/mmap_helper.h
blob: 645c5e3aac847771542f15c278f5c743605a910b (plain)
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
/* mmap_helper.h

This file is part of Cygwin.

This software is a copyrighted work licensed under the terms of the
Cygwin license.  Please consult the file "CYGWIN_LICENSE" for
details. */

#ifndef _MMAP_HELPER_H
#define _MMAP_HELPER_H
#define _MMIOWRAP(__ptr, __len, __func) \
({ \
  BOOL __res; \
  for (int __i = 0; __i < 2; __i++) \
    { \
      __res = __func; \
      if (__res || __i > 0) \
	break; \
      DWORD __errcode = GetLastError (); \
      if (__errcode != ERROR_NOACCESS) \
	break; \
      switch (mmap_is_attached_or_noreserve (__ptr, __len)) \
	{ \
	case MMAP_NORESERVE_COMMITED: \
	  continue; \
	case MMAP_RAISE_SIGBUS: \
	  raise(SIGBUS); \
	default: \
	  break; \
	} \
      break; \
    } \
  __res; \
})

#define _MMSOCKWRAP(__ptr, __count, __func) \
({ \
  int __res; \
  for (int __i = 0; __i < 2; __i++) \
    { \
      __res = __func; \
      if (!__res || __i > 0) \
	break; \
      DWORD __errcode = WSAGetLastError (); \
      if (__errcode != WSAEFAULT) \
	break; \
      for (unsigned __j = 0; __j < __count; __j++) \
	switch (mmap_is_attached_or_noreserve (__ptr[__j].buf, __ptr[__j].len)) \
	  { \
	  case MMAP_NORESERVE_COMMITED: \
	    goto keeptrying; \
	  case MMAP_RAISE_SIGBUS: \
	    raise(SIGBUS); \
	  default: \
	    break; \
	  } \
      break; \
    keeptrying: \
      continue; \
    } \
  __res; \
})

extern inline BOOL
mmReadFile (HANDLE hFile, LPVOID lpBuffer, DWORD nNumberOfBytesToRead,
	    LPDWORD lpNumberOfBytesRead, LPOVERLAPPED lpOverlapped)
{
  return _MMIOWRAP (lpBuffer, nNumberOfBytesToRead,
		    (ReadFile (hFile, lpBuffer, nNumberOfBytesToRead,
			       lpNumberOfBytesRead, lpOverlapped)));
}

#ifdef _WINSOCK_H
extern inline int
mmWSARecvFrom (SOCKET s, LPWSABUF lpBuffers, DWORD dwBufferCount,
	    LPDWORD lpNumberOfBytesRecvd, LPDWORD lpFlags,
	    struct sockaddr* lpFrom,
	    LPINT lpFromlen, LPWSAOVERLAPPED lpOverlapped,
	    LPWSAOVERLAPPED_COMPLETION_ROUTINE lpCompletionRoutine)
{
  return _MMSOCKWRAP (lpBuffers, dwBufferCount,
		      (mmWSARecvFrom(s, lpBuffers, dwBufferCount,
				     lpNumberOfBytesRecvd, lpFlags, lpFrom,
				     lpFromlen, lpOverlapped,
				     lpCompletionRoutine)));
}
#endif /*_WINSOCK_H*/

#endif /*_MMAP_HELPER_H*/