diff options
| author | jsmall-nvidia <jsmall@nvidia.com> | 2022-08-26 20:32:53 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-08-26 20:32:53 -0400 |
| commit | 5c2c2cfc9918bb43225159e67a851e196e17759a (patch) | |
| tree | 216009d02afe9dc17b074fdd394141ef71472268 /source/compiler-core/slang-slice-allocator.cpp | |
| parent | ef067bef2f2188a4b3c420cbcd8d223874888ed2 (diff) | |
DownstreamCompileOptions using POD types (#2381)
* #include an absolute path didn't work - because paths were taken to always be relative.
* Make DownstreamCompileOptions use POD types.
* CharSliceAllocator -> SliceAllocator
Added SliceConverter
CharSliceCaster -> SliceCaster
* First attempt at zero terminating around blobs.
* Fix clang warning.
* Add SlangTerminatedChars
Make Blob implementations support it.
Make most blobs 'terminated'.
* Fix bug setting up sourceFiles for CommandLineDownstreamCompiler.
* Traffic in TerminatedCharSlice for sourceFiles.
Use ArtifactDesc to generate temporary file names for source.
* Fix typo in testing for shared library/C++.
Diffstat (limited to 'source/compiler-core/slang-slice-allocator.cpp')
| -rw-r--r-- | source/compiler-core/slang-slice-allocator.cpp | 94 |
1 files changed, 94 insertions, 0 deletions
diff --git a/source/compiler-core/slang-slice-allocator.cpp b/source/compiler-core/slang-slice-allocator.cpp new file mode 100644 index 000000000..9985f6b19 --- /dev/null +++ b/source/compiler-core/slang-slice-allocator.cpp @@ -0,0 +1,94 @@ +// slang-slice-allocator.cpp +#include "slang-slice-allocator.h" + +#include "../core/slang-blob.h" + +namespace Slang { + +/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! SliceConverter !!!!!!!!!!!!!!!!!!!!!!!!!!! */ + +/* static */ List<String> SliceConverter::toList(const Slice<TerminatedCharSlice>& in) +{ + List<String> list; + const auto count = in.count; + + list.setCount(count); + for (Index i = 0; i < count; ++i) + { + list[i] = asStringSlice(in[i]); + } + return list; +} + +/* static */TerminatedCharSlice SliceConverter::toTerminatedCharSlice(SliceAllocator& allocator, ISlangBlob* blob) +{ + const auto size = blob->getBufferSize(); + + if (size == 0) + { + return TerminatedCharSlice(); + } + + // If there is a 0 at the end byte, we are zero terminated + const char* chars = (const char*)blob->getBufferPointer(); + if (chars[size - 1] == 0) + { + return TerminatedCharSlice(chars, Count(size - 1)); + } + + // See if it has a castable interface + ComPtr<ICastable> castable; + if (SLANG_SUCCEEDED(blob->queryInterface(ICastable::getTypeGuid(), (void**)castable.writeRef()))) + { + if (castable->castAs(SlangTerminatedChars::getTypeGuid())) + { + return TerminatedCharSlice(chars, Count(size)); + } + } + + // We are out of options, we just have to allocate with zero termination which allocateString does + auto dst = allocator.getArena().allocateString(chars, Count(size)); + return TerminatedCharSlice(dst, Count(size)); +} + +/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! SliceAllocator !!!!!!!!!!!!!!!!!!!!!!!!!!! */ + +TerminatedCharSlice SliceAllocator::allocate(const char* in) +{ + const size_t length = ::strlen(in); + auto dst = m_arena.allocateString(in, length); + return TerminatedCharSlice(dst, length); +} + +TerminatedCharSlice SliceAllocator::allocate(const UnownedStringSlice& slice) +{ + const auto length = slice.getLength(); + auto dst = m_arena.allocateString(slice.begin(), length); + return TerminatedCharSlice(dst, length); +} + +TerminatedCharSlice SliceAllocator::allocate(const Slice<char>& slice) +{ + const auto count = slice.count; + auto dst = m_arena.allocateString(slice.begin(), count); + return TerminatedCharSlice(dst, count); +} + +Slice<TerminatedCharSlice> SliceAllocator::allocate(const List<String>& in) +{ + const auto count = in.getCount(); + if (count == 0) + { + return Slice<TerminatedCharSlice>(nullptr, 0); + } + + auto dst = m_arena.allocateArray<TerminatedCharSlice>(count); + for (Index i = 0; i < count; ++i) + { + dst[i] = allocate(in[i]); + } + + return Slice<TerminatedCharSlice>(dst, count); +} + +} // namespace Slang |
