Skip to content

Commit e899321

Browse files
committed
Refactor remaining cases of memset(this, ...)
Use Memset<T> as the first base class of cache key types to ensure they get initialized before any other base classes or members get constructed. Bug: b/134932616 Change-Id: I8f28252696d6e017db11da068180d2425cdf1d57 Reviewed-on: https://swiftshader-review.googlesource.com/c/SwiftShader/+/33249 Tested-by: Nicolas Capens <[email protected]> Presubmit-Ready: Nicolas Capens <[email protected]> Kokoro-Presubmit: kokoro <[email protected]> Reviewed-by: Ben Clayton <[email protected]>
1 parent 302a972 commit e899321

6 files changed

Lines changed: 34 additions & 44 deletions

File tree

src/Device/PixelProcessor.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,35 +20,31 @@
2020
#include "Vulkan/VkDebug.hpp"
2121
#include "Vulkan/VkImageView.hpp"
2222

23-
#include <string.h>
23+
#include <cstring>
2424

2525
namespace sw
2626
{
27-
unsigned int PixelProcessor::States::computeHash()
27+
uint32_t PixelProcessor::States::computeHash()
2828
{
29-
unsigned int *state = (unsigned int*)this;
30-
unsigned int hash = 0;
29+
uint32_t *state = reinterpret_cast<uint32_t*>(this);
30+
uint32_t hash = 0;
3131

32-
for(unsigned int i = 0; i < sizeof(States) / 4; i++)
32+
for(unsigned int i = 0; i < sizeof(States) / sizeof(uint32_t); i++)
3333
{
3434
hash ^= state[i];
3535
}
3636

3737
return hash;
3838
}
3939

40-
PixelProcessor::State::State()
41-
{
42-
memset(this, 0, sizeof(State));
43-
}
44-
4540
bool PixelProcessor::State::operator==(const State &state) const
4641
{
4742
if(hash != state.hash)
4843
{
4944
return false;
5045
}
5146

47+
static_assert(is_memcmparable<State>::value, "Cannot memcmp State");
5248
return memcmp(static_cast<const States*>(this), static_cast<const States*>(&state), sizeof(States)) == 0;
5349
}
5450

src/Device/PixelProcessor.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,11 @@ namespace sw
3030
class PixelProcessor
3131
{
3232
public:
33-
struct States
33+
struct States : Memset<States>
3434
{
35-
unsigned int computeHash();
35+
States() : Memset(this, 0) {}
36+
37+
uint32_t computeHash();
3638

3739
uint64_t shaderID;
3840

@@ -70,16 +72,14 @@ namespace sw
7072

7173
struct State : States
7274
{
73-
State();
74-
7575
bool operator==(const State &state) const;
7676

7777
int colorWriteActive(int index) const
7878
{
7979
return (colorWriteMask >> (index * 4)) & 0xF;
8080
}
8181

82-
unsigned int hash;
82+
uint32_t hash;
8383
};
8484

8585
struct Stencil

src/Device/SetupProcessor.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,33 +23,31 @@
2323
#include "Vulkan/VkDebug.hpp"
2424
#include "Pipeline/SpirvShader.hpp"
2525

26+
#include <cstring>
27+
2628
namespace sw
2729
{
28-
unsigned int SetupProcessor::States::computeHash()
30+
uint32_t SetupProcessor::States::computeHash()
2931
{
30-
unsigned int *state = (unsigned int*)this;
31-
unsigned int hash = 0;
32+
uint32_t *state = reinterpret_cast<uint32_t*>(this);
33+
uint32_t hash = 0;
3234

33-
for(unsigned int i = 0; i < sizeof(States) / 4; i++)
35+
for(unsigned int i = 0; i < sizeof(States) / sizeof(uint32_t); i++)
3436
{
3537
hash ^= state[i];
3638
}
3739

3840
return hash;
3941
}
4042

41-
SetupProcessor::State::State(int i)
42-
{
43-
memset(this, 0, sizeof(State));
44-
}
45-
4643
bool SetupProcessor::State::operator==(const State &state) const
4744
{
4845
if(hash != state.hash)
4946
{
5047
return false;
5148
}
5249

50+
static_assert(is_memcmparable<State>::value, "Cannot memcmp States");
5351
return memcmp(static_cast<const States*>(this), static_cast<const States*>(&state), sizeof(States)) == 0;
5452
}
5553

src/Device/SetupProcessor.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@ namespace sw
3232
class SetupProcessor
3333
{
3434
public:
35-
struct States
35+
struct States : Memset<States>
3636
{
37-
unsigned int computeHash();
37+
States() : Memset(this, 0) {}
38+
39+
uint32_t computeHash();
3840

3941
bool isDrawPoint : 1;
4042
bool isDrawLine : 1;
@@ -53,11 +55,9 @@ namespace sw
5355

5456
struct State : States
5557
{
56-
State(int i = 0);
57-
5858
bool operator==(const State &states) const;
5959

60-
unsigned int hash;
60+
uint32_t hash;
6161
};
6262

6363
typedef bool (*RoutinePointer)(Primitive *primitive, const Triangle *triangle, const Polygon *polygon, const DrawData *draw);

src/Device/VertexProcessor.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
#include "System/Math.hpp"
2020
#include "Vulkan/VkDebug.hpp"
2121

22-
#include <string.h>
22+
#include <cstring>
2323

2424
namespace sw
2525
{
@@ -31,31 +31,27 @@ namespace sw
3131
}
3232
}
3333

34-
unsigned int VertexProcessor::States::computeHash()
34+
uint32_t VertexProcessor::States::computeHash()
3535
{
36-
unsigned int *state = (unsigned int*)this;
37-
unsigned int hash = 0;
36+
uint32_t *state = reinterpret_cast<uint32_t*>(this);
37+
uint32_t hash = 0;
3838

39-
for(unsigned int i = 0; i < sizeof(States) / 4; i++)
39+
for(unsigned int i = 0; i < sizeof(States) / sizeof(uint32_t); i++)
4040
{
4141
hash ^= state[i];
4242
}
4343

4444
return hash;
4545
}
4646

47-
VertexProcessor::State::State()
48-
{
49-
memset(this, 0, sizeof(State));
50-
}
51-
5247
bool VertexProcessor::State::operator==(const State &state) const
5348
{
5449
if(hash != state.hash)
5550
{
5651
return false;
5752
}
5853

54+
static_assert(is_memcmparable<State>::value, "Cannot memcmp States");
5955
return memcmp(static_cast<const States*>(this), static_cast<const States*>(&state), sizeof(States)) == 0;
6056
}
6157

src/Device/VertexProcessor.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,11 @@ namespace sw
4949
class VertexProcessor
5050
{
5151
public:
52-
struct States
52+
struct States : Memset<States>
5353
{
54-
unsigned int computeHash();
54+
States() : Memset(this, 0) {}
55+
56+
uint32_t computeHash();
5557

5658
uint64_t shaderID;
5759

@@ -73,11 +75,9 @@ namespace sw
7375

7476
struct State : States
7577
{
76-
State();
77-
7878
bool operator==(const State &state) const;
7979

80-
unsigned int hash;
80+
uint32_t hash;
8181
};
8282

8383
typedef void (*RoutinePointer)(Vertex *output, unsigned int *batch, VertexTask *vertexTask, DrawData *draw);

0 commit comments

Comments
 (0)