xerus
a general purpose tensor library
allocator.h
Go to the documentation of this file.
1 // Xerus - A General Purpose Tensor Library
2 // Copyright (C) 2014-2017 Benjamin Huber and Sebastian Wolf.
3 //
4 // Xerus is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU Affero General Public License as published
6 // by the Free Software Foundation, either version 3 of the License,
7 // or (at your option) any later version.
8 //
9 // Xerus is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU Affero General Public License for more details.
13 //
14 // You should have received a copy of the GNU Affero General Public License
15 // along with Xerus. If not, see <http://www.gnu.org/licenses/>.
16 //
17 // For further information on Xerus visit https://libXerus.org
18 // or contact us at contact@libXerus.org.
19 
26 #pragma once
27 
28 #ifdef XERUS_REPLACE_ALLOCATOR
29 
30  #include <cstdio>
31  #include <cstdlib>
32  #include <vector>
33  #include <array>
34  #include <set>
35  #include <ext/malloc_allocator.h>
36 
37  namespace xerus { namespace misc {
38 
39  extern void* (*r_malloc)(size_t);
40  extern void (*r_free)(void*);
41 
42  using Mallocator = __gnu_cxx::malloc_allocator<void*>;
43 
44  struct AllocatorStorage {
45  static constexpr const size_t POOL_SIZE = 4*1024*1024;
46  static constexpr const size_t BUCKET_SIZE = 64;
47  static constexpr const size_t ALIGNMENT = 64;
48  static constexpr const size_t NUM_BUCKETS = 64;
49  static constexpr const size_t SMALLEST_NOT_CACHED_SIZE = BUCKET_SIZE * NUM_BUCKETS - 1;
50 
51  static_assert(BUCKET_SIZE > 1, "Buckets need to be at least 2 bytes large.");
52  static_assert(BUCKET_SIZE % ALIGNMENT == 0, "Bucket size needs to be aligned");
53  static_assert(ALIGNMENT <= 256, "Only alignment up to 256 bytes is supported as only a single byte is used to indicate the offset.");
54  static_assert(NUM_BUCKETS < 255, "atm only a single byte is used to store bucket size");
55 
56  std::array<std::vector<uint8_t*, Mallocator>, NUM_BUCKETS> buckets;
57  std::vector<std::pair<uint8_t*, uint8_t*>, Mallocator> pools;
58 
59  AllocatorStorage();
60  ~AllocatorStorage();
61 
62  void create_new_pool();
63 
64  unsigned long allocCount[NUM_BUCKETS];
65  long maxAlloc[NUM_BUCKETS];
66  long currAlloc[NUM_BUCKETS];
67  };
68 
69  extern thread_local AllocatorStorage astore;
70  }}
71 
72 #endif
73 
The main namespace of xerus.
Definition: basic.h:37