summaryrefslogtreecommitdiff
path: root/db2/os/db_os_mmap.c
blob: 0cd8fad0b0158ce3ae61897f99984533926eaae8 (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
/*-
 * See the file LICENSE for redistribution information.
 *
 * Copyright (c) 1996, 1997
 *	Sleepycat Software.  All rights reserved.
 */

#include "config.h"

#ifndef lint
static const char sccsid[] = "@(#)db_os_mmap.c	10.4 (Sleepycat) 6/28/97";
#endif /* not lint */

#ifndef NO_SYSTEM_INCLUDES
#include <sys/types.h>
#include <sys/mman.h>

#include <errno.h>
#endif

#include "db_int.h"
#include "os_ext.h"

/*
 * __db_mmap --
 *	Map in some shared memory backed by a file descriptor.
 *
 * PUBLIC: int __db_mmap __P((int, size_t, int, int, void *));
 */
int
__db_mmap(fd, len, is_private, rdonly, addr)
	int fd, is_private, rdonly;
	size_t len;
	void *addr;
{
#ifdef _WIN32
	/* We have not implemented copy-on-write here */
	void * pMemory = 0;
	HANDLE hFile = (HANDLE)_get_osfhandle(fd);
	HANDLE hMemory = CreateFileMapping(
	      hFile,
	      0,
	      (rdonly ? PAGE_READONLY : PAGE_READWRITE),
	      0,
	      len, /* This code fails if the library is ever compiled on a 64-bit machine */
	      0
	      );
	if (NULL == hMemory)
	{
	      return errno;
	}
	pMemory = MapViewOfFile(
	      hMemory,
	      (rdonly ? FILE_MAP_READ : FILE_MAP_ALL_ACCESS),
	      0,
	      0,
	      len
	      );
	CloseHandle(hMemory);
	*(void **)addr = pMemory;
	return 0;

#else /* !_WIN32 */

	void *p;
	int flags, prot;

	flags = is_private ? MAP_PRIVATE : MAP_SHARED;
#ifdef MAP_HASSEMAPHORE
	flags += MAP_HASSEMAPHORE;
#endif
	prot = PROT_READ | (rdonly ? 0 : PROT_WRITE);

#ifndef MAP_FAILED			/* XXX: Mmap(2) failure return. */
#define	MAP_FAILED	-1
#endif
	if ((p =
	    mmap(NULL, len, prot, flags, fd, (off_t)0)) == (void *)MAP_FAILED)
		return (errno);

	*(void **)addr = p;
	return (0);
#endif /* _WIN32 */
}

/*
 * __db_unmap --
 *	Release the specified shared memory.
 *
 * PUBLIC: int __db_munmap __P((void *, size_t));
 */
int
__db_munmap(addr, len)
	void *addr;
	size_t len;
{
	/*
	 * !!!
	 * The argument len is always the same length as was mapped.
	 */
#ifdef _WIN32
	return (!UnmapViewOfFile(addr) ? errno : 0);
#else
	return (munmap(addr, len) ? errno : 0);
#endif
}