/
sdi-tool
/
SDI
Обзор
Документация
Войти
/
sdi-tool
/
SDI
Код
Пакеты
0
Релизы
0
Аналитика
dev
src/utils/TempAllocator.cpp
92 строки
2 KB
WindR
Refactor logging, add file downloader
07 ноя 2024, 18:11
07 ноя 2024, 18:11
c762bea
Код
Авторство
О чём код?
#include "BaseUtil.h" /* Temp allocator is meant for allocating temporary values that don't need to outlive this stack frame. It's an alternative to using various AutoFree* classes. It's a very fast bump allocator. You must periodically call ResetTempAllocator() to free memory used by allocator. A good place to do it is at the beginning of window message loop. */ thread_local static PoolAllocator* gTempAllocator = nullptr; // forbid inlining to not blow out the size of callers NO_INLINE Allocator* GetTempAllocator() { if (gTempAllocator) { return gTempAllocator; } gTempAllocator = new PoolAllocator(); // this can be large because 64k is nothing and it's used frequently gTempAllocator->minBlockSize = 64 * 1024; return gTempAllocator; } void DestroyTempAllocator() { delete gTempAllocator; gTempAllocator = nullptr; } void ResetTempAllocator() { if (gTempAllocator) { gTempAllocator->Reset(true); } } namespace str { TempStr DupTemp(const char* s, size_t cb) { return str::Dup(GetTempAllocator(), s, cb); } TempWStr DupTemp(const WCHAR* s, size_t cch) { return str::Dup(GetTempAllocator(), s, cch); } TempStr JoinTemp(const char* s1, const char* s2, const char* s3) { return Join(GetTempAllocator(), s1, s2, s3); } TempWStr JoinTemp(const WCHAR* s1, const WCHAR* s2, const WCHAR* s3) { return Join(GetTempAllocator(), s1, s2, s3); } TempStr FormatTemp(const char* fmt, ...) { va_list args; va_start(args, fmt); char* res = FmtVWithAllocator(GetTempAllocator(), fmt, args); va_end(args); return res; } } // namespace str TempStr ToUtf8Temp(const WCHAR* s, size_t cch) { if (!s) { ReportIf((int)cch > 0); return nullptr; } return strconv::WStrToUtf8(s, cch, GetTempAllocator()); } TempWStr ToWStrTemp(const char* s, size_t cb) { if (!s) { ReportIf((int)cb > 0); return nullptr; } return strconv::Utf8ToWStr(s, cb, GetTempAllocator()); } // handles embedded 0 in the string TempWStr ToWStrTemp(const str::Str& str) { if (str.IsEmpty()) { return nullptr; } char* s = str.CStr(); size_t cb = str.Size(); return strconv::Utf8ToWStr(s, cb, GetTempAllocator()); }