This file is indexed.

/usr/include/osmium/memory/buffer.hpp is in libosmium2-dev 2.11.4-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
#ifndef OSMIUM_MEMORY_BUFFER_HPP
#define OSMIUM_MEMORY_BUFFER_HPP

/*

This file is part of Osmium (http://osmcode.org/libosmium).

Copyright 2013-2017 Jochen Topf <jochen@topf.org> and others (see README).

Boost Software License - Version 1.0 - August 17th, 2003

Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:

The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.

*/

#include <algorithm>
#include <cassert>
#include <cstddef>
#include <cstring>
#include <functional>
#include <iterator>
#include <memory>
#include <stdexcept>
#include <utility>

#include <osmium/memory/item.hpp>
#include <osmium/memory/item_iterator.hpp>
#include <osmium/osm/entity.hpp>
#include <osmium/util/compatibility.hpp>

namespace osmium {

    /**
     * Exception thrown by the osmium::memory::Buffer class when somebody tries
     * to write data into a buffer and it doesn't fit. Buffers with internal
     * memory management will not throw this exception, but increase their size.
     */
    struct buffer_is_full : public std::runtime_error {

        buffer_is_full() :
            std::runtime_error("Osmium buffer is full") {
        }

    }; // struct buffer_is_full

    /**
     * @brief Memory management of items in buffers and iterators over this data.
     */
    namespace memory {

        /**
         * A memory area for storing OSM objects and other items. Each item stored
         * has a type and a length. See the Item class for details.
         *
         * Data can be added to a buffer piece by piece using reserve_space() and
         * add_item(). After all data that together forms an item is added, it must
         * be committed using the commit() call. Usually this is done through the
         * Builder class and its derived classes.
         *
         * You can iterate over all items in a buffer using the iterators returned
         * by begin(), end(), cbegin(), and cend().
         *
         * Buffers exist in two flavours, those with external memory management and
         * those with internal memory management. If you already have some memory
         * with data in it (for instance read from disk), you create a Buffer with
         * external memory management. It is your job then to free the memory once
         * the buffer isn't used any more. If you don't have memory already, you can
         * create a Buffer object and have it manage the memory internally. It will
         * dynamically allocate memory and free it again after use.
         *
         * By default, if a buffer gets full it will throw a buffer_is_full exception.
         * You can use the set_full_callback() method to set a callback functor
         * which will be called instead of throwing an exception. The full
         * callback functionality is deprecated and will be removed in the
         * future. See the documentation for set_full_callback() for alternatives.
         */
        class Buffer {

        public:

            // This is needed so we can call std::back_inserter() on a Buffer.
            using value_type = Item;

            enum class auto_grow : bool {
                yes = true,
                no  = false
            }; // enum class auto_grow

        private:

            std::unique_ptr<unsigned char[]> m_memory;
            unsigned char* m_data;
            size_t m_capacity;
            size_t m_written;
            size_t m_committed;
#ifndef NDEBUG
            uint8_t m_builder_count{0};
#endif
            auto_grow m_auto_grow{auto_grow::no};
            std::function<void(Buffer&)> m_full;

            static size_t calculate_capacity(size_t capacity) noexcept {
                // The majority of all Nodes will fit into this size.
                constexpr static const size_t min_capacity = 64;
                if (capacity < min_capacity) {
                    return min_capacity;
                }
                return capacity;
            }

        public:

            /**
             * The constructor without any parameters creates an invalid,
             * buffer, ie an empty hull of a buffer that has no actual memory
             * associated with it. It can be used to signify end-of-data.
             *
             * Most methods of the Buffer class will not work with an invalid
             * buffer.
             */
            Buffer() noexcept :
                m_memory(),
                m_data(nullptr),
                m_capacity(0),
                m_written(0),
                m_committed(0) {
            }

            /**
             * Constructs a valid externally memory-managed buffer using the
             * given memory and size.
             *
             * @param data A pointer to some already initialized data.
             * @param size The size of the initialized data.
             *
             * @throws std::invalid_argument if the size isn't a multiple of
             *         the alignment.
             */
            explicit Buffer(unsigned char* data, size_t size) :
                m_memory(),
                m_data(data),
                m_capacity(size),
                m_written(size),
                m_committed(size) {
                if (size % align_bytes != 0) {
                    throw std::invalid_argument("buffer size needs to be multiple of alignment");
                }
            }

            /**
             * Constructs a valid externally memory-managed buffer with the
             * given capacity that already contains 'committed' bytes of data.
             *
             * @param data A pointer to some (possibly initialized) data.
             * @param capacity The size of the memory for this buffer.
             * @param committed The size of the initialized data. If this is 0, the buffer startes out empty.
             *
             * @throws std::invalid_argument if the capacity or committed isn't
             *         a multiple of the alignment.
             */
            explicit Buffer(unsigned char* data, size_t capacity, size_t committed) :
                m_memory(),
                m_data(data),
                m_capacity(capacity),
                m_written(committed),
                m_committed(committed) {
                if (capacity % align_bytes != 0) {
                    throw std::invalid_argument("buffer capacity needs to be multiple of alignment");
                }
                if (committed % align_bytes != 0) {
                    throw std::invalid_argument("buffer parameter 'committed' needs to be multiple of alignment");
                }
            }

            /**
             * Constructs a valid internally memory-managed buffer with the
             * given capacity.
             * Will internally get dynamic memory of the required size.
             * The dynamic memory will be automatically freed when the Buffer
             * is destroyed.
             *
             * @param capacity The (initial) size of the memory for this buffer.
             * @param auto_grow Should this buffer automatically grow when it
             *        becomes to small?
             *
             * @throws std::invalid_argument if the capacity isn't a multiple
             *         of the alignment.
             */
            explicit Buffer(size_t capacity, auto_grow auto_grow = auto_grow::yes) :
                m_memory(new unsigned char[calculate_capacity(capacity)]),
                m_data(m_memory.get()),
                m_capacity(calculate_capacity(capacity)),
                m_written(0),
                m_committed(0),
                m_auto_grow(auto_grow) {
                if (m_capacity % align_bytes != 0) {
                    throw std::invalid_argument("buffer capacity needs to be multiple of alignment");
                }
            }

            // buffers can not be copied
            Buffer(const Buffer&) = delete;
            Buffer& operator=(const Buffer&) = delete;

            // buffers can be moved
            Buffer(Buffer&&) = default;
            Buffer& operator=(Buffer&&) = default;

            ~Buffer() = default;

#ifndef NDEBUG
            void increment_builder_count() noexcept {
                ++m_builder_count;
            }

            void decrement_builder_count() noexcept {
                assert(m_builder_count > 0);
                --m_builder_count;
            }

            uint8_t builder_count() const noexcept {
                return m_builder_count;
            }
#endif

            /**
             * Return a pointer to data inside the buffer.
             *
             * @pre The buffer must be valid.
             */
            unsigned char* data() const noexcept {
                assert(m_data && "This must be a valid buffer");
                return m_data;
            }

            /**
             * Returns the capacity of the buffer, ie how many bytes it can
             * contain. Always returns 0 on invalid buffers.
             */
            size_t capacity() const noexcept {
                return m_capacity;
            }

            /**
             * Returns the number of bytes already filled in this buffer.
             * Always returns 0 on invalid buffers.
             */
            size_t committed() const noexcept {
                return m_committed;
            }

            /**
             * Returns the number of bytes currently filled in this buffer that
             * are not yet committed.
             * Always returns 0 on invalid buffers.
             */
            size_t written() const noexcept {
                return m_written;
            }

            /**
             * This tests if the current state of the buffer is aligned
             * properly. Can be used for asserts.
             *
             * @pre The buffer must be valid.
             */
            bool is_aligned() const noexcept {
                assert(m_data && "This must be a valid buffer");
                return (m_written % align_bytes == 0) && (m_committed % align_bytes == 0);
            }

            /**
             * Set functor to be called whenever the buffer is full
             * instead of throwing buffer_is_full.
             *
             * The behaviour is undefined if you call this on an invalid
             * buffer.
             *
             * @pre The buffer must be valid.
             *
             * @deprecated
             * Callback functionality will be removed in the future. Either
             * detect the buffer_is_full exception or use a buffer with
             * auto_grow::yes. If you want to avoid growing buffers, check
             * that the used size of the buffer (committed()) is small enough
             * compared to the capacity (for instance small than 90% of the
             * capacity) before adding anything to the Buffer. If the buffer
             * is initialized with auto_grow::yes, it will still grow in the
             * rare case that a very large object will be added taking more
             * than the difference between committed() and capacity().
             */
            OSMIUM_DEPRECATED void set_full_callback(std::function<void(Buffer&)> full) {
                assert(m_data && "This must be a valid buffer");
                m_full = full;
            }

            /**
             * Grow capacity of this buffer to the given size.
             * This works only with internally memory-managed buffers.
             * If the given size is not larger than the current capacity,
             * nothing is done.
             *
             * @pre The buffer must be valid.
             *
             * @param size New capacity.
             *
             * @throws std::logic_error if the buffer doesn't use internal
             *         memory management.
             * @throws std::invalid_argument if the size isn't a multiple
             *         of the alignment.
             * @throws std::bad_alloc if there isn't enough memory available.
             */
            void grow(size_t size) {
                assert(m_data && "This must be a valid buffer");
                if (!m_memory) {
                    throw std::logic_error("Can't grow Buffer if it doesn't use internal memory management.");
                }
                if (m_capacity < size) {
                    if (size % align_bytes != 0) {
                        throw std::invalid_argument("buffer capacity needs to be multiple of alignment");
                    }
                    std::unique_ptr<unsigned char[]> memory(new unsigned char[size]);
                    std::copy_n(m_memory.get(), m_capacity, memory.get());
                    using std::swap;
                    swap(m_memory, memory);
                    m_data = m_memory.get();
                    m_capacity = size;
                }
            }

            /**
             * Mark currently written bytes in the buffer as committed.
             *
             * @pre The buffer must be valid.
             * @pre The buffer must be aligned properly (as indicated
             *      by is_aligned().
             * @pre No builder can be open on this buffer.
             *
             * @returns Number of committed bytes before this commit. Can be
             *          used as an offset into the buffer to get to the
             *          object being committed by this call.
             */
            size_t commit() {
                assert(m_data && "This must be a valid buffer");
                assert(m_builder_count == 0 && "Make sure there are no Builder objects still in scope");
                assert(is_aligned());

                const size_t offset = m_committed;
                m_committed = m_written;
                return offset;
            }

            /**
             * Roll back changes in buffer to last committed state.
             *
             * @pre The buffer must be valid.
             * @pre No builder can be open on this buffer.
             */
            void rollback() {
                assert(m_data && "This must be a valid buffer");
                assert(m_builder_count == 0 && "Make sure there are no Builder objects still in scope");
                m_written = m_committed;
            }

            /**
             * Clear the buffer.
             *
             * No-op on an invalid buffer.
             *
             * @pre No builder can be open on this buffer.
             *
             * @returns Number of bytes in the buffer before it was cleared.
             */
            size_t clear() {
                assert(m_builder_count == 0 && "Make sure there are no Builder objects still in scope");
                const size_t committed = m_committed;
                m_written = 0;
                m_committed = 0;
                return committed;
            }

            /**
             * Get the data in the buffer at the given offset.
             *
             * @pre The buffer must be valid.
             *
             * @tparam T Type we want to the data to be interpreted as.
             *
             * @returns Reference of given type pointing to the data in the
             *          buffer.
             */
            template <typename T>
            T& get(const size_t offset) const {
                assert(m_data && "This must be a valid buffer");
                return *reinterpret_cast<T*>(&m_data[offset]);
            }

            /**
             * Reserve space of given size in buffer and return pointer to it.
             * This is the only way of adding data to the buffer. You reserve
             * the space and then fill it.
             *
             * Note that you have to eventually call commit() to actually
             * commit this data.
             *
             * If there isn't enough space in the buffer, one of three things
             * can happen:
             *
             * * If you have set a callback with set_full_callback(), it is
             *   called. After the call returns, you must have either grown
             *   the buffer or cleared it by calling buffer.clear(). (Usage
             *   of the full callback is deprecated and this functionality
             *   will be removed in the future. See the documentation for
             *   set_full_callback() for alternatives.
             * * If no callback is defined and this buffer uses internal
             *   memory management, the buffers capacity is grown, so that
             *   the new data will fit.
             * * Else the buffer_is_full exception is thrown.
             *
             * @pre The buffer must be valid.
             *
             * @param size Number of bytes to reserve.
             *
             * @returns Pointer to reserved space. Note that this pointer is
             *          only guaranteed to be valid until the next call to
             *          reserve_space().
             *
             * @throws osmium::buffer_is_full if the buffer is full there is
             *         no callback defined and the buffer isn't auto-growing.
             */
            unsigned char* reserve_space(const size_t size) {
                assert(m_data && "This must be a valid buffer");
                // try to flush the buffer empty first.
                if (m_written + size > m_capacity && m_full) {
                    m_full(*this);
                }
                // if there's still not enough space, then try growing the buffer.
                if (m_written + size > m_capacity) {
                    if (m_memory && (m_auto_grow == auto_grow::yes)) {
                        // double buffer size until there is enough space
                        size_t new_capacity = m_capacity * 2;
                        while (m_written + size > new_capacity) {
                            new_capacity *= 2;
                        }
                        grow(new_capacity);
                    } else {
                        throw osmium::buffer_is_full();
                    }
                }
                unsigned char* data = &m_data[m_written];
                m_written += size;
                return data;
            }

            /**
             * Add an item to the buffer. The size of the item is stored inside
             * the item, so we know how much memory to copy.
             *
             * Note that you have to eventually call commit() to actually
             * commit this data.
             *
             * @pre The buffer must be valid.
             *
             * @tparam T Class of the item to be copied.
             *
             * @param item Reference to the item to be copied.
             *
             * @returns Reference to newly copied data in the buffer.
             */
            template <typename T>
            T& add_item(const T& item) {
                assert(m_data && "This must be a valid buffer");
                unsigned char* target = reserve_space(item.padded_size());
                std::copy_n(reinterpret_cast<const unsigned char*>(&item), item.padded_size(), target);
                return *reinterpret_cast<T*>(target);
            }

            /**
             * Add committed contents of the given buffer to this buffer.
             *
             * @pre The buffer must be valid.
             * @pre No builder can be open on this buffer.
             *
             * Note that you have to eventually call commit() to actually
             * commit this data.
             *
             * @param buffer The source of the copy. Must be valid.
             */
            void add_buffer(const Buffer& buffer) {
                assert(m_data && "This must be a valid buffer");
                assert(buffer && "Buffer parameter must be a valid buffer");
                assert(m_builder_count == 0 && "Make sure there are no Builder objects still in scope");
                unsigned char* target = reserve_space(buffer.committed());
                std::copy_n(buffer.data(), buffer.committed(), target);
            }

            /**
             * Add an item to the buffer. This function is provided so that
             * you can use std::back_inserter.
             *
             * @pre The buffer must be valid.
             * @pre No builder can be open on this buffer.
             *
             * @param item The item to be added.
             */
            void push_back(const osmium::memory::Item& item) {
                assert(m_data && "This must be a valid buffer");
                assert(m_builder_count == 0 && "Make sure there are no Builder objects still in scope");
                add_item(item);
                commit();
            }

            /**
             * An iterator that can be used to iterate over all items of
             * type T in a buffer.
             */
            template <typename T>
            using t_iterator = osmium::memory::ItemIterator<T>;

            /**
             * A const iterator that can be used to iterate over all items of
             * type T in a buffer.
             */
            template <typename T>
            using t_const_iterator = osmium::memory::ItemIterator<const T>;

            /**
             * An iterator that can be used to iterate over all OSMEntity
             * objects in a buffer.
             */
            using iterator = t_iterator<osmium::OSMEntity>;

            /**
             * A const iterator that can be used to iterate over all OSMEntity
             * objects in a buffer.
             */
            using const_iterator = t_const_iterator<osmium::OSMEntity>;

            template <typename T>
            ItemIteratorRange<T> select() {
                return ItemIteratorRange<T>{m_data, m_data + m_committed};
            }

            template <typename T>
            ItemIteratorRange<const T> select() const {
                return ItemIteratorRange<const T>{m_data, m_data + m_committed};
            }

            /**
             * Get iterator for iterating over all items of type T in the
             * buffer.
             *
             * @pre The buffer must be valid.
             *
             * @returns Iterator to first item of type T in the buffer.
             */
            template <typename T>
            t_iterator<T> begin() {
                assert(m_data && "This must be a valid buffer");
                return t_iterator<T>(m_data, m_data + m_committed);
            }

            /**
             * Get iterator for iterating over all objects of class OSMEntity
             * in the buffer.
             *
             * @pre The buffer must be valid.
             *
             * @returns Iterator to first OSMEntity in the buffer.
             */
            iterator begin() {
                assert(m_data && "This must be a valid buffer");
                return iterator(m_data, m_data + m_committed);
            }

            /**
             * Get iterator for iterating over all items of type T in the
             * buffer.
             *
             * @pre The buffer must be valid.
             *
             * @returns Iterator to first item of type T after given offset
             *          in the buffer.
             */
            template <typename T>
            t_iterator<T> get_iterator(size_t offset) {
                assert(m_data && "This must be a valid buffer");
                return t_iterator<T>(m_data + offset, m_data + m_committed);
            }

            /**
             * Get iterator for iterating over all objects of class OSMEntity
             * in the buffer.
             *
             * @pre The buffer must be valid.
             *
             * @returns Iterator to first OSMEntity after given offset in the
             *          buffer.
             */
            iterator get_iterator(size_t offset) {
                assert(m_data && "This must be a valid buffer");
                return iterator(m_data + offset, m_data + m_committed);
            }

            /**
             * Get iterator for iterating over all items of type T in the
             * buffer.
             *
             * @pre The buffer must be valid.
             *
             * @returns End iterator.
             */
            template <typename T>
            t_iterator<T> end() {
                assert(m_data && "This must be a valid buffer");
                return t_iterator<T>(m_data + m_committed, m_data + m_committed);
            }

            /**
             * Get iterator for iterating over all objects of class OSMEntity
             * in the buffer.
             *
             * @pre The buffer must be valid.
             *
             * @returns End iterator.
             */
            iterator end() {
                assert(m_data && "This must be a valid buffer");
                return iterator(m_data + m_committed, m_data + m_committed);
            }

            template <typename T>
            t_const_iterator<T> cbegin() const {
                assert(m_data && "This must be a valid buffer");
                return t_const_iterator<T>(m_data, m_data + m_committed);
            }

            const_iterator cbegin() const {
                assert(m_data && "This must be a valid buffer");
                return const_iterator(m_data, m_data + m_committed);
            }

            template <typename T>
            t_const_iterator<T> get_iterator(size_t offset) const {
                assert(m_data && "This must be a valid buffer");
                return t_const_iterator<T>(m_data + offset, m_data + m_committed);
            }

            const_iterator get_iterator(size_t offset) const {
                assert(m_data && "This must be a valid buffer");
                return const_iterator(m_data + offset, m_data + m_committed);
            }

            template <typename T>
            t_const_iterator<T> cend() const {
                assert(m_data && "This must be a valid buffer");
                return t_const_iterator<T>(m_data + m_committed, m_data + m_committed);
            }

            const_iterator cend() const {
                assert(m_data && "This must be a valid buffer");
                return const_iterator(m_data + m_committed, m_data + m_committed);
            }

            template <typename T>
            t_const_iterator<T> begin() const {
                return cbegin<T>();
            }

            const_iterator begin() const {
                return cbegin();
            }

            template <typename T>
            t_const_iterator<T> end() const {
                return cend<T>();
            }

            const_iterator end() const {
                return cend();
            }

            /**
             * In a bool context any valid buffer is true.
             */
            explicit operator bool() const noexcept {
                return m_data != nullptr;
            }

            void swap(Buffer& other) {
                using std::swap;

                swap(m_memory, other.m_memory);
                swap(m_data, other.m_data);
                swap(m_capacity, other.m_capacity);
                swap(m_written, other.m_written);
                swap(m_committed, other.m_committed);
                swap(m_auto_grow, other.m_auto_grow);
                swap(m_full, other.m_full);
            }

            /**
             * Purge removed items from the buffer. This is done by moving all
             * non-removed items forward in the buffer overwriting removed
             * items and then correcting the m_written and m_committed numbers.
             *
             * Note that calling this function invalidates all iterators on
             * this buffer and all offsets in this buffer.
             *
             * For every non-removed item that moves its position, the function
             * 'moving_in_buffer' is called on the given callback object with
             * the old and new offsets in the buffer where the object used to
             * be and is now, respectively. This call can be used to update any
             * indexes.
             *
             * @pre The buffer must be valid.
             */
            template <typename TCallbackClass>
            void purge_removed(TCallbackClass* callback) {
                assert(m_data && "This must be a valid buffer");
                if (begin() == end()) {
                    return;
                }

                iterator it_write = begin();

                iterator next;
                for (iterator it_read = begin(); it_read != end(); it_read = next) {
                    next = std::next(it_read);
                    if (!it_read->removed()) {
                        if (it_read != it_write) {
                            assert(it_read.data() >= data());
                            assert(it_write.data() >= data());
                            size_t old_offset = static_cast<size_t>(it_read.data() - data());
                            size_t new_offset = static_cast<size_t>(it_write.data() - data());
                            callback->moving_in_buffer(old_offset, new_offset);
                            std::memmove(it_write.data(), it_read.data(), it_read->padded_size());
                        }
                        it_write.advance_once();
                    }
                }

                assert(it_write.data() >= data());
                m_written = static_cast<size_t>(it_write.data() - data());
                m_committed = m_written;
            }

        }; // class Buffer

        inline void swap(Buffer& lhs, Buffer& rhs) {
            lhs.swap(rhs);
        }

        /**
         * Compare two buffers for equality.
         *
         * Buffers are equal if they are both invalid or if they are both
         * valid and have the same data pointer, capacity and committed
         * data.
         */
        inline bool operator==(const Buffer& lhs, const Buffer& rhs) noexcept {
            if (!lhs || !rhs) {
                return !lhs && !rhs;
            }
            return lhs.data() == rhs.data() && lhs.capacity() == rhs.capacity() && lhs.committed() == rhs.committed();
        }

        inline bool operator!=(const Buffer& lhs, const Buffer& rhs) noexcept {
            return ! (lhs == rhs);
        }

    } // namespace memory

} // namespace osmium

#endif // OSMIUM_MEMORY_BUFFER_HPP