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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
|
/*
* Copyright Andrey Semashev 2007 - 2015.
* Distributed under the Boost Software License, Version 1.0.
* (See accompanying file LICENSE_1_0.txt or copy at
* http://www.boost.org/LICENSE_1_0.txt)
*/
/*!
* \file attribute_value_set.cpp
* \author Andrey Semashev
* \date 19.04.2007
*
* \brief This header is the Boost.Log library implementation, see the library documentation
* at http://www.boost.org/doc/libs/release/libs/log/doc/html/index.html.
*/
#include <new>
#include <memory>
#include <boost/array.hpp>
#include <boost/intrusive/options.hpp>
#include <boost/intrusive/list.hpp>
#include <boost/intrusive/link_mode.hpp>
#include <boost/intrusive/derivation_value_traits.hpp>
#include <boost/log/attributes/attribute_name.hpp>
#include <boost/log/attributes/attribute_value.hpp>
#include <boost/log/attributes/attribute_value_set.hpp>
#include "alignment_gap_between.hpp"
#include "attribute_set_impl.hpp"
#include "stateless_allocator.hpp"
#include <boost/log/detail/header.hpp>
namespace boost {
BOOST_LOG_OPEN_NAMESPACE
BOOST_FORCEINLINE attribute_value_set::node_base::node_base() :
m_pPrev(NULL),
m_pNext(NULL)
{
}
BOOST_FORCEINLINE attribute_value_set::node::node(key_type const& key, mapped_type& data, bool dynamic) :
node_base(),
m_Value(key, mapped_type()),
m_DynamicallyAllocated(dynamic)
{
m_Value.second.swap(data);
}
//! Container implementation
struct attribute_value_set::implementation
{
public:
typedef key_type::id_type id_type;
private:
typedef attribute_set::implementation attribute_set_impl_type;
typedef boost::log::aux::stateless_allocator< char > stateless_allocator;
//! Node base class traits for the intrusive list
struct node_traits
{
typedef node_base node;
typedef node* node_ptr;
typedef node const* const_node_ptr;
static node* get_next(const node* n) { return n->m_pNext; }
static void set_next(node* n, node* next) { n->m_pNext = next; }
static node* get_previous(const node* n) { return n->m_pPrev; }
static void set_previous(node* n, node* prev) { n->m_pPrev = prev; }
};
//! Contained node traits for the intrusive list
typedef intrusive::derivation_value_traits<
node,
node_traits,
intrusive::normal_link
> value_traits;
//! A container that provides iteration through elements of the container
typedef intrusive::list<
node,
intrusive::value_traits< value_traits >,
intrusive::constant_time_size< true >
> node_list;
//! A hash table bucket
struct bucket
{
//! Points to the first element in the bucket
node* first;
//! Points to the last element in the bucket (not the one after the last!)
node* last;
bucket() : first(NULL), last(NULL) {}
};
//! A list of buckets
typedef boost::array< bucket, 1U << BOOST_LOG_HASH_TABLE_SIZE_LOG > buckets;
//! Element disposer
struct disposer
{
typedef void result_type;
void operator() (node* p) const BOOST_NOEXCEPT
{
if (!p->m_DynamicallyAllocated)
p->~node();
else
delete p;
}
};
private:
//! Pointer to the source-specific attributes
attribute_set_impl_type* m_pSourceAttributes;
//! Pointer to the thread-specific attributes
attribute_set_impl_type* m_pThreadAttributes;
//! Pointer to the global attributes
attribute_set_impl_type* m_pGlobalAttributes;
//! The container with elements
node_list m_Nodes;
//! The pointer to the beginning of the storage of the elements
node* m_pStorage;
//! The pointer to the end of the allocated elements within the storage
node* m_pEnd;
//! The pointer to the end of storage
node* m_pEOS;
//! Hash table buckets
buckets m_Buckets;
private:
//! Constructor
implementation(
node* storage,
node* eos,
attribute_set_impl_type* source_attrs,
attribute_set_impl_type* thread_attrs,
attribute_set_impl_type* global_attrs
) :
m_pSourceAttributes(source_attrs),
m_pThreadAttributes(thread_attrs),
m_pGlobalAttributes(global_attrs),
m_pStorage(storage),
m_pEnd(storage),
m_pEOS(eos)
{
}
//! Destructor
~implementation()
{
m_Nodes.clear_and_dispose(disposer());
}
//! The function allocates memory and creates the object
static implementation* create(
size_type element_count,
attribute_set_impl_type* source_attrs,
attribute_set_impl_type* thread_attrs,
attribute_set_impl_type* global_attrs)
{
// Calculate the buffer size
const size_type header_size = sizeof(implementation) +
aux::alignment_gap_between< implementation, node >::value;
const size_type buffer_size = header_size + element_count * sizeof(node);
implementation* p = reinterpret_cast< implementation* >(stateless_allocator().allocate(buffer_size));
node* const storage = reinterpret_cast< node* >(reinterpret_cast< char* >(p) + header_size);
new (p) implementation(storage, storage + element_count, source_attrs, thread_attrs, global_attrs);
return p;
}
public:
//! The function allocates memory and creates the object
static implementation* create(
attribute_set const& source_attrs,
attribute_set const& thread_attrs,
attribute_set const& global_attrs,
size_type reserve_count)
{
return create(
source_attrs.m_pImpl->size() + thread_attrs.m_pImpl->size() + global_attrs.m_pImpl->size() + reserve_count,
source_attrs.m_pImpl,
thread_attrs.m_pImpl,
global_attrs.m_pImpl);
}
//! The function allocates memory and creates the object
static implementation* create(
attribute_value_set const& source_attrs,
attribute_set const& thread_attrs,
attribute_set const& global_attrs,
size_type reserve_count)
{
implementation* p = create(
source_attrs.m_pImpl->size() + thread_attrs.m_pImpl->size() + global_attrs.m_pImpl->size() + reserve_count,
NULL,
thread_attrs.m_pImpl,
global_attrs.m_pImpl);
p->copy_nodes_from(source_attrs.m_pImpl);
return p;
}
//! The function allocates memory and creates the object
static implementation* create(
BOOST_RV_REF(attribute_value_set) source_attrs,
attribute_set const& thread_attrs,
attribute_set const& global_attrs,
size_type reserve_count)
{
implementation* p = source_attrs.m_pImpl;
source_attrs.m_pImpl = NULL;
p->m_pThreadAttributes = thread_attrs.m_pImpl;
p->m_pGlobalAttributes = global_attrs.m_pImpl;
return p;
}
//! The function allocates memory and creates the object
static implementation* create(size_type reserve_count)
{
return create(reserve_count, NULL, NULL, NULL);
}
//! Creates a copy of the object
static implementation* copy(implementation* that)
{
// Create new object
implementation* p = create(that->size(), NULL, NULL, NULL);
// Copy all elements
p->copy_nodes_from(that);
return p;
}
//! Destroys the object and releases the memory
static void destroy(implementation* p)
{
const size_type buffer_size = reinterpret_cast< char* >(p->m_pEOS) - reinterpret_cast< char* >(p);
p->~implementation();
stateless_allocator().deallocate(reinterpret_cast< stateless_allocator::pointer >(p), buffer_size);
}
//! Returns the pointer to the first element
node_base* begin()
{
freeze();
return m_Nodes.begin().pointed_node();
}
//! Returns the pointer after the last element
node_base* end()
{
return m_Nodes.end().pointed_node();
}
//! Returns the number of elements in the container
size_type size()
{
freeze();
return m_Nodes.size();
}
//! Looks for the element with an equivalent key
node_base* find(key_type key)
{
// First try to find an acquired element
bucket& b = get_bucket(key.id());
node* p = b.first;
if (p)
{
// The bucket is not empty, search among the elements
p = find_in_bucket(key, b);
if (p->m_Value.first == key)
return p;
}
// Element not found, try to acquire the value from attribute sets
return freeze_node(key, b, p);
}
//! Freezes all elements of the container
void freeze()
{
if (m_pSourceAttributes)
{
freeze_nodes_from(m_pSourceAttributes);
m_pSourceAttributes = NULL;
}
if (m_pThreadAttributes)
{
freeze_nodes_from(m_pThreadAttributes);
m_pThreadAttributes = NULL;
}
if (m_pGlobalAttributes)
{
freeze_nodes_from(m_pGlobalAttributes);
m_pGlobalAttributes = NULL;
}
}
//! Inserts an element
std::pair< node*, bool > insert(key_type key, mapped_type const& mapped)
{
bucket& b = get_bucket(key.id());
node* p = find_in_bucket(key, b);
if (!p || p->m_Value.first != key)
{
p = insert_node(key, b, p, mapped);
return std::pair< node*, bool >(p, true);
}
else
{
return std::pair< node*, bool >(p, false);
}
}
private:
//! The function returns a bucket for the specified element
bucket& get_bucket(id_type id)
{
return m_Buckets[id & (buckets::static_size - 1)];
}
//! Attempts to find an element with the specified key in the bucket
node* find_in_bucket(key_type key, bucket const& b)
{
typedef node_list::node_traits node_traits;
typedef node_list::value_traits value_traits;
// All elements within the bucket are sorted to speedup the search.
node* p = b.first;
while (p != b.last && p->m_Value.first.id() < key.id())
{
p = value_traits::to_value_ptr(node_traits::get_next(p));
}
return p;
}
//! Acquires the attribute value from the attribute sets
node_base* freeze_node(key_type key, bucket& b, node* where)
{
attribute_set::iterator it;
if (m_pSourceAttributes)
{
it = m_pSourceAttributes->find(key);
if (it != m_pSourceAttributes->end())
{
// The attribute is found, acquiring the value
return insert_node(key, b, where, it->second.get_value());
}
}
if (m_pThreadAttributes)
{
it = m_pThreadAttributes->find(key);
if (it != m_pThreadAttributes->end())
{
// The attribute is found, acquiring the value
return insert_node(key, b, where, it->second.get_value());
}
}
if (m_pGlobalAttributes)
{
it = m_pGlobalAttributes->find(key);
if (it != m_pGlobalAttributes->end())
{
// The attribute is found, acquiring the value
return insert_node(key, b, where, it->second.get_value());
}
}
// The attribute is not found
return m_Nodes.end().pointed_node();
}
//! The function inserts a node into the container
node* insert_node(key_type key, bucket& b, node* where, mapped_type data)
{
node* p;
if (m_pEnd != m_pEOS)
{
p = m_pEnd++;
new (p) node(key, data, false);
}
else
{
p = new node(key, data, true);
}
node_list::iterator it;
if (b.first == NULL)
{
// The bucket is empty
b.first = b.last = p;
it = m_Nodes.end();
}
else if (where == b.first)
{
// The new element should become the first element of the bucket
it = m_Nodes.iterator_to(*where);
b.first = p;
}
else if (where == b.last && key.id() > where->m_Value.first.id())
{
// The new element should become the last element of the bucket
it = m_Nodes.iterator_to(*where);
++it;
b.last = p;
}
else
{
// The new element should be within the bucket
it = m_Nodes.iterator_to(*where);
}
m_Nodes.insert(it, *p);
return p;
}
//! Acquires attribute values from the set of attributes
void freeze_nodes_from(attribute_set_impl_type* attrs)
{
attribute_set::const_iterator it = attrs->begin(), end = attrs->end();
for (; it != end; ++it)
{
key_type key = it->first;
bucket& b = get_bucket(key.id());
node* p = b.first;
if (p)
{
p = find_in_bucket(key, b);
if (p->m_Value.first == key)
continue; // the element is already frozen
}
insert_node(key, b, p, it->second.get_value());
}
}
//! Copies nodes of the container
void copy_nodes_from(implementation* from)
{
// Copy all elements
node_list::iterator it = from->m_Nodes.begin(), end = from->m_Nodes.end();
for (; it != end; ++it)
{
node* n = m_pEnd++;
mapped_type data = it->m_Value.second;
new (n) node(it->m_Value.first, data, false);
m_Nodes.push_back(*n);
// Since nodes within buckets are ordered, we can simply append the node to the end of the bucket
bucket& b = get_bucket(n->m_Value.first.id());
if (b.first == NULL)
b.first = b.last = n;
else
b.last = n;
}
}
};
//! The constructor creates an empty set
BOOST_LOG_API attribute_value_set::attribute_value_set(
size_type reserve_count
) :
m_pImpl(implementation::create(reserve_count))
{
}
//! The constructor adopts three attribute sets to the set
BOOST_LOG_API attribute_value_set::attribute_value_set(
attribute_set const& source_attrs,
attribute_set const& thread_attrs,
attribute_set const& global_attrs,
size_type reserve_count
) :
m_pImpl(implementation::create(source_attrs, thread_attrs, global_attrs, reserve_count))
{
}
//! The constructor adopts three attribute sets to the set
BOOST_LOG_API attribute_value_set::attribute_value_set(
attribute_value_set const& source_attrs,
attribute_set const& thread_attrs,
attribute_set const& global_attrs,
size_type reserve_count
) :
m_pImpl(implementation::create(source_attrs, thread_attrs, global_attrs, reserve_count))
{
}
//! The constructor adopts three attribute sets to the set
BOOST_LOG_API void attribute_value_set::construct(
attribute_value_set& source_attrs,
attribute_set const& thread_attrs,
attribute_set const& global_attrs,
size_type reserve_count
)
{
m_pImpl = implementation::create(boost::move(source_attrs), thread_attrs, global_attrs, reserve_count);
}
//! Copy constructor
BOOST_LOG_API attribute_value_set::attribute_value_set(attribute_value_set const& that)
{
if (that.m_pImpl)
m_pImpl = implementation::copy(that.m_pImpl);
else
m_pImpl = NULL;
}
//! Destructor
BOOST_LOG_API attribute_value_set::~attribute_value_set() BOOST_NOEXCEPT
{
if (m_pImpl)
{
implementation::destroy(m_pImpl);
m_pImpl = NULL;
}
}
// Iterator generators
BOOST_LOG_API attribute_value_set::const_iterator
attribute_value_set::begin() const
{
return const_iterator(m_pImpl->begin(), const_cast< attribute_value_set* >(this));
}
BOOST_LOG_API attribute_value_set::const_iterator
attribute_value_set::end() const
{
return const_iterator(m_pImpl->end(), const_cast< attribute_value_set* >(this));
}
//! The method returns number of elements in the container
BOOST_LOG_API attribute_value_set::size_type
attribute_value_set::size() const
{
return m_pImpl->size();
}
//! Internal lookup implementation
BOOST_LOG_API attribute_value_set::const_iterator
attribute_value_set::find(key_type key) const
{
return const_iterator(m_pImpl->find(key), const_cast< attribute_value_set* >(this));
}
//! The method acquires values of all adopted attributes. Users don't need to call it, since will always get an already frozen set.
BOOST_LOG_API void attribute_value_set::freeze()
{
m_pImpl->freeze();
}
//! Inserts an element into the set
BOOST_LOG_API std::pair< attribute_value_set::const_iterator, bool >
attribute_value_set::insert(key_type key, mapped_type const& mapped)
{
std::pair< node*, bool > res = m_pImpl->insert(key, mapped);
return std::pair< const_iterator, bool >(const_iterator(res.first, this), res.second);
}
BOOST_LOG_CLOSE_NAMESPACE // namespace log
} // namespace boost
#include <boost/log/detail/footer.hpp>
|