Skip to content

Commit

Permalink
Move cellkind number calculation to CellKind.h
Browse files Browse the repository at this point in the history
Summary:
Move the calculation of the number of CellKinds to `CellKind.h`
so that we can use it in other headers.

Given that it is now a constexpr value, we can simplify the
`MetadataStorage` construction to be a `std::array` and initialiser
list.

Reviewed By: dulinriley

Differential Revision: D27991149

fbshipit-source-id: e6d5bb818bca9ee8b332891356c278f5b4aeadd7
  • Loading branch information
neildhar authored and facebook-github-bot committed Apr 28, 2021
1 parent e206637 commit 99199d9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 29 deletions.
6 changes: 6 additions & 0 deletions include/hermes/VM/CellKind.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#ifndef HERMES_VM_CELLKIND_H
#define HERMES_VM_CELLKIND_H

#include <cstddef>
#include <type_traits>

namespace hermes {
Expand Down Expand Up @@ -43,6 +44,11 @@ cellKindsContiguousAscending(CellKind v1, CellKind v2, T... rest) {

const char *cellKindStr(CellKind kind);

static constexpr size_t kNumCellKinds = 0
#define CELL_KIND(name) +1
#include "hermes/VM/CellKinds.def"
;

} // namespace vm
} // namespace hermes

Expand Down
37 changes: 8 additions & 29 deletions lib/VM/BuildMetadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,6 @@
namespace hermes {
namespace vm {

static size_t getNumCellKinds() {
// This embeds the same value as the CellKind enum, but adds one more at the
// end to know how many values it contains.
enum CellKinds {
#define CELL_KIND(name) name,
#include "hermes/VM/CellKinds.def"
#undef CELL_KIND
numKinds,
};
return numKinds;
}

/// Creates and populates the storage for the metadata of the classes.
/// The caller takes ownership of the memory.
static const Metadata *buildStorage() {
// For each class of object, initialize its metadata
// Only run this once per class, not once per Runtime instantiation.
Metadata *storage = new Metadata[getNumCellKinds()];
size_t i = 0;
#define CELL_KIND(name) \
storage[i++] = buildMetadata(CellKind::name##Kind, name##BuildMeta);
#include "hermes/VM/CellKinds.def"
#undef CELL_KIND
assert(i == getNumCellKinds() && "Incorrect number of metadata populated");
return storage;
}

Metadata buildMetadata(CellKind kind, BuildMetadataCallback *builder) {
const GCCell *base;
#ifdef HERMES_UBSAN
Expand All @@ -66,10 +39,16 @@ Metadata buildMetadata(CellKind kind, BuildMetadataCallback *builder) {
}

MetadataTable getMetadataTable() {
// For each class of object, initialize its metadata
// Only run this once per class, not once per Runtime instantiation.
// We intentionally leak memory here in order to avoid any static destructors
// running at exit time.
static const Metadata *storage = buildStorage();
return MetadataTable(storage, getNumCellKinds());
static const auto *storage = new std::array<Metadata, kNumCellKinds>{
#define CELL_KIND(name) buildMetadata(CellKind::name##Kind, name##BuildMeta),
#include "hermes/VM/CellKinds.def"
#undef CELL_KIND
};
return *storage;
}

} // namespace vm
Expand Down

0 comments on commit 99199d9

Please sign in to comment.