File: ipool.h

package info (click to toggle)
etlcpp 20.44.2%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 21,692 kB
  • sloc: cpp: 302,710; ansic: 11,683; sh: 1,420; asm: 301; python: 281; makefile: 16
file content (667 lines) | stat: -rw-r--r-- 22,090 bytes parent folder | download
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
///\file

/******************************************************************************
The MIT License(MIT)

Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com

Copyright(c) 2014 John Wellbelove

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

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 AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
******************************************************************************/

#ifndef ETL_IPOOL_INCLUDED
#define ETL_IPOOL_INCLUDED

#include "platform.h"
#include "error_handler.h"
#include "exception.h"
#include "iterator.h"
#include "static_assert.h"
#include "utility.h"
#include "memory.h"
#include "placement_new.h"

#define ETL_POOL_CPP03_CODE 0

namespace etl
{
  //***************************************************************************
  /// The base class for pool exceptions.
  ///\ingroup pool
  //***************************************************************************
  class pool_exception : public exception
  {
  public:

    pool_exception(string_type reason_, string_type file_name_, numeric_type line_number_)
      : exception(reason_, file_name_, line_number_)
    {}
  };

  //***************************************************************************
  /// The exception thrown when the pool has no more free items.
  ///\ingroup pool
  //***************************************************************************
  class pool_no_allocation : public pool_exception
  {
  public:

    explicit pool_no_allocation(string_type file_name_, numeric_type line_number_)
      : pool_exception(ETL_ERROR_TEXT("pool:allocation", ETL_POOL_FILE_ID"A"), file_name_, line_number_)
    {}
  };

  //***************************************************************************
  /// The exception thrown when an object is released which does not belong to the pool.
  ///\ingroup pool
  //***************************************************************************
  class pool_object_not_in_pool : public pool_exception
  {
  public:

    pool_object_not_in_pool(string_type file_name_, numeric_type line_number_)
      : pool_exception(ETL_ERROR_TEXT("pool:not in pool", ETL_POOL_FILE_ID"B"), file_name_, line_number_)
    {}
  };

  //***************************************************************************
  /// The exception thrown when an the type requested is larger than the element size.
  ///\ingroup pool
  //***************************************************************************
  class pool_element_size : public pool_exception
  {
  public:

    pool_element_size(string_type file_name_, numeric_type line_number_)
      : pool_exception(ETL_ERROR_TEXT("pool:element size", ETL_POOL_FILE_ID"C"), file_name_, line_number_)
    {}
  };

  //***************************************************************************
  ///\ingroup pool
  //***************************************************************************
  class ipool
  {
  public:

    typedef size_t size_type;

  private:

    //***************************************************************************
    /// Iterator helper functions
    //***************************************************************************

    const char* buffer_end() const
    {
      return p_buffer + Item_Size * items_initialised;
    }

    //***************************************************************************
    /// Optimization: a candidate for being in free list, but no guarantee
    /// if false, cannot be part of free list, and therefore is allocated
    /// if true, possibly in free list, but still needs to be checked via free list
    //***************************************************************************
    bool is_pointing_into_pool_or_end_or_nullptr(const char *address) const
    {
      return address == ETL_NULLPTR || (p_buffer <= address && address <= buffer_end());
    }

    //***************************************************************************
    /// Iterate free list to confirm specified address is included or not
    //***************************************************************************
    bool is_in_free_list(const char* address) const
    {
      const char* i = p_next;
      while (i != ETL_NULLPTR)
      {
        if (address == i)
        {
          return true;
        }
        i = *reinterpret_cast<const char* const*>(i);
      }
      return false;
    }

  public:

    //***************************************************************************
    template<bool is_const>
    class ipool_iterator
    {
    public:

      friend class ipool;

      typedef typename etl::conditional<is_const, const char*, char*>::type         value_type;
      typedef typename etl::conditional<is_const, const char*&, char*&>::type       reference;
      typedef typename etl::conditional<is_const, const char**, char**>::type       pointer;
      typedef ptrdiff_t                                                             difference_type;
      typedef ETL_OR_STD::forward_iterator_tag                                      iterator_category;
      typedef typename etl::conditional<is_const, const void*, void*>::type         void_type;
      typedef typename etl::conditional<is_const, const ipool, ipool>::type         pool_type;
      typedef typename etl::conditional<is_const, const char* const*, char**>::type pointer_type;

      //***************************************************************************
      ipool_iterator(const ipool_iterator& other)
        : p_current(other.p_current)
        , p_pool(other.p_pool)
      {
        find_allocated();
      }

      //***************************************************************************
      ipool_iterator& operator ++()
      {
        p_current = p_current + p_pool->Item_Size;
        find_allocated();
        return *this;
      }

      //***************************************************************************
      ipool_iterator operator ++(int)
      {
        ipool_iterator temp(*this);
        p_current = p_current + p_pool->Item_Size;
        find_allocated();
        return temp;
      }

      //***************************************************************************
      ipool_iterator& operator =(const ipool_iterator& other)
      {
        p_current = other.p_current;
        p_pool = other.p_pool;
        return *this;
      }

      //***************************************************************************
      void_type operator *() const
      {
        return p_current;
      }

      //***************************************************************************
      template <typename T>
      T& get() const
      {
        return *reinterpret_cast<T*>(p_current);
      }

      //***************************************************************************
      friend bool operator == (const ipool_iterator& lhs, const ipool_iterator& rhs)
      {
        return lhs.p_current == rhs.p_current;
      }

      //***************************************************************************  
      friend bool operator != (const ipool_iterator& lhs, const ipool_iterator& rhs)
      {
        return !(lhs == rhs);
      }

    private:

      //***************************************************************************
      /// Find allocated item by increasing p_current, starting at p_current.
      /// Leave p_current at buffer_end() if no further allocated item found.
      //***************************************************************************
      void find_allocated()
      {
        while (p_current < p_pool->buffer_end())
        {
          value_type value = *reinterpret_cast<pointer_type>(p_current);
          if (!p_pool->is_pointing_into_pool_or_end_or_nullptr(value))
          {
            return;
          }
          if (!p_pool->is_in_free_list(p_current))
          {
            return;
          }
          p_current += p_pool->Item_Size;
        }
      }

      //***************************************************************************
      /// Constructor
      //***************************************************************************
      ipool_iterator(value_type p, pool_type* pool_)
        : p_current(p)
        , p_pool(pool_)
      {
        find_allocated();
      }

      value_type p_current;
      pool_type* p_pool;
    };

    template<bool is_const>
    friend class ipool_iterator;

    typedef ipool_iterator<false> iterator;

    //***************************************************************************
    class const_iterator : public ipool_iterator<true>
    {
    public:
      const_iterator(const ipool_iterator& other) : ipool_iterator(other) {}
      const_iterator(const ipool_iterator<false>& other) : ipool_iterator(other.p_current, other.p_pool) {}
      const_iterator(value_type p, pool_type* pool_) : ipool_iterator<true>(p, pool_) {}
    };

    //***************************************************************************
    iterator begin()
    {
      return iterator(p_buffer, this);
    }

    //***************************************************************************
    iterator end()
    {
      return iterator(p_buffer + Item_Size * items_initialised, this);
    }

    //***************************************************************************
    const_iterator begin() const
    {
      return const_iterator(p_buffer, this);
    }

    //***************************************************************************
    const_iterator end() const
    {
      return const_iterator(p_buffer + Item_Size * items_initialised, this);
    }

    //***************************************************************************
    const_iterator cbegin() const
    {
      return const_iterator(p_buffer, this);
    }

    //***************************************************************************
    const_iterator cend() const
    {
      return const_iterator(p_buffer + Item_Size * items_initialised, this);
    }

    //*************************************************************************
    /// Allocate storage for an object from the pool.
    /// If asserts or exceptions are enabled and there are no more free items an
    /// etl::pool_no_allocation if thrown, otherwise a null pointer is returned.
    //*************************************************************************
    template <typename T>
    T* allocate()
    {
      if (sizeof(T) > Item_Size)
      {
        ETL_ASSERT(false, ETL_ERROR(etl::pool_element_size));
      }

      return reinterpret_cast<T*>(allocate_item());
    }

#if ETL_CPP11_NOT_SUPPORTED || ETL_POOL_CPP03_CODE || ETL_USING_STLPORT
    //*************************************************************************
    /// Allocate storage for an object from the pool and create default.
    /// If asserts or exceptions are enabled and there are no more free items an
    /// etl::pool_no_allocation if thrown, otherwise a null pointer is returned.
    //*************************************************************************
    template <typename T>
    T* create()
    {
      T* p = allocate<T>();

      if (p)
      {
        ::new (p) T();
      }

      return p;
    }

    //*************************************************************************
    /// Allocate storage for an object from the pool and create with 1 parameter.
    /// If asserts or exceptions are enabled and there are no more free items an
    /// etl::pool_no_allocation if thrown, otherwise a null pointer is returned.
    //*************************************************************************
    template <typename T, typename T1>
    T* create(const T1& value1)
    {
      T* p = allocate<T>();

      if (p)
      {
        ::new (p) T(value1);
      }

      return p;
    }

    template <typename T, typename T1, typename T2>
    T* create(const T1& value1, const T2& value2)
    {
      T* p = allocate<T>();

      if (p)
      {
        ::new (p) T(value1, value2);
      }

      return p;
    }

    template <typename T, typename T1, typename T2, typename T3>
    T* create(const T1& value1, const T2& value2, const T3& value3)
    {
      T* p = allocate<T>();

      if (p)
      {
        ::new (p) T(value1, value2, value3);
      }

      return p;
    }

    template <typename T, typename T1, typename T2, typename T3, typename T4>
    T* create(const T1& value1, const T2& value2, const T3& value3, const T4& value4)
    {
      T* p = allocate<T>();

      if (p)
      {
        ::new (p) T(value1, value2, value3, value4);
      }

      return p;
    }
#else
    //*************************************************************************
    /// Emplace with variadic constructor parameters.
    //*************************************************************************
    template <typename T, typename... Args>
    T* create(Args&&... args)
    {
      T* p = allocate<T>();

      if (p)
      {
        ::new (p) T(etl::forward<Args>(args)...);
      }

      return p;
    }
#endif

    //*************************************************************************
    /// Destroys the object.
    /// Undefined behaviour if the pool does not contain a 'T'.
    /// \param p_object A pointer to the object to be destroyed.
    //*************************************************************************
    template <typename T>
    void destroy(const T* const p_object)
    {
      if (sizeof(T) > Item_Size)
      {
        ETL_ASSERT(false, ETL_ERROR(etl::pool_element_size));
      }

      p_object->~T();
      release(p_object);
    }

    //*************************************************************************
    /// Release an object in the pool.
    /// If asserts or exceptions are enabled and the object does not belong to this
    /// pool then an etl::pool_object_not_in_pool is thrown.
    /// \param p_object A pointer to the object to be released.
    //*************************************************************************
    void release(const void* const p_object)
    {
      const uintptr_t p = uintptr_t(p_object);
      release_item((char*)p);
    }

    //*************************************************************************
    /// Release all objects in the pool.
    //*************************************************************************
    void release_all()
    {
      items_allocated = 0;
      items_initialised = 0;
      p_next = p_buffer;
    }

    //*************************************************************************
    /// Check to see if the object belongs to the pool.
    /// \param p_object A pointer to the object to be checked.
    /// \return <b>true<\b> if it does, otherwise <b>false</b>
    //*************************************************************************
    bool is_in_pool(const void* const p_object) const
    {
      const uintptr_t p = uintptr_t(p_object);
      return is_item_in_pool((const char*)p);
    }

    //*************************************************************************
    /// Returns the maximum number of items in the pool.
    //*************************************************************************
    size_t max_size() const
    {
      return Max_Size;
    }

    //*************************************************************************
    /// Returns the maximum size of an item in the pool.
    //*************************************************************************
    size_t max_item_size() const
    {
      return Item_Size;
    }

    //*************************************************************************
    /// Returns the maximum number of items in the pool.
    //*************************************************************************
    size_t capacity() const
    {
      return Max_Size;
    }

    //*************************************************************************
    /// Returns the number of free items in the pool.
    //*************************************************************************
    size_t available() const
    {
      return Max_Size - items_allocated;
    }

    //*************************************************************************
    /// Returns the number of allocated items in the pool.
    //*************************************************************************
    size_t size() const
    {
      return items_allocated;
    }

    //*************************************************************************
    /// Checks to see if there are no allocated items in the pool.
    /// \return <b>true</b> if there are none allocated.
    //*************************************************************************
    bool empty() const
    {
      return items_allocated == 0;
    }

    //*************************************************************************
    /// Checks to see if there are no free items in the pool.
    /// \return <b>true</b> if there are none free.
    //*************************************************************************
    bool full() const
    {
      return items_allocated == Max_Size;
    }

  protected:

    //*************************************************************************
    /// Constructor
    //*************************************************************************
    ipool(char* p_buffer_, uint32_t item_size_, uint32_t max_size_)
      : p_buffer(p_buffer_),
      p_next(p_buffer_),
      items_allocated(0),
      items_initialised(0),
      Item_Size(item_size_),
      Max_Size(max_size_)
    {
    }

  private:
    
    static ETL_CONSTANT uintptr_t invalid_item_ptr = 1;

    //*************************************************************************
    /// Allocate an item from the pool.
    //*************************************************************************
    char* allocate_item()
    {
      char* p_value = ETL_NULLPTR;

      // Any free space left?
      if (items_allocated < Max_Size)
      {
        // Initialise another one if necessary.
        if (items_initialised < Max_Size)
        {
          char* p = p_buffer + (items_initialised * Item_Size);
          char* np = p + Item_Size;
          *reinterpret_cast<char**>(p) = np;
          ++items_initialised;
        }

        // Get the address of new allocated item.
        p_value = p_next;

        ++items_allocated;
        if (items_allocated < Max_Size)
        {
          // Set up the pointer to the next free item
          p_next = *reinterpret_cast<char**>(p_next);
        }
        else
        {
          // No more left!
          p_next = ETL_NULLPTR;
        }

        // invalid pointer, outside pool
        // needs to be different from ETL_NULLPTR since ETL_NULLPTR is used
        // as list endmarker
        *reinterpret_cast<uintptr_t*>(p_value) = invalid_item_ptr;
      }
      else
      {
        ETL_ASSERT(false, ETL_ERROR(pool_no_allocation));
      }

      return p_value;
    }

    //*************************************************************************
    /// Release an item back to the pool.
    //*************************************************************************
    void release_item(char* p_value)
    {
      // Does it belong to us?
      ETL_ASSERT(is_item_in_pool(p_value), ETL_ERROR(pool_object_not_in_pool));

      if (items_allocated > 0) 
      {
        // Point it to the current free item.
        *(uintptr_t*)p_value = reinterpret_cast<uintptr_t>(p_next);

        p_next = p_value;

        --items_allocated;
      }
      else 
      {
        ETL_ASSERT_FAIL(ETL_ERROR(pool_no_allocation));
      }
    }

    //*************************************************************************
    /// Check if the item belongs to this pool.
    //*************************************************************************
    bool is_item_in_pool(const char* p) const
    {
      // Within the range of the buffer?
      intptr_t distance = p - p_buffer;
      bool is_within_range = (distance >= 0) && (distance <= intptr_t((Item_Size * Max_Size) - Item_Size));

      // Modulus and division can be slow on some architectures, so only do this in debug.
#if ETL_IS_DEBUG_BUILD
      // Is the address on a valid object boundary?
      bool is_valid_address = ((distance % Item_Size) == 0);
#else
      bool is_valid_address = true;
#endif

      return is_within_range && is_valid_address;
    }

    // Disable copy construction and assignment.
    ipool(const ipool&);
    ipool& operator =(const ipool&);

    char* p_buffer;
    char* p_next;

    uint32_t  items_allocated;   ///< The number of items allocated.
    uint32_t  items_initialised; ///< The number of items initialised.

    const uint32_t Item_Size;    ///< The size of allocated items.
    const uint32_t Max_Size;     ///< The maximum number of objects that can be allocated.

    //*************************************************************************
    /// Destructor.
    //*************************************************************************
#if defined(ETL_POLYMORPHIC_POOL) || defined(ETL_POLYMORPHIC_CONTAINERS)
  public:
    virtual ~ipool()
    {
    }
#else
  protected:
    ~ipool()
    {
    }
#endif
  };
}

#endif