3 * Copyright 2002 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 3, 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.
27 #include <gr_circular_file.h>
30 #ifdef HAVE_SYS_MMAN_H
33 #include <sys/types.h>
45 static const int HEADER_SIZE = 4096;
46 static const int HEADER_MAGIC = 0xEB021026;
48 static const int HD_MAGIC = 0;
49 static const int HD_HEADER_SIZE = 1; // integer offsets into header
50 static const int HD_BUFFER_SIZE = 2;
51 static const int HD_BUFFER_BASE = 3;
52 static const int HD_BUFFER_CURRENT = 4;
54 gr_circular_file::gr_circular_file (const char *filename,
55 bool writable, int size)
56 : d_fd (-1), d_header (0), d_buffer (0), d_mapped_size (0), d_bytes_read (0)
61 mm_prot = PROT_READ | PROT_WRITE;
63 d_fd = open (filename, O_CREAT | O_RDWR | O_TRUNC, 0664);
68 #ifdef HAVE_MMAP /* FIXME */
69 ftruncate (d_fd, size + HEADER_SIZE);
76 d_fd = open (filename, O_RDONLY);
84 if (fstat (d_fd, &statbuf) < 0){
89 if (statbuf.st_size < HEADER_SIZE){
90 fprintf (stderr, "%s: file too small to be circular buffer\n", filename);
94 d_mapped_size = statbuf.st_size;
96 void *p = mmap (0, d_mapped_size, mm_prot, MAP_SHARED, d_fd, 0);
98 perror ("gr_circular_file: mmap failed");
102 d_header = (int *) p;
104 perror ("gr_circular_file: mmap unsupported by this system");
108 if (writable){ // init header
111 fprintf (stderr, "gr_circular_buffer: size must be > 0 when writable\n");
115 d_header[HD_MAGIC] = HEADER_MAGIC;
116 d_header[HD_HEADER_SIZE] = HEADER_SIZE;
117 d_header[HD_BUFFER_SIZE] = size;
118 d_header[HD_BUFFER_BASE] = HEADER_SIZE; // right after header
119 d_header[HD_BUFFER_CURRENT] = 0;
122 // sanity check (the asserts are a bit unforgiving...)
124 assert (d_header[HD_MAGIC] == HEADER_MAGIC);
125 assert (d_header[HD_HEADER_SIZE] == HEADER_SIZE);
126 assert (d_header[HD_BUFFER_SIZE] > 0);
127 assert (d_header[HD_BUFFER_BASE] >= d_header[HD_HEADER_SIZE]);
128 assert (d_header[HD_BUFFER_BASE] + d_header[HD_BUFFER_SIZE] <= d_mapped_size);
129 assert (d_header[HD_BUFFER_CURRENT] >= 0 &&
130 d_header[HD_BUFFER_CURRENT] < d_header[HD_BUFFER_SIZE]);
133 d_buffer = (unsigned char *) d_header + d_header[HD_BUFFER_BASE];
136 gr_circular_file::~gr_circular_file ()
139 if (munmap ((char *) d_header, d_mapped_size) < 0){
140 perror ("gr_circular_file: munmap");
148 gr_circular_file::write (void *vdata, int nbytes)
150 unsigned char *data = (unsigned char *) vdata;
151 int buffer_size = d_header[HD_BUFFER_SIZE];
152 int buffer_current = d_header[HD_BUFFER_CURRENT];
155 int n = std::min (nbytes, buffer_size - buffer_current);
156 memcpy (d_buffer + buffer_current, data, n);
159 if (buffer_current >= buffer_size)
166 d_header[HD_BUFFER_CURRENT] = buffer_current;
171 gr_circular_file::read (void *vdata, int nbytes)
173 unsigned char *data = (unsigned char *) vdata;
174 int buffer_current = d_header[HD_BUFFER_CURRENT];
175 int buffer_size = d_header[HD_BUFFER_SIZE];
178 nbytes = std::min (nbytes, buffer_size - d_bytes_read);
181 int offset = (buffer_current + d_bytes_read) % buffer_size;
182 int n = std::min (nbytes, buffer_size - offset);
183 memcpy (data, d_buffer + offset, n);
193 gr_circular_file::reset_read_pointer ()