summaryrefslogtreecommitdiff
path: root/gnuradio-runtime/lib/gr_vmcircbuf_createfilemapping.cc
blob: 1b4d9700a50760405896f55dbb881b9b3482cca6 (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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/* -*- c++ -*- */
/*
 * Copyright 2003,2005,2011 Free Software Foundation, Inc.
 *
 * This file is part of GNU Radio
 *
 * GNU Radio is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3, or (at your option)
 * any later version.
 *
 * GNU Radio is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with GNU Radio; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street,
 * Boston, MA 02110-1301, USA.
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdexcept>
#include <assert.h>
#include <unistd.h>
#include <fcntl.h>
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_MMAN_H
#include <sys/mman.h>
#endif
#include <errno.h>
#include <stdio.h>
#include <gr_pagesize.h>
#include <gr_vmcircbuf_createfilemapping.h>
#include <boost/format.hpp>

#ifdef HAVE_CREATEFILEMAPPING
// Print Windows error (could/should be global?)
static void
werror( char *where, DWORD last_error )
{
  char buf[1024];

  FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM,
		 NULL,
		 last_error,
		 0,    // default language
		 buf,
		 sizeof(buf)/sizeof(TCHAR),    // buffer size
		 NULL );
  fprintf( stderr, "%s: Error %d: %s", where, last_error, buf );
  return;
}
#endif


gr_vmcircbuf_createfilemapping::gr_vmcircbuf_createfilemapping (int size)
  : gr_vmcircbuf (size)
{
#if !defined(HAVE_CREATEFILEMAPPING)
  fprintf (stderr, "%s: createfilemapping is not available\n",__FUNCTION__);
  throw std::runtime_error ("gr_vmcircbuf_createfilemapping");
#else
  static int s_seg_counter = 0;

  if (size <= 0 || (size % gr_pagesize ()) != 0){
    fprintf (stderr, "gr_vmcircbuf_createfilemapping: invalid size = %d\n", size);
    throw std::runtime_error ("gr_vmcircbuf_createfilemapping");
  }

  std::string seg_name = str(boost::format("/gnuradio-%d-%d") % getpid () % s_seg_counter);

  d_handle = CreateFileMapping(INVALID_HANDLE_VALUE,    // use paging file
			       NULL,                    // default security
			       PAGE_READWRITE,          // read/write access
			       0,                       // max. object size
			       size,                    // buffer size
			       seg_name.c_str());       // name of mapping object

  s_seg_counter++;
  if (d_handle == NULL || d_handle == INVALID_HANDLE_VALUE){
    std::string msg = str(boost::format(
	      "gr_vmcircbuf_mmap_createfilemapping: CreateFileMapping [%s]") %
	      seg_name );
    werror((char *) msg.c_str(), GetLastError() );
    throw std::runtime_error ("gr_vmcircbuf_mmap_createfilemapping");
  }

  // Allocate virtual memory of the needed size, then free it so we can use it
  LPVOID first_tmp;
  first_tmp = VirtualAlloc( NULL, 2*size, MEM_RESERVE, PAGE_NOACCESS );
  if (first_tmp == NULL){
    werror( "gr_vmcircbuf_mmap_createfilemapping: VirtualAlloc", GetLastError());
    CloseHandle(d_handle);         // cleanup
    throw std::runtime_error ("gr_vmcircbuf_mmap_createfilemapping");
  }

  if (VirtualFree(first_tmp, 0, MEM_RELEASE) == 0){
    werror( "gr_vmcircbuf_mmap_createfilemapping: VirtualFree", GetLastError());
    CloseHandle(d_handle);         // cleanup
    throw std::runtime_error ("gr_vmcircbuf_mmap_createfilemapping");
  }

  d_first_copy =  MapViewOfFileEx((HANDLE)d_handle,   // handle to map object
				   FILE_MAP_WRITE,    // read/write permission
				   0,
				   0,
				   size,
				   first_tmp);
  if (d_first_copy != first_tmp){
    werror( "gr_vmcircbuf_mmap_createfilemapping: MapViewOfFileEx(1)", GetLastError());
    CloseHandle(d_handle);         // cleanup
    throw std::runtime_error ("gr_vmcircbuf_mmap_createfilemapping");
  }

  d_second_copy =  MapViewOfFileEx((HANDLE)d_handle,   // handle to map object
				   FILE_MAP_WRITE,     // read/write permission
				   0,
				   0,
				   size,
				   (char *)first_tmp + size);//(LPVOID) ((char *)d_first_copy + size));

  if (d_second_copy != (char *)first_tmp + size){
    werror( "gr_vmcircbuf_mmap_createfilemapping: MapViewOfFileEx(2)", GetLastError());
    UnmapViewOfFile(d_first_copy);
    CloseHandle(d_handle);                      // cleanup
    throw std::runtime_error ("gr_vmcircbuf_mmap_createfilemapping");
  }

#ifdef DEBUG
  fprintf (stderr,"gr_vmcircbuf_mmap_createfilemapping: contiguous? mmap %p %p %p %p\n",
	   (char *)d_first_copy, (char *)d_second_copy, size, (char *)d_first_copy + size);
#endif

  // Now remember the important stuff
  d_base = (char *) d_first_copy;
  d_size = size;
#endif /*HAVE_CREATEFILEMAPPING*/
}

gr_vmcircbuf_createfilemapping::~gr_vmcircbuf_createfilemapping ()
{
#ifdef HAVE_CREATEFILEMAPPING
  if (UnmapViewOfFile(d_first_copy) == 0)
  {
    werror("gr_vmcircbuf_createfilemapping: UnmapViewOfFile(d_first_copy)", GetLastError());
  }
  d_base=NULL;
  if (UnmapViewOfFile(d_second_copy) == 0)
  {
    werror("gr_vmcircbuf_createfilemapping: UnmapViewOfFile(d_second_copy)", GetLastError());
  }
  //d_second=NULL;
  CloseHandle(d_handle);
#endif
}

// ----------------------------------------------------------------
//      The factory interface
// ----------------------------------------------------------------


gr_vmcircbuf_factory *gr_vmcircbuf_createfilemapping_factory::s_the_factory = 0;

gr_vmcircbuf_factory *
gr_vmcircbuf_createfilemapping_factory::singleton ()
{
  if (s_the_factory)
    return s_the_factory;
  s_the_factory = new gr_vmcircbuf_createfilemapping_factory ();
  return s_the_factory;
}

int
gr_vmcircbuf_createfilemapping_factory::granularity ()
{
#ifdef HAVE_CREATEFILEMAPPING
  //  return 65536;//TODO, check, is this needed or can we just use gr_pagesize()
  SYSTEM_INFO system_info;
  GetSystemInfo(&system_info);
  //fprintf(stderr,"win32 AllocationGranularity %p\n",(int)system_info.dwAllocationGranularity);
  return (int)system_info.dwAllocationGranularity;
#else
  return gr_pagesize ();
#endif
}

gr_vmcircbuf *
gr_vmcircbuf_createfilemapping_factory::make (int size)
{
  try
  {
    return new gr_vmcircbuf_createfilemapping (size);
  }
  catch (...)
  {
    return 0;
  }
}