Update.
[glibc.git] / db2 / os / db_os_mmap.c
blob0cd8fad0b0158ce3ae61897f99984533926eaae8
1 /*-
2 * See the file LICENSE for redistribution information.
4 * Copyright (c) 1996, 1997
5 * Sleepycat Software. All rights reserved.
6 */
8 #include "config.h"
10 #ifndef lint
11 static const char sccsid[] = "@(#)db_os_mmap.c 10.4 (Sleepycat) 6/28/97";
12 #endif /* not lint */
14 #ifndef NO_SYSTEM_INCLUDES
15 #include <sys/types.h>
16 #include <sys/mman.h>
18 #include <errno.h>
19 #endif
21 #include "db_int.h"
22 #include "os_ext.h"
25 * __db_mmap --
26 * Map in some shared memory backed by a file descriptor.
28 * PUBLIC: int __db_mmap __P((int, size_t, int, int, void *));
30 int
31 __db_mmap(fd, len, is_private, rdonly, addr)
32 int fd, is_private, rdonly;
33 size_t len;
34 void *addr;
36 #ifdef _WIN32
37 /* We have not implemented copy-on-write here */
38 void * pMemory = 0;
39 HANDLE hFile = (HANDLE)_get_osfhandle(fd);
40 HANDLE hMemory = CreateFileMapping(
41 hFile,
43 (rdonly ? PAGE_READONLY : PAGE_READWRITE),
45 len, /* This code fails if the library is ever compiled on a 64-bit machine */
48 if (NULL == hMemory)
50 return errno;
52 pMemory = MapViewOfFile(
53 hMemory,
54 (rdonly ? FILE_MAP_READ : FILE_MAP_ALL_ACCESS),
57 len
59 CloseHandle(hMemory);
60 *(void **)addr = pMemory;
61 return 0;
63 #else /* !_WIN32 */
65 void *p;
66 int flags, prot;
68 flags = is_private ? MAP_PRIVATE : MAP_SHARED;
69 #ifdef MAP_HASSEMAPHORE
70 flags += MAP_HASSEMAPHORE;
71 #endif
72 prot = PROT_READ | (rdonly ? 0 : PROT_WRITE);
74 #ifndef MAP_FAILED /* XXX: Mmap(2) failure return. */
75 #define MAP_FAILED -1
76 #endif
77 if ((p =
78 mmap(NULL, len, prot, flags, fd, (off_t)0)) == (void *)MAP_FAILED)
79 return (errno);
81 *(void **)addr = p;
82 return (0);
83 #endif /* _WIN32 */
87 * __db_unmap --
88 * Release the specified shared memory.
90 * PUBLIC: int __db_munmap __P((void *, size_t));
92 int
93 __db_munmap(addr, len)
94 void *addr;
95 size_t len;
98 * !!!
99 * The argument len is always the same length as was mapped.
101 #ifdef _WIN32
102 return (!UnmapViewOfFile(addr) ? errno : 0);
103 #else
104 return (munmap(addr, len) ? errno : 0);
105 #endif