Welcome to mirror list, hosted at ThFree Co, Russian Federation.

BLI_linear_allocator.hh « blenlib « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: cebf878580c47be6a41eb3d8ec1d99ca9b284915 (plain)
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
/*
 * This program 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
 * of the License, or (at your option) any later version.
 *
 * This program 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 this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

/** \file
 * \ingroup bli
 *
 * A linear allocator is the simplest form of an allocator. It never reuses any memory, and
 * therefore does not need a deallocation method. It simply hands out consecutive buffers of
 * memory. When the current buffer is full, it reallocates a new larger buffer and continues.
 */

#ifndef __BLI_LINEAR_ALLOCATOR_HH__
#define __BLI_LINEAR_ALLOCATOR_HH__

#include "BLI_string_ref.hh"
#include "BLI_utility_mixins.hh"
#include "BLI_vector.hh"

namespace BLI {

template<typename Allocator = GuardedAllocator> class LinearAllocator : NonCopyable, NonMovable {
 private:
  Allocator m_allocator;
  Vector<void *> m_owned_buffers;
  Vector<ArrayRef<char>> m_unused_borrowed_buffers;

  uintptr_t m_current_begin;
  uintptr_t m_current_end;
  uint m_next_min_alloc_size;

#ifdef DEBUG
  uint m_debug_allocated_amount = 0;
#endif

 public:
  LinearAllocator()
  {
    m_current_begin = 0;
    m_current_end = 0;
    m_next_min_alloc_size = 64;
  }

  ~LinearAllocator()
  {
    for (void *ptr : m_owned_buffers) {
      m_allocator.deallocate(ptr);
    }
  }

  void provide_buffer(void *buffer, uint size)
  {
    m_unused_borrowed_buffers.append(ArrayRef<char>((char *)buffer, size));
  }

  template<uint Size, uint Alignment>
  void provide_buffer(AlignedBuffer<Size, Alignment> &aligned_buffer)
  {
    this->provide_buffer(aligned_buffer.ptr(), Size);
  }

  template<typename T> T *allocate()
  {
    return (T *)this->allocate(sizeof(T), alignof(T));
  }

  template<typename T> MutableArrayRef<T> allocate_array(uint length)
  {
    return MutableArrayRef<T>((T *)this->allocate(sizeof(T) * length), length);
  }

  void *allocate(uint size, uint alignment = 4)
  {
    BLI_assert(alignment >= 1);
    BLI_assert(is_power_of_2_i(alignment));

#ifdef DEBUG
    m_debug_allocated_amount += size;
#endif

    uintptr_t alignment_mask = alignment - 1;
    uintptr_t potential_allocation_begin = (m_current_begin + alignment_mask) & ~alignment_mask;
    uintptr_t potential_allocation_end = potential_allocation_begin + size;

    if (potential_allocation_end <= m_current_end) {
      m_current_begin = potential_allocation_end;
      return (void *)potential_allocation_begin;
    }
    else {
      this->allocate_new_buffer(size + alignment);
      return this->allocate(size, alignment);
    }
  };

  StringRefNull copy_string(StringRef str)
  {
    uint alloc_size = str.size() + 1;
    char *buffer = (char *)this->allocate(alloc_size, 1);
    str.copy(buffer, alloc_size);
    return StringRefNull((const char *)buffer);
  }

  template<typename T, typename... Args> T *construct(Args &&... args)
  {
    void *buffer = this->allocate(sizeof(T), alignof(T));
    T *value = new (buffer) T(std::forward<Args>(args)...);
    return value;
  }

  template<typename T, typename... Args>
  ArrayRef<T *> construct_elements_and_pointer_array(uint n, Args &&... args)
  {
    void *pointer_buffer = this->allocate(n * sizeof(T *), alignof(T *));
    void *element_buffer = this->allocate(n * sizeof(T), alignof(T));

    MutableArrayRef<T *> pointers((T **)pointer_buffer, n);
    T *elements = (T *)element_buffer;

    for (uint i : IndexRange(n)) {
      pointers[i] = elements + i;
    }
    for (uint i : IndexRange(n)) {
      new (elements + i) T(std::forward<Args>(args)...);
    }

    return pointers;
  }

  template<typename T> MutableArrayRef<T> construct_array_copy(ArrayRef<T> source)
  {
    T *buffer = (T *)this->allocate(source.byte_size(), alignof(T));
    uninitialized_copy_n(source.begin(), source.size(), buffer);
    return MutableArrayRef<T>(buffer, source.size());
  }

 private:
  void allocate_new_buffer(uint min_allocation_size)
  {
    for (uint i : m_unused_borrowed_buffers.index_range()) {
      ArrayRef<char> buffer = m_unused_borrowed_buffers[i];
      if (buffer.size() >= min_allocation_size) {
        m_unused_borrowed_buffers.remove_and_reorder(i);
        m_current_begin = (uintptr_t)buffer.begin();
        m_current_end = (uintptr_t)buffer.end();
        return;
      }
    }

    uint size_in_bytes = power_of_2_min_u(std::max(min_allocation_size, m_next_min_alloc_size));
    m_next_min_alloc_size = size_in_bytes * 2;

    void *buffer = m_allocator.allocate(size_in_bytes, __func__);
    m_owned_buffers.append(buffer);
    m_current_begin = (uintptr_t)buffer;
    m_current_end = m_current_begin + size_in_bytes;
  }
};

}  // namespace BLI

#endif /* __BLI_LINEAR_ALLOCATOR_HH__ */