-
Notifications
You must be signed in to change notification settings - Fork 11
[Support] Add cross-platform memory stream utility #121
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
50bcca1
[Support] Add cross-platform memory stream utility
rsuderman 659367c
Merge remote-tracking branch 'origin/main' into memstream
rsuderman e8cd88f
Addressed review comments
rsuderman a18f389
clang-tidy
rsuderman 55dbf57
clang-tidy miss
rsuderman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,219 @@ | ||
| // Copyright 2025 Advanced Micro Devices, Inc. | ||
| // | ||
| // Licensed under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
|
||
| //===----------------------------------------------------------------------===// | ||
| // | ||
| // This file contains a cross-platform memory stream utility that provides | ||
| // a FILE* interface backed by memory. On Linux, it uses open_memstream. | ||
| // On Windows, it uses a pure C++ implementation with temporary files. | ||
| // | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
rsuderman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| #ifndef FUSILLI_SUPPORT_MEMSTREAM_H | ||
| #define FUSILLI_SUPPORT_MEMSTREAM_H | ||
|
|
||
| #include <cstdio> | ||
| #include <cstdlib> | ||
| #include <cstring> | ||
| #include <string> | ||
|
|
||
| #include "fusilli/support/target_platform.h" | ||
rsuderman marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| #ifdef FUSILLI_PLATFORM_LINUX | ||
| #include <fcntl.h> | ||
| #include <vector> | ||
rsuderman marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| #elif FUSILLI_PLATFORM_WINDOWS | ||
| #include <stdio.h> | ||
rsuderman marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| #include <windows.h> | ||
| #else | ||
| #error "MemStream is only implemented for Windows and Linux platforms." | ||
| #endif | ||
|
|
||
| namespace fusilli { | ||
|
|
||
| // A cross-platform memory stream that provides a FILE* interface backed by | ||
| // dynamically growing memory. This allows C-style fprintf operations to write | ||
| // to an in-memory buffer that can later be retrieved as a std::string. | ||
| // | ||
| // On Linux/POSIX, this uses the native open_memstream function. | ||
| // On Windows, this uses a temporary file. | ||
| // | ||
| // Example usage: | ||
| // MemStream ms; | ||
| // if (ms.isValid()) { | ||
| // fprintf(ms.stream(), "Hello, %s!", "world"); | ||
| // std::string result = ms.str(); // "Hello, world!" | ||
| // } | ||
| // | ||
| class MemStream { | ||
| public: | ||
| #ifdef FUSILLI_PLATFORM_WINDOWS | ||
| // Windows implementation using a temporary file. | ||
| MemStream() : stream_(nullptr), tempFilePath_() { | ||
|
Comment on lines
+53
to
+54
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The disk I/O can be significant compared to the pure memory operations in the linux case. What about alternatives like |
||
| // Create a temporary file. | ||
| char tempPath[MAX_PATH]; | ||
| char tempFileName[MAX_PATH]; | ||
| if (GetTempPathA(MAX_PATH, tempPath) == 0) { | ||
| return; | ||
| } | ||
| if (GetTempFileNameA(tempPath, "mem", 0, tempFileName) == 0) { | ||
| return; | ||
| } | ||
rsuderman marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| tempFilePath_ = tempFileName; | ||
| stream_ = fopen(tempFilePath_.c_str(), "w+b"); | ||
| } | ||
|
|
||
| ~MemStream() { | ||
| if (stream_) { | ||
| fclose(stream_); | ||
| stream_ = nullptr; | ||
| } | ||
| if (!tempFilePath_.empty()) { | ||
| std::remove(tempFilePath_.c_str()); | ||
| } | ||
| } | ||
|
|
||
| FILE *stream() { return stream_; } | ||
| bool isValid() const { return stream_ != nullptr; } | ||
|
|
||
| // Retrieve the contents as a string. | ||
| std::string str() { | ||
rsuderman marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
rsuderman marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (!stream_) | ||
| return ""; | ||
|
|
||
| // Flush pending writes. | ||
| fflush(stream_); | ||
|
|
||
| // Get the size. | ||
| long currentPos = ftell(stream_); | ||
| fseek(stream_, 0, SEEK_END); | ||
| long size = ftell(stream_); | ||
| fseek(stream_, 0, SEEK_SET); | ||
|
|
||
| // Read the contents. | ||
| std::string result(static_cast<size_t>(size), '\0'); | ||
| size_t bytesRead = | ||
| fread(result.data(), 1, static_cast<size_t>(size), stream_); | ||
| result.resize(bytesRead); | ||
|
|
||
| // Restore position. | ||
| fseek(stream_, currentPos, SEEK_SET); | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| // Get current size of the stream. | ||
| size_t size() { | ||
rsuderman marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| if (!stream_) | ||
| return 0; | ||
| fflush(stream_); | ||
| long currentPos = ftell(stream_); | ||
| fseek(stream_, 0, SEEK_END); | ||
| long endPos = ftell(stream_); | ||
| fseek(stream_, currentPos, SEEK_SET); | ||
| return static_cast<size_t>(endPos); | ||
| } | ||
|
|
||
| private: | ||
| FILE *stream_; | ||
| std::string tempFilePath_; | ||
|
|
||
| #elif FUSILLI_PLATFORM_LINUX | ||
| // Linux/POSIX implementation using open_memstream. | ||
| MemStream() : buffer_(nullptr), size_(0), stream_(nullptr) { | ||
| stream_ = open_memstream(&buffer_, &size_); | ||
| } | ||
|
|
||
| ~MemStream() { | ||
| if (stream_) { | ||
| fclose(stream_); | ||
| stream_ = nullptr; | ||
| } | ||
| if (buffer_) { | ||
| free(buffer_); | ||
| buffer_ = nullptr; | ||
| } | ||
| } | ||
|
|
||
| FILE *stream() { return stream_; } | ||
| bool isValid() const { return stream_ != nullptr; } | ||
|
|
||
| // Retrieve the contents as a string. | ||
| std::string str() { | ||
| if (!stream_) | ||
| return ""; | ||
| fflush(stream_); | ||
| return std::string(buffer_, size_); | ||
| } | ||
|
|
||
| // Get current size of the stream. | ||
| size_t size() { | ||
| if (!stream_) | ||
| return 0; | ||
| fflush(stream_); | ||
| return size_; | ||
| } | ||
|
|
||
| private: | ||
| char *buffer_; | ||
| size_t size_; | ||
| FILE *stream_; | ||
| #else | ||
| #error "MemStream is only implemented for Windows and Linux platforms." | ||
| #endif | ||
|
|
||
| public: | ||
| // Implicit conversion to FILE* for convenience. | ||
| operator FILE *() { return stream(); } | ||
|
|
||
| // Delete copy/move operations to prevent resource management issues. | ||
| MemStream(const MemStream &) = delete; | ||
| MemStream &operator=(const MemStream &) = delete; | ||
| MemStream(MemStream &&) = delete; | ||
| MemStream &operator=(MemStream &&) = delete; | ||
| }; | ||
|
|
||
| // An RAII adapter for writing to C++ std::strings using C-style fprints. | ||
| // This is useful for interfacing with C APIs that use FILE* for output. | ||
| // | ||
| // Example usage: | ||
| // std::string output; | ||
| // { | ||
| // FprintAdapter adapter(output); | ||
| // fprintf(adapter, "Value: %d", 42); | ||
| // } // adapter destructor copies content to output | ||
| // // output now contains "Value: 42" | ||
rsuderman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| // | ||
| class FprintAdapter { | ||
| public: | ||
| explicit FprintAdapter(std::string &output) : output_(output), ms_() {} | ||
|
|
||
| ~FprintAdapter() { | ||
| if (ms_.isValid()) { | ||
| output_ = ms_.str(); | ||
| } | ||
| } | ||
|
|
||
| FILE *stream() { return ms_.stream(); } | ||
| bool isValid() const { return ms_.isValid(); } | ||
|
|
||
| // Implicit conversion to FILE* for convenience. | ||
| operator FILE *() { return stream(); } | ||
|
|
||
| // Delete copy/move operations. | ||
| FprintAdapter(const FprintAdapter &) = delete; | ||
| FprintAdapter &operator=(const FprintAdapter &) = delete; | ||
| FprintAdapter(FprintAdapter &&) = delete; | ||
| FprintAdapter &operator=(FprintAdapter &&) = delete; | ||
|
|
||
| private: | ||
| std::string &output_; | ||
| MemStream ms_; | ||
| }; | ||
|
|
||
| } // namespace fusilli | ||
|
|
||
| #endif // FUSILLI_SUPPORT_MEMSTREAM_H | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.