Skip to content

Commit

Permalink
performance gain
Browse files Browse the repository at this point in the history
  • Loading branch information
Boris committed Aug 23, 2023
1 parent c6730ab commit c5ea24f
Show file tree
Hide file tree
Showing 34 changed files with 1,138 additions and 355 deletions.
8 changes: 8 additions & 0 deletions Examples/Xilinx/ZYBOZ7/DemoRTSHA/.cproject
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@
<listOptionValue builtIn="false" value="&quot;${workspace_loc:/DemoRTSHA/../../../../Include}&quot;"/>
<listOptionValue builtIn="false" value="../../include"/>
</option>
<option id="xilinx.gnu.compiler.symbols.defined.745630054" name="Defined symbols (-D)" superClass="xilinx.gnu.compiler.symbols.defined" valueType="definedSymbols">
<listOptionValue builtIn="false" value="RTSHA_USE_CACHE=1"/>
<listOptionValue builtIn="false" value="XIL_ZYNQ_CACHE=1"/>
</option>
<inputType id="xilinx.gnu.armv7.cxx.compiler.input.686576199" name="C++ source files" superClass="xilinx.gnu.armv7.cxx.compiler.input"/>
</tool>
<tool id="xilinx.gnu.armv7.toolchain.archiver.532177058" name="ARM v7 archiver" superClass="xilinx.gnu.armv7.toolchain.archiver"/>
Expand Down Expand Up @@ -123,6 +127,10 @@
<listOptionValue builtIn="false" value="../Include"/>
<listOptionValue builtIn="false" value="../../../../../include"/>
</option>
<option id="xilinx.gnu.compiler.symbols.defined.1666710269" name="Defined symbols (-D)" superClass="xilinx.gnu.compiler.symbols.defined" valueType="definedSymbols">
<listOptionValue builtIn="false" value="RTSHA_USE_CACHE=1"/>
<listOptionValue builtIn="false" value="XIL_ZYNQ_CACHE=1"/>
</option>
<inputType id="xilinx.gnu.armv7.cxx.compiler.input.1335291458" name="C++ source files" superClass="xilinx.gnu.armv7.cxx.compiler.input"/>
</tool>
<tool id="xilinx.gnu.armv7.toolchain.archiver.1337757913" name="ARM v7 archiver" superClass="xilinx.gnu.armv7.toolchain.archiver"/>
Expand Down
1 change: 0 additions & 1 deletion Examples/Xilinx/ZYBOZ7/DemoRTSHA/src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ int main()

init_platform();


_heap = NULL;

HeapCallbacksStruct callbacks;
Expand Down
2 changes: 1 addition & 1 deletion ide/vs2022/RTSHALibrary.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@
<ClInclude Include="..\..\include\BigMemoryPage.h" />
<ClInclude Include="..\..\include\errors.h" />
<ClInclude Include="..\..\include\FastPlusAllocator.h" />
<ClInclude Include="..\..\include\ForwardListAllocator.h" />
<ClInclude Include="..\..\include\FreeLinkedList.h" />
<ClInclude Include="..\..\include\FreeList.h" />
<ClInclude Include="..\..\include\FreeListArray.h" />
<ClInclude Include="..\..\include\FreeMap.h" />
<ClInclude Include="..\..\include\Heap.h" />
<ClInclude Include="..\..\include\HeapCallbacks.h" />
<ClInclude Include="..\..\include\internal.h" />
<ClInclude Include="..\..\include\InternListAllocator.h" />
<ClInclude Include="..\..\include\InternMapAllocator.h" />
<ClInclude Include="..\..\include\MemoryBlock.h" />
<ClInclude Include="..\..\include\MemoryPage.h" />
Expand Down
6 changes: 3 additions & 3 deletions ide/vs2022/RTSHALibrary.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@
<ClInclude Include="..\..\include\InternMapAllocator.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\..\include\InternListAllocator.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\..\include\MemoryPage.h">
<Filter>Header Files</Filter>
</ClInclude>
Expand Down Expand Up @@ -69,6 +66,9 @@
<ClInclude Include="..\..\include\FreeLinkedList.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="..\..\include\ForwardListAllocator.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\..\src\MemoryPage.cpp">
Expand Down
39 changes: 33 additions & 6 deletions include/BigMemoryPage.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,30 @@
/******************************************************************************
The MIT License(MIT)
Real Time Safety Heap Allocator (RTSHA)
https://github.com/borisRadonic/RTSHA
Copyright(c) 2023 Boris Radonic
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
******************************************************************************/

#pragma once
#include <stdint.h>
#include "MemoryPage.h"
Expand Down Expand Up @@ -42,18 +69,18 @@ namespace rtsha
*
* \return On success, a pointer to the memory block allocated by the function.
*/
virtual void* allocate_block(const size_t& size) final;
virtual void* allocate_block(const size_t& size) noexcept final;

/**
* @brief Frees the specified memory block.
* @param block The memory block to be freed.
*/
virtual void free_block(MemoryBlock& block) final;
virtual void free_block(MemoryBlock& block) noexcept final;

/**
* @brief Creates the initial first two free blocks within the page.
*/
void createInitialFreeBlocks();
void createInitialFreeBlocks() noexcept;

protected:

Expand All @@ -62,18 +89,18 @@ namespace rtsha
* @param block The block to be split.
* @param size The desired size of the block after splitting.
*/
void splitBlock(MemoryBlock& block, size_t size);
void splitBlock(MemoryBlock& block, size_t size) noexcept;

/**
* @brief Merges the specified block with its left neighbor.
* @param block The block to be merged with its left neighbor.
*/
void mergeLeft(MemoryBlock& block);
void mergeLeft(MemoryBlock& block) noexcept;

/**
* @brief Merges the specified block with its right neighbor.
* @param block The block to be merged with its right neighbor.
*/
void mergeRight(MemoryBlock& block);
void mergeRight(MemoryBlock& block) noexcept;
};
}
36 changes: 32 additions & 4 deletions include/FastPlusAllocator.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,30 @@
/******************************************************************************
The MIT License(MIT)
Real Time Safety Heap Allocator (RTSHA)
https://github.com/borisRadonic/RTSHA
Copyright(c) 2023 Boris Radonic
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
******************************************************************************/

#pragma once
#include <cstdlib>
#include <new>
Expand Down Expand Up @@ -52,18 +79,19 @@ namespace rtsha
* @throws std::bad_alloc If memory allocation fails.
* @throws std::bad_array_new_length If the allocation size exceeds system limits.
*/
[[nodiscard]] T* allocate(std::size_t n)
[[nodiscard]] T* allocate(std::size_t n) noexcept
{
if (n > std::numeric_limits<std::size_t>::max() / sizeof(T))
throw std::bad_array_new_length();
{
return nullptr;
}

if (auto p = static_cast<T*>(rtsha_malloc(n * sizeof(T))))
{
//report(p, n);
return p;
}

throw std::bad_alloc();
return nullptr;
}

/**
Expand Down
149 changes: 149 additions & 0 deletions include/ForwardListAllocator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
/******************************************************************************
The MIT License(MIT)
Real Time Safety Heap Allocator (RTSHA)
https://github.com/borisRadonic/RTSHA
Copyright(c) 2023 Boris Radonic
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
******************************************************************************/

#pragma once
#include "MemoryPage.h"
#include <cstdlib>

#ifdef _RTSHA_DIAGNOSTIK
#include <iostream>
#endif

namespace rtsha
{
/**
* @class ForwardListAllocator
* @brief Custom allocator tailored STL forward_list
*
* This allocator is designed to efficiently manage memory for lists, leveraging
* specific characteristics of the `rtsha_page` structure and a reserved small storage.
*
* @tparam T The data type the allocator is responsible for.
*/
template<class T>
struct ForwardListAllocator
{
/**
* @brief Defines the type of elements managed by the allocator.
*/
typedef T value_type;

/**
* @brief Constructs the allocator with a given memory page
*
* @param page Pointer to the rtsha_page the allocator should use to allocate the memory for 'forward_list'.
*/
explicit ForwardListAllocator(MemoryPage& page ) noexcept
: _page(page)
{
}

/**
* @brief Copy constructor allowing for type conversion.
*
* called from 'std::forward_list'
*
* @tparam U The data type of the other allocator.
* @param rhs The other allocator to be copied from.
*/
template<class U>
constexpr ForwardListAllocator(const ForwardListAllocator <U>& rhs) noexcept
{
this->_page = rhs._page;
}

/**
* @brief Allocates a memory for an array of `n` objects of type `T`.
* It is called from 'forward_list' every time when 'push' method is called
* Memory of the free block specified in lastFreeBlockAddress is used as a storage.
*
* @param n Number of objects to allocate memory for.
* @return Pointer to the allocated block of memory.
*/
[[nodiscard]] rtsha_attr_inline T* allocate(std::size_t n) noexcept
{
if (n > std::numeric_limits<std::size_t>::max() / sizeof(T))
{
return nullptr;
}
/*max. 1 block for std::forward list*/
if (n != 1U)
{
return nullptr;
}

if (auto p = static_cast<T*>(_page.allocate_block(n * sizeof(T)) ))
{
return p;
}
return nullptr;
}

/**
* @brief Deallocates memory. In this custom allocator, the deallocation is a no-op.
*
* It is called from 'forward_list' every time when 'pop' method is called
*
*/
rtsha_attr_inline void deallocate(T* p, std::size_t n) noexcept
{
if (p != nullptr)
{
size_t address = (size_t)ptr;
address -= sizeof(rtsha_block); /*skip size and pointer to prev*/
MemoryBlock block(reinterpret_cast<rtsha_block*>(address));
if (block.isValid() && !block.isFree())
{
_page.free_block(block);
}
}
}

MemoryPage& _page; ///< The memory page managed by the allocator.
};

/**
* @brief Compares two `ForwardListAllocator` objects for equality.
*
* @tparam T Type associated with the first allocator.
* @tparam U Type associated with the second allocator.
* @return True as, by design, all instances of this allocator are interchangeable.
*/
template<class T, class U>
bool operator==(const ForwardListAllocator <T>&, const ForwardListAllocator <U>&) noexcept { return true; }

/**
* @brief Compares two `ForwardListAllocator` objects for inequality.
*
* @tparam T Type associated with the first allocator.
* @tparam U Type associated with the second allocator.
* @return False as, by design, all instances of this allocator are interchangeable.
*/
template<class T, class U>
bool operator!=(const ForwardListAllocator <T>&, const ForwardListAllocator <U>&) noexcept { return false; }
}

Loading

0 comments on commit c5ea24f

Please sign in to comment.