summaryrefslogtreecommitdiff
path: root/ace/Mem_Map.h
blob: b8358d120b6509b18f23cc5bce97a086f88e4770 (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/* -*- C++ -*- */
// $Id$


// ============================================================================
//
// = LIBRARY
//    ace
// 
// = FILENAME
//    Mem_Map.h 
//
// = AUTHOR
//    Doug Schmidt 
// 
// ============================================================================

#if !defined (ACE_MEM_MAP_H)
#define ACE_MEM_MAP_H

#include "ace/ACE.h"

class ACE_Export ACE_Mem_Map
  // = TITLE
  //     C++ interface to the mmap(2) UNIX system call. 
{
public:
  // = Initialization and termination methods.

  ACE_Mem_Map (void);
  // Default constructor.

  ACE_Mem_Map (ACE_HANDLE handle, 
	       int length = -1, 
	       int prot = PROT_RDWR, 
	       int share = MAP_PRIVATE, 
	       void *addr = 0, 
	       off_t pos = 0);
  // Map a file from an open file descriptor <handle>.  This function
  // will lookup the length of the file if it is not given.

  ACE_Mem_Map (LPCTSTR filename,
	       int len = -1, 
	       int flags = O_RDWR | O_CREAT, 
	       int mode = ACE_DEFAULT_FILE_PERMS, 
	       int prot = PROT_RDWR, 
	       int share = MAP_PRIVATE, 
	       void *addr = 0, 
	       off_t pos = 0);
  // Map a file specified by <file_name>.

  int map (ACE_HANDLE handle, 
	   int length = -1, 
	   int prot = PROT_RDWR, 
	   int share = MAP_PRIVATE, 
	   void *addr = 0,
	   off_t pos = 0);
  // Map a file from an open file descriptor <handle>.  This function
  // will lookup the length of the file if it is not given.

  int map (int length = -1, 
	   int prot = PROT_RDWR, 
	   int share = MAP_PRIVATE, 
	   void *addr = 0, 
	   off_t pos = 0);
  // Remap the file associated with <handle_>.

  int map (LPCTSTR filename,
	   int len = -1, 
	   int flags = O_RDWR | O_CREAT, 
	   int mode = ACE_DEFAULT_FILE_PERMS, 
	   int prot = PROT_RDWR, 
	   int share = MAP_PRIVATE, 
	   void *addr = 0, 
	   off_t pos = 0);
  // Map a file specified by <filename>.

  ~ACE_Mem_Map (void);
  // Destructor.

  int open (LPCTSTR filename,
	    int flags = O_RDWR | O_CREAT, 
	    int mode = ACE_DEFAULT_FILE_PERMS);
  // Open the file without mapping it.

  int close (void);
  // Close down the <handle_> if necessary.

  int operator () (void *&addr);
  // This operator passes back the starting address of the mapped
  // file.

  void *addr (void) const;
  // Return the base address.

  size_t size (void) const;
  // This function returns the number of bytes currently mapped in the
  // file.

  int unmap (int len = -1);
  // Unmap the region starting at <base_addr_>.

  int unmap (void *addr, int len);
  // Unmap the region starting at <addr_>.

  int sync (ssize_t len = -1, int flags = MS_SYNC);
  // Sync <len> bytes of the memory region to the backing store
  // starting at <base_addr_>.  If <len> == -1 then sync the whole
  // region.

  int sync (void *addr, size_t len, int flags = MS_SYNC);
  // Sync <len> bytes of the memory region to the backing store
  // starting at <addr_>.

  int protect (ssize_t len = -1, int prot = PROT_RDWR);
  // Change the protection of the pages of the mapped region to <prot>
  // starting at <base_addr_> up to <len> bytes.  If <len> == -1 then
  // change protection of all pages in the mapped region.

  int protect (void *addr, size_t len, int prot = PROT_RDWR);
  // Change the protection of the pages of the mapped region to <prot>
  // starting at <addr> up to <len> bytes.

  int remove (void);
  // Close down and remove the file from the file system.

  int advise (int behavior, int len = -1);
  // Hook into the underlying VM system.

  ACE_HANDLE handle (void) const;
  // Return the underlying <handle_>.

  const TCHAR *filename (void) const;	
  // Return the name of file that is mapped (if any).

  void dump (void) const;
  // Dump the state of an object.

  ACE_ALLOC_HOOK_DECLARE;
  // Declare the dynamic allocation hooks.

private:
  void *base_addr_;
  // Base address of the memory-mapped file.

  TCHAR filename_[MAXPATHLEN + 1];
  // Name of the file that is mapped.

  size_t length_;
  // Length of the mapping.
  
  ACE_HANDLE handle_;
  // HANDLE for the open file.

  ACE_HANDLE file_mapping_;
  // HANDLE for the open mapping.

  int close_handle_;
  // Keeps track of whether we need to close the handle.  This is set
  // if we opened the file.

  int map_it (ACE_HANDLE handle, 
	      int len = -1, 
	      int prot = PROT_RDWR, 
	      int share = MAP_SHARED, 
	      void *addr = 0, 
	      off_t pos = 0);
  // This method does the dirty work of actually calling ::mmap to map
  // the file into memory.

  // = Disallow copying and assignment.
  ACE_Mem_Map (const ACE_Mem_Map &) {}
  void operator = (const ACE_Mem_Map &) {}
};

#if defined (__ACE_INLINE__)
#include "ace/Mem_Map.i"
#endif /* __ACE_INLINE__ */

#endif /* ACE_MEM_MAP_H */