Skip to content

Commit b3ca0f3

Browse files
committed
[scudo] Use MemMap in Vector
Reviewed By: Chia-hungDuan Differential Revision: https://reviews.llvm.org/D159449
1 parent 078651b commit b3ca0f3

File tree

1 file changed

+32
-19
lines changed
  • compiler-rt/lib/scudo/standalone

1 file changed

+32
-19
lines changed

compiler-rt/lib/scudo/standalone/vector.h

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,20 @@
99
#ifndef SCUDO_VECTOR_H_
1010
#define SCUDO_VECTOR_H_
1111

12-
#include "common.h"
12+
#include "mem_map.h"
1313

1414
#include <string.h>
1515

1616
namespace scudo {
1717

18-
// A low-level vector based on map. May incur a significant memory overhead for
19-
// small vectors. The current implementation supports only POD types.
18+
// A low-level vector based on map. It stores the contents inline up to a fixed
19+
// capacity, or in an external memory buffer if it grows bigger than that. May
20+
// incur a significant memory overhead for small vectors. The current
21+
// implementation supports only POD types.
22+
//
23+
// NOTE: This class is not meant to be used directly, use Vector<T> instead.
2024
template <typename T> class VectorNoCtor {
2125
public:
22-
constexpr void init(uptr InitialCapacity = 0) {
23-
Data = &LocalData[0];
24-
CapacityBytes = sizeof(LocalData);
25-
if (InitialCapacity > capacity())
26-
reserve(InitialCapacity);
27-
}
28-
void destroy() {
29-
if (Data != &LocalData[0])
30-
unmap(Data, CapacityBytes, 0, &MapData);
31-
}
3226
T &operator[](uptr I) {
3327
DCHECK_LT(I, Size);
3428
return Data[I];
@@ -78,24 +72,43 @@ template <typename T> class VectorNoCtor {
7872
const T *end() const { return data() + size(); }
7973
T *end() { return data() + size(); }
8074

75+
protected:
76+
constexpr void init(uptr InitialCapacity = 0) {
77+
Data = &LocalData[0];
78+
CapacityBytes = sizeof(LocalData);
79+
if (InitialCapacity > capacity())
80+
reserve(InitialCapacity);
81+
}
82+
void destroy() {
83+
if (Data != &LocalData[0])
84+
ExternalBuffer.unmap(ExternalBuffer.getBase(),
85+
ExternalBuffer.getCapacity());
86+
}
87+
8188
private:
8289
void reallocate(uptr NewCapacity) {
8390
DCHECK_GT(NewCapacity, 0);
8491
DCHECK_LE(Size, NewCapacity);
92+
93+
MemMapT NewExternalBuffer;
8594
NewCapacity = roundUp(NewCapacity * sizeof(T), getPageSizeCached());
86-
T *NewData = reinterpret_cast<T *>(
87-
map(nullptr, NewCapacity, "scudo:vector", 0, &MapData));
88-
memcpy(NewData, Data, Size * sizeof(T));
95+
NewExternalBuffer.map(/*Addr=*/0U, NewCapacity, "scudo:vector");
96+
T *NewExternalData = reinterpret_cast<T *>(NewExternalBuffer.getBase());
97+
98+
memcpy(NewExternalData, Data, Size * sizeof(T));
8999
destroy();
90-
Data = NewData;
100+
101+
Data = NewExternalData;
91102
CapacityBytes = NewCapacity;
103+
ExternalBuffer = NewExternalBuffer;
92104
}
93105

94106
T *Data = nullptr;
95-
T LocalData[256 / sizeof(T)] = {};
96107
uptr CapacityBytes = 0;
97108
uptr Size = 0;
98-
[[no_unique_address]] MapPlatformData MapData = {};
109+
110+
T LocalData[256 / sizeof(T)] = {};
111+
MemMapT ExternalBuffer;
99112
};
100113

101114
template <typename T> class Vector : public VectorNoCtor<T> {

0 commit comments

Comments
 (0)