summaryrefslogtreecommitdiff
path: root/gnuradio-runtime/lib/gr_circular_file.cc
blob: 6f710c49b1e46dbe4b67d9b3eb4dbc9a5129efc7 (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
/* -*- c++ -*- */
/*
 * Copyright 2002,2010 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 <gr_circular_file.h>

#include <unistd.h>
#ifdef HAVE_SYS_MMAN_H
#include <sys/mman.h>
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>

#include <algorithm>
#include <stdio.h>
#include <string.h>

#ifdef HAVE_IO_H
#include <io.h>
#endif

static const int HEADER_SIZE = 4096;
static const int HEADER_MAGIC = 0xEB021026;

static const int HD_MAGIC = 0;
static const int HD_HEADER_SIZE = 1;	// integer offsets into header
static const int HD_BUFFER_SIZE = 2;
static const int HD_BUFFER_BASE = 3;
static const int HD_BUFFER_CURRENT = 4;

gr_circular_file::gr_circular_file (const char *filename,
				    bool writable, int size)
  : d_fd (-1), d_header (0), d_buffer (0), d_mapped_size (0), d_bytes_read (0)
{
  int	mm_prot;
  if (writable){
#ifdef HAVE_MMAP
    mm_prot = PROT_READ | PROT_WRITE;
#endif
    d_fd = open (filename, O_CREAT | O_RDWR | O_TRUNC, 0664);
    if (d_fd < 0){
      perror (filename);
      exit (1);
    }
#ifdef HAVE_MMAP	/* FIXME */
    if(ftruncate (d_fd, size + HEADER_SIZE) != 0) {
      perror (filename);
      exit (1);
    }
#endif
  }
  else {
#ifdef HAVE_MMAP
    mm_prot = PROT_READ;
#endif
    d_fd = open (filename, O_RDONLY);
    if (d_fd < 0){
      perror (filename);
      exit (1);
    }
  }

  struct stat statbuf;
  if (fstat (d_fd, &statbuf) < 0){
    perror (filename);
    exit (1);
  }

  if (statbuf.st_size < HEADER_SIZE){
    fprintf (stderr, "%s: file too small to be circular buffer\n", filename);
    exit (1);
  }

  d_mapped_size = statbuf.st_size;
#ifdef HAVE_MMAP
  void *p = mmap (0, d_mapped_size, mm_prot, MAP_SHARED, d_fd, 0);
  if (p == MAP_FAILED){
    perror ("gr_circular_file: mmap failed");
    exit (1);
  }

  d_header = (int *) p;
#else
    perror ("gr_circular_file: mmap unsupported by this system");
    exit (1);
#endif

  if (writable){       	// init header

    if (size < 0){
      fprintf (stderr, "gr_circular_buffer: size must be > 0 when writable\n");
      exit (1);
    }

    d_header[HD_MAGIC] = HEADER_MAGIC;
    d_header[HD_HEADER_SIZE] = HEADER_SIZE;
    d_header[HD_BUFFER_SIZE] = size;
    d_header[HD_BUFFER_BASE] = HEADER_SIZE;	// right after header
    d_header[HD_BUFFER_CURRENT] = 0;
  }

  // sanity check (the asserts are a bit unforgiving...)

  assert (d_header[HD_MAGIC] == HEADER_MAGIC);
  assert (d_header[HD_HEADER_SIZE] == HEADER_SIZE);
  assert (d_header[HD_BUFFER_SIZE] > 0);
  assert (d_header[HD_BUFFER_BASE] >= d_header[HD_HEADER_SIZE]);
  assert (d_header[HD_BUFFER_BASE] + d_header[HD_BUFFER_SIZE] <= d_mapped_size);
  assert (d_header[HD_BUFFER_CURRENT] >= 0 &&
	  d_header[HD_BUFFER_CURRENT] < d_header[HD_BUFFER_SIZE]);

  d_bytes_read = 0;
  d_buffer = (unsigned char *) d_header + d_header[HD_BUFFER_BASE];
}

gr_circular_file::~gr_circular_file ()
{
#ifdef HAVE_MMAP
  if (munmap ((char *) d_header, d_mapped_size) < 0){
    perror ("gr_circular_file: munmap");
    exit (1);
  }
#endif
  close (d_fd);
}

bool
gr_circular_file::write (void *vdata, int nbytes)
{
  unsigned char *data = (unsigned char *) vdata;
  int	buffer_size = d_header[HD_BUFFER_SIZE];
  int	buffer_current = d_header[HD_BUFFER_CURRENT];

  while (nbytes > 0){
    int n = std::min (nbytes, buffer_size - buffer_current);
    memcpy (d_buffer + buffer_current, data, n);

    buffer_current += n;
    if (buffer_current >= buffer_size)
      buffer_current = 0;

    data += n;
    nbytes -= n;
  }

  d_header[HD_BUFFER_CURRENT] = buffer_current;
  return true;
}

int
gr_circular_file::read (void *vdata, int nbytes)
{
  unsigned char *data = (unsigned char *) vdata;
  int	buffer_current = d_header[HD_BUFFER_CURRENT];
  int	buffer_size = d_header[HD_BUFFER_SIZE];
  int	total = 0;

  nbytes = std::min (nbytes, buffer_size - d_bytes_read);

  while (nbytes > 0){
    int offset = (buffer_current + d_bytes_read) % buffer_size;
    int n = std::min (nbytes, buffer_size - offset);
    memcpy (data, d_buffer + offset, n);
    data += n;
    d_bytes_read += n;
    total += n;
    nbytes -= n;
  }
  return total;
}

void
gr_circular_file::reset_read_pointer ()
{
  d_bytes_read = 0;
}