Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/include/fst/cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <algorithm>
#include <functional>
#include <list>
#include <memory>
#include <vector>

#include <fst/flags.h>
Expand Down Expand Up @@ -70,7 +71,7 @@ class CacheState {

using ArcAllocator = M;
using StateAllocator =
typename ArcAllocator::template rebind<CacheState<A, M>>::other;
typename std::allocator_traits<ArcAllocator>::template rebind_alloc<CacheState<A, M>>;

// Provides STL allocator for arcs.
explicit CacheState(const ArcAllocator &alloc)
Expand Down
76 changes: 42 additions & 34 deletions src/include/fst/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -230,13 +230,13 @@ template <typename T>
class BlockAllocator {
public:
using Allocator = std::allocator<T>;
using size_type = typename Allocator::size_type;
using difference_type = typename Allocator::difference_type;
using pointer = typename Allocator::pointer;
using const_pointer = typename Allocator::const_pointer;
using reference = typename Allocator::reference;
using const_reference = typename Allocator::const_reference;
using value_type = typename Allocator::value_type;
using size_type = typename std::allocator_traits<Allocator>::size_type;
using difference_type = typename std::allocator_traits<Allocator>::difference_type;
using pointer = typename std::allocator_traits<Allocator>::pointer;
using const_pointer = typename std::allocator_traits<Allocator>::const_pointer;
using value_type = typename std::allocator_traits<Allocator>::value_type;
using reference = value_type&;
using const_reference = const value_type&;

template <typename U>
struct rebind {
Expand All @@ -261,31 +261,36 @@ class BlockAllocator {
if (Arenas()->DecrRefCount() == 0) delete Arenas();
}

pointer address(reference ref) const { return Allocator().address(ref); }

const_pointer address(const_reference ref) const {
return Allocator().address(ref);
size_type max_size() const {
auto alloc = Allocator();
return std::allocator_traits<Allocator>::max_size(alloc);
}

size_type max_size() const { return Allocator().max_size(); }

template <class U, class... Args>
void construct(U *p, Args &&... args) {
Allocator().construct(p, std::forward<Args>(args)...);
auto alloc = Allocator();
return std::allocator_traits<Allocator>::construct(alloc, p, std::forward<Args>(args)...);
}

void destroy(pointer p) { Allocator().destroy(p); }
void destroy(pointer p) {
auto alloc = Allocator();
return std::allocator_traits<Allocator>::destroy(alloc, p);
}

pointer allocate(size_type n, const void *hint = nullptr) {
if (n * kAllocFit <= kAllocSize) {
return static_cast<pointer>(Arena()->Allocate(n));
} else {
return Allocator().allocate(n, hint);
auto alloc = Allocator();
return std::allocator_traits<Allocator>::allocate(alloc, n, hint);
}
}

void deallocate(pointer p, size_type n) {
if (n * kAllocFit > kAllocSize) Allocator().deallocate(p, n);
if (n * kAllocFit > kAllocSize){
auto alloc = Allocator();
std::allocator_traits<Allocator>::deallocate(alloc, p, n);
}
}

MemoryArenaCollection *Arenas() const { return arenas_; }
Expand Down Expand Up @@ -322,13 +327,13 @@ template <typename T>
class PoolAllocator {
public:
using Allocator = std::allocator<T>;
using size_type = typename Allocator::size_type;
using difference_type = typename Allocator::difference_type;
using pointer = typename Allocator::pointer;
using const_pointer = typename Allocator::const_pointer;
using reference = typename Allocator::reference;
using const_reference = typename Allocator::const_reference;
using value_type = typename Allocator::value_type;
using size_type = typename std::allocator_traits<Allocator>::size_type;
using difference_type = typename std::allocator_traits<Allocator>::difference_type;
using pointer = typename std::allocator_traits<Allocator>::pointer;
using const_pointer = typename std::allocator_traits<Allocator>::const_pointer;
using value_type = typename std::allocator_traits<Allocator>::value_type;
using reference = value_type&;
using const_reference = const value_type&;

template <typename U>
struct rebind {
Expand All @@ -353,20 +358,21 @@ class PoolAllocator {
if (Pools()->DecrRefCount() == 0) delete Pools();
}

pointer address(reference ref) const { return Allocator().address(ref); }

const_pointer address(const_reference ref) const {
return Allocator().address(ref);
size_type max_size() const {
auto alloc = Allocator();
return std::allocator_traits<Allocator>::max_size(alloc);
}

size_type max_size() const { return Allocator().max_size(); }

template <class U, class... Args>
void construct(U *p, Args &&... args) {
Allocator().construct(p, std::forward<Args>(args)...);
auto alloc = Allocator();
std::allocator_traits<Allocator>::construct(alloc, p, std::forward<Args>(args)...);
}

void destroy(pointer p) { Allocator().destroy(p); }
void destroy(pointer p) {
auto alloc = Allocator();
std::allocator_traits<Allocator>::destroy(alloc, p);
}

pointer allocate(size_type n, const void *hint = nullptr) {
if (n == 1) {
Expand All @@ -384,7 +390,8 @@ class PoolAllocator {
} else if (n <= 64) {
return static_cast<pointer>(Pool<64>()->Allocate());
} else {
return Allocator().allocate(n, hint);
auto alloc = Allocator();
return std::allocator_traits<Allocator>::allocate(alloc, n, hint);
}
}

Expand All @@ -404,7 +411,8 @@ class PoolAllocator {
} else if (n <= 64) {
Pool<64>()->Free(p);
} else {
Allocator().deallocate(p, n);
auto alloc = Allocator();
return std::allocator_traits<Allocator>::deallocate(alloc, p, n);
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/include/fst/vector-fst.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#ifndef FST_VECTOR_FST_H_
#define FST_VECTOR_FST_H_

#include <memory>
#include <string>
#include <utility>
#include <vector>
Expand Down Expand Up @@ -35,7 +36,7 @@ class VectorState {
using Weight = typename Arc::Weight;
using ArcAllocator = M;
using StateAllocator =
typename ArcAllocator::template rebind<VectorState<Arc, M>>::other;
typename std::allocator_traits<ArcAllocator>::template rebind_alloc<VectorState<Arc, M>>;

// Provide STL allocator for arcs.
explicit VectorState(const ArcAllocator &alloc)
Expand Down