Skip to content

Commit cf62f13

Browse files
authored
ShortList<T> and core.natvis improvements. (shader-slang#1430)
* ShortList<T> and core.natvis improvements. * Fix gcc build. * add `getBuffer()` accessor to `GetArrayViewResult`
1 parent ffd0b9c commit cf62f13

11 files changed

+699
-72
lines changed

source/core/core.natvis

+46-5
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
</Expand>
1919
</Type>
2020

21-
<Type Name="Slang::List&lt;*&gt;">
21+
<Type Name="Slang::List&lt;*,*&gt;">
2222
<DisplayString>{{ size={m_count} }}</DisplayString>
2323
<Expand>
2424
<Item Name="[size]">m_count</Item>
@@ -30,6 +30,18 @@
3030
</Expand>
3131
</Type>
3232

33+
<Type Name="Slang::ShortList&lt;*,*,*&gt;">
34+
<DisplayString>{{ size={m_count} }}</DisplayString>
35+
<Expand>
36+
<Item Name="[size]">m_count</Item>
37+
<Item Name="[capacity]">m_capacity</Item>
38+
<IndexListItems>
39+
<Size>m_count</Size>
40+
<ValueNode Condition="$i&lt;$T2">m_shortBuffer + $i</ValueNode>
41+
<ValueNode Condition="$i&gt;=$T2">m_buffer + $i - $T2</ValueNode>
42+
</IndexListItems>
43+
</Expand>
44+
</Type>
3345

3446
<Type Name="Slang::Array&lt;*,*&gt;">
3547
<DisplayString>{{ size={m_count} }}</DisplayString>
@@ -47,10 +59,33 @@
4759
<Expand>
4860
<Item Name="[size]">_count</Item>
4961
<Item Name="[capacity]">bucketSizeMinusOne + 1</Item>
50-
<ArrayItems>
51-
<Size>bucketSizeMinusOne + 1</Size>
52-
<ValuePointer>hashMap</ValuePointer>
53-
</ArrayItems>
62+
<CustomListItems MaxItemsPerView="5000" ExcludeView="Test">
63+
<Variable Name="iBucket" InitialValue="0" />
64+
<Variable Name="pBucket" InitialValue="hashMap" />
65+
<Variable Name="isDeleted" InitialValue="0" />
66+
<Variable Name="isEmpty" InitialValue="0" />
67+
<Size>_count</Size>
68+
<Exec>pBucket = hashMap</Exec>
69+
<Loop>
70+
<If Condition="iBucket &gt;= bucketSizeMinusOne + 1">
71+
<Break/>
72+
</If>
73+
<Exec>
74+
isDeleted = marks.m_buffer.m_count &gt; (iBucket*2+1)/32
75+
? ((marks.m_buffer.m_buffer[(iBucket*2+1)/32]&amp;(1&lt;&lt;(iBucket*2+1)%32)) != 0)
76+
: 0
77+
</Exec>
78+
<Exec>
79+
isEmpty = marks.m_buffer.m_count &gt; (iBucket*2)/32
80+
? ((marks.m_buffer.m_buffer[(iBucket*2)/32]&amp;(1&lt;&lt;(iBucket*2)%32)) == 0)
81+
: 1
82+
</Exec>
83+
<If Condition="isDeleted+isEmpty==0">
84+
<Item>*(hashMap + iBucket)</Item>
85+
</If>
86+
<Exec>iBucket++</Exec>
87+
</Loop>
88+
</CustomListItems>
5489
</Expand>
5590
</Type>
5691

@@ -103,4 +138,10 @@
103138
<StringView>(m_sizeThenContents + 1),s</StringView>
104139
</Type>
105140

141+
<Type Name="Slang::UnownedStringSlice">
142+
<DisplayString>{m_begin,[m_end-m_begin]s}</DisplayString>
143+
<StringView>m_begin,[m_end-m_begin]s</StringView>
144+
145+
</Type>
146+
106147
</AutoVisualizer>

source/core/core.vcxproj

+1
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@
198198
<ClInclude Include="slang-secure-crt.h" />
199199
<ClInclude Include="slang-semantic-version.h" />
200200
<ClInclude Include="slang-shared-library.h" />
201+
<ClInclude Include="slang-short-list.h" />
201202
<ClInclude Include="slang-smart-pointer.h" />
202203
<ClInclude Include="slang-std-writers.h" />
203204
<ClInclude Include="slang-stream.h" />

source/core/core.vcxproj.filters

+3
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,9 @@
138138
<ClInclude Include="windows\slang-win-visual-studio-util.h">
139139
<Filter>Header Files</Filter>
140140
</ClInclude>
141+
<ClInclude Include="slang-short-list.h">
142+
<Filter>Header Files</Filter>
143+
</ClInclude>
141144
</ItemGroup>
142145
<ItemGroup>
143146
<ClCompile Include="slang-blob.cpp">

source/core/slang-allocator.h

+70
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
#ifndef SLANG_CORE_ALLOCATOR_H
22
#define SLANG_CORE_ALLOCATOR_H
33

4+
#include "slang-common.h"
5+
46
#include <stdlib.h>
57
#ifdef _MSC_VER
68
# include <malloc.h>
79
#endif
810

11+
#include <type_traits>
12+
913
namespace Slang
1014
{
1115
inline void* alignedAllocate(size_t size, size_t alignment)
@@ -59,6 +63,72 @@ namespace Slang
5963
return alignedDeallocate(ptr);
6064
}
6165
};
66+
67+
// Helper utilties for calling allocators.
68+
template<typename T, int isPOD>
69+
class Initializer
70+
{
71+
72+
};
73+
74+
template<typename T>
75+
class Initializer<T, 0>
76+
{
77+
public:
78+
static void initialize(T* buffer, int size)
79+
{
80+
for (int i = 0; i < size; i++)
81+
new (buffer + i) T();
82+
}
83+
};
84+
template<typename T>
85+
class Initializer<T, 1>
86+
{
87+
public:
88+
static void initialize(T* buffer, int size)
89+
{
90+
// It's pod so no initialization required
91+
//for (int i = 0; i < size; i++)
92+
// new (buffer + i) T;
93+
}
94+
};
95+
96+
template<typename T, typename TAllocator>
97+
class AllocateMethod
98+
{
99+
public:
100+
static inline T* allocateArray(Index count)
101+
{
102+
TAllocator allocator;
103+
T* rs = (T*)allocator.allocate(count * sizeof(T));
104+
Initializer<T, std::is_pod<T>::value>::initialize(rs, count);
105+
return rs;
106+
}
107+
static inline void deallocateArray(T* ptr, Index count)
108+
{
109+
TAllocator allocator;
110+
if (!std::is_trivially_destructible<T>::value)
111+
{
112+
for (Index i = 0; i < count; i++)
113+
ptr[i].~T();
114+
}
115+
allocator.deallocate(ptr);
116+
}
117+
};
118+
119+
template<typename T>
120+
class AllocateMethod<T, StandardAllocator>
121+
{
122+
public:
123+
static inline T* allocateArray(Index count)
124+
{
125+
return new T[count];
126+
}
127+
static inline void deallocateArray(T* ptr, Index /*bufferSize*/)
128+
{
129+
delete[] ptr;
130+
}
131+
};
62132
}
63133

64134
#endif

source/core/slang-basic.h

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#include "slang-string.h"
77
#include "slang-array.h"
88
#include "slang-list.h"
9+
#include "slang-short-list.h"
910
#include "slang-smart-pointer.h"
1011
#include "slang-exception.h"
1112
#include "slang-dictionary.h"

source/core/slang-dictionary.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ namespace Slang
8282
private:
8383
inline int GetProbeOffset(int /*probeId*/) const
8484
{
85-
// quadratic probing
85+
// linear probing
8686
return 1;
8787
}
8888
private:

source/core/slang-list.h

+7-66
Original file line numberDiff line numberDiff line change
@@ -14,72 +14,6 @@
1414

1515
namespace Slang
1616
{
17-
18-
template<typename T, int isPOD>
19-
class Initializer
20-
{
21-
22-
};
23-
24-
template<typename T>
25-
class Initializer<T, 0>
26-
{
27-
public:
28-
static void initialize(T* buffer, int size)
29-
{
30-
for (int i = 0; i<size; i++)
31-
new (buffer + i) T();
32-
}
33-
};
34-
template<typename T>
35-
class Initializer<T, 1>
36-
{
37-
public:
38-
static void initialize(T* buffer, int size)
39-
{
40-
// It's pod so no initialization required
41-
//for (int i = 0; i < size; i++)
42-
// new (buffer + i) T;
43-
}
44-
};
45-
46-
template<typename T, typename TAllocator>
47-
class AllocateMethod
48-
{
49-
public:
50-
static inline T* allocateArray(Index count)
51-
{
52-
TAllocator allocator;
53-
T * rs = (T*)allocator.allocate(count * sizeof(T));
54-
Initializer<T, std::is_pod<T>::value>::initialize(rs, count);
55-
return rs;
56-
}
57-
static inline void deallocateArray(T* ptr, Index count)
58-
{
59-
TAllocator allocator;
60-
if (!std::is_trivially_destructible<T>::value)
61-
{
62-
for (Index i = 0; i < count; i++)
63-
ptr[i].~T();
64-
}
65-
allocator.deallocate(ptr);
66-
}
67-
};
68-
69-
template<typename T>
70-
class AllocateMethod<T, StandardAllocator>
71-
{
72-
public:
73-
static inline T* allocateArray(Index count)
74-
{
75-
return new T[count];
76-
}
77-
static inline void deallocateArray(T* ptr, Index /*bufferSize*/)
78-
{
79-
delete [] ptr;
80-
}
81-
};
82-
8317
// List is container of values of a type held consecutively in memory (much like std::vector)
8418
//
8519
// Note that in this implementation, the underlying memory is backed via an allocation of T[capacity]
@@ -102,6 +36,7 @@ namespace Slang
10236
}
10337
template<typename... Args>
10438
List(const T& val, Args... args)
39+
: m_buffer(nullptr), m_count(0), m_capacity(0)
10540
{
10641
_init(val, args...);
10742
}
@@ -611,13 +546,19 @@ namespace Slang
611546
{
612547
return AllocateMethod<T, TAllocator>::allocateArray(count);
613548
}
549+
static void _free(T* buffer, Index count)
550+
{
551+
return AllocateMethod<T, TAllocator>::deallocateArray(buffer, count);
552+
}
614553

615554
template<typename... Args>
616555
void _init(const T& val, Args... args)
617556
{
618557
add(val);
619558
_init(args...);
620559
}
560+
561+
void _init() {}
621562
};
622563

623564
template<typename T>

0 commit comments

Comments
 (0)