第三方框架改为仓内子集并完善构建忽略规则
完整 NIM_Duilib_Framework 仓库移至父目录,third_party 仅保留实际使用的 base、duilib、bin/resources 与许可证;.gitignore 覆盖 MSBuild 与打包产物。
This commit is contained in:
1 parent
249e8548de
commit
e1e26acc49
391 files changed
+342850
No files matched your search
@@ -0,0 +1,271 @@
|
||||
// This file defines the block buffer
|
||||
|
||||
#ifndef BASE_MEMORY_BLOCKBUFFER_H_
|
||||
#define BASE_MEMORY_BLOCKBUFFER_H_
|
||||
|
||||
#include <new>
|
||||
#include <assert.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
namespace nbase
|
||||
{
|
||||
|
||||
// boost::pool min accord
|
||||
template <unsigned BlockSize>
|
||||
struct default_block_allocator_malloc_free
|
||||
{
|
||||
enum { requested_size = BlockSize };
|
||||
|
||||
static char * ordered_malloc(size_t n)
|
||||
{
|
||||
return (char *)malloc(requested_size * n);
|
||||
}
|
||||
static void ordered_free(char * const block, size_t)
|
||||
{
|
||||
free(block);
|
||||
}
|
||||
};
|
||||
|
||||
template <unsigned BlockSize>
|
||||
struct default_block_allocator_new_delete
|
||||
{
|
||||
enum { requested_size = BlockSize };
|
||||
|
||||
static char * ordered_malloc(size_t n)
|
||||
{
|
||||
return new (std::nothrow) char[requested_size * n];
|
||||
}
|
||||
static void ordered_free(char * const block, size_t)
|
||||
{
|
||||
delete [] block;
|
||||
}
|
||||
};
|
||||
|
||||
#ifdef USE_ALLOCATOR_NEW_DELETE
|
||||
|
||||
typedef default_block_allocator_new_delete<1*1024> def_block_alloc_1k;
|
||||
typedef default_block_allocator_new_delete<2*1024> def_block_alloc_2k;
|
||||
typedef default_block_allocator_new_delete<4*1024> def_block_alloc_4k;
|
||||
typedef default_block_allocator_new_delete<8*1024> def_block_alloc_8k;
|
||||
typedef default_block_allocator_new_delete<16*1024> def_block_alloc_16k;
|
||||
typedef default_block_allocator_new_delete<32*1024> def_block_alloc_32k;
|
||||
|
||||
#else
|
||||
|
||||
typedef default_block_allocator_malloc_free<1*1024> def_block_alloc_1k;
|
||||
typedef default_block_allocator_malloc_free<2*1024> def_block_alloc_2k;
|
||||
typedef default_block_allocator_malloc_free<4*1024> def_block_alloc_4k;
|
||||
typedef default_block_allocator_malloc_free<8*1024> def_block_alloc_8k;
|
||||
typedef default_block_allocator_malloc_free<16*1024> def_block_alloc_16k;
|
||||
typedef default_block_allocator_malloc_free<32*1024> def_block_alloc_32k;
|
||||
|
||||
#endif // USE_ALLOCATOR_NEW_DELETE
|
||||
|
||||
template <typename BlockAllocator = def_block_alloc_4k, unsigned MaxBlocks = 2>
|
||||
class BlockBuffer
|
||||
{
|
||||
public:
|
||||
typedef BlockAllocator allocator;
|
||||
enum { max_blocks = MaxBlocks };
|
||||
enum { npos = size_t(-1) };
|
||||
|
||||
BlockBuffer() { block_ = 0; size_ = 0; data_ = (char *)""; }
|
||||
virtual ~BlockBuffer() { free(); }
|
||||
|
||||
inline bool empty() const { return size() == 0; }
|
||||
inline size_t block() const { return block_; }
|
||||
inline size_t blocksize() const { return allocator::requested_size; }
|
||||
inline size_t capacity() const { return block_ * allocator::requested_size; }
|
||||
inline size_t maxsize() const { return max_blocks * allocator::requested_size; }
|
||||
inline size_t maxfree() const { return maxsize() - size(); }
|
||||
inline size_t freespace() const { return capacity() - size(); }
|
||||
|
||||
inline char * data() { return data_; }
|
||||
inline size_t size() const { return size_; }
|
||||
|
||||
bool resize(size_t n, char c = 0);
|
||||
bool reserve(size_t n);
|
||||
bool append(const char *app, size_t len);
|
||||
bool replace(size_t pos, const char *rep, size_t n);
|
||||
void erase(size_t pos = 0, size_t n = npos, bool hold = false);
|
||||
|
||||
static size_t current_total_blocks() { return s_current_total_blocks; }
|
||||
static size_t peak_total_blocks() { return s_peak_total_blocks; }
|
||||
|
||||
protected:
|
||||
bool increase_capacity(size_t increase_size);
|
||||
char * tail() { return data_ + size_; }
|
||||
void size(size_t size) { assert(size <= capacity()); size_ = size; }
|
||||
|
||||
private:
|
||||
void free();
|
||||
static size_t s_current_total_blocks;
|
||||
static size_t s_peak_total_blocks;
|
||||
|
||||
char *data_;
|
||||
size_t size_;
|
||||
size_t block_;
|
||||
|
||||
BlockBuffer(const BlockBuffer&);
|
||||
void operator = (const BlockBuffer &);
|
||||
};
|
||||
|
||||
template <typename BlockAllocator, unsigned MaxBlocks>
|
||||
size_t BlockBuffer<BlockAllocator, MaxBlocks >::s_current_total_blocks = 0;
|
||||
|
||||
template <typename BlockAllocator, unsigned MaxBlocks>
|
||||
size_t BlockBuffer<BlockAllocator, MaxBlocks >::s_peak_total_blocks = 0;
|
||||
|
||||
template <typename BlockAllocator, unsigned MaxBlocks>
|
||||
inline void BlockBuffer<BlockAllocator, MaxBlocks >::free()
|
||||
{
|
||||
if (block_ > 0)
|
||||
{
|
||||
allocator::ordered_free(data_, block_);
|
||||
s_current_total_blocks -= block_;
|
||||
data_ = (char *)"";
|
||||
block_ = 0;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename BlockAllocator, unsigned MaxBlocks>
|
||||
inline bool BlockBuffer<BlockAllocator, MaxBlocks >::append(const char *app, size_t len)
|
||||
{
|
||||
if (len == 0)
|
||||
return true; // no data
|
||||
|
||||
if (increase_capacity(len))
|
||||
{
|
||||
memmove(tail(), app, len); // append
|
||||
size_ += len;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
template <typename BlockAllocator, unsigned MaxBlocks>
|
||||
inline bool BlockBuffer<BlockAllocator, MaxBlocks >::reserve(size_t n)
|
||||
{
|
||||
return (n <= capacity() || increase_capacity(n - capacity()));
|
||||
}
|
||||
|
||||
template <typename BlockAllocator, unsigned MaxBlocks>
|
||||
inline bool BlockBuffer<BlockAllocator, MaxBlocks >::resize(size_t n, char c)
|
||||
{
|
||||
if (n > size()) // increase
|
||||
{
|
||||
size_t len = n - size();
|
||||
if (!increase_capacity(len))
|
||||
return false;
|
||||
memset(tail(), c, len);
|
||||
}
|
||||
size_ = n;
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename BlockAllocator, unsigned MaxBlocks>
|
||||
inline bool BlockBuffer<BlockAllocator, MaxBlocks >::replace(size_t pos, const char *rep, size_t n)
|
||||
{
|
||||
if (pos >= size()) // out_of_range ?
|
||||
return append(rep, n);
|
||||
|
||||
if (pos + n >= size()) // replace all beginning with position pos
|
||||
{
|
||||
size_ = pos;
|
||||
return append(rep, n);
|
||||
}
|
||||
if (n > 0)
|
||||
memmove(data_ + pos, rep, n);
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename BlockAllocator, unsigned MaxBlocks>
|
||||
inline void BlockBuffer<BlockAllocator, MaxBlocks >::erase(size_t pos, size_t n, bool hold)
|
||||
{
|
||||
assert(pos <= size()); // out_of_range debug.
|
||||
|
||||
size_t m = size() - pos; // can erase
|
||||
if (n >= m)
|
||||
size_ = pos; // all clear after pos
|
||||
else
|
||||
{
|
||||
size_ -= n;
|
||||
memmove(data_ + pos, data_ + pos + n, m - n);
|
||||
}
|
||||
|
||||
if (empty() && !hold)
|
||||
free();
|
||||
}
|
||||
|
||||
/*
|
||||
* after success increase_capacity : freespace() >= increase_size
|
||||
* if false : does not affect exist data
|
||||
*/
|
||||
template <typename BlockAllocator, unsigned MaxBlocks>
|
||||
inline bool BlockBuffer<BlockAllocator, MaxBlocks >::increase_capacity(size_t increase_size)
|
||||
{
|
||||
if (increase_size == 0)
|
||||
return true;
|
||||
|
||||
size_t newblock = block_;
|
||||
|
||||
size_t free = freespace();
|
||||
//if (increase_size > maxsize() - free) throw std::out_of_range("BlockBuffer out of range");
|
||||
if (free >= increase_size)
|
||||
return true;
|
||||
increase_size -= free;
|
||||
newblock += increase_size / allocator::requested_size;
|
||||
if ((increase_size % allocator::requested_size) > 0)
|
||||
newblock ++;
|
||||
|
||||
if (newblock > max_blocks) return false;
|
||||
char *newdata = (char *)(allocator::ordered_malloc(newblock));
|
||||
if (0 == newdata)
|
||||
return false;
|
||||
|
||||
if (block_ > 0)
|
||||
{ // copy old data and free old block
|
||||
memcpy(newdata, data_, size_);
|
||||
allocator::ordered_free(data_, block_);
|
||||
}
|
||||
|
||||
s_current_total_blocks += newblock - block_;
|
||||
if (s_current_total_blocks > s_peak_total_blocks)
|
||||
s_peak_total_blocks = s_current_total_blocks;
|
||||
|
||||
data_ = newdata;
|
||||
block_ = newblock;
|
||||
return true;
|
||||
}
|
||||
|
||||
template <typename Buffer>
|
||||
class ForwardBuffer
|
||||
{
|
||||
public:
|
||||
ForwardBuffer(Buffer &buffer) : buffer_(&buffer), position_(0) {}
|
||||
~ForwardBuffer() { if (buffer_) buffer_->erase(0, position_); }
|
||||
|
||||
char * data() { return buffer_->data() + position_; }
|
||||
size_t size() { return buffer_->size() - position_; }
|
||||
|
||||
void erase(size_t pos, size_t n)
|
||||
{
|
||||
if (pos == 0)
|
||||
{
|
||||
position_ += n;
|
||||
assert(position_ <= buffer_->size());
|
||||
}
|
||||
else
|
||||
buffer_->erase(position_ + pos, n);
|
||||
}
|
||||
bool empty() const { return buffer_->size() == position_; }
|
||||
void release() { buffer_ = NULL; }
|
||||
private:
|
||||
Buffer *buffer_; // release need pointer.
|
||||
size_t position_;
|
||||
};
|
||||
|
||||
} // namespace nbase
|
||||
|
||||
#endif // BASE_MEMORY_BLOCKBUFFER_H_
|
||||
@@ -0,0 +1,32 @@
|
||||
// a implementation of scoped handle which ensures the safe use of stardard handles
|
||||
|
||||
#ifndef BASE_MEMORY_SCOPED_STD_HANDLE_H_
|
||||
#define BASE_MEMORY_SCOPED_STD_HANDLE_H_
|
||||
|
||||
#include <stdio.h>
|
||||
#include "base/base_config.h"
|
||||
#include "base/base_export.h"
|
||||
#include "base/base_types.h"
|
||||
|
||||
namespace nbase
|
||||
{
|
||||
|
||||
// This class wraps the c library function free() in a class that can be
|
||||
// passed as a template argument to scoped_ptr_malloc below.
|
||||
class BASE_EXPORT DeleterFree {
|
||||
public:
|
||||
inline void operator()(void* x) const {
|
||||
free(x);
|
||||
}
|
||||
};
|
||||
|
||||
class BASE_EXPORT DeleterFileClose {
|
||||
public:
|
||||
inline void operator()(FILE * x) const {
|
||||
fclose(x);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
#endif // BASE_MEMORY_SCOPED_STD_HANDLE_H_
|
||||
@@ -0,0 +1,102 @@
|
||||
// The LazyInstance<Type> class manages a single instance of Type,
|
||||
// which will be lazily created on the first time it's accessed
|
||||
|
||||
#ifndef BASE_MEMORY_LAZY_INSTANCE_H_
|
||||
#define BASE_MEMORY_LAZY_INSTANCE_H_
|
||||
|
||||
#include "base/base_config.h"
|
||||
#include "base/third_party/chrome/atomicops.h"
|
||||
#include "base/thread/thread.h"
|
||||
#include "base/util/at_exit.h"
|
||||
//#include "base/log/log.h"
|
||||
|
||||
namespace nbase
|
||||
{
|
||||
|
||||
template<typename Type>
|
||||
class LazyInstance
|
||||
{
|
||||
public:
|
||||
|
||||
LazyInstance() : instance_(NULL)
|
||||
{
|
||||
base::subtle::NoBarrier_Store(&state_, kNone);
|
||||
}
|
||||
|
||||
~LazyInstance()
|
||||
{
|
||||
// |instance_| should be deleted by |OnExit|
|
||||
//DCHECK(instance_ == 0);
|
||||
}
|
||||
|
||||
Type& Get()
|
||||
{
|
||||
return *Pointer();
|
||||
}
|
||||
|
||||
Type* Pointer()
|
||||
{
|
||||
using namespace base::subtle;
|
||||
|
||||
if (Acquire_Load(&state_) != kCreated)
|
||||
{
|
||||
Atomic32 state =
|
||||
NoBarrier_CompareAndSwap(&state_, kNone, kCreating);
|
||||
if (state == kNone)
|
||||
{
|
||||
// we take the chance to create the instance
|
||||
instance_ = reinterpret_cast<AtomicWord>(new Type());
|
||||
AtExitManager::RegisterCallback(OnExit, this);
|
||||
Release_Store(&state_, kCreated);
|
||||
}
|
||||
else if (state != kCreated)
|
||||
{
|
||||
// wait, util another thread created the instance
|
||||
while (Acquire_Load(&state_) != kCreated)
|
||||
Thread::YieldThread();
|
||||
}
|
||||
}
|
||||
|
||||
return reinterpret_cast<Type *>(instance_);
|
||||
}
|
||||
|
||||
bool operator ==(Type *object) const
|
||||
{
|
||||
switch (base::subtle::NoBarrier_Load(&state_))
|
||||
{
|
||||
case kNone:
|
||||
return object == NULL;
|
||||
case kCreating:
|
||||
case kCreated:
|
||||
return instance_ == object;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private:
|
||||
enum
|
||||
{
|
||||
kNone = 0,
|
||||
kCreating,
|
||||
kCreated,
|
||||
kDeleting,
|
||||
};
|
||||
|
||||
static void OnExit(void *lazy_instance)
|
||||
{
|
||||
LazyInstance<Type>* me =
|
||||
reinterpret_cast<LazyInstance<Type>*>(lazy_instance);
|
||||
delete reinterpret_cast<Type*>(me->instance_);
|
||||
base::subtle::NoBarrier_Store(&me->instance_, 0);
|
||||
}
|
||||
|
||||
base::subtle::Atomic32 state_;
|
||||
base::subtle::AtomicWord instance_;
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(LazyInstance);
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // BASE_MEMORY_LAZY_INSTANCE_H_
|
||||
@@ -0,0 +1,76 @@
|
||||
#ifndef BASE_MEMORY_SINGLETON_H_
|
||||
#define BASE_MEMORY_SINGLETON_H_
|
||||
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include "../util/at_exit.h"
|
||||
namespace nbase
|
||||
{
|
||||
//饿汉模式
|
||||
#define SINGLETON_DEFINE(TypeName) \
|
||||
static TypeName* GetInstance() \
|
||||
{ \
|
||||
static TypeName type_instance; \
|
||||
return &type_instance; \
|
||||
} \
|
||||
\
|
||||
TypeName(const TypeName&) = delete; \
|
||||
TypeName& operator=(const TypeName&) = delete
|
||||
|
||||
// 懒汉模式
|
||||
//release_atexitmanager 是否由atexitmanager来释放
|
||||
template <typename TSingleton,bool release_atexitmanager = true>
|
||||
class Singleton
|
||||
{
|
||||
/* 如果TSingleton的构造函数是private / protected需要在类定义时引入此宏, 如下所示
|
||||
class Test : public Singleton<Test>
|
||||
{
|
||||
SingletonHideConstructor(Test)
|
||||
private:
|
||||
Test() = default;
|
||||
~Test() = default;
|
||||
}
|
||||
*/
|
||||
#define SingletonHideConstructor(TSingleton) \
|
||||
friend Singleton<typename TSingleton>::TSingletonDefaultDelete;\
|
||||
template<class _Ty,\
|
||||
class... _Types> inline\
|
||||
friend typename std::enable_if<!std::is_array<_Ty>::value,\
|
||||
std::unique_ptr<_Ty> >::type std::make_unique(_Types&&... _Args);
|
||||
private:
|
||||
friend TSingleton;
|
||||
using TSingletonDefaultDelete = std::default_delete<TSingleton>;
|
||||
using TSingletonPtr = std::unique_ptr<TSingleton, TSingletonDefaultDelete>;
|
||||
private:
|
||||
Singleton(void) = default;
|
||||
virtual ~Singleton() = default;
|
||||
Singleton(const Singleton&) = delete;
|
||||
Singleton(Singleton&&) = delete;
|
||||
Singleton& operator = (const Singleton&) = delete;
|
||||
public:
|
||||
static const TSingletonPtr& GetInstance()
|
||||
{
|
||||
static TSingletonPtr instance = nullptr;
|
||||
static std::once_flag oc;
|
||||
std::call_once(oc, [&] {
|
||||
instance = std::make_unique<TSingleton>();
|
||||
if (release_atexitmanager)
|
||||
{
|
||||
AtExitManager::RegisterCallback([&](void*) {
|
||||
instance.reset(nullptr);
|
||||
#if _MSC_VER > 1900
|
||||
oc._Opaque = 0;
|
||||
#else
|
||||
oc._Flag = 0;
|
||||
#endif
|
||||
|
||||
}, nullptr);
|
||||
}
|
||||
});
|
||||
return instance;
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // BASE_MEMORY_SINGLETON_H_
|
||||
Reference in new issue
Block a user