Skip to content

Remove std allocation #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: unity
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions crnlib/allocator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#pragma once
#include "crn_mem.h"

namespace crnlib
{
template <class T>
class allocator
{
public:
using value_type = T;
template <class U> allocator(allocator<U> const&) noexcept {}
allocator() noexcept {};

value_type* allocate(std::size_t n)
{
return static_cast<T*>(crnlib_malloc(n * sizeof(T)));
}

void deallocate(value_type* p, std::size_t) noexcept
{
crnlib_free(p);
}
};

template <class T, class U>
bool operator==(allocator<T> const&, allocator<U> const&) noexcept
{
return true;
}

template <class T, class U>
bool operator!=(allocator<T> const& x, allocator<U> const& y) noexcept
{
return !(x == y);
}
}
6 changes: 3 additions & 3 deletions crnlib/crn_dxt_hc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -716,7 +716,7 @@ void dxt_hc::determine_color_endpoints() {
m_pTask_pool->queue_task(&Node::sort_task, i, &nodes[i]);
m_pTask_pool->join();

std::priority_queue<Node> queue;
std::priority_queue<Node, std::vector<Node, allocator<Node>>> queue;
for (uint i = 0; i < nodes.size(); i++)
queue.push(nodes[i]);

Expand Down Expand Up @@ -1055,7 +1055,7 @@ void dxt_hc::create_color_selector_codebook() {
m_pTask_pool->queue_task(&SelectorNode::sort_task, i, &nodes[i]);
m_pTask_pool->join();

std::priority_queue<SelectorNode> queue;
std::priority_queue<SelectorNode, std::vector<SelectorNode, allocator<SelectorNode>>> queue;
for (uint i = 0; i < nodes.size(); i++)
queue.push(nodes[i]);

Expand Down Expand Up @@ -1213,7 +1213,7 @@ void dxt_hc::create_alpha_selector_codebook() {
m_pTask_pool->queue_task(&SelectorNode::sort_task, i, &nodes[i]);
m_pTask_pool->join();

std::priority_queue<SelectorNode> queue;
std::priority_queue<SelectorNode, std::vector<SelectorNode, allocator<SelectorNode>>> queue;
for (uint i = 0; i < nodes.size(); i++)
queue.push(nodes[i]);

Expand Down
10 changes: 6 additions & 4 deletions crnlib/crn_tree_clusterizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "crn_matrix.h"
#include "crn_threading.h"
#include <queue>
#include "allocator.h"

namespace crnlib {
template <typename VectorType>
Expand Down Expand Up @@ -33,7 +34,7 @@ class tree_clusterizer {

void split_alternative_node_task(uint64, void* pData_ptr) {
split_alternative_node_task_params* pParams = (split_alternative_node_task_params*)pData_ptr;
std::priority_queue<NodeInfo> node_queue;
std::priority_queue<NodeInfo, std::vector<NodeInfo, allocator<NodeInfo>>> node_queue;
uint begin_node = pParams->alternative_node, end_node = begin_node, splits = 0;

m_nodes[end_node] = m_nodes[pParams->main_node];
Expand Down Expand Up @@ -78,7 +79,7 @@ class tree_clusterizer {
root.m_variance = (float)(ttsum - (root.m_centroid.dot(root.m_centroid) / root.m_total_weight));
root.m_centroid *= (1.0f / root.m_total_weight);

std::priority_queue<NodeInfo> node_queue;
std::priority_queue<NodeInfo, std::vector<NodeInfo, allocator<NodeInfo>>> node_queue;
uint begin_node = 0, end_node = begin_node, splits = 0;
m_nodes[end_node] = root;
node_queue.push(NodeInfo(end_node, root.m_variance));
Expand All @@ -89,7 +90,7 @@ class tree_clusterizer {
while (splits < max_splits && node_queue.size() != num_tasks && split_node(node_queue, end_node, pTask_pool))
splits++;
if (node_queue.size() == num_tasks) {
std::priority_queue<NodeInfo> alternative_node_queue = node_queue;
std::priority_queue<NodeInfo, std::vector<NodeInfo, allocator<NodeInfo>>> alternative_node_queue = node_queue;
uint alternative_node = max_splits << 1, alternative_max_splits = max_splits / num_tasks;
crnlib::vector<split_alternative_node_task_params> params(num_tasks);
for (uint task = 0; !alternative_node_queue.empty(); alternative_node_queue.pop(), alternative_node += alternative_max_splits << 1, task++) {
Expand Down Expand Up @@ -193,7 +194,8 @@ class tree_clusterizer {
}
}

bool split_node(std::priority_queue<NodeInfo>& node_queue, uint& end_node, task_pool* pTask_pool = 0) {
bool split_node(std::priority_queue<NodeInfo, std::vector<NodeInfo, allocator<NodeInfo>>>& node_queue,
uint& end_node, task_pool* pTask_pool = 0) {
if (node_queue.empty())
return false;

Expand Down
1 change: 1 addition & 0 deletions crnlib/crnlib.2010.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,7 @@
<ClInclude Include="crn_dxt_image.h" />
<ClInclude Include="crn_ktx_texture.h" />
<ClInclude Include="crn_mipmapped_texture.h" />
<ClInclude Include="allocator.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
Expand Down