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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
|
//=============================================================================
/**
* @file Mem_Map_Test.cpp
*
* $Id: Mem_Map_Test.cpp 93638 2011-03-24 13:16:05Z johnnyw $
*
* This test illustrates the use of ACE_Mem_Map to reverse a
* file. The test first creates a dummy file for testing, then
* reverses the file and then reverses it again to get back the
* original file.
*
*
* @author Prashant Jain <pjain@cs.wustl.edu>
*/
//=============================================================================
#include "test_config.h"
#include "ace/Mem_Map.h"
#include "ace/ACE.h"
#include "ace/OS_NS_string.h"
#include "ace/OS_NS_unistd.h"
#include "ace/OS_NS_fcntl.h"
#include "ace/OS_Memory.h"
#if !defined (ACE_LACKS_MMAP)
static const char ACE_ALPHABET[] = "abcdefghijklmnopqrstuvwxyz";
static const int LINE_LENGTH = 10;
static const int NUM_LINES = 15;
static void
reverse_file (ACE_HANDLE file_handle,
char *array,
size_t size)
{
int count = 0;
// LynxOS 3.0.0/PowerPC needs the volatile qualifier, with -O2
// optimization enabled and without ACE_HAS_INLINE.
volatile size_t i = size;
--i;
if (array[i] == '\0')
array[i] = '\n';
while (i-- > 0)
{
if (array[i] == '\n')
{
ACE_OS::write (file_handle, array + i + 1, count);
ACE_OS::write (file_handle, ACE_TEXT ("\n"), 1);
count = 0;
}
else
count++;
}
ACE_OS::write (file_handle, array, count+1);
}
static int
create_test_file (ACE_TCHAR *filename, int line_length, int num_lines)
{
char *mybuf = 0;
ACE_NEW_RETURN (mybuf, char[line_length + 1], -1);
const char *c = ACE_ALPHABET;
const char *d = c;
#if defined (__QNXNTO__) || (defined (ACE_VXWORKS) && (ACE_VXWORKS <= 0x670))
// For NTO has to applied to open the file, as Mem_Map can map only shared memory
ACE_Mem_Map mmap_4_open;
mmap_4_open.open (filename, O_RDWR | O_CREAT | O_TRUNC, ACE_DEFAULT_FILE_PERMS);
ACE_HANDLE file_handle = mmap_4_open.handle();
#else
ACE_HANDLE file_handle = ACE_OS::open (filename,
O_RDWR | O_CREAT | O_TRUNC,
ACE_DEFAULT_FILE_PERMS);
#endif
if (file_handle == ACE_INVALID_HANDLE)
{
delete [] mybuf;
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("Open failed for %s\n"),
filename),
-1);
}
for (int j = 0; j < num_lines; j++)
{
for (int i = 0; i < line_length; i++)
{
mybuf[i] = *c;
c++;
}
mybuf[line_length] = '\0';
c = ++d;
if (ACE_OS::write (file_handle, mybuf, line_length) != line_length)
{
delete [] mybuf;
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("%p (%d) <%s>\n"),
ACE_TEXT ("Write to file failed:"),
ACE_ERRNO_GET,
filename),
-1);
}
if (ACE_OS::write (file_handle, ACE_TEXT ("\n"), 1) != 1)
{
delete [] mybuf;
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("write to file %s failed\n"),
filename),
-1);
}
}
#if defined (__QNXNTO__) || (defined (ACE_VXWORKS) && (ACE_VXWORKS <= 0x670))
mmap_4_open.close();
#else
ACE_OS::close (file_handle);
#endif
delete [] mybuf;
return 0;
}
#endif /* !ACE_LACKS_MMAP */
int
run_main (int, ACE_TCHAR *[])
{
ACE_START_TEST (ACE_TEXT ("Mem_Map_Test"));
#if !defined (ACE_LACKS_MMAP)
#if defined (__QNXNTO__) || (defined (ACE_VXWORKS) && (ACE_VXWORKS <= 0x670))
ACE_ERROR ((LM_INFO,
ACE_TEXT ("mmap on QNX Neutrino/VxWorks can map only shared memory files\n")));
#endif
// = Initialize the temporary variable names
ACE_TCHAR test_file[MAXPATHLEN + 1];
ACE_TCHAR temp_file1[MAXPATHLEN + 1];
ACE_TCHAR temp_file2[MAXPATHLEN + 1];
// Get the temporary directory
// - 18 is for the filenames, ace_mem_map_temp_1 is the longest
if (ACE::get_temp_dir (test_file, MAXPATHLEN - 18) == -1)
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("Temporary path too long\n")),
-1);
// Copy the temp directory to the other variables
ACE_OS::strcpy (temp_file1, test_file);
ACE_OS::strcpy (temp_file2, test_file);
// Add the filenames to the end
ACE_OS::strcat (test_file,
ACE_TEXT ("ace_mem_map_test"));
ACE_OS::strcat (temp_file1,
ACE_TEXT ("ace_mem_map_temp_1"));
ACE_OS::strcat (temp_file2,
ACE_TEXT ("ace_mem_map_temp_2"));
// First create a test file to work on
if (create_test_file (test_file, LINE_LENGTH, NUM_LINES) != 0)
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("Create test file failed\n")),
-1);
ACE_Mem_Map mmap;
// First memory map the test file
if (mmap.map (test_file) == -1)
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("%n: %p %s\n%a"),
ACE_TEXT ("mmap"),
test_file),
-1);
// Now create a temporary file for intermediate processing
#if defined (__QNXNTO__) || (defined (ACE_VXWORKS) && (ACE_VXWORKS <= 0x670))
ACE_Mem_Map mmap_4_open;
mmap_4_open.open(temp_file1,
O_RDWR | O_TRUNC | O_CREAT,
ACE_DEFAULT_FILE_PERMS);
ACE_HANDLE temp_file_handle = mmap_4_open.handle();
#else
ACE_HANDLE temp_file_handle = ACE_OS::open (temp_file1,
O_RDWR | O_TRUNC | O_CREAT,
ACE_DEFAULT_FILE_PERMS);
#endif
if (temp_file_handle == ACE_INVALID_HANDLE)
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("Open failed\n")),
-1);
// Reverse the original file and write the output to the temporary
// file.
reverse_file (temp_file_handle,
(char *) mmap.addr (),
mmap.size ());
#if defined (__QNXNTO__) || (defined (ACE_VXWORKS) && (ACE_VXWORKS <= 0x670))
mmap_4_open.close();
#else
ACE_OS::close (temp_file_handle);
#endif
ACE_Mem_Map temp_mmap;
// Now memory map the temporary file
if (temp_mmap.map (temp_file1) == -1)
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("%n: %p %s\n%a"),
ACE_TEXT ("mmap"),
temp_file1),
-1);
#if defined (__QNXNTO__) || (defined (ACE_VXWORKS) && (ACE_VXWORKS <= 0x670))
mmap_4_open.open(temp_file2,
O_RDWR | O_TRUNC | O_CREAT,
ACE_DEFAULT_FILE_PERMS);
temp_file_handle = mmap_4_open.handle();
#else
temp_file_handle = ACE_OS::open (temp_file2,
O_RDWR | O_TRUNC | O_CREAT,
ACE_DEFAULT_FILE_PERMS);
#endif
if ( temp_file_handle == ACE_INVALID_HANDLE)
ACE_ERROR_RETURN ((LM_ERROR, ACE_TEXT ("Open failed\n")), -1);
// Now reverse the temporary file and write everything to the second
// temporary file.
reverse_file (temp_file_handle,
(char *) temp_mmap.addr (),
temp_mmap.size ());
#if defined (__QNXNTO__) || (defined (ACE_VXWORKS) && (ACE_VXWORKS <= 0x670))
mmap_4_open.close();
#else
ACE_OS::close (temp_file_handle);
#endif
// Memory map the second temporary file
ACE_Mem_Map temp_mmap2;
if (temp_mmap2.map (temp_file2) == -1)
ACE_ERROR_RETURN ((LM_ERROR,
ACE_TEXT ("%n: %p %s\n%a"),
ACE_TEXT ("mmap"),
temp_file2),
-1);
// Now do a memcmp -- the orig file and the second temporary file
// should be identical.
ACE_TEST_ASSERT (ACE_OS::memcmp (temp_mmap2.addr (),
mmap.addr (),
mmap.size ()) == 0);
// Delete the test file
mmap.remove ();
// Delete ACE_TEMP_TEST_FILE
temp_mmap.remove ();
// Delete ACE_TEMP_TEST_FILE_2
temp_mmap2.remove ();
#else /* !ACE_LACKS_MMAP */
ACE_ERROR ((LM_INFO,
ACE_TEXT ("mmap is not supported on this platform\n")));
#endif /* !ACE_LACKS_MMAP */
ACE_END_TEST;
return 0;
}
|