Skip to content

Commit

Permalink
Move string allocation APIs to cxplat library
Browse files Browse the repository at this point in the history
Signed-off-by: Dave Thaler <[email protected]>
  • Loading branch information
dthaler committed Aug 19, 2023
1 parent bb99536 commit b731cb7
Show file tree
Hide file tree
Showing 16 changed files with 375 additions and 172 deletions.
26 changes: 25 additions & 1 deletion cxplat/cxplat_test/cxplat_memory_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,28 @@ TEST_CASE("reallocate aligned", "[memory]")
cxplat_free(new_buffer);
}

TEST_CASE("cxplat_free null", "[ex]") { cxplat_free(nullptr); }
TEST_CASE("cxplat_free null", "[memory]") { cxplat_free(nullptr); }

TEST_CASE("cxplat_duplicate_string", "[memory]")
{
char* string = cxplat_duplicate_string("test");
REQUIRE(string != nullptr);
REQUIRE(strcmp(string, "test") == 0);

cxplat_free(string);
}

TEST_CASE("cxplat_duplicate_utf8_string", "[memory]")
{
const char string[] = "test";
cxplat_utf8_string_t source = CXPLAT_UTF8_STRING_FROM_CONST_STRING(string);
REQUIRE(source.length == strlen(string));

cxplat_utf8_string_t destination;
cxplat_status_t status = cxplat_duplicate_utf8_string(&destination, &source);
REQUIRE(status == CXPLAT_STATUS_SUCCESS);
REQUIRE(destination.length == source.length);
REQUIRE(memcmp(destination.value, source.value, source.length) == 0);

cxplat_utf8_string_free(&destination);
}
52 changes: 52 additions & 0 deletions cxplat/inc/cxplat_memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
// SPDX-License-Identifier: MIT
#pragma once

#pragma warning(push)
#pragma warning(disable: 4005 4083 4616)
#include <stdint.h>
#include <driverspecs.h>
#pragma warning(pop)

#ifdef __cplusplus
extern "C"
Expand All @@ -18,6 +21,25 @@ extern "C"
CxPlatNonPagedPoolNxCacheAligned = CxPlatNonPagedPoolNx + 4,
} cxplat_pool_type_t;

/**
* @brief A UTF-8 encoded string.
* Notes:
* 1) This string is not NULL terminated, instead relies on length.
* 2) A single UTF-8 code point (aka character) could be 1-4 bytes in
* length.
*
*/
typedef struct _cxplat_utf8_string
{
uint8_t* value;
size_t length;
} cxplat_utf8_string_t;

#define CXPLAT_UTF8_STRING_FROM_CONST_STRING(x) \
{ \
((uint8_t*)(x)), sizeof((x)) - 1 \
}

/**
* @brief Allocate memory.
* @param[in] size Size of memory to allocate.
Expand Down Expand Up @@ -91,6 +113,36 @@ extern "C"
void
cxplat_free_cache_aligned(_Frees_ptr_opt_ void* memory);

/**
* @brief Allocate and copy a UTF-8 string.
*
* @param[out] destination Pointer to memory where the new UTF-8 character
* sequence will be allocated.
* @param[in] source UTF-8 string that will be copied.
* @retval CXPLAT_STATUS_SUCCESS The operation was successful.
* @retval CXPLAT_STATUS_NO_MEMORY Unable to allocate resources for this
* UTF-8 string.
*/
_Must_inspect_result_ cxplat_status_t
cxplat_duplicate_utf8_string(_Out_ cxplat_utf8_string_t* destination, _In_ const cxplat_utf8_string_t* source);

/**
* @brief Free a UTF-8 string allocated by cxplat_duplicate_utf8_string.
*
* @param[in,out] string The string to free.
*/
void
cxplat_utf8_string_free(_Inout_ cxplat_utf8_string_t* string);

/**
* @brief Duplicate a null-terminated string.
*
* @param[in] source String to duplicate.
* @return Pointer to the duplicated string or NULL if out of memory.
*/
_Must_inspect_result_ char*
cxplat_duplicate_string(_In_z_ const char* source);

#ifdef __cplusplus
}
#endif
5 changes: 4 additions & 1 deletion cxplat/inc/winkernel/cxplat_winkernel.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// Copyright (c) Microsoft Corporation
// SPDX-License-Identifier: MIT
#pragma once
#include <ntdef.h>
#if !defined(_AMD64_) && defined(_M_AMD64)
#define _AMD64_
#endif
#include <ntdef.h> // for NTSTATUS
#include <ntstatus.h>

// Map specific cxplat_status_t values to HRESULT values.
Expand Down
19 changes: 19 additions & 0 deletions cxplat/src/cxplat_winkernel/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
# Copyright (c) Microsoft Corporation
# SPDX-License-Identifier: MIT

add_library(cxplat_winkernel STATIC
../../inc/cxplat.h
../../inc/cxplat_common.h
../../inc/cxplat_fault_injection.h
../../inc/cxplat_memory.h
../../inc/winkernel/cxplat_platform.h
../../inc/winkernel/cxplat_winkernel.h
../memory.c
memory_winkernel.c
)

target_include_directories(cxplat_winkernel PRIVATE
"."
"../../inc"
"../../inc/winkernel"
)

set(defs UNICODE _UNICODE CXPLAT_SOURCE)
target_compile_definitions(cxplat_winkernel PRIVATE ${defs})
Loading

0 comments on commit b731cb7

Please sign in to comment.