Skip to content

Commit

Permalink
Remove libstdc++ dependency from NativeAOT
Browse files Browse the repository at this point in the history
  • Loading branch information
am11 committed Oct 12, 2022
1 parent c6c8bb8 commit cd2825c
Show file tree
Hide file tree
Showing 18 changed files with 225 additions and 188 deletions.
15 changes: 8 additions & 7 deletions src/coreclr/gc/env/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,18 @@
#define _CRT_SECURE_NO_WARNINGS
#endif // _CRT_SECURE_NO_WARNINGS

#include <stdint.h>
#include <stddef.h>
#include <cstddef>
#include <assert.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
#include <assert.h>
#include <stdarg.h>
#include <memory.h>
#include <stdlib.h>

#if defined(FEATURE_NATIVEAOT) && !defined(TARGET_WINDOWS)
#include "CommonTypes.h"
#else
#include <new>
#endif

#ifdef TARGET_UNIX
#include <pthread.h>
Expand Down
4 changes: 2 additions & 2 deletions src/coreclr/gc/gc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11488,6 +11488,8 @@ bool gc_heap::is_region_demoted (uint8_t* obj)
return demoted_p;
}

static GCSpinLock write_barrier_spin_lock;

inline
void gc_heap::set_region_gen_num (heap_segment* region, int gen_num)
{
Expand All @@ -11510,8 +11512,6 @@ void gc_heap::set_region_gen_num (heap_segment* region, int gen_num)
{
if ((region_start < ephemeral_low) || (ephemeral_high < region_end))
{
static GCSpinLock write_barrier_spin_lock;

while (true)
{
if (Interlocked::CompareExchange(&write_barrier_spin_lock.lock, 0, -1) < 0)
Expand Down
6 changes: 3 additions & 3 deletions src/coreclr/gc/gc.h
Original file line number Diff line number Diff line change
Expand Up @@ -253,9 +253,9 @@ struct alloc_context : gc_alloc_context

class IGCHeapInternal : public IGCHeap {
public:
virtual int GetNumberOfHeaps () = 0;
virtual int GetHomeHeapNumber () = 0;
virtual size_t GetPromotedBytes(int heap_index) = 0;
virtual int GetNumberOfHeaps () PURE_VIRTUAL
virtual int GetHomeHeapNumber () PURE_VIRTUAL
virtual size_t GetPromotedBytes(int heap_index) PURE_VIRTUAL

unsigned GetMaxGeneration()
{
Expand Down
218 changes: 111 additions & 107 deletions src/coreclr/gc/gcinterface.h

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/coreclr/gc/handletablecache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ void SpinUntil(void *pCond, BOOL fNonZero)
#endif //_DEBUG

// on MP machines, allow ourselves some spin time before sleeping
static uint32_t uNonSleepSpins = 8 * (GCToEEInterface::GetCurrentProcessCpuCount() - 1);
uint32_t uNonSleepSpins = 8 * (GCToEEInterface::GetCurrentProcessCpuCount() - 1);

// spin until the specified condition is met
while ((*(uintptr_t *)pCond != 0) != (fNonZero != 0))
Expand Down
16 changes: 6 additions & 10 deletions src/coreclr/gc/unix/events.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#include <cstdint>
#include <cstddef>
#include <cassert>
#include <memory>
#include <mutex>
#include <pthread.h>
#include <errno.h>
#include "config.gc.h"
Expand Down Expand Up @@ -280,36 +275,37 @@ bool GCEvent::CreateManualEventNoThrow(bool initialState)
bool GCEvent::CreateOSAutoEventNoThrow(bool initialState)
{
assert(m_impl == nullptr);
std::unique_ptr<GCEvent::Impl> event(new (std::nothrow) GCEvent::Impl(false, initialState));
GCEvent::Impl* event(new (nothrow) GCEvent::Impl(false, initialState));
if (!event)
{
return false;
}

if (!event->Initialize())
{
delete event;
return false;
}

m_impl = event.release();
m_impl = event;
return true;
}

bool GCEvent::CreateOSManualEventNoThrow(bool initialState)
{
assert(m_impl == nullptr);
std::unique_ptr<GCEvent::Impl> event(new (std::nothrow) GCEvent::Impl(true, initialState));
GCEvent::Impl* event(new (nothrow) GCEvent::Impl(true, initialState));
if (!event)
{
return false;
}

if (!event->Initialize())
{
delete event;
return false;
}

m_impl = event.release();
m_impl = event;
return true;
}

16 changes: 6 additions & 10 deletions src/coreclr/gc/unix/gcenv.unix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,10 @@
// The .NET Foundation licenses this file to you under the MIT license.

#define _WITH_GETLINE
#include <cstdint>
#include <cstddef>
#include <cstdio>
#include <cassert>
#define __STDC_FORMAT_MACROS
#include <cinttypes>
#include <memory>
#include <pthread.h>
#include <signal.h>
#include <stdio.h>

#include "config.gc.h"
#include "common.h"
Expand Down Expand Up @@ -38,8 +33,6 @@
#define FALLTHROUGH
#endif

#include <algorithm>

#if HAVE_SYS_TIME_H
#include <sys/time.h>
#else
Expand Down Expand Up @@ -927,7 +920,7 @@ static size_t GetLogicalProcessorCacheSizeFromOS()
}
else
{
cacheSize = std::max(cacheSize, size);
cacheSize = cacheSize > size ? cacheSize : size;
}
}
}
Expand Down Expand Up @@ -958,7 +951,10 @@ static size_t GetLogicalProcessorCacheSizeFromOS()
// Assume L3$/CPU grows linearly from 256K to 1.5M/CPU as logicalCPUs grows from 2 to 12 CPUs
DWORD logicalCPUs = g_totalCpuCount;

cacheSize = logicalCPUs * std::min(1536, std::max(256, (int)logicalCPUs * 128)) * 1024;
int cpus = (int)logicalCPUs * 128;
int maxVal = 256 > cpus ? cpus : 256;
int minVal = 1536 < maxVal ? 1536 : maxVal;
cacheSize = logicalCPUs * minVal * 1024;
}
#endif

Expand Down
3 changes: 3 additions & 0 deletions src/coreclr/nativeaot/Bootstrap/base/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@ if (CLR_CMAKE_TARGET_WIN32)
add_library(bootstrapper.GuardCF STATIC ${SOURCES})
install_static_library(bootstrapper.GuardCF aotsdk nativeaot)
target_compile_options(bootstrapper.GuardCF PRIVATE $<$<OR:$<COMPILE_LANGUAGE:C>,$<COMPILE_LANGUAGE:CXX>>:/guard:cf>)
else()
add_library(stdc++compat STATIC ../stdcppshim.cpp)
install_static_library(stdc++compat aotsdk nativeaot)
endif()
30 changes: 30 additions & 0 deletions src/coreclr/nativeaot/Bootstrap/stdcppshim.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#include <stdlib.h>

namespace std
{
struct nothrow_t {};
extern const nothrow_t nothrow = {};
}

void* operator new(size_t n, const std::nothrow_t&) noexcept
{
return malloc(n);
}

void* operator new[](size_t n, const std::nothrow_t&) noexcept
{
return malloc(n);
}

void operator delete(void *p) noexcept
{
free(p);
}

void operator delete[](void *p) noexcept
{
free(p);
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ The .NET Foundation licenses this file to you under the MIT license.
<NativeLibrary Condition="$(NativeLib) == ''" Include="$(IlcSdkPath)libbootstrapper.a" />
<NativeLibrary Condition="$(NativeLib) != ''" Include="$(IlcSdkPath)libbootstrapperdll.a" />
<NativeLibrary Include="$(IlcSdkPath)$(FullRuntimeName).a" />
<NativeLibrary Condition="'$(LinkStandardCPlusPlusLibrary)' != 'true'" Include="$(IlcSdkPath)libstdc++compat.a" />
</ItemGroup>

<ItemGroup>
Expand Down Expand Up @@ -96,7 +97,7 @@ The .NET Foundation licenses this file to you under the MIT license.
<LinkerArg Include="-Wl,--as-needed" Condition="'$(TargetOS)' != 'OSX'" />
<LinkerArg Include="-Wl,-e0x0" Condition="'$(NativeLib)' == 'Shared' and '$(TargetOS)' != 'OSX'" />
<LinkerArg Include="-pthread" Condition="'$(TargetOS)' != 'OSX'" />
<LinkerArg Include="-lstdc++" />
<LinkerArg Include="-lstdc++" Condition="'$(LinkStandardCPlusPlusLibrary)' == 'true'" />
<LinkerArg Include="-ldl" />
<LinkerArg Include="-lm" />
<LinkerArg Include="-lobjc" Condition="'$(TargetOS)' == 'OSX'" />
Expand Down
1 change: 1 addition & 0 deletions src/coreclr/nativeaot/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ if(CLR_CMAKE_HOST_UNIX)

add_compile_options(-fno-rtti) # Native AOT runtime doesn't use RTTI
add_compile_options(-fno-exceptions) # Native AOT runtime doesn't use C++ exception handling
add_compile_options(-nostdlib)

if(CLR_CMAKE_TARGET_OSX)
add_definitions(-D_XOPEN_SOURCE)
Expand Down
3 changes: 0 additions & 3 deletions src/coreclr/nativeaot/Runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,6 @@ if (WIN32)
${GC_DIR}/handletablepriv.h
${GC_DIR}/objecthandle.h
${GC_DIR}/softwarewritewatch.h)
endif(WIN32)

if(WIN32)

include_directories(windows)

Expand Down
32 changes: 16 additions & 16 deletions src/coreclr/nativeaot/Runtime/ICodeManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -165,50 +165,50 @@ enum class AssociatedDataFlags : unsigned char
class ICodeManager
{
public:
virtual bool IsSafePoint(PTR_VOID pvAddress) = 0;
virtual bool IsSafePoint(PTR_VOID pvAddress) PURE_VIRTUAL

virtual bool FindMethodInfo(PTR_VOID ControlPC,
MethodInfo * pMethodInfoOut) = 0;
MethodInfo * pMethodInfoOut) PURE_VIRTUAL

virtual bool IsFunclet(MethodInfo * pMethodInfo) = 0;
virtual bool IsFunclet(MethodInfo * pMethodInfo) PURE_VIRTUAL

virtual PTR_VOID GetFramePointer(MethodInfo * pMethodInfo,
REGDISPLAY * pRegisterSet) = 0;
REGDISPLAY * pRegisterSet) PURE_VIRTUAL

virtual void EnumGcRefs(MethodInfo * pMethodInfo,
PTR_VOID safePointAddress,
REGDISPLAY * pRegisterSet,
GCEnumContext * hCallback,
bool isActiveStackFrame) = 0;
bool isActiveStackFrame) PURE_VIRTUAL

virtual bool UnwindStackFrame(MethodInfo * pMethodInfo,
REGDISPLAY * pRegisterSet, // in/out
PInvokeTransitionFrame** ppPreviousTransitionFrame) = 0; // out
PInvokeTransitionFrame** ppPreviousTransitionFrame) PURE_VIRTUAL // out

virtual uintptr_t GetConservativeUpperBoundForOutgoingArgs(MethodInfo * pMethodInfo,
REGDISPLAY * pRegisterSet) = 0;
REGDISPLAY * pRegisterSet) PURE_VIRTUAL

virtual bool IsUnwindable(PTR_VOID pvAddress) = 0;
virtual bool IsUnwindable(PTR_VOID pvAddress) PURE_VIRTUAL

virtual bool GetReturnAddressHijackInfo(MethodInfo * pMethodInfo,
REGDISPLAY * pRegisterSet, // in
PTR_PTR_VOID * ppvRetAddrLocation, // out
GCRefKind * pRetValueKind) = 0; // out
GCRefKind * pRetValueKind) PURE_VIRTUAL // out

virtual PTR_VOID RemapHardwareFaultToGCSafePoint(MethodInfo * pMethodInfo, PTR_VOID controlPC) = 0;
virtual PTR_VOID RemapHardwareFaultToGCSafePoint(MethodInfo * pMethodInfo, PTR_VOID controlPC) PURE_VIRTUAL

virtual bool EHEnumInit(MethodInfo * pMethodInfo, PTR_VOID * pMethodStartAddress, EHEnumState * pEHEnumState) = 0;
virtual bool EHEnumInit(MethodInfo * pMethodInfo, PTR_VOID * pMethodStartAddress, EHEnumState * pEHEnumState) PURE_VIRTUAL

virtual bool EHEnumNext(EHEnumState * pEHEnumState, EHClause * pEHClause) = 0;
virtual bool EHEnumNext(EHEnumState * pEHEnumState, EHClause * pEHClause) PURE_VIRTUAL

virtual PTR_VOID GetMethodStartAddress(MethodInfo * pMethodInfo) = 0;
virtual PTR_VOID GetMethodStartAddress(MethodInfo * pMethodInfo) PURE_VIRTUAL

virtual PTR_VOID GetOsModuleHandle() = 0;
virtual PTR_VOID GetOsModuleHandle() PURE_VIRTUAL

virtual void * GetClasslibFunction(ClasslibFunctionId functionId) = 0;
virtual void * GetClasslibFunction(ClasslibFunctionId functionId) PURE_VIRTUAL

// Returns any custom data attached to the method. Format:
// AssociatedDataFlags // 1 byte. Flags describing the data stored
// Data (stream of bytes) // Variable size (depending on flags). Custom data associated with method
virtual PTR_VOID GetAssociatedData(PTR_VOID ControlPC) = 0;
virtual PTR_VOID GetAssociatedData(PTR_VOID ControlPC) PURE_VIRTUAL
};
9 changes: 8 additions & 1 deletion src/coreclr/nativeaot/Runtime/inc/CommonTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,16 @@

#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <stdlib.h>
#include <stdio.h>
#include <new>

#ifdef TARGET_WINDOWS
#define PURE_VIRTUAL = 0;
#else
#define PURE_VIRTUAL { assert(!"pure virtual function called"); while(true); }
#endif

using std::nothrow;
using std::size_t;
using std::uintptr_t;
Expand Down
Loading

0 comments on commit cd2825c

Please sign in to comment.