Skip to content

Commit

Permalink
RDF 1.4.0
Browse files Browse the repository at this point in the history
  • Loading branch information
ahosier authored and GitHub Enterprise committed Sep 20, 2024
1 parent 800064d commit 3c47717
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 8 deletions.
4 changes: 2 additions & 2 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2022 Advanced Micro Devices, Inc.
Copyright (c) 2022-2024 Advanced Micro Devices, Inc.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand All @@ -18,4 +18,4 @@ 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.
SOFTWARE.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,5 @@ Patch releases (for example, `1.1.1`) will be bumped for bug fixes and other imp
* Fix bug in `rdfm` which would incorrectly write the last chunk's data when merging an empty chunk.
* Change validation for the `rdfChunkFileReadChunk*` read functions such that a `nullptr` for the `buffer` argument is valid if the requested size is 0. Previously, this would fail with an invalid argument error.
* Remove support for [VCPKG](https://vcpkg.io/) again. Unfortunately, the upstream port file has never been finished, and the relatively intrusive support added in 1.2 caused more problems than it solved. If there's interest in re-adding VCPKG support, please open an issue or PR.
* **1.4.0**
* Allow files to be opened in shareable mode. An 'is_shareable' flag has been added to the rdfStreamFromFileCreateInfo structure (default is false).
2 changes: 2 additions & 0 deletions rdf/inc/amdrdf.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ struct rdfStreamFromFileCreateInfo
const char* filename;
rdfStreamAccess accessMode;
rdfFileMode fileMode;
bool is_shareable;
};

/**
Expand Down Expand Up @@ -338,6 +339,7 @@ class Stream final
info.filename = filename;
info.fileMode = fileMode;
info.accessMode = streamAccess;
info.is_shareable = false;

RDF_CHECK_CALL(rdfStreamFromFile(&info, &result.stream_));
return result;
Expand Down
104 changes: 98 additions & 6 deletions rdf/src/amdrdf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,16 @@
#include <limits>
#include <stdexcept>

#if RDF_PLATFORM_WINDOWS
// Define NOMINMAX to prevent conflict between <limits> and <windows.h> include files.
#define NOMINMAX

#include <corecrt_io.h>
#include <fcntl.h>
#include <io.h>
#include <windows.h>
#endif // #if RDF_PLATFORM_WINDOWS

#include <algorithm>
// We use map so we don't have to provide a hash function for chunkId, which
// is a bit tricky with C++11 and old compilers
Expand Down Expand Up @@ -245,6 +255,11 @@ namespace internal
std::unique_ptr<IStream> OpenFile(const char* filename,
rdfStreamAccess access,
rdfFileMode fileMode);

std::unique_ptr<IStream> OpenSharedFile(const char* filename,
rdfStreamAccess access,
rdfFileMode fileMode);

std::unique_ptr<IStream> CreateFile(const char* filename);

std::unique_ptr<IStream> CreateReadOnlyMemoryStream(const std::int64_t bufferSize,
Expand Down Expand Up @@ -1054,11 +1069,72 @@ namespace internal
}

//////////////////////////////////////////////////////////////////////
std::unique_ptr<IStream> OpenFile(const char* filename,
std::unique_ptr<IStream> OpenSharedFile(const char* filename,
rdfStreamAccess accessMode,
rdfFileMode fileMode)
{
#if RDF_PLATFORM_WINDOWS
const char* mode = nullptr;

// For a shareable file on Windows, the file needs to be opened with the CreateFile() function.
unsigned long desired_access = 0;
unsigned long share_mode = FILE_SHARE_READ | FILE_SHARE_WRITE;
unsigned long creation_disposition = 0;
unsigned long flags_and_attributes = FILE_ATTRIBUTE_NORMAL;

if (accessMode == rdfStreamAccess::rdfStreamAccessRead) {
desired_access |= GENERIC_READ;
if (fileMode == rdfFileModeOpen) {
creation_disposition = OPEN_EXISTING;
mode = "rb";
} else if (fileMode == rdfFileModeCreate) {
throw std::runtime_error("Cannot create file in read-only mode");
}
} else if (accessMode == rdfStreamAccess::rdfStreamAccessReadWrite) {
desired_access |= GENERIC_READ | GENERIC_WRITE;
if (fileMode == rdfFileModeOpen) {
creation_disposition = OPEN_EXISTING;
mode = "r+b";
} else if (fileMode == rdfFileModeCreate) {
creation_disposition = OPEN_EXISTING;
mode = "w+b";
}
} else {
assert(false);
}

HANDLE object_handle = ::CreateFile(filename,
desired_access,
share_mode,
NULL,
creation_disposition,
flags_and_attributes,
NULL);

// Convert the object handle to a file descriptor.
int file_handle = _open_osfhandle((intptr_t)object_handle, _O_BINARY);
auto fd = _fdopen(file_handle, mode);
if (fd == nullptr) {
CloseHandle(object_handle);
throw std::runtime_error("Error opening file");
}

// Disable inheritance of the file handle.
SetHandleInformation(object_handle, HANDLE_FLAG_INHERIT, 0);
return rdf_make_unique<Filestream>(fd, accessMode);
#else
return OpenFile(filename, accessMode, fileMode);
#endif // #if RDF_PLATFORM_WINDOWS
}



std::unique_ptr<IStream> OpenFile(const char* filename,
rdfStreamAccess accessMode,
rdfFileMode fileMode)
{
const char* mode = nullptr;

if (accessMode == rdfStreamAccessRead) {
if (fileMode == rdfFileModeOpen) {
mode = "rb";
Expand All @@ -1080,6 +1156,10 @@ namespace internal
throw std::runtime_error("Could not open file");
}

#if RDF_PLATFORM_WINDOWS
// Disable inheritance of the file handle.
SetHandleInformation((HANDLE)_get_osfhandle(_fileno(fd)), HANDLE_FLAG_INHERIT, 0);
#endif // #if RDF_PLATFORM_WINDOWS
return rdf_make_unique<Filestream>(fd, accessMode);
}

Expand All @@ -1090,7 +1170,12 @@ namespace internal
if (fd == nullptr) {
throw std::runtime_error("Could not create file");
}

#if RDF_PLATFORM_WINDOWS
else {
// Disable inheritance of the file handle.
SetHandleInformation((HANDLE)_get_osfhandle(_fileno(fd)), HANDLE_FLAG_INHERIT, 0);
}
#endif // #if RDF_PLATFORM_WINDOWS
return rdf_make_unique<Filestream>(fd, rdfStreamAccessReadWrite);
}

Expand Down Expand Up @@ -1155,8 +1240,7 @@ to open an existing file for read/write or create a new one.
The creation file mode requires read/write access, as creating a new file
for read-only would result in an unusable stream.
*/
int RDF_EXPORT rdfStreamFromFile(const rdfStreamFromFileCreateInfo* info,
rdfStream** handle)
int RDF_EXPORT rdfStreamFromFile(const rdfStreamFromFileCreateInfo* info, rdfStream** handle)
{
RDF_C_API_BEGIN

Expand All @@ -1174,8 +1258,16 @@ int RDF_EXPORT rdfStreamFromFile(const rdfStreamFromFileCreateInfo* info,

*handle = new rdfStream;
try {
(*handle)->stream =
rdf::internal::OpenFile(info->filename, info->accessMode, info->fileMode);
if (info->is_shareable)
{
(*handle)->stream =
rdf::internal::OpenSharedFile(info->filename, info->accessMode, info->fileMode);
}
else
{
(*handle)->stream =
rdf::internal::OpenFile(info->filename, info->accessMode, info->fileMode);
}
} catch (...) {
delete *handle;
*handle = nullptr;
Expand Down

0 comments on commit 3c47717

Please sign in to comment.