Skip to content

Commit 3ed960e

Browse files
committed
8253640: Make MEMFLAGS an enum class
Reviewed-by: stuefe, tschatzl
1 parent 86491a5 commit 3ed960e

8 files changed

+38
-34
lines changed

src/hotspot/share/gc/g1/g1RegionToSpaceMapper.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ G1RegionToSpaceMapper::G1RegionToSpaceMapper(ReservedSpace rs,
4242
size_t page_size,
4343
size_t region_granularity,
4444
size_t commit_factor,
45-
MemoryType type) :
45+
MEMFLAGS type) :
4646
_listener(NULL),
4747
_storage(rs, used_size, page_size),
4848
_region_granularity(region_granularity),
@@ -67,7 +67,7 @@ class G1RegionsLargerThanCommitSizeMapper : public G1RegionToSpaceMapper {
6767
size_t page_size,
6868
size_t alloc_granularity,
6969
size_t commit_factor,
70-
MemoryType type) :
70+
MEMFLAGS type) :
7171
G1RegionToSpaceMapper(rs, actual_size, page_size, alloc_granularity, commit_factor, type),
7272
_pages_per_region(alloc_granularity / (page_size * commit_factor)) {
7373

@@ -130,7 +130,7 @@ class G1RegionsSmallerThanCommitSizeMapper : public G1RegionToSpaceMapper {
130130
size_t page_size,
131131
size_t alloc_granularity,
132132
size_t commit_factor,
133-
MemoryType type) :
133+
MEMFLAGS type) :
134134
G1RegionToSpaceMapper(rs, actual_size, page_size, alloc_granularity, commit_factor, type),
135135
_regions_per_page((page_size * commit_factor) / alloc_granularity) {
136136

@@ -240,7 +240,7 @@ G1RegionToHeteroSpaceMapper::G1RegionToHeteroSpaceMapper(ReservedSpace rs,
240240
size_t page_size,
241241
size_t alloc_granularity,
242242
size_t commit_factor,
243-
MemoryType type) :
243+
MEMFLAGS type) :
244244
G1RegionToSpaceMapper(rs, actual_size, page_size, alloc_granularity, commit_factor, type),
245245
_rs(rs),
246246
_dram_mapper(NULL),
@@ -337,7 +337,7 @@ G1RegionToSpaceMapper* G1RegionToSpaceMapper::create_heap_mapper(ReservedSpace r
337337
size_t page_size,
338338
size_t region_granularity,
339339
size_t commit_factor,
340-
MemoryType type) {
340+
MEMFLAGS type) {
341341
if (AllocateOldGenAt != NULL) {
342342
G1RegionToHeteroSpaceMapper* mapper = new G1RegionToHeteroSpaceMapper(rs, actual_size, page_size, region_granularity, commit_factor, type);
343343
if (!mapper->initialize()) {
@@ -355,7 +355,7 @@ G1RegionToSpaceMapper* G1RegionToSpaceMapper::create_mapper(ReservedSpace rs,
355355
size_t page_size,
356356
size_t region_granularity,
357357
size_t commit_factor,
358-
MemoryType type) {
358+
MEMFLAGS type) {
359359
if (region_granularity >= (page_size * commit_factor)) {
360360
return new G1RegionsLargerThanCommitSizeMapper(rs, actual_size, page_size, region_granularity, commit_factor, type);
361361
} else {

src/hotspot/share/gc/g1/g1RegionToSpaceMapper.hpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,9 @@ class G1RegionToSpaceMapper : public CHeapObj<mtGC> {
5353
// Mapping management
5454
CHeapBitMap _region_commit_map;
5555

56-
MemoryType _memory_type;
56+
MEMFLAGS _memory_type;
5757

58-
G1RegionToSpaceMapper(ReservedSpace rs, size_t used_size, size_t page_size, size_t region_granularity, size_t commit_factor, MemoryType type);
58+
G1RegionToSpaceMapper(ReservedSpace rs, size_t used_size, size_t page_size, size_t region_granularity, size_t commit_factor, MEMFLAGS type);
5959

6060
void fire_on_commit(uint start_idx, size_t num_regions, bool zero_filled);
6161
public:
@@ -85,14 +85,14 @@ class G1RegionToSpaceMapper : public CHeapObj<mtGC> {
8585
size_t page_size,
8686
size_t region_granularity,
8787
size_t byte_translation_factor,
88-
MemoryType type);
88+
MEMFLAGS type);
8989

9090
static G1RegionToSpaceMapper* create_heap_mapper(ReservedSpace rs,
9191
size_t actual_size,
9292
size_t page_size,
9393
size_t region_granularity,
9494
size_t byte_translation_factor,
95-
MemoryType type);
95+
MEMFLAGS type);
9696
};
9797

9898
// G1RegionToSpaceMapper implementation where
@@ -106,10 +106,10 @@ class G1RegionToHeteroSpaceMapper : public G1RegionToSpaceMapper {
106106
uint _start_index_of_dram;
107107
size_t _page_size;
108108
size_t _commit_factor;
109-
MemoryType _type;
109+
MEMFLAGS _type;
110110

111111
public:
112-
G1RegionToHeteroSpaceMapper(ReservedSpace rs, size_t used_size, size_t page_size, size_t region_granularity, size_t commit_factor, MemoryType type);
112+
G1RegionToHeteroSpaceMapper(ReservedSpace rs, size_t used_size, size_t page_size, size_t region_granularity, size_t commit_factor, MEMFLAGS type);
113113
bool initialize();
114114
uint num_committed_dram() const;
115115
uint num_committed_nvdimm() const;

src/hotspot/share/memory/allocation.hpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,14 +151,20 @@ class AllocatedObj {
151151
/*
152152
* Memory types
153153
*/
154-
enum MemoryType {
154+
enum class MEMFLAGS {
155155
MEMORY_TYPES_DO(MEMORY_TYPE_DECLARE_ENUM)
156156
mt_number_of_types // number of memory types (mtDontTrack
157157
// is not included as validate type)
158158
};
159159

160-
typedef MemoryType MEMFLAGS;
160+
#define MEMORY_TYPE_SHORTNAME(type, human_readable) \
161+
constexpr MEMFLAGS type = MEMFLAGS::type;
161162

163+
// Generate short aliases for the enum values. E.g. mtGC instead of MEMFLAGS::mtGC.
164+
MEMORY_TYPES_DO(MEMORY_TYPE_SHORTNAME)
165+
166+
// Make an int version of the sentinel end value.
167+
constexpr int mt_number_of_types = static_cast<int>(MEMFLAGS::mt_number_of_types);
162168

163169
#if INCLUDE_NMT
164170

src/hotspot/share/services/mallocTracker.hpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -145,12 +145,6 @@ class MallocMemorySnapshot : public ResourceObj {
145145
return &_malloc[index];
146146
}
147147

148-
inline MallocMemory* by_index(int index) {
149-
assert(index >= 0, "Index out of bound");
150-
assert(index < mt_number_of_types, "Index out of bound");
151-
return &_malloc[index];
152-
}
153-
154148
inline MemoryCounter* malloc_overhead() {
155149
return &_tracking_header;
156150
}
@@ -269,7 +263,7 @@ class MallocHeader {
269263
return;
270264
}
271265

272-
_flags = flags;
266+
_flags = NMTUtil::flag_to_index(flags);
273267
set_size(size);
274268
if (level == NMT_detail) {
275269
size_t bucket_idx;

src/hotspot/share/services/memBaseline.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ int compare_malloc_site(const MallocSite& s1, const MallocSite& s2) {
6464
int compare_malloc_site_and_type(const MallocSite& s1, const MallocSite& s2) {
6565
int res = compare_malloc_site(s1, s2);
6666
if (res == 0) {
67-
res = (int)(s1.flag() - s2.flag());
67+
res = (int)(NMTUtil::flag_to_index(s1.flag()) - NMTUtil::flag_to_index(s2.flag()));
6868
}
6969

7070
return res;

src/hotspot/share/services/memReporter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ void MemDetailReporter::report_malloc_sites() {
261261
stack->print_on(out);
262262
out->print("%29s", " ");
263263
MEMFLAGS flag = malloc_site->flag();
264-
assert((flag >= 0 && flag < (int)mt_number_of_types) && flag != mtNone,
264+
assert(NMTUtil::flag_is_valid(flag) && flag != mtNone,
265265
"Must have a valid memory type");
266266
print_malloc(malloc_site->size(), malloc_site->count(),flag);
267267
out->print_cr("\n");

src/hotspot/share/services/nmtCommon.hpp

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,21 @@ const int NMT_TrackingStackDepth = 4;
4747
// A few common utilities for native memory tracking
4848
class NMTUtil : AllStatic {
4949
public:
50+
// Check if index is a valid MEMFLAGS enum value (including mtNone)
51+
static inline bool flag_index_is_valid(int index) {
52+
return index >= 0 && index < mt_number_of_types;
53+
}
54+
55+
// Check if flag value is a valid MEMFLAGS enum value (including mtNone)
56+
static inline bool flag_is_valid(MEMFLAGS flag) {
57+
const int index = static_cast<int>(flag);
58+
return flag_index_is_valid(index);
59+
}
60+
5061
// Map memory type to index
5162
static inline int flag_to_index(MEMFLAGS flag) {
52-
const int index = flag & 0xff;
53-
assert(index >= 0 && index < (int)mt_number_of_types, "Index out of bounds");
54-
return index;
63+
assert(flag_is_valid(flag), "Invalid flag");
64+
return static_cast<int>(flag);
5565
}
5666

5767
// Map memory type to human readable name
@@ -61,8 +71,8 @@ class NMTUtil : AllStatic {
6171

6272
// Map an index to memory type
6373
static MEMFLAGS index_to_flag(int index) {
64-
assert(index >= 0 && index < (int) mt_number_of_types, "Index out of bounds");
65-
return (MEMFLAGS)index;
74+
assert(flag_index_is_valid(index), "Invalid flag");
75+
return static_cast<MEMFLAGS>(index);
6676
}
6777

6878
// Memory size scale

src/hotspot/share/services/virtualMemoryTracker.hpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,6 @@ class VirtualMemorySnapshot : public ResourceObj {
9797
return &_virtual_memory[index];
9898
}
9999

100-
inline VirtualMemory* by_index(int index) {
101-
assert(index >= 0, "Index out of bound");
102-
assert(index < mt_number_of_types, "Index out of bound");
103-
return &_virtual_memory[index];
104-
}
105-
106100
inline size_t total_reserved() const {
107101
size_t amount = 0;
108102
for (int index = 0; index < mt_number_of_types; index ++) {

0 commit comments

Comments
 (0)