add event profiler, json profile dump, cleanup
This commit is contained in:
50
include/bounce/common/profiler.h
Normal file
50
include/bounce/common/profiler.h
Normal file
@@ -0,0 +1,50 @@
|
||||
/*
|
||||
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#ifndef B3_PROFILER_H
|
||||
#define B3_PROFILER_H
|
||||
|
||||
#include <bounce/common/settings.h>
|
||||
|
||||
#define B3_PROFILE(name) b3ProfileScope scope(name)
|
||||
|
||||
// You should implement this function to use your own profiler.
|
||||
bool b3PushProfileScope(const char* name);
|
||||
|
||||
// You should implement this function to use your own profiler.
|
||||
void b3PopProfileScope();
|
||||
|
||||
struct b3ProfileScope
|
||||
{
|
||||
b3ProfileScope(const char* name)
|
||||
{
|
||||
b = b3PushProfileScope(name);
|
||||
}
|
||||
|
||||
~b3ProfileScope()
|
||||
{
|
||||
if (b)
|
||||
{
|
||||
b3PopProfileScope();
|
||||
}
|
||||
}
|
||||
private:
|
||||
bool b;
|
||||
};
|
||||
|
||||
#endif
|
@@ -1,283 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#ifndef B3_OBJECT_ARRAY_H
|
||||
#define B3_OBJECT_ARRAY_H
|
||||
|
||||
#include <bounce/common/settings.h>
|
||||
|
||||
// An array for objects.
|
||||
template <typename T>
|
||||
class b3ObjectArray
|
||||
{
|
||||
public:
|
||||
const T& operator[](u32 i) const
|
||||
{
|
||||
B3_ASSERT(i < m_count);
|
||||
return m_elements[i];
|
||||
}
|
||||
|
||||
T& operator[](u32 i)
|
||||
{
|
||||
B3_ASSERT(i < m_count);
|
||||
return m_elements[i];
|
||||
}
|
||||
|
||||
const T* Get(u32 i) const
|
||||
{
|
||||
B3_ASSERT(i < m_count);
|
||||
return m_elements + i;
|
||||
}
|
||||
|
||||
T* Get(u32 i)
|
||||
{
|
||||
B3_ASSERT(i < m_count);
|
||||
return m_elements + i;
|
||||
}
|
||||
|
||||
const T* Elements() const
|
||||
{
|
||||
return m_elements;
|
||||
}
|
||||
|
||||
T* Elements()
|
||||
{
|
||||
return m_elements;
|
||||
}
|
||||
|
||||
void PushBack(const T& ele)
|
||||
{
|
||||
if (m_count == m_capacity)
|
||||
{
|
||||
// There is no capacity for one more element.
|
||||
T* oldElements = m_elements;
|
||||
m_capacity *= 2;
|
||||
m_elements = (T*)b3Alloc(m_capacity * sizeof(T));
|
||||
|
||||
for (u32 i = 0; i < m_count; ++i)
|
||||
{
|
||||
T* old = oldElements + i;
|
||||
T* e = m_elements + i;
|
||||
new (e) T(*old);
|
||||
old->~T();
|
||||
}
|
||||
|
||||
if (oldElements != m_localElements)
|
||||
{
|
||||
b3Free(oldElements);
|
||||
}
|
||||
}
|
||||
|
||||
B3_ASSERT(m_count < m_capacity);
|
||||
T* e = m_elements + m_count;
|
||||
new(e) T(ele);
|
||||
++m_count;
|
||||
}
|
||||
|
||||
void PopBack()
|
||||
{
|
||||
B3_ASSERT(m_count > 0);
|
||||
--m_count;
|
||||
}
|
||||
|
||||
const T& Back() const
|
||||
{
|
||||
B3_ASSERT(m_count > 0);
|
||||
return m_elements[m_count - 1];
|
||||
}
|
||||
|
||||
T& Back()
|
||||
{
|
||||
B3_ASSERT(m_count > 0);
|
||||
return m_elements[m_count - 1];
|
||||
}
|
||||
|
||||
u32 Count() const
|
||||
{
|
||||
return m_count;
|
||||
}
|
||||
|
||||
bool Empty() const
|
||||
{
|
||||
return m_count == 0;
|
||||
}
|
||||
|
||||
void Resize(u32 size)
|
||||
{
|
||||
if (m_capacity < size)
|
||||
{
|
||||
// There is no capacity for the requested size.
|
||||
T* oldElements = m_elements;
|
||||
m_capacity = 2 * size;
|
||||
m_elements = (T*)b3Alloc(m_capacity * sizeof(T));
|
||||
|
||||
// Construct and copy objects.
|
||||
for (u32 i = 0; i < m_count; ++i)
|
||||
{
|
||||
T* old = oldElements + i;
|
||||
T* e = m_elements + i;
|
||||
new (e) T(*old);
|
||||
old->~T();
|
||||
}
|
||||
|
||||
// Construct objects up to the requested size.
|
||||
for (u32 i = m_count; i < size; ++i)
|
||||
{
|
||||
T* e = m_elements + i;
|
||||
new (e) T();
|
||||
}
|
||||
|
||||
if (oldElements != m_localElements)
|
||||
{
|
||||
b3Free(oldElements);
|
||||
}
|
||||
|
||||
m_count = size;
|
||||
return;
|
||||
}
|
||||
|
||||
B3_ASSERT(m_capacity >= size);
|
||||
if (size < m_count)
|
||||
{
|
||||
// Destroy objects beyond the requested size.
|
||||
for (u32 i = size; i < m_count; ++i)
|
||||
{
|
||||
T* e = m_elements + i;
|
||||
e->~T();
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Construct objects up to the requested size.
|
||||
for (u32 i = m_count; i < size; ++i)
|
||||
{
|
||||
T* e = m_elements + i;
|
||||
new (e) T();
|
||||
}
|
||||
}
|
||||
|
||||
m_count = size;
|
||||
}
|
||||
|
||||
void Swap(const b3ObjectArray<T>& other)
|
||||
{
|
||||
if (m_elements == other.m_elements)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Destroy all objects.
|
||||
for (u32 i = 0; i < m_count; ++i)
|
||||
{
|
||||
T* e = m_elements + i;
|
||||
e->~T();
|
||||
}
|
||||
|
||||
// Ensure sufficient capacity for a copy.
|
||||
if (m_capacity < other.m_count)
|
||||
{
|
||||
if (m_elements != m_localElements)
|
||||
{
|
||||
b3Free(m_elements);
|
||||
}
|
||||
m_capacity = 2 * other.m_count;
|
||||
m_elements = (T*)b3Alloc(m_capacity * sizeof(T));
|
||||
}
|
||||
|
||||
// Copy.
|
||||
B3_ASSERT(m_capacity >= other.m_count);
|
||||
for (u32 i = 0; i < other.m_count; ++i)
|
||||
{
|
||||
T* e2 = other.m_elements + i;
|
||||
T* e1 = m_elements + i;
|
||||
new (e1) T(*e2);
|
||||
}
|
||||
m_count = other.m_count;
|
||||
}
|
||||
protected:
|
||||
b3ObjectArray(T* elements, u32 N)
|
||||
{
|
||||
B3_ASSERT(N > 0);
|
||||
m_localElements = elements;
|
||||
m_capacity = N;
|
||||
m_elements = m_localElements;
|
||||
m_count = 0;
|
||||
}
|
||||
|
||||
b3ObjectArray(const b3ObjectArray<T>& other)
|
||||
{
|
||||
Swap(other);
|
||||
}
|
||||
|
||||
~b3ObjectArray()
|
||||
{
|
||||
if (m_elements != m_localElements)
|
||||
{
|
||||
for (u32 i = 0; i < m_count; ++i)
|
||||
{
|
||||
T* e = m_elements + i;
|
||||
e->~T();
|
||||
}
|
||||
b3Free(m_elements);
|
||||
}
|
||||
}
|
||||
|
||||
void operator=(const b3ObjectArray<T>& other)
|
||||
{
|
||||
Swap(other);
|
||||
}
|
||||
|
||||
u32 m_capacity;
|
||||
T* m_elements;
|
||||
u32 m_count;
|
||||
T* m_localElements;
|
||||
};
|
||||
|
||||
template <typename T, u32 N>
|
||||
class b3StackObjectArray : public b3ObjectArray<T>
|
||||
{
|
||||
public:
|
||||
b3StackObjectArray<T, N>() : b3ObjectArray<T>(m_stackElements, N)
|
||||
{
|
||||
}
|
||||
|
||||
b3StackObjectArray<T, N>(const b3StackObjectArray<T, N>& other) : b3ObjectArray<T>(other)
|
||||
{
|
||||
}
|
||||
|
||||
b3StackObjectArray<T, N>(const b3ObjectArray<T>& other) : b3ObjectArray<T>(other)
|
||||
{
|
||||
}
|
||||
|
||||
void operator=(const b3StackObjectArray<T, N>& other)
|
||||
{
|
||||
Swap(other);
|
||||
}
|
||||
|
||||
void operator=(const b3ObjectArray<T>& other)
|
||||
{
|
||||
Swap(other);
|
||||
}
|
||||
|
||||
protected:
|
||||
//@todo
|
||||
// u8 m_bytes[N * sizeof(T)];
|
||||
T m_stackElements[N];
|
||||
};
|
||||
|
||||
#endif
|
114
include/bounce/common/template/queue.h
Normal file
114
include/bounce/common/template/queue.h
Normal file
@@ -0,0 +1,114 @@
|
||||
/*
|
||||
* Copyright (c) 2016-2016 Irlan Robson http://www.irlan.net
|
||||
*
|
||||
* This software is provided 'as-is', without any express or implied
|
||||
* warranty. In no event will the authors be held liable for any damages
|
||||
* arising from the use of this software.
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software. If you use this software
|
||||
* in a product, an acknowledgment in the product documentation would be
|
||||
* appreciated but is not required.
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
*/
|
||||
|
||||
#ifndef B3_QUEUE_H
|
||||
#define B3_QUEUE_H
|
||||
|
||||
#include <bounce/common/settings.h>
|
||||
|
||||
// A bounded FIFO queue with a fixed capacity of N.
|
||||
// The capacity must be greater than one.
|
||||
// The elements are allocated on the stack.
|
||||
template <typename T, u32 N>
|
||||
class b3BoundedQueue
|
||||
{
|
||||
public:
|
||||
b3BoundedQueue()
|
||||
{
|
||||
B3_ASSERT(N > 1);
|
||||
m_count = 0;
|
||||
m_back = 0;
|
||||
m_front = 0;
|
||||
}
|
||||
|
||||
~b3BoundedQueue()
|
||||
{
|
||||
}
|
||||
|
||||
T* Push(const T& ele)
|
||||
{
|
||||
if ((m_count + 1) == N)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
B3_ASSERT(m_back < N);
|
||||
T* e = m_elements + m_back;
|
||||
*e = ele;
|
||||
|
||||
m_back = (m_back + 1) % N;
|
||||
B3_ASSERT(m_back != m_front);
|
||||
++m_count;
|
||||
return e;
|
||||
}
|
||||
|
||||
bool Pop()
|
||||
{
|
||||
if (m_front == m_back)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
B3_ASSERT(m_front != m_back);
|
||||
m_front = (m_front + 1) % N;
|
||||
B3_ASSERT(m_count > 0);
|
||||
--m_count;
|
||||
return true;
|
||||
}
|
||||
|
||||
const T& Front() const
|
||||
{
|
||||
B3_ASSERT(m_count > 0);
|
||||
return m_elements[m_front];
|
||||
}
|
||||
|
||||
T& Front()
|
||||
{
|
||||
B3_ASSERT(m_count > 0);
|
||||
return m_elements[m_front];
|
||||
}
|
||||
|
||||
const T& Back() const
|
||||
{
|
||||
B3_ASSERT(m_count > 0);
|
||||
return m_elements[m_back > 0 ? m_back - 1 : N - 1];
|
||||
}
|
||||
|
||||
T& Back()
|
||||
{
|
||||
B3_ASSERT(m_count > 0);
|
||||
return m_elements[m_back > 0 ? m_back - 1 : N - 1];
|
||||
}
|
||||
|
||||
u32 Count() const
|
||||
{
|
||||
return m_count;
|
||||
}
|
||||
|
||||
bool IsEmpty() const
|
||||
{
|
||||
return m_back == m_front;
|
||||
}
|
||||
private:
|
||||
T m_elements[N];
|
||||
u32 m_count;
|
||||
u32 m_back;
|
||||
u32 m_front;
|
||||
};
|
||||
|
||||
#endif
|
@@ -21,7 +21,9 @@
|
||||
|
||||
#include <bounce/common/settings.h>
|
||||
|
||||
// A growable stack for plain-old-data (POD).
|
||||
// A growable LIFO stack with an initial capacity of N.
|
||||
// If the stack capacity exceeds the initial capacity, the heap
|
||||
// is used to increase the capacity of the stack.
|
||||
template <typename T, u32 N>
|
||||
class b3Stack
|
||||
{
|
||||
|
Reference in New Issue
Block a user