Skip to content

Commit

Permalink
Update for V5.1
Browse files Browse the repository at this point in the history
  • Loading branch information
knonomura committed Oct 24, 2022
1 parent 3e92688 commit 7d21db6
Show file tree
Hide file tree
Showing 8 changed files with 1,777 additions and 83 deletions.
433 changes: 426 additions & 7 deletions utility/util/allocator.cpp

Large diffs are not rendered by default.

337 changes: 322 additions & 15 deletions utility/util/allocator.h

Large diffs are not rendered by default.

25 changes: 20 additions & 5 deletions utility/util/container.h
Original file line number Diff line number Diff line change
Expand Up @@ -1787,8 +1787,8 @@ class WeakMap {

private:
template<typename Ev>
struct Enrty {
Enrty() : referenceCount_(0) {};
struct Entry {
Entry() : referenceCount_(0) {};

Ev value_;
size_t referenceCount_;
Expand All @@ -1797,7 +1797,7 @@ class WeakMap {
WeakMap(const WeakMap&);
WeakMap& operator=(const WeakMap&);

typedef Enrty<V> EntryType;
typedef Entry<V> EntryType;
typedef std::map<K, EntryType*> BaseType;

BaseType base_;
Expand Down Expand Up @@ -2629,7 +2629,7 @@ void XArray<T, Alloc>::reserveInternal(size_t requestedCapacity) {

const uint32_t MIN_CAPACITY_BIT = 4;
const size_t usedSize = this->size();
const size_t newCapacity = (1U << std::max<uint32_t>(
size_t newCapacity = (1U << std::max<uint32_t>(
MIN_CAPACITY_BIT,
static_cast<uint32_t>(sizeof(uint32_t) * CHAR_BIT) -
nlz(static_cast<uint32_t>(requestedCapacity - 1)) ));
Expand All @@ -2639,6 +2639,21 @@ void XArray<T, Alloc>::reserveInternal(size_t requestedCapacity) {
requestedCapacity << ")");
}

#if UTIL_MEMORY_POOL_AGGRESSIVE
typedef detail::DirectAllocationUtils Utils;
if (newCapacity >= (1U << (Utils::LARGE_ELEMENT_BITS - 1)) / sizeof(T)) {
const size_t margin =
(Utils::ELEMENT_MARGIN_SIZE + sizeof(T) - 1) / sizeof(T);
if (margin <= newCapacity - requestedCapacity) {
newCapacity -= margin;
}
else if (newCapacity < (1U << (sizeof(uint32_t) * CHAR_BIT - 1)) &&
newCapacity > margin) {
newCapacity += newCapacity - margin;
}
}
#endif

T *newData;
try {
newData = allocator_.allocate(newCapacity);
Expand Down Expand Up @@ -3345,7 +3360,7 @@ WeakMap<K, V>::WeakMap() {

template<typename K, typename V>
WeakMap<K, V>::~WeakMap() {
for (typename std::map<K, Enrty<V>*>::iterator it = base_.begin();
for (typename std::map<K, Entry<V>*>::iterator it = base_.begin();
it != base_.end(); ++it) {
delete it->second;
}
Expand Down
41 changes: 41 additions & 0 deletions utility/util/os.h
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,47 @@ struct FileLib {
#endif
};

class Mutex;
namespace detail {
struct DirectMemoryUtils {
static void* mallocDirect(size_t size) UTIL_NOEXCEPT;
static void freeDirect(void *ptr) UTIL_NOEXCEPT;
};

class DirectMutex {
public:
DirectMutex() throw();
~DirectMutex();

void lock() throw();
void unlock() throw();

private:
struct Binder;

struct LocalData {
#ifdef UTIL_HAVE_POSIX_MUTEX
pthread_mutex_t mutex_;
#else
CRITICAL_SECTION cs_;
#endif
};

void* data() throw();

LocalData data_;
};

class DirectMutex::Binder {
public:
Binder(DirectMutex &src, Mutex &target) throw();
~Binder();

private:
Mutex &target_;
};
}

}

#endif
58 changes: 53 additions & 5 deletions utility/util/thread.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -356,11 +356,10 @@ UTIL_FLAG_TYPE UTIL_MUTEX_ERRORCHECK = 0;

struct Mutex::Data {
#ifdef UTIL_HAVE_POSIX_MUTEX
Data(const pthread_mutexattr_t *attr);
#else
Data();
explicit Data(const pthread_mutexattr_t *attr);
#endif

Data() throw();
~Data();

#ifdef UTIL_HAVE_POSIX_MUTEX
Expand All @@ -382,11 +381,17 @@ Mutex::Data::Data(const pthread_mutexattr_t *attr) {
UTIL_THROW_PLATFORM_ERROR(NULL);
}
}
#endif

Mutex::Data::Data() throw() {
#ifdef UTIL_HAVE_POSIX_MUTEX
if (0 != pthread_mutex_init(&mutex_, NULL)) {
assert(false);
}
#else
Mutex::Data::Data() {
InitializeCriticalSection(&cs_);
}
#endif
}

Mutex::Data::~Data() {
#ifdef UTIL_HAVE_POSIX_MUTEX
Expand Down Expand Up @@ -450,6 +455,10 @@ bool Mutex::tryLock(uint32_t msec) {
UTIL_THROW_NOIMPL_UTIL();
}

Mutex::Mutex(const FalseType&) :
data_(UTIL_NULLPTR) {
}

MutexAttribute::MutexAttribute() : data_(new Data()) {
#ifdef UTIL_HAVE_POSIX_MUTEX
if (0 != pthread_mutexattr_init(&data_->attr_)) {
Expand Down Expand Up @@ -1427,4 +1436,43 @@ ConflictionDetectorScope::~ConflictionDetectorScope() try {
catch (...) {
}


namespace detail {

DirectMutex::DirectMutex() throw() {
new (data()) Mutex::Data();
}

DirectMutex::~DirectMutex() {
static_cast<Mutex::Data*>(data())->~Data();
}

void DirectMutex::lock() throw() {
Mutex mutex((FalseType()));
Binder binder(*this, mutex);
mutex.lock();
}

void DirectMutex::unlock() throw() {
Mutex mutex((FalseType()));
Binder binder(*this, mutex);
mutex.unlock();
}

void* DirectMutex::data() throw() {
UTIL_STATIC_ASSERT(sizeof(LocalData) == sizeof(Mutex::Data));
return &data_;
}

DirectMutex::Binder::Binder(DirectMutex &src, Mutex &target) throw() :
target_(target) {
target_.data_.reset(static_cast<Mutex::Data*>(src.data()));
}

DirectMutex::Binder::~Binder() {
target_.data_.release();
}

}

}
7 changes: 7 additions & 0 deletions utility/util/thread.h
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,9 @@ class ConditionAttribute {
};

class MutexAttribute;
namespace detail {
class DirectMutex;
}

extern UTIL_FLAG_TYPE UTIL_MUTEX_DEFAULT;
extern UTIL_FLAG_TYPE UTIL_MUTEX_FAST;
Expand All @@ -257,9 +260,13 @@ class Mutex {
void unlock(void);

private:
friend class detail::DirectMutex;

Mutex(const Mutex&);
Mutex& operator=(const Mutex&);

Mutex(const FalseType&);

struct Data;
UTIL_UNIQUE_PTR<Data> data_;
};
Expand Down
Loading

0 comments on commit 7d21db6

Please sign in to comment.