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
|
/* -*- c++ -*- */
/*
* Copyright 2005 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 2, 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., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "time_series.h"
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <iostream>
time_series::time_series(size_t itemsize, const std::string filename,
unsigned long long starting_offset,
long long nsamples_to_process)
: d_itemsize(itemsize), d_filename(filename), d_fd(-1),
d_start(starting_offset), d_buffer(0)
{
if ((d_fd = open(d_filename.c_str(), O_RDONLY | O_LARGEFILE, 0660)) == -1){
perror(d_filename.c_str());
throw std::string("open failed: ") + d_filename;
}
struct stat statbuf;
if (fstat(d_fd, &statbuf) == -1){
perror(d_filename.c_str());
throw std::string("fstat failed: ") + d_filename;
}
d_filesize = statbuf.st_size;
d_limit = d_filesize / d_itemsize;
if (d_start > d_limit){
std::string s = std::string("d_start > filesize: ") + d_filename;
std::cerr << s
<< " d_start " << d_start
<< " d_limit " << d_limit << std::endl;
throw s;
}
if (nsamples_to_process > 0)
if ((d_start + nsamples_to_process) < d_limit)
d_limit = d_start + nsamples_to_process;
d_buffer = mmap(0, d_filesize, PROT_READ, MAP_SHARED, d_fd, 0);
if (d_buffer == MAP_FAILED){
perror("mmap");
throw std::string("mmap failed: ") + d_filename;
}
}
time_series::~time_series()
{
munmap(d_buffer, d_filesize);
close(d_fd);
}
const void *
time_series::seek(unsigned long long pos, unsigned long long blocksize) const
{
if ((d_start + pos + blocksize) >= d_limit)
return 0;
return (const void *)((char *)d_buffer + ((d_start + pos) * d_itemsize));
}
long long
time_series::nsamples_available(unsigned long long pos) const
{
if ((d_start + pos) >= d_limit)
return 0;
return d_limit - (d_start + pos);
}
|