Skip to content

Commit

Permalink
Removed use of Allocator::setMemoryTracking(..)
Browse files Browse the repository at this point in the history
  • Loading branch information
robertosfield committed Jun 24, 2024
1 parent 9d3ae99 commit b77494d
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 31 deletions.
1 change: 0 additions & 1 deletion examples/app/vsgviewer/vsgviewer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ int main(int argc, char** argv)
windowTraits->apiDumpLayer = arguments.read({"--api", "-a"});
windowTraits->synchronizationLayer = arguments.read("--sync");
bool reportAverageFrameRate = arguments.read("--fps");
if (int mt = 0; arguments.read({"--memory-tracking", "--mt"}, mt)) vsg::Allocator::instance()->setMemoryTracking(mt);
if (arguments.read("--double-buffer")) windowTraits->swapchainPreferences.imageCount = 2;
if (arguments.read("--triple-buffer")) windowTraits->swapchainPreferences.imageCount = 3; // default
if (arguments.read("--IMMEDIATE")) { windowTraits->swapchainPreferences.presentMode = VK_PRESENT_MODE_IMMEDIATE_KHR; }
Expand Down
1 change: 0 additions & 1 deletion examples/commands/vsgocclusionquery/vsgocclusionquery.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ int main(int argc, char** argv)
windowTraits->windowTitle = "vsgocclusionquery";
windowTraits->debugLayer = arguments.read({"--debug", "-d"});
windowTraits->apiDumpLayer = arguments.read({"--api", "-a"});
if (int mt = 0; arguments.read({"--memory-tracking", "--mt"}, mt)) vsg::Allocator::instance()->setMemoryTracking(mt);
if (arguments.read("--double-buffer")) windowTraits->swapchainPreferences.imageCount = 2;
if (arguments.read("--triple-buffer")) windowTraits->swapchainPreferences.imageCount = 3; // default
if (arguments.read("--IMMEDIATE")) windowTraits->swapchainPreferences.presentMode = VK_PRESENT_MODE_IMMEDIATE_KHR;
Expand Down
1 change: 0 additions & 1 deletion examples/commands/vsgtimestamps/vsgtimestamps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ int main(int argc, char** argv)
windowTraits->windowTitle = "vsgtimestamps";
windowTraits->debugLayer = arguments.read({"--debug", "-d"});
windowTraits->apiDumpLayer = arguments.read({"--api", "-a"});
if (int mt = 0; arguments.read({"--memory-tracking", "--mt"}, mt)) vsg::Allocator::instance()->setMemoryTracking(mt);
if (arguments.read("--double-buffer")) windowTraits->swapchainPreferences.imageCount = 2;
if (arguments.read("--triple-buffer")) windowTraits->swapchainPreferences.imageCount = 3; // default
if (arguments.read("--IMMEDIATE")) windowTraits->swapchainPreferences.presentMode = VK_PRESENT_MODE_IMMEDIATE_KHR;
Expand Down
45 changes: 18 additions & 27 deletions examples/core/vsgallocator/vsgallocator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,52 +9,44 @@
#include <iostream>
#include <thread>

class CustomAllocator : public vsg::Allocator
class StdAllocator : public vsg::Allocator
{
public:
CustomAllocator(std::unique_ptr<Allocator> in_nestedAllocator = {}) :
StdAllocator(std::unique_ptr<Allocator> in_nestedAllocator = {}) :
vsg::Allocator(std::move(in_nestedAllocator))
{
if (memoryTracking & vsg::MEMORY_TRACKING_REPORT_ACTIONS)
{
std::cout << "CustomAllocator()" << this << std::endl;
}
}

~CustomAllocator()
~StdAllocator()
{
if (memoryTracking & vsg::MEMORY_TRACKING_REPORT_ACTIONS)
{
std::cout << "~CustomAllocator() " << this << std::endl;
}
}

void report(std::ostream& out) const override
{
std::cout << "CustomAllocator::report() " << allocatorMemoryBlocks.size() << std::endl;
vsg::Allocator::report(out);
out << "StdAllocator::report() " << std::endl;
}

void* allocate(std::size_t size, vsg::AllocatorAffinity allocatorAffinity = vsg::ALLOCATOR_AFFINITY_OBJECTS) override
void* allocate(std::size_t size, vsg::AllocatorAffinity) override
{
void* ptr = Allocator::allocate(size, allocatorAffinity);
if (memoryTracking & vsg::MEMORY_TRACKING_REPORT_ACTIONS)
{
std::cout << "CustomAllocator::allocate(" << size << ", " << allocatorAffinity << ") ptr = " << ptr << std::endl;
}
return ptr;
return operator new (size); //, std::align_val_t{default_alignment});
}

bool deallocate(void* ptr, std::size_t size) override
{
if (memoryTracking & vsg::MEMORY_TRACKING_REPORT_ACTIONS)
{
std::cout << "CustomAllocator::deallocate(" << ptr << ")" << std::endl;
}
return Allocator::deallocate(ptr, size);
if (nestedAllocator && nestedAllocator->deallocate(ptr, size)) return true;

operator delete (ptr);//, std::align_val_t{default_alignment});
return true;
}

size_t deleteEmptyMemoryBlocks() override { return 0; }
size_t totalAvailableSize() const override { return 0; }
size_t totalReservedSize() const override { return 0; }
size_t totalMemorySize() const override { return 0; }
void setBlockSize(vsg::AllocatorAffinity, size_t) {}
};


struct SceneStatistics : public vsg::Inherit<vsg::ConstVisitor, SceneStatistics>
{
std::map<const char*, size_t> objectCounts;
Expand All @@ -77,8 +69,7 @@ int main(int argc, char** argv)
vsg::CommandLine arguments(&argc, argv);

// Allocaotor related command line settings
if (arguments.read("--custom")) vsg::Allocator::instance().reset(new CustomAllocator(std::move(vsg::Allocator::instance())));
if (int mt; arguments.read({"--memory-tracking", "--mt"}, mt)) vsg::Allocator::instance()->setMemoryTracking(mt);
if (arguments.read("--std")) vsg::Allocator::instance().reset(new StdAllocator(std::move(vsg::Allocator::instance())));
if (int type; arguments.read("--allocator", type)) vsg::Allocator::instance()->allocatorType = vsg::AllocatorType(type);
if (size_t objectsBlockSize; arguments.read("--objects", objectsBlockSize)) vsg::Allocator::instance()->setBlockSize(vsg::ALLOCATOR_AFFINITY_OBJECTS, objectsBlockSize);
if (size_t nodesBlockSize; arguments.read("--nodes", nodesBlockSize)) vsg::Allocator::instance()->setBlockSize(vsg::ALLOCATOR_AFFINITY_NODES, nodesBlockSize);
Expand Down
1 change: 0 additions & 1 deletion examples/nodes/vsgannotation/vsgannotation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ int main(int argc, char** argv)
windowTraits->apiDumpLayer = arguments.read({"--api", "-a"});
windowTraits->synchronizationLayer = arguments.read("--sync");
bool reportAverageFrameRate = arguments.read("--fps");
if (int mt = 0; arguments.read({"--memory-tracking", "--mt"}, mt)) vsg::Allocator::instance()->setMemoryTracking(mt);
if (arguments.read("--double-buffer")) windowTraits->swapchainPreferences.imageCount = 2;
if (arguments.read("--triple-buffer")) windowTraits->swapchainPreferences.imageCount = 3; // default
if (arguments.read("--IMMEDIATE")) { windowTraits->swapchainPreferences.presentMode = VK_PRESENT_MODE_IMMEDIATE_KHR; }
Expand Down
68 changes: 68 additions & 0 deletions examples/nodes/vsggroups/vsggroups.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,15 +180,83 @@ std::shared_ptr<experimental::SharedPtrNode> createSharedPtrQuadTree(unsigned in
return t;
}


// consider tcmalloc? https://goog-perftools.sourceforge.net/doc/tcmalloc.html
// consider Alloc https://www.codeproject.com/Articles/1084801/Replace-malloc-free-with-a-Fast-Fixed-Block-Memory
class StdAllocator : public vsg::Allocator
{
public:
StdAllocator(std::unique_ptr<Allocator> in_nestedAllocator = {}) :
vsg::Allocator(std::move(in_nestedAllocator))
{
}

~StdAllocator()
{
}

void report(std::ostream& out) const override
{
out << "StdAllocator::report() " << std::endl;
}

void* allocate(std::size_t size, vsg::AllocatorAffinity) override
{
return operator new (size); //, std::align_val_t{default_alignment});
}

bool deallocate(void* ptr, std::size_t size) override
{
if (nestedAllocator && nestedAllocator->deallocate(ptr, size)) return true;

operator delete (ptr);//, std::align_val_t{default_alignment});
return true;
}

size_t deleteEmptyMemoryBlocks() override { return 0; }
size_t totalAvailableSize() const override { return 0; }
size_t totalReservedSize() const override { return 0; }
size_t totalMemorySize() const override { return 0; }
void setBlockSize(vsg::AllocatorAffinity, size_t) {}
};

const size_t KB = 1024;
const size_t MB = 1024 * KB;
const size_t GB = 1024 * MB;

struct Units
{
Units(size_t v) : value(v) {}

size_t value;
};

std::ostream& operator<<(std::ostream& out, const Units& size)
{
if (size.value>GB) out << static_cast<double>(size.value)/static_cast<double>(GB) << " gigabytes";
else if (size.value>MB) out << static_cast<double>(size.value)/static_cast<double>(MB) << " megabytes";
else if (size.value>KB) out << static_cast<double>(size.value)/static_cast<double>(KB) <<" kilobytes";
else out << size.value<<" bytes";
return out;
}
int main(int argc, char** argv)
{
vsg::CommandLine arguments(&argc, argv);
if (arguments.read("--std")) vsg::Allocator::instance().reset(new StdAllocator(std::move(vsg::Allocator::instance())));

auto numLevels = arguments.value(11u, {"-l", "--levels"});
auto numTraversals = arguments.value(10u, {"-t", "--traversals"});
auto type = arguments.value(std::string("vsg::Group"), "--type");
auto quiet = arguments.read("-q");
auto inputFilename = arguments.value<vsg::Path>("", "-i");
auto outputFilename = arguments.value<vsg::Path>("", "-o");

size_t unit = arguments.value<size_t>(MB, "--unit");
if (int allocatorType; arguments.read("--allocator", allocatorType)) vsg::Allocator::instance()->allocatorType = vsg::AllocatorType(allocatorType);
if (size_t objectsBlockSize; arguments.read("--objects", objectsBlockSize)) vsg::Allocator::instance()->setBlockSize(vsg::ALLOCATOR_AFFINITY_OBJECTS, objectsBlockSize * unit);
if (size_t nodesBlockSize; arguments.read("--nodes", nodesBlockSize)) vsg::Allocator::instance()->setBlockSize(vsg::ALLOCATOR_AFFINITY_NODES, nodesBlockSize * unit);
if (size_t dataBlockSize; arguments.read("--data", dataBlockSize)) vsg::Allocator::instance()->setBlockSize(vsg::ALLOCATOR_AFFINITY_DATA, dataBlockSize * unit);

vsg::ref_ptr<vsg::RecordTraversal> vsg_recordTraversal(arguments.read("-d") ? new vsg::RecordTraversal : nullptr);
vsg::ref_ptr<VsgConstVisitor> vsg_ConstVisitor(arguments.read("-c") ? new VsgConstVisitor : nullptr);
if (arguments.errors()) return arguments.writeErrorMessages(std::cerr);
Expand Down

0 comments on commit b77494d

Please sign in to comment.