File: vmcircbuf_mmap_tmpfile.cc

package info (click to toggle)
gnuradio 3.7.13.4-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 45,992 kB
  • sloc: cpp: 155,723; python: 88,934; xml: 42,165; ansic: 41,036; fortran: 927; asm: 803; sh: 224; lisp: 31; makefile: 17
file content (200 lines) | stat: -rw-r--r-- 5,918 bytes parent folder | download | duplicates (5)
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
/* -*- c++ -*- */
/*
 * Copyright 2003,2011,2013 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 "vmcircbuf_mmap_tmpfile.h"
#include <stdexcept>
#include <assert.h>
#include <unistd.h>
#include <stdlib.h>
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_MMAN_H
#include <sys/mman.h>
#endif
#include <fcntl.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include "pagesize.h"
#include <gnuradio/sys_paths.h>

namespace gr {

  vmcircbuf_mmap_tmpfile::vmcircbuf_mmap_tmpfile (int size)
    : gr::vmcircbuf (size)
  {
#if !defined(HAVE_MMAP)
    fprintf(stderr, "gr::vmcircbuf_mmap_tmpfile: mmap or mkstemp is not available\n");
    throw std::runtime_error("gr::vmcircbuf_mmap_tmpfile");
#else
    gr::thread::scoped_lock guard(s_vm_mutex);

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

    int seg_fd = -1;
    char seg_name[1024];

    static int s_seg_counter = 0;

    // open a temporary file that we'll map in a bit later
    while(1) {
      snprintf(seg_name, sizeof(seg_name),
               "%s/gnuradio-%d-%d-XXXXXX", gr::tmp_path(), getpid(), s_seg_counter);
      s_seg_counter++;

      seg_fd = open(seg_name, O_RDWR | O_CREAT | O_EXCL, 0600);
      if(seg_fd == -1) {
        if(errno == EEXIST) // File already exists (shouldn't happen).  Try again
          continue;

        char msg[1024];
        snprintf(msg, sizeof (msg),
                 "gr::vmcircbuf_mmap_tmpfile: open [%s]", seg_name);
        perror(msg);
        throw std::runtime_error("gr::vmcircbuf_mmap_tmpfile");
      }
      break;
    }

    if(unlink (seg_name) == -1) {
      perror("gr::vmcircbuf_mmap_tmpfile: unlink");
      throw std::runtime_error ("gr::vmcircbuf_mmap_tmpfile");
    }

    // We've got a valid file descriptor to a tmp file.
    // Now set it's length to 2x what we really want and mmap it in.
    if(ftruncate (seg_fd, (off_t) 2 * size) == -1) {
      close(seg_fd);						// cleanup
      perror("gr::vmcircbuf_mmap_tmpfile: ftruncate (1)");
      throw std::runtime_error("gr::vmcircbuf_mmap_tmpfile");
    }

    void *first_copy = mmap(0, 2 * size,
                            PROT_READ | PROT_WRITE, MAP_SHARED,
                            seg_fd, (off_t)0);

    if(first_copy == MAP_FAILED) {
      close(seg_fd);						// cleanup
      perror("gr::vmcircbuf_mmap_tmpfile: mmap (1)");
      throw std::runtime_error ("gr::vmcircbuf_mmap_tmpfile");
    }

    // unmap the 2nd half
    if(munmap ((char *) first_copy + size, size) == -1) {
      close(seg_fd);						// cleanup
      perror("gr::vmcircbuf_mmap_tmpfile: munmap (1)");
      throw std::runtime_error("gr::vmcircbuf_mmap_tmpfile");
    }

    // map the first half into the now available hole where the
    // second half used to be.
    void *second_copy = mmap((char*)first_copy + size, size,
                             PROT_READ | PROT_WRITE, MAP_SHARED,
                             seg_fd, (off_t)0);

    if(second_copy == MAP_FAILED) {
      munmap(first_copy, size);					// cleanup
      close(seg_fd);
      perror("gr::vmcircbuf_mmap_tmpfile: mmap(2)");
      throw std::runtime_error("gr::vmcircbuf_mmap_tmpfile");
    }

    // check for contiguity
    if((char*)second_copy != (char*)first_copy + size) {
      munmap(first_copy, size);					// cleanup
      munmap(second_copy, size);
      close(seg_fd);
      perror("gr::vmcircbuf_mmap_tmpfile: non-contiguous second copy");
      throw std::runtime_error("gr::vmcircbuf_mmap_tmpfile");
    }

    // cut the tmp file down to size
    if(ftruncate (seg_fd, (off_t) size) == -1) {
      munmap(first_copy, size);					// cleanup
      munmap(second_copy, size);
      close(seg_fd);
      perror("gr::vmcircbuf_mmap_tmpfile: ftruncate (2)");
      throw std::runtime_error("gr::vmcircbuf_mmap_tmpfile");
    }

    close(seg_fd);	// fd no longer needed.  The mapping is retained.

    // Now remember the important stuff

    d_base = (char*)first_copy;
    d_size = size;
#endif
  }

  vmcircbuf_mmap_tmpfile::~vmcircbuf_mmap_tmpfile()
  {
#if defined(HAVE_MMAP)
    gr::thread::scoped_lock guard(s_vm_mutex);

    if(munmap(d_base, 2 * d_size) == -1) {
      perror("gr::vmcircbuf_mmap_tmpfile: munmap(2)");
    }
#endif
  }

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

  gr::vmcircbuf_factory *vmcircbuf_mmap_tmpfile_factory::s_the_factory = 0;

  gr::vmcircbuf_factory *
  vmcircbuf_mmap_tmpfile_factory::singleton()
  {
    if(s_the_factory)
      return s_the_factory;

    s_the_factory = new gr::vmcircbuf_mmap_tmpfile_factory();
    return s_the_factory;
  }

  int
  vmcircbuf_mmap_tmpfile_factory::granularity()
  {
    return gr::pagesize();
  }

  gr::vmcircbuf *
  vmcircbuf_mmap_tmpfile_factory::make(int size)
  {
    try {
      return new vmcircbuf_mmap_tmpfile(size);
    }
    catch (...) {
      return 0;
    }
  }

} /* namespace gr */