3 * Copyright 2003,2005 Free Software Foundation, Inc.
5 * This file is part of GNU Radio
7 * GNU Radio is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
12 * GNU Radio is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Radio; see the file COPYING. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street,
20 * Boston, MA 02110-1301, USA.
30 #ifdef HAVE_SYS_TYPES_H
31 #include <sys/types.h>
33 #ifdef HAVE_SYS_MMAN_H
38 #include <gr_pagesize.h>
39 #include <gr_tmp_path.h>
40 #include <gr_vmcircbuf_createfilemapping.h>
42 #ifdef HAVE_CREATEFILEMAPPING
43 // Print Windows error (could/should be global?)
45 werror( char *where, DWORD last_error )
49 FormatMessage( FORMAT_MESSAGE_FROM_SYSTEM,
52 0, // default language
54 sizeof(buf)/sizeof(TCHAR), // buffer size
56 fprintf( stderr, "%s: Error %d: %s", where, last_error, buf );
62 gr_vmcircbuf_createfilemapping::gr_vmcircbuf_createfilemapping (int size)
65 #if !defined(HAVE_CREATEFILEMAPPING)
66 fprintf (stderr, "%s: createfilemapping is not available\n",__FUNCTION__);
67 throw std::runtime_error ("gr_vmcircbuf_createfilemapping");
69 static int s_seg_counter = 0;
71 if (size <= 0 || (size % gr_pagesize ()) != 0){
72 fprintf (stderr, "gr_vmcircbuf_createfilemapping: invalid size = %d\n", size);
73 throw std::runtime_error ("gr_vmcircbuf_createfilemapping");
77 snprintf (seg_name, sizeof (seg_name), "/gnuradio-%d-%d", getpid (), s_seg_counter);
79 d_handle = CreateFileMapping(INVALID_HANDLE_VALUE, // use paging file
80 NULL, // default security
81 PAGE_READWRITE, // read/write access
82 0, // max. object size
84 seg_name); // name of mapping object
87 if (d_handle == NULL || d_handle == INVALID_HANDLE_VALUE){
89 snprintf( msg, sizeof(msg),
90 "gr_vmcircbuf_mmap_createfilemapping: CreateFileMapping [%s]",
92 werror( msg, GetLastError() );
93 throw std::runtime_error ("gr_vmcircbuf_mmap_createfilemapping");
96 // Allocate virtual memory of the needed size, then free it so we can use it
98 first_tmp = VirtualAlloc( NULL, 2*size, MEM_RESERVE, PAGE_NOACCESS );
99 if (first_tmp == NULL){
100 werror( "gr_vmcircbuf_mmap_createfilemapping: VirtualAlloc", GetLastError());
101 CloseHandle(d_handle); // cleanup
102 throw std::runtime_error ("gr_vmcircbuf_mmap_createfilemapping");
105 if (VirtualFree(first_tmp, 0, MEM_RELEASE) == 0){
106 werror( "gr_vmcircbuf_mmap_createfilemapping: VirtualFree", GetLastError());
107 CloseHandle(d_handle); // cleanup
108 throw std::runtime_error ("gr_vmcircbuf_mmap_createfilemapping");
111 d_first_copy = MapViewOfFileEx((HANDLE)d_handle, // handle to map object
112 FILE_MAP_WRITE, // read/write permission
117 if (d_first_copy != first_tmp){
118 werror( "gr_vmcircbuf_mmap_createfilemapping: MapViewOfFileEx(1)", GetLastError());
119 CloseHandle(d_handle); // cleanup
120 throw std::runtime_error ("gr_vmcircbuf_mmap_createfilemapping");
123 d_second_copy = MapViewOfFileEx((HANDLE)d_handle, // handle to map object
124 FILE_MAP_WRITE, // read/write permission
128 (char *)first_tmp + size);//(LPVOID) ((char *)d_first_copy + size));
130 if (d_second_copy != (char *)first_tmp + size){
131 werror( "gr_vmcircbuf_mmap_createfilemapping: MapViewOfFileEx(2)", GetLastError());
132 UnmapViewOfFile(d_first_copy);
133 CloseHandle(d_handle); // cleanup
134 throw std::runtime_error ("gr_vmcircbuf_mmap_createfilemapping");
138 fprintf (stderr,"gr_vmcircbuf_mmap_createfilemapping: contiguous? mmap %p %p %p %p\n",
139 (char *)d_first_copy, (char *)d_second_copy, size, (char *)d_first_copy + size);
142 // Now remember the important stuff
143 d_base = (char *) d_first_copy;
145 #endif /*HAVE_CREATEFILEMAPPING*/
148 gr_vmcircbuf_createfilemapping::~gr_vmcircbuf_createfilemapping ()
150 #ifdef HAVE_CREATEFILEMAPPING
151 if (UnmapViewOfFile(d_first_copy) == 0)
153 werror("gr_vmcircbuf_createfilemapping: UnmapViewOfFile(d_first_copy)", GetLastError());
156 if (UnmapViewOfFile(d_second_copy) == 0)
158 werror("gr_vmcircbuf_createfilemapping: UnmapViewOfFile(d_second_copy)", GetLastError());
161 CloseHandle(d_handle);
165 // ----------------------------------------------------------------
166 // The factory interface
167 // ----------------------------------------------------------------
170 gr_vmcircbuf_factory *gr_vmcircbuf_createfilemapping_factory::s_the_factory = 0;
172 gr_vmcircbuf_factory *
173 gr_vmcircbuf_createfilemapping_factory::singleton ()
176 return s_the_factory;
177 s_the_factory = new gr_vmcircbuf_createfilemapping_factory ();
178 return s_the_factory;
182 gr_vmcircbuf_createfilemapping_factory::granularity ()
184 #ifdef HAVE_CREATEFILEMAPPING
185 // return 65536;//TODO, check, is this needed or can we just use gr_pagesize()
186 SYSTEM_INFO system_info;
187 GetSystemInfo(&system_info);
188 //fprintf(stderr,"win32 AllocationGranularity %p\n",(int)system_info.dwAllocationGranularity);
189 return (int)system_info.dwAllocationGranularity;
191 return gr_pagesize ();
196 gr_vmcircbuf_createfilemapping_factory::make (int size)
200 return new gr_vmcircbuf_createfilemapping (size);