Skip to content

Commit

Permalink
Add basic memory APIs (#10)
Browse files Browse the repository at this point in the history
Add basic memory APIs and tests.

Additionally
- Delete BasicTest.cpp missed in a previous PR
- Fix DEBUG define in Debug builds
- Add some CxPlatLogAssert implementations missed in a previous PR
  • Loading branch information
nigriMSFT authored Apr 30, 2024
1 parent 8d4bab8 commit cce3d00
Show file tree
Hide file tree
Showing 20 changed files with 805 additions and 34 deletions.
5 changes: 4 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,9 @@ if(WIN32)
set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE} /LTCG /IGNORE:4075 /DEBUG /OPT:REF /OPT:ICF")
set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /LTCG /IGNORE:4075 /DEBUG /OPT:REF /OPT:ICF")

set(CMAKE_C_FLAGS_DEBUG "${CMAKE_C_FLAGS_DEBUG} -DDEBUG")
set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -DDEBUG")

message(STATUS "Configuring for statically-linked CRT")
set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>")

Expand All @@ -333,7 +336,7 @@ else() #!WIN32
set(MARCH -march=native)
endif()

set(CMAKE_C_FLAGS_DEBUG "-Og -fno-omit-frame-pointer")
set(CMAKE_C_FLAGS_DEBUG "-Og -fno-omit-frame-pointer -DDEBUG")
set(CMAKE_C_FLAGS_MINSIZEREL "-Os -DNDEBUG")
set(CMAKE_C_FLAGS_RELWITHDEBINFO "-O3 -fno-omit-frame-pointer ${MARCH} -DNDEBUG")
set(CMAKE_C_FLAGS_RELEASE "-O3 ${MARCH} -DNDEBUG")
Expand Down
11 changes: 11 additions & 0 deletions inc/cxplat.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,17 @@ CxPlatUninitialize(
void
);

#ifdef DEBUG
void
CxPlatSetAllocFailDenominator(
_In_ int32_t Value
);

int32_t
CxPlatGetAllocFailDenominator(
);
#endif

#if defined(__cplusplus)
}
#endif
183 changes: 183 additions & 0 deletions inc/cxplat_posix.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,162 @@ extern "C" {

#define UNREFERENCED_PARAMETER(P) (void)(P)

typedef unsigned char BOOLEAN;

inline
short
InterlockedIncrement16(
_Inout_ _Interlocked_operand_ short volatile *Addend
)
{
return __sync_add_and_fetch(Addend, (short)1);
}

inline
short
InterlockedDecrement16(
_Inout_ _Interlocked_operand_ short volatile *Addend
)
{
return __sync_sub_and_fetch(Addend, (short)1);
}

inline
long
InterlockedIncrement(
_Inout_ _Interlocked_operand_ long volatile *Addend
)
{
return __sync_add_and_fetch(Addend, (long)1);
}

inline
long
InterlockedDecrement(
_Inout_ _Interlocked_operand_ long volatile *Addend
)
{
return __sync_sub_and_fetch(Addend, (long)1);
}

inline
int64_t
InterlockedIncrement64(
_Inout_ _Interlocked_operand_ int64_t volatile *Addend
)
{
return __sync_add_and_fetch(Addend, (int64_t)1);
}

inline
int64_t
InterlockedDecrement64(
_Inout_ _Interlocked_operand_ int64_t volatile *Addend
)
{
return __sync_sub_and_fetch(Addend, (int64_t)1);
}

inline
long
InterlockedAnd(
_Inout_ _Interlocked_operand_ long volatile *Destination,
_In_ long Value
)
{
return __sync_and_and_fetch(Destination, Value);
}

inline
long
InterlockedOr(
_Inout_ _Interlocked_operand_ long volatile *Destination,
_In_ long Value
)
{
return __sync_or_and_fetch(Destination, Value);
}

inline
short
InterlockedCompareExchange16(
_Inout_ _Interlocked_operand_ short volatile *Destination,
_In_ short ExChange,
_In_ short Comperand
)
{
return __sync_val_compare_and_swap(Destination, Comperand, ExChange);
}

inline
short
InterlockedCompareExchange(
_Inout_ _Interlocked_operand_ long volatile *Destination,
_In_ long ExChange,
_In_ long Comperand
)
{
return __sync_val_compare_and_swap(Destination, Comperand, ExChange);
}

inline
int64_t
InterlockedCompareExchange64(
_Inout_ _Interlocked_operand_ int64_t volatile *Destination,
_In_ int64_t ExChange,
_In_ int64_t Comperand
)
{
return __sync_val_compare_and_swap(Destination, Comperand, ExChange);
}

inline
int64_t
InterlockedExchangeAdd64(
_Inout_ _Interlocked_operand_ int64_t volatile *Addend,
_In_ int64_t Value
)
{
return __sync_fetch_and_add(Addend, Value);
}

inline
void*
InterlockedExchangePointer(
_Inout_ _Interlocked_operand_ void* volatile *Target,
_In_opt_ void* Value
)
{
return __sync_lock_test_and_set(Target, Value);
}

inline
void*
InterlockedFetchAndClearPointer(
_Inout_ _Interlocked_operand_ void* volatile *Target
)
{
return __sync_fetch_and_and(Target, 0);
}

inline
BOOLEAN
InterlockedFetchAndClearBoolean(
_Inout_ _Interlocked_operand_ BOOLEAN volatile *Target
)
{
return __sync_fetch_and_and(Target, 0);
}

inline
BOOLEAN
InterlockedFetchAndSetBoolean(
_Inout_ _Interlocked_operand_ BOOLEAN volatile *Target
)
{
return __sync_fetch_and_or(Target, 1);
}

//
// Status Codes
//
Expand All @@ -59,6 +215,7 @@ extern "C" {
#define CXPLAT_SUCCEEDED(X) ((int)(X) <= 0)

#define CXPLAT_STATUS_SUCCESS ((CXPLAT_STATUS)0) // 0
#define CXPLAT_STATUS_OUT_OF_MEMORY ((CXPLAT_STATUS)ENOMEM) // 12

//
// Code Annotations
Expand Down Expand Up @@ -112,6 +269,32 @@ CxPlatLogAssert(
#define CXPLAT_DBG_ASSERTMSG(exp, msg)
#endif

//
// Allocation/Memory Interfaces
//

_Ret_maybenull_
void*
CxPlatAlloc(
_In_ size_t ByteCount,
_In_ uint32_t Tag
);

void
CxPlatFree(
__drv_freesMem(Mem) _Frees_ptr_ void* Mem,
_In_ uint32_t Tag
);

#define CXPLAT_ALLOC_PAGED(Size, Tag) CxPlatAlloc(Size, Tag)
#define CXPLAT_ALLOC_NONPAGED(Size, Tag) CxPlatAlloc(Size, Tag)
#define CXPLAT_FREE(Mem, Tag) CxPlatFree((void*)Mem, Tag)

#define CxPlatZeroMemory(Destination, Length) memset((Destination), 0, (Length))
#define CxPlatCopyMemory(Destination, Source, Length) memcpy((Destination), (Source), (Length))
#define CxPlatMoveMemory(Destination, Source, Length) memmove((Destination), (Source), (Length))
#define CxPlatSecureZeroMemory CxPlatZeroMemory // TODO - Something better?

//
// Crypto Interfaces
//
Expand Down
14 changes: 14 additions & 0 deletions inc/cxplat_winkernel.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ typedef UINT64 uint64_t;
#define CXPLAT_SUCCEEDED(X) NT_SUCCESS(X)

#define CXPLAT_STATUS_SUCCESS STATUS_SUCCESS // 0x0
#define CXPLAT_STATUS_OUT_OF_MEMORY STATUS_NO_MEMORY // 0xc0000017

//
// Code Annotations
Expand Down Expand Up @@ -124,6 +125,19 @@ CxPlatLogAssert(
#define CXPLAT_DBG_ASSERTMSG(_exp, _msg) CXPLAT_ASSERT_NOOP(_exp, CXPLAT_WIDE_STRING(_msg))
#endif

//
// Allocation/Memory Interfaces
//

#define CXPLAT_ALLOC_PAGED(Size, Tag) ExAllocatePool2(POOL_FLAG_PAGED | POOL_FLAG_UNINITIALIZED, Size, Tag)
#define CXPLAT_ALLOC_NONPAGED(Size, Tag) ExAllocatePool2(POOL_FLAG_NON_PAGED | POOL_FLAG_UNINITIALIZED, Size, Tag)
#define CXPLAT_FREE(Mem, Tag) ExFreePoolWithTag((void*)Mem, Tag)

#define CxPlatZeroMemory RtlZeroMemory
#define CxPlatCopyMemory RtlCopyMemory
#define CxPlatMoveMemory RtlMoveMemory
#define CxPlatSecureZeroMemory RtlSecureZeroMemory

//
// Crypto Interfaces
//
Expand Down
29 changes: 29 additions & 0 deletions inc/cxplat_winuser.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ extern "C" {
#define CXPLAT_SUCCEEDED(X) SUCCEEDED(X)

#define CXPLAT_STATUS_SUCCESS S_OK // 0x0
#define CXPLAT_STATUS_OUT_OF_MEMORY E_OUTOFMEMORY // 0x8007000e

//
// Code Annotations
Expand Down Expand Up @@ -106,6 +107,34 @@ CxPlatLogAssert(
#define CXPLAT_DBG_ASSERTMSG(_exp, _msg) CXPLAT_ASSERT_NOOP(_exp, CXPLAT_WIDE_STRING(_msg))
#endif

//
// Allocation/Memory Interfaces
//

_Ret_maybenull_
_Post_writable_byte_size_(ByteCount)
DECLSPEC_ALLOCATOR
void*
CxPlatAlloc(
_In_ size_t ByteCount,
_In_ uint32_t Tag
);

void
CxPlatFree(
__drv_freesMem(Mem) _Frees_ptr_ void* Mem,
_In_ uint32_t Tag
);

#define CXPLAT_ALLOC_PAGED(Size, Tag) CxPlatAlloc(Size, Tag)
#define CXPLAT_ALLOC_NONPAGED(Size, Tag) CxPlatAlloc(Size, Tag)
#define CXPLAT_FREE(Mem, Tag) CxPlatFree((void*)Mem, Tag)

#define CxPlatZeroMemory RtlZeroMemory
#define CxPlatCopyMemory RtlCopyMemory
#define CxPlatMoveMemory RtlMoveMemory
#define CxPlatSecureZeroMemory RtlSecureZeroMemory

//
// Crypto Interfaces
//
Expand Down
2 changes: 1 addition & 1 deletion src/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
if("${CX_PLATFORM}" STREQUAL "winuser")
set(SOURCES ${SOURCES} cxplat_winuser.c)
else()
set(SOURCES ${SOURCES} cxplat_posix.c)
set(SOURCES ${SOURCES} cxplat_posix.c inline.c)
endif()

add_library(cxplat STATIC ${SOURCES})
Expand Down
2 changes: 1 addition & 1 deletion src/lib/cxplat.kernel.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
<ItemDefinitionGroup Condition="'$(Configuration)'=='Debug'">
<ClCompile>
<RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary>
<PreprocessorDefinitions>CX_PLATFORM_WINKERNEL;VER_BUILD_ID=$(CXPLAT_VER_BUILD_ID);VER_SUFFIX=$(CXPLAT_VER_SUFFIX);VER_GIT_HASH=$(CXPLAT_VER_GIT_HASH);SECURITY_KERNEL;SECURITY_WIN32;_DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<PreprocessorDefinitions>CX_PLATFORM_WINKERNEL;VER_BUILD_ID=$(CXPLAT_VER_BUILD_ID);VER_SUFFIX=$(CXPLAT_VER_SUFFIX);VER_GIT_HASH=$(CXPLAT_VER_GIT_HASH);SECURITY_KERNEL;SECURITY_WIN32;_DEBUG;DEBUG;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<MultiProcessorCompilation>true</MultiProcessorCompilation>
</ClCompile>
</ItemDefinitionGroup>
Expand Down
Loading

0 comments on commit cce3d00

Please sign in to comment.