0
0
mirror of https://github.com/yse/easy_profiler.git synced 2025-01-14 00:27:55 +08:00

(profiler core) The right way of avoiding static analyzer warnings ^_^ No need to insert unnecessary checks

This commit is contained in:
Victor Zarubkin 2016-08-21 14:46:16 +03:00
parent e40da7921d
commit 3e1b8ce46f
3 changed files with 26 additions and 28 deletions

View File

@ -55,7 +55,7 @@ Block will be automatically completed by destructor
static const profiler::BlockSourceInfo TOKEN_CONCATENATE(unique_profiler_source_name_,__LINE__)(__FILE__, __LINE__);\
profiler::Block TOKEN_CONCATENATE(unique_profiler_mark_name_,__LINE__)(name,profiler::colors::Clay,profiler::BLOCK_TYPE_BLOCK,\
TOKEN_CONCATENATE(unique_profiler_source_name_,__LINE__).id());\
profiler::beginBlock(&TOKEN_CONCATENATE(unique_profiler_mark_name_,__LINE__));
profiler::beginBlock(TOKEN_CONCATENATE(unique_profiler_mark_name_,__LINE__));
/** Macro of beginning of block with custom name and custom identification
@ -80,7 +80,7 @@ Block will be automatically completed by destructor
static const profiler::BlockSourceInfo TOKEN_CONCATENATE(unique_profiler_source_name_,__LINE__)(__FILE__, __LINE__);\
profiler::Block TOKEN_CONCATENATE(unique_profiler_mark_name_,__LINE__)(name,block_group,profiler::BLOCK_TYPE_BLOCK,\
TOKEN_CONCATENATE(unique_profiler_source_name_,__LINE__).id());\
profiler::beginBlock(&TOKEN_CONCATENATE(unique_profiler_mark_name_,__LINE__));
profiler::beginBlock(TOKEN_CONCATENATE(unique_profiler_mark_name_,__LINE__));
/** Macro of beginning of function block with default identification
@ -140,13 +140,13 @@ void foo()
static const profiler::BlockSourceInfo TOKEN_CONCATENATE(unique_profiler_source_name_,__LINE__)(__FILE__, __LINE__);\
profiler::Block TOKEN_CONCATENATE(unique_profiler_mark_name_,__LINE__)(name,0,profiler::BLOCK_TYPE_EVENT,\
TOKEN_CONCATENATE(unique_profiler_source_name_,__LINE__).id());\
profiler::beginBlock(&TOKEN_CONCATENATE(unique_profiler_mark_name_,__LINE__));
profiler::beginBlock(TOKEN_CONCATENATE(unique_profiler_mark_name_,__LINE__));
#define PROFILER_ADD_EVENT_GROUPED(name,block_group)\
static const profiler::BlockSourceInfo TOKEN_CONCATENATE(unique_profiler_source_name_,__LINE__)(__FILE__, __LINE__);\
profiler::Block TOKEN_CONCATENATE(unique_profiler_mark_name_,__LINE__)(name,block_group,profiler::BLOCK_TYPE_EVENT,\
TOKEN_CONCATENATE(unique_profiler_source_name_,__LINE__).id());\
profiler::beginBlock(&TOKEN_CONCATENATE(unique_profiler_mark_name_,__LINE__));
profiler::beginBlock(TOKEN_CONCATENATE(unique_profiler_mark_name_,__LINE__));
/** Macro enabling profiler
\ingroup profiler
@ -254,7 +254,7 @@ namespace profiler
class BlockSourceInfo;
extern "C"{
void PROFILER_API beginBlock(Block* _block);
void PROFILER_API beginBlock(Block& _block);
void PROFILER_API endBlock();
void PROFILER_API setEnabled(bool isEnable);
unsigned int PROFILER_API dumpBlocksToFile(const char* filename);
@ -363,7 +363,7 @@ namespace profiler
static SerializedBlock* create(const Block &block, uint64_t& memory_size);
static void destroy(SerializedBlock* that);
SerializedBlock(const profiler::Block* block, uint16_t name_length);
SerializedBlock(const profiler::Block& block, uint16_t name_length);
SerializedBlock(const SerializedBlock&) = delete;
SerializedBlock& operator = (const SerializedBlock&) = delete;

View File

@ -20,7 +20,7 @@ extern "C"{
{
MANAGER.setEnabled(isEnable);
}
void PROFILER_API beginBlock(Block* _block)
void PROFILER_API beginBlock(Block& _block)
{
MANAGER.beginBlock(_block);
}
@ -41,7 +41,7 @@ SerializedBlock* SerializedBlock::create(const Block& block, uint64_t& memory_si
auto name_length = static_cast<uint16_t>(strlen(block.getName()));
auto size = static_cast<uint16_t>(sizeof(BaseBlockData) + name_length + 1);
auto data = ::new char[size];
::new (static_cast<void*>(data)) SerializedBlock(&block, name_length);
::new (static_cast<void*>(data)) SerializedBlock(block, name_length);
memory_size += size;
return reinterpret_cast<SerializedBlock*>(data);
}
@ -51,11 +51,11 @@ void SerializedBlock::destroy(SerializedBlock* that)
::delete[] reinterpret_cast<char*>(that);
}
SerializedBlock::SerializedBlock(const Block* block, uint16_t name_length)
: BaseBlockData(*block)
SerializedBlock::SerializedBlock(const Block& block, uint16_t name_length)
: BaseBlockData(block)
{
auto name = const_cast<char*>(getName());
strncpy(name, block->getName(), name_length);
strncpy(name, block.getName(), name_length);
name[name_length] = 0;
}
@ -91,16 +91,17 @@ ProfileManager& ProfileManager::instance()
return m_profileManager;
}
void ProfileManager::beginBlock(Block* _block)
void ProfileManager::beginBlock(Block& _block)
{
if (!m_isEnabled || !_block)
if (!m_isEnabled)
return;
if (BLOCK_TYPE_BLOCK == _block->getType()){
if (BLOCK_TYPE_BLOCK == _block.getType()){
guard_lock_t lock(m_spin);
m_openedBracketsMap[_block->getThreadId()].push(_block);
m_openedBracketsMap[_block.getThreadId()].emplace(_block);
}
else{
_internalInsertBlock(*_block);
_internalInsertBlock(_block);
}
}
@ -118,14 +119,10 @@ void ProfileManager::endBlock()
if (stackOfOpenedBlocks.empty())
return;
Block* lastBlock = stackOfOpenedBlocks.top();
if (lastBlock )
{
if(!lastBlock->isFinished())
lastBlock->finish();
_internalInsertBlock(*lastBlock);
}
Block& lastBlock = stackOfOpenedBlocks.top();
if(!lastBlock.isFinished())
lastBlock.finish();
_internalInsertBlock(lastBlock);
stackOfOpenedBlocks.pop();
}
@ -135,7 +132,7 @@ void ProfileManager::setEnabled(bool isEnable)
m_isEnabled = isEnable;
}
void ProfileManager::_internalInsertBlock(profiler::Block& _block)
void ProfileManager::_internalInsertBlock(const profiler::Block& _block)
{
guard_lock_t lock(m_storedSpin);
m_blocks.emplace_back(SerializedBlock::create(_block, m_blocksMemorySize));

View File

@ -28,6 +28,7 @@ along with this program.If not, see <http://www.gnu.org/licenses/>.
#include <list>
#include <set>
#include <vector>
#include <functional>
#ifdef WIN32
#include <Windows.h>
@ -54,7 +55,7 @@ class ProfileManager
bool m_isEnabled = false;
typedef std::stack<profiler::Block*> stack_of_blocks_t;
typedef std::stack<::std::reference_wrapper<profiler::Block> > stack_of_blocks_t;
typedef std::map<size_t, stack_of_blocks_t> map_of_threads_stacks;
typedef std::set<size_t> set_of_thread_id;
typedef std::vector<profiler::SourceBlock> sources;
@ -65,7 +66,7 @@ class ProfileManager
profiler::spin_lock m_storedSpin;
typedef profiler::guard_lock<profiler::spin_lock> guard_lock_t;
void _internalInsertBlock(profiler::Block &_block);
void _internalInsertBlock(const profiler::Block &_block);
sources m_sources;
@ -81,7 +82,7 @@ public:
static ProfileManager& instance();
~ProfileManager();
void beginBlock(profiler::Block* _block);
void beginBlock(profiler::Block& _block);
void endBlock();
void setEnabled(bool isEnable);
unsigned int dumpBlocksToFile(const char* filename);