yum-mirror/slang
Making it easier to work with shaders
git clone https://git.yummers.dev/yum-mirror/slang
f65d756bf
master
1// slang-slice-allocator.cpp 2#include "slang-slice-allocator.h" 3 4#include "../core/slang-blob.h" 5 6namespace Slang 7{ 8 9/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! SliceUtil !!!!!!!!!!!!!!!!!!!!!!!!!!! */ 10 11/* static */ List < String > SliceUtil ::toList (const Slice < TerminatedCharSlice >& in ) 12{ 13List < String > list ; 14const auto count = in .count ; 15 16list .setCount (count ); 17for (Index i = 0 ;i < count ;++ i ) 18 { 19list [i ]= asStringSlice (in [i ]); 20 } 21return list ; 22} 23 24/* static */ const char * SliceUtil ::getTerminated (ISlangBlob * blob ,TerminatedCharSlice & outSlice ) 25{ 26const auto size = blob -> getBufferSize (); 27if (size == 0 ) 28 { 29outSlice = TerminatedCharSlice (); 30return outSlice .begin (); 31 } 32 33// If there is a 0 at the end byte, we are zero terminated 34const char * chars = (const char * )blob -> getBufferPointer (); 35if (chars [size - 1 ]== 0 ) 36 { 37outSlice = TerminatedCharSlice (chars ,Count (size - 1 )); 38return chars ; 39 } 40 41// See if it has a castable interface 42ComPtr < ICastable > castable ; 43if (SLANG_SUCCEEDED (blob -> queryInterface (SLANG_IID_PPV_ARGS (castable .writeRef ())))) 44 { 45if (castable -> castAs (SlangTerminatedChars ::getTypeGuid ())) 46 { 47outSlice = TerminatedCharSlice (chars ,Count (size )); 48return chars ; 49 } 50 } 51 52return nullptr ; 53} 54 55/* static */ TerminatedCharSlice SliceUtil ::toTerminatedCharSlice ( 56SliceAllocator & allocator , 57ISlangBlob * blob ) 58{ 59TerminatedCharSlice slice ; 60if (SliceUtil ::getTerminated (blob ,slice )) 61 { 62return slice ; 63 } 64const auto size = blob -> getBufferSize (); 65// We are out of options, we just have to allocate with zero termination which allocateString 66// does 67auto dst = 68allocator .getArena ().allocateString ((const char * )blob -> getBufferPointer (),Count (size )); 69return TerminatedCharSlice (dst ,Count (size )); 70} 71 72/* static */ TerminatedCharSlice SliceUtil ::toTerminatedCharSlice ( 73StringBuilder & storage , 74ISlangBlob * blob ) 75{ 76TerminatedCharSlice slice ; 77if (SliceUtil ::getTerminated (blob ,slice )) 78 { 79return slice ; 80 } 81 82const auto size = blob -> getBufferSize (); 83auto chars = (const char * )blob -> getBufferPointer (); 84 85storage .clear (); 86storage .append (UnownedStringSlice (chars ,size )); 87 88return TerminatedCharSlice (storage .getBuffer (),Count (size )); 89} 90 91/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! SliceAllocator !!!!!!!!!!!!!!!!!!!!!!!!!!! */ 92 93TerminatedCharSlice SliceAllocator ::allocate (const char * in ) 94{ 95const size_t length = ::strlen (in ); 96auto dst = m_arena .allocateString (in ,length ); 97return TerminatedCharSlice (dst ,length ); 98} 99 100TerminatedCharSlice SliceAllocator ::allocate (const UnownedStringSlice & slice ) 101{ 102const auto length = slice .getLength (); 103auto dst = m_arena .allocateString (slice .begin (),length ); 104return TerminatedCharSlice (dst ,length ); 105} 106 107TerminatedCharSlice SliceAllocator ::allocate (const Slice < char >& slice ) 108{ 109const auto count = slice .count ; 110auto dst = m_arena .allocateString (slice .begin (),count ); 111return TerminatedCharSlice (dst ,count ); 112} 113 114Slice < TerminatedCharSlice > SliceAllocator ::allocate (const List < String >& in ) 115{ 116const auto count = in .getCount (); 117if (count == 0 ) 118 { 119return Slice < TerminatedCharSlice > (nullptr ,0 ); 120 } 121 122auto dst = m_arena .allocateArray < TerminatedCharSlice > (count ); 123for (Index i = 0 ;i < count ;++ i ) 124 { 125dst [i ]= allocate (in [i ]); 126 } 127 128return Slice < TerminatedCharSlice > (dst ,count ); 129} 130 131}// namespace Slang