LSST Applications g180d380827+0f66a164bb,g2079a07aa2+86d27d4dc4,g2305ad1205+7d304bc7a0,g29320951ab+500695df56,g2bbee38e9b+0e5473021a,g337abbeb29+0e5473021a,g33d1c0ed96+0e5473021a,g3a166c0a6a+0e5473021a,g3ddfee87b4+e42ea45bea,g48712c4677+36a86eeaa5,g487adcacf7+2dd8f347ac,g50ff169b8f+96c6868917,g52b1c1532d+585e252eca,g591dd9f2cf+c70619cc9d,g5a732f18d5+53520f316c,g5ea96fc03c+341ea1ce94,g64a986408d+f7cd9c7162,g858d7b2824+f7cd9c7162,g8a8a8dda67+585e252eca,g99cad8db69+469ab8c039,g9ddcbc5298+9a081db1e4,ga1e77700b3+15fc3df1f7,gb0e22166c9+60f28cb32d,gba4ed39666+c2a2e4ac27,gbb8dafda3b+c92fc63c7e,gbd866b1f37+f7cd9c7162,gc120e1dc64+02c66aa596,gc28159a63d+0e5473021a,gc3e9b769f7+b0068a2d9f,gcf0d15dbbd+e42ea45bea,gdaeeff99f8+f9a426f77a,ge6526c86ff+84383d05b3,ge79ae78c31+0e5473021a,gee10cc3b42+585e252eca,gff1a9f87cc+f7cd9c7162,w.2024.17
LSST Data Management Base Package
Loading...
Searching...
No Matches
BaseTable.cc
Go to the documentation of this file.
1// -*- lsst-c++ -*-
2
3#include <memory>
4
5#include "boost/shared_ptr.hpp" // only for ndarray
6
13
14namespace lsst {
15namespace afw {
16namespace table {
17
18// =============== Block ====================================================================================
19
20// This is a block of memory that doles out record-sized chunks when a table asks for them.
21// It inherits from ndarray::Manager so we can return ndarrays that refer to the memory in the
22// block with correct reference counting (ndarray::Manager is just an empty base class with an
23// internal reference count - it's like a shared_ptr without the pointer and template parameter.
24//
25// Records are allocated in Blocks for two reasons:
26// - it allows tables to be either totally contiguous in memory (enabling column views) or
27// not (enabling dynamic addition of records) all in one class.
28// - it saves us from ever having to reallocate all the records associated with a table
29// when we run out of space (that's what a std::vector-like model would require). This keeps
30// records and/or iterators to them from being invalidated, and it keeps tables from having
31// to track all the records whose data it owns.
32
33namespace {
34
35class Block : public ndarray::Manager {
36public:
37 using Ptr = boost::intrusive_ptr<Block>;
38
39 // If the last chunk allocated isn't needed after all (usually because of an exception in a constructor)
40 // we reuse it immediately. If it wasn't the last chunk allocated, it can't be reclaimed until
41 // the entire block goes out of scope.
42 static void reclaim(std::size_t recordSize, void *data, ndarray::Manager::Ptr const &manager) {
43 Ptr block = boost::static_pointer_cast<Block>(manager);
44 if (reinterpret_cast<char *>(data) + recordSize == block->_next) {
45 block->_next -= recordSize;
46 }
47 }
48
49 // Ensure we have space for at least the given number of records as a contiguous block.
50 // May not actually allocate anything if we already do.
51 static void preallocate(std::size_t recordSize, std::size_t recordCount, ndarray::Manager::Ptr &manager) {
52 Ptr block = boost::static_pointer_cast<Block>(manager);
53 if (!block || static_cast<std::size_t>(block->_end - block->_next) < recordSize * recordCount) {
54 block = Ptr(new Block(recordSize, recordCount));
55 manager = block;
56 }
57 }
58
59 static std::size_t getBufferSize(std::size_t recordSize, ndarray::Manager::Ptr const &manager) {
60 Ptr block = boost::static_pointer_cast<Block>(manager);
61 return static_cast<std::size_t>(block->_end - block->_next) / recordSize;
62 }
63
64 // Get the next chunk from the block, making a new block and installing it into the table
65 // if we're all out of space.
66 static void *get(std::size_t recordSize, ndarray::Manager::Ptr &manager) {
67 Ptr block = boost::static_pointer_cast<Block>(manager);
68 if (!block || block->_next == block->_end) {
69 block = Ptr(new Block(recordSize, BaseTable::nRecordsPerBlock));
70 manager = block;
71 }
72 void *r = block->_next;
73 block->_next += recordSize;
74 return r;
75 }
76
77 // Block is also keeper of the special number that says what alignment boundaries are needed for
78 // schemas. Before we start using a schema, we need to first ensure it meets that requirement,
79 // and pad it if not.
80 static void padSchema(Schema &schema) {
81 static int const MIN_RECORD_ALIGN = sizeof(AllocType);
82 std::size_t remainder = schema.getRecordSize() % MIN_RECORD_ALIGN;
83 if (remainder) {
84 detail::Access::padSchema(schema, MIN_RECORD_ALIGN - remainder);
85 }
86 }
87
88private:
89 struct AllocType {
90 double element[2];
91 };
92
93 explicit Block(std::size_t recordSize, std::size_t recordCount)
94 : _mem(new AllocType[(recordSize * recordCount) / sizeof(AllocType)]),
95 _next(reinterpret_cast<char *>(_mem.get())),
96 _end(_next + recordSize * recordCount) {
97 assert((recordSize * recordCount) % sizeof(AllocType) == 0);
98 std::fill(_next, _end, 0); // initialize to zero; we'll later initialize floats to NaN.
99 }
100
102 char *_next;
103 char *_end;
104};
105
106} // namespace
107
108// =============== BaseTable implementation (see header for docs) ===========================================
109
110void BaseTable::preallocate(std::size_t n) { Block::preallocate(_schema.getRecordSize(), n, _manager); }
111
113 if (_manager) {
114 return Block::getBufferSize(_schema.getRecordSize(), _manager);
115 } else {
116 return 0;
117 }
118}
119
123
125
128 output->assign(input);
129 return output;
130}
131
134 output->assign(input, mapper);
135 return output;
136}
137
139 return std::make_shared<io::FitsWriter>(fitsfile, flags);
140}
141
145
147
149 : _schema(schema), _metadata(metadata) {
150 Block::padSchema(_schema);
151 _schema.disconnectAliases();
152 _schema.getAliasMap()->_table = this;
153}
154
155BaseTable::~BaseTable() { _schema.getAliasMap()->_table = nullptr; }
156
157namespace {
158
159// A Schema Functor used to set destroy variable-length array fields using an explicit call to their
160// destructor (necessary since we used placement new). All other fields are ignored, as they're POD.
161struct RecordDestroyer {
162 template <typename T>
163 void operator()(SchemaItem<T> const &item) const {}
164
165 template <typename T>
166 void operator()(SchemaItem<Array<T> > const &item) const {
167 using Element = ndarray::Array<T, 1, 1>;
168 if (item.key.isVariableLength()) {
169 (*reinterpret_cast<Element *>(data + item.key.getOffset())).~Element();
170 }
171 }
172
173 void operator()(SchemaItem<std::string> const &item) const {
174 if (item.key.isVariableLength()) {
175 using std::string; // invoking the destructor on a qualified name doesn't compile in gcc 4.8.1
176 // https://stackoverflow.com/q/24593942
177 (*reinterpret_cast<string *>(data + item.key.getOffset())).~string();
178 }
179 }
180
181 char *data;
182};
183
184} // namespace
185
186detail::RecordData BaseTable::_makeNewRecordData() {
187 auto data = Block::get(_schema.getRecordSize(), _manager);
188 return detail::RecordData{
190 _manager // manager always points to the most recently-used block.
191 };
192}
193
194void BaseTable::_destroy(BaseRecord &record) {
195 assert(record._table.get() == this);
196 RecordDestroyer f = {reinterpret_cast<char *>(record._data)};
197 _schema.forEach(f);
198 if (record._manager == _manager) Block::reclaim(_schema.getRecordSize(), record._data, _manager);
199}
200
201/*
202 * JFB has no idea whether the default value below is sensible, or even whether
203 * it should be expressed ultimately as an approximate size in bytes rather than a
204 * number of records; the answer probably depends on both the typical size of
205 * records and the typical number of records.
206 */
208
209// =============== BaseCatalog instantiation =================================================================
210
211template class CatalogT<BaseRecord>;
212template class CatalogT<BaseRecord const>;
213} // namespace table
214} // namespace afw
215} // namespace lsst
char * data
Definition BaseRecord.cc:61
double element[2]
Definition BaseTable.cc:90
SchemaMapper * mapper
A simple struct that combines the two arguments that must be passed to most cfitsio routines and cont...
Definition fits.h:308
Tag types used to declare specialized field types.
Definition misc.h:31
Base class for all records.
Definition BaseRecord.h:31
virtual std::shared_ptr< BaseRecord > _makeRecord()
Default-construct an associated record (protected implementation).
Definition BaseTable.cc:146
virtual std::shared_ptr< io::FitsWriter > makeFitsWriter(fits::Fits *fitsfile, int flags) const
Definition BaseTable.cc:138
void preallocate(std::size_t nRecords)
Allocate contiguous space for new records in advance.
Definition BaseTable.cc:110
std::shared_ptr< BaseRecord > copyRecord(BaseRecord const &input)
Deep-copy a record, requiring that it have the same schema as this table.
Definition BaseTable.cc:126
BaseTable(Schema const &schema, std::shared_ptr< daf::base::PropertyList > metadata=nullptr)
Construct from a schema.
Definition BaseTable.cc:148
std::size_t getBufferSize() const
Return the number of additional records space has been already been allocated for.
Definition BaseTable.cc:112
static Schema makeMinimalSchema()
Return a minimal schema for Base tables and records.
Definition BaseTable.cc:124
static int nRecordsPerBlock
Number of records in each memory block.
Definition BaseTable.h:76
static std::shared_ptr< BaseTable > make(Schema const &schema)
Construct a new table.
Definition BaseTable.cc:120
virtual std::shared_ptr< BaseTable > _clone() const
Clone implementation with noncovariant return types.
Definition BaseTable.cc:142
std::shared_ptr< BaseRecord > makeRecord()
Default-construct an associated record.
Definition BaseTable.h:108
Defines the fields and offsets for a table.
Definition Schema.h:51
void forEach(F &func) const
Apply a functor to each SchemaItem in the Schema.
Definition Schema.h:214
void disconnectAliases()
Sever the connection between this schema and any others with which it shares aliases.
Definition Schema.cc:540
std::shared_ptr< AliasMap > getAliasMap() const
Return the map of aliases.
Definition Schema.h:279
std::size_t getRecordSize() const
Return the raw size of a record in bytes.
Definition Schema.h:149
A mapping between the keys of two Schemas, used to copy data between them.
static void padSchema(Schema &schema, std::size_t bytes)
Definition Access.h:88
T fill(T... args)
T remainder(T... args)