Skip to content
This repository has been archived by the owner on Jul 12, 2024. It is now read-only.

Commit

Permalink
feat: kmalloc,kfree,kalloc;new
Browse files Browse the repository at this point in the history
  • Loading branch information
MiroKaku committed Jun 17, 2022
1 parent d45ca46 commit 356220f
Show file tree
Hide file tree
Showing 12 changed files with 462 additions and 71 deletions.
36 changes: 36 additions & 0 deletions inc/malloc_km.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* PROJECT: Universal C++ RunTime (UCXXRT)
* FILE: malloc_km.h
* DATA: 2022/06/17
*
* PURPOSE: Universal C++ RunTime
*
* LICENSE: Relicensed under The MIT License from The CC BY 4.0 License
*
* DEVELOPER: MiroKaku (miro.kaku AT Outlook.com)
*/

#pragma once
#include <malloc.h>


extern "C" _CRT_HYBRIDPATCHABLE __declspec(noinline) _CRTRESTRICT
void* __cdecl kmalloc(
_In_ _CRT_GUARDOVERFLOW size_t size,
_In_ int pool_type,
_In_ unsigned long tag
);

extern "C" _CRT_HYBRIDPATCHABLE __declspec(noinline)
void __cdecl kfree(
_Pre_maybenull_ _Post_invalid_ void* block,
_In_ unsigned long tag
);

extern "C" _CRT_HYBRIDPATCHABLE __declspec(noinline) _CRTRESTRICT
void* __cdecl kcalloc(
_In_ _CRT_GUARDOVERFLOW size_t const count,
_In_ _CRT_GUARDOVERFLOW size_t const size,
_In_ int pool_type,
_In_ unsigned long tag
);
32 changes: 0 additions & 32 deletions inc/malloc_tag.h

This file was deleted.

25 changes: 25 additions & 0 deletions inc/new_km.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* PROJECT: Universal C++ RunTime (UCXXRT)
* FILE: new_km.h
* DATA: 2022/06/17
*
* PURPOSE: Universal C++ RunTime
*
* LICENSE: Relicensed under The MIT License from The CC BY 4.0 License
*
* DEVELOPER: MiroKaku (miro.kaku AT Outlook.com)
*/

//
// Defines the user-defined operator new.
//

#include <vcruntime_new.h>

// user-defined operator new functions
_NODISCARD _VCRT_ALLOCATOR void* __CRTDECL operator new (size_t const size, int pool_type, unsigned long tag);
_NODISCARD _VCRT_ALLOCATOR void* __CRTDECL operator new[](size_t const size, int pool_type, unsigned long tag);

// user-defined operator deallocation functions
void __CRTDECL operator delete (void* const block, int pool_type, unsigned long tag) noexcept;
void __CRTDECL operator delete[](void* const block, int pool_type, unsigned long tag) noexcept;
37 changes: 0 additions & 37 deletions inc/new_tag.h

This file was deleted.

5 changes: 5 additions & 0 deletions msvc/ucxxrt.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@
<ClCompile Include="..\src\crt\vcruntime\delete_array_size.cpp" />
<ClCompile Include="..\src\crt\vcruntime\delete_array_size_align.cpp" />
<ClCompile Include="..\src\crt\vcruntime\delete_debug.cpp" />
<ClCompile Include="..\src\crt\vcruntime\delete_km.cpp" />
<ClCompile Include="..\src\crt\vcruntime\delete_scalar.cpp" />
<ClCompile Include="..\src\crt\vcruntime\delete_scalar_align.cpp" />
<ClCompile Include="..\src\crt\vcruntime\delete_scalar_align_nothrow.cpp" />
Expand Down Expand Up @@ -299,6 +300,7 @@
<ClCompile Include="..\src\crt\vcruntime\new_array_align_nothrow.cpp" />
<ClCompile Include="..\src\crt\vcruntime\new_array_nothrow.cpp" />
<ClCompile Include="..\src\crt\vcruntime\new_debug.cpp" />
<ClCompile Include="..\src\crt\vcruntime\new_km.cpp" />
<ClCompile Include="..\src\crt\vcruntime\new_mode.cpp" />
<ClCompile Include="..\src\crt\vcruntime\new_scalar.cpp" />
<ClCompile Include="..\src\crt\vcruntime\new_scalar_align.cpp" />
Expand Down Expand Up @@ -342,6 +344,7 @@
<ClCompile Include="..\src\ucrt\heap\align.cpp" />
<ClCompile Include="..\src\ucrt\heap\calloc.cpp" />
<ClCompile Include="..\src\ucrt\heap\calloc_base.cpp" />
<ClCompile Include="..\src\ucrt\heap\calloc_km.cpp" />
<ClCompile Include="..\src\ucrt\heap\debug_heap.cpp">
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">true</ExcludedFromBuild>
<ExcludedFromBuild Condition="'$(Configuration)|$(Platform)'=='Release|ARM64'">true</ExcludedFromBuild>
Expand All @@ -351,8 +354,10 @@
<ClCompile Include="..\src\ucrt\heap\expand.cpp" />
<ClCompile Include="..\src\ucrt\heap\free.cpp" />
<ClCompile Include="..\src\ucrt\heap\free_base.cpp" />
<ClCompile Include="..\src\ucrt\heap\free_km.cpp" />
<ClCompile Include="..\src\ucrt\heap\malloc.cpp" />
<ClCompile Include="..\src\ucrt\heap\malloc_base.cpp" />
<ClCompile Include="..\src\ucrt\heap\malloc_km.cpp" />
<ClCompile Include="..\src\ucrt\heap\msize.cpp" />
<ClCompile Include="..\src\ucrt\heap\new_handler.cpp" />
<ClCompile Include="..\src\ucrt\heap\new_mode.cpp" />
Expand Down
15 changes: 15 additions & 0 deletions msvc/ucxxrt.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,21 @@
<ClCompile Include="..\src\crt\vcruntime\user.cpp">
<Filter>ucxxrt\crt\vcruntime</Filter>
</ClCompile>
<ClCompile Include="..\src\ucrt\heap\malloc_km.cpp">
<Filter>ucxxrt\ucrt\heap</Filter>
</ClCompile>
<ClCompile Include="..\src\ucrt\heap\free_km.cpp">
<Filter>ucxxrt\ucrt\heap</Filter>
</ClCompile>
<ClCompile Include="..\src\ucrt\heap\calloc_km.cpp">
<Filter>ucxxrt\ucrt\heap</Filter>
</ClCompile>
<ClCompile Include="..\src\crt\vcruntime\delete_km.cpp">
<Filter>ucxxrt\crt\vcruntime</Filter>
</ClCompile>
<ClCompile Include="..\src\crt\vcruntime\new_km.cpp">
<Filter>ucxxrt\crt\vcruntime</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<MARMASM Include="..\src\crt\arm\chkstk.asm">
Expand Down
45 changes: 45 additions & 0 deletions src/crt/vcruntime/delete_km.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* PROJECT: Universal C++ RunTime (UCXXRT)
* FILE: delete_km.cpp
* DATA: 2022/06/17
*
* PURPOSE: Universal C++ RunTime
*
* LICENSE: Relicensed under The MIT License from The CC BY 4.0 License
*
* DEVELOPER: MiroKaku (miro.kaku AT Outlook.com)
*/

#include <crtdbg.h>
#include <malloc.h>
#include <vcruntime_new.h>
#include <vcstartup_internal.h>

////////////////////////////////////////////////////////////////
// delete() Fallback Ordering
//
// +-------------+
// |delete_scalar<----+-----------------------+
// +--^----------+ | |
// | | |
// +--+---------+ +--+---------------+ +----+----------------+
// |delete_array| |delete_scalar_size| |delete_scalar_nothrow|
// +--^----^----+ +------------------+ +---------------------+
// | |
// | +-------------------+
// | |
// +--+--------------+ +------+-------------+
// |delete_array_size| |delete_array_nothrow|
// +-----------------+ +--------------------+

_CRT_SECURITYCRITICAL_ATTRIBUTE
void __CRTDECL operator delete(void* const block, int /*pool_type*/, unsigned long tag) noexcept
{
kfree(block, tag);
}

_CRT_SECURITYCRITICAL_ATTRIBUTE
void __CRTDECL operator delete[](void* const block, int pool_type, unsigned long tag) noexcept
{
operator delete(block, pool_type, tag);
}
64 changes: 64 additions & 0 deletions src/crt/vcruntime/new_km.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* PROJECT: Universal C++ RunTime (UCXXRT)
* FILE: new_km.cpp
* DATA: 2022/06/17
*
* PURPOSE: Universal C++ RunTime
*
* LICENSE: Relicensed under The MIT License from The CC BY 4.0 License
*
* DEVELOPER: MiroKaku (miro.kaku AT Outlook.com)
*/

#include <stdlib.h>
#include <vcruntime_new.h>
#include <vcstartup_internal.h>

// Enable the compiler to elide null checks during LTCG
#pragma comment(linker, "/ThrowingNew")

////////////////////////////////////
// new() Fallback Ordering
//
// +----------+
// |new_scalar<---------------+
// +----^-----+ |
// | |
// +----+-------------+ +----+----+
// |new_scalar_nothrow| |new_array|
// +------------------+ +----^----+
// |
// +------------+----+
// |new_array_nothrow|
// +-----------------+

_CRT_SECURITYCRITICAL_ATTRIBUTE
void* __CRTDECL operator new(size_t const size, int pool_type, unsigned long tag)
{
for (;;)
{
if (void* const block = kmalloc(size, pool_type, tag))
{
return block;
}

if (_callnewh(size) == 0)
{
if (size == SIZE_MAX)
{
__scrt_throw_std_bad_array_new_length();
}
else
{
__scrt_throw_std_bad_alloc();
}
}

// The new handler was successful; try to allocate again...
}
}

void* __CRTDECL operator new[](size_t const size, int pool_type, unsigned long tag)
{
return operator new(size, pool_type, tag);
}
Loading

0 comments on commit 356220f

Please sign in to comment.