summaryrefslogtreecommitdiff
path: root/innobase/os/os0shm.c
blob: e03440cd4f44295595abfe88ebd5c4a27962f103 (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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/******************************************************
The interface to the operating system
shared memory primitives

(c) 1995 Innobase Oy

Created 9/23/1995 Heikki Tuuri
*******************************************************/

#include "os0shm.h"
#ifdef UNIV_NONINL
#include "os0shm.ic"
#endif

#ifdef __WIN__
#include "windows.h"

typedef	HANDLE	os_shm_t;
#endif

/********************************************************************
Creates an area of shared memory. It can be named so that
different processes may access it in the same computer.
If an area with the same name already exists, returns
a handle to that area (where the size of the area is
not changed even if this call requests a different size).
To use the area, it first has to be mapped to the process
address space by os_shm_map. */

os_shm_t
os_shm_create(
/*==========*/
			/* out, own: handle to the shared
			memory area, NULL if error */
	ulint	size,	/* in: area size < 4 GB */
	char*	name)	/* in: name of the area as a null-terminated
			string */
{
#ifdef __WIN__
	os_shm_t	shm;

	ut_a(name);
	ut_a(size > 0);
	ut_a(size < 0xFFFFFFFF);

	/* In Windows NT shared memory is created as a memory mapped
	file */
	shm = CreateFileMapping((HANDLE)0xFFFFFFFF, /* use operating system
						    swap file as the backing
						    file */
				NULL,		    /* default security
						    descriptor */
				PAGE_READWRITE,	    /* allow reading and
						    writing */
				0,		    /* size must be less
						    than 4 GB */
				(DWORD)size,	
				name);			
	return(shm);
#else
	UT_NOT_USED(size);
	UT_NOT_USED(name);

	return(NULL);
#endif
}

/***************************************************************************
Frees a shared memory area. The area can be freed only after it
has been unmapped in all the processes where it was mapped. */

ibool
os_shm_free(
/*========*/
				/* out: TRUE if success */
	os_shm_t	shm)	/* in, own: handle to a shared memory area */
{
#ifdef __WIN__

	BOOL	ret;

	ut_a(shm);

	ret = CloseHandle(shm);

	if (ret) {
		return(TRUE);
	} else {
		return(FALSE);
	}
#else
	UT_NOT_USED(shm);
#endif
}

/***************************************************************************
Maps a shared memory area in the address space of a process. */

void*
os_shm_map(
/*=======*/
				/* out: address of the area, NULL if error */
	os_shm_t	shm)	/* in: handle to a shared memory area */
{
#ifdef __WIN__
	void*	mem;

	ut_a(shm);

	mem = MapViewOfFile(shm,
			    FILE_MAP_ALL_ACCESS,  /* read and write access
			    			  allowed */
			    0,			  /* map from start of */
			    0,			  /* area */
			    0);			  /* map the whole area */
	return(mem);
#else
	UT_NOT_USED(shm);
#endif
}
		
/***************************************************************************
Unmaps a shared memory area from the address space of a process. */

ibool
os_shm_unmap(
/*=========*/
			/* out: TRUE if succeed */
	void*	addr)	/* in: address of the area */
{
#ifdef __WIN__
	BOOL	ret;

	ut_a(addr);

	ret = UnmapViewOfFile(addr);

	if (ret) {
		return(TRUE);
	} else {
		return(FALSE);
	}
#else
	UT_NOT_USED(addr);
#endif
}