bytecode_ptr: add make_zeroed_bytecode_ptr

Rather than always zeroing memory.
This commit is contained in:
Justin Viiret 2017-04-04 11:02:33 +10:00 committed by Matthew Barr
parent 5653fa55a1
commit 63fe84c3f1
19 changed files with 40 additions and 24 deletions

View File

@ -162,7 +162,7 @@ bytecode_ptr<FDR> FDRCompiler::setupFDR() {
headerSize, tabSize, confirmTmp.size(), floodControlTmp.size(),
size);
auto fdr = make_bytecode_ptr<FDR>(size, 64);
auto fdr = make_zeroed_bytecode_ptr<FDR>(size, 64);
assert(fdr); // otherwise would have thrown std::bad_alloc
fdr->size = size;

View File

@ -284,7 +284,7 @@ bytecode_ptr<FDRConfirm> getFDRConfirm(const vector<hwlmLiteral> &lits,
sizeof(LitInfo) * lits.size() + totalLitSize;
size = ROUNDUP_N(size, alignof(FDRConfirm));
auto fdrc = make_bytecode_ptr<FDRConfirm>(size);
auto fdrc = make_zeroed_bytecode_ptr<FDRConfirm>(size);
assert(fdrc); // otherwise would have thrown std::bad_alloc
fdrc->andmsk = andmsk;
@ -373,7 +373,7 @@ setupFullConfs(const vector<hwlmLiteral> &lits,
u32 totalConfSwitchSize = nBuckets * sizeof(u32);
u32 totalSize = ROUNDUP_16(totalConfSwitchSize + totalConfirmSize);
auto buf = make_bytecode_ptr<u8>(totalSize, 16);
auto buf = make_zeroed_bytecode_ptr<u8>(totalSize, 16);
assert(buf); // otherwise would have thrown std::bad_alloc
u32 *confBase = (u32 *)buf.get();

View File

@ -207,7 +207,7 @@ bytecode_ptr<u8> setupFDRFloodControl(const vector<hwlmLiteral> &lits,
size_t floodStructSize = sizeof(FDRFlood) * nDistinctFloods;
size_t totalSize = ROUNDUP_16(floodHeaderSize + floodStructSize);
auto buf = make_bytecode_ptr<u8>(totalSize, 16);
auto buf = make_zeroed_bytecode_ptr<u8>(totalSize, 16);
assert(buf); // otherwise would have thrown std::bad_alloc
u32 *floodHeader = (u32 *)buf.get();

View File

@ -324,7 +324,7 @@ bytecode_ptr<FDR> TeddyCompiler::build() {
floodControlTmp.size(),
16 * maskWidth);
auto fdr = make_bytecode_ptr<FDR>(size, 64);
auto fdr = make_zeroed_bytecode_ptr<FDR>(size, 64);
assert(fdr); // otherwise would have thrown std::bad_alloc
Teddy *teddy = (Teddy *)fdr.get(); // ugly
u8 *teddy_base = (u8 *)teddy;

View File

@ -165,7 +165,8 @@ bytecode_ptr<HWLM> hwlmBuild(const vector<hwlmLiteral> &lits, bool make_small,
throw ResourceLimitError();
}
auto h = make_bytecode_ptr<HWLM>(ROUNDUP_CL(sizeof(HWLM)) + engSize, 64);
const size_t hwlm_len = ROUNDUP_CL(sizeof(HWLM)) + engSize;
auto h = make_zeroed_bytecode_ptr<HWLM>(hwlm_len, 64);
h->type = engType;
memcpy(HWLM_DATA(h.get()), eng.get(), engSize);

View File

@ -74,7 +74,7 @@ bytecode_ptr<noodTable> noodBuildTable(const hwlmLiteral &lit) {
const auto &s = lit.s;
size_t noodle_len = sizeof(noodTable) + s.length();
auto n = make_bytecode_ptr<noodTable>(noodle_len);
auto n = make_zeroed_bytecode_ptr<noodTable>(noodle_len);
assert(n);
size_t key_offset = findNoodFragOffset(lit);

View File

@ -579,7 +579,7 @@ buildCastle(const CastleProto &proto,
total_size = ROUNDUP_N(total_size, alignof(mmbit_sparse_iter));
total_size += byte_length(stale_iter); // stale sparse iter
auto nfa = make_bytecode_ptr<NFA>(total_size);
auto nfa = make_zeroed_bytecode_ptr<NFA>(total_size);
nfa->type = verify_u8(CASTLE_NFA);
nfa->length = verify_u32(total_size);
nfa->nPositions = verify_u32(subs.size());

View File

@ -1116,7 +1116,7 @@ bytecode_ptr<NFA> goughCompile(raw_som_dfa &raw, u8 somPrecision,
gi.stream_som_loc_width = somPrecision;
u32 gough_size = ROUNDUP_N(curr_offset, 16);
auto gough_dfa = make_bytecode_ptr<NFA>(gough_size);
auto gough_dfa = make_zeroed_bytecode_ptr<NFA>(gough_size);
memcpy(gough_dfa.get(), basic_dfa.get(), basic_dfa->length);
memcpy((char *)gough_dfa.get() + haig_offset, &gi, sizeof(gi));

View File

@ -1797,7 +1797,7 @@ struct Factory {
u32 tableOffset, tugMaskOffset;
size_t len = repeatAllocSize(br, &tableOffset, &tugMaskOffset);
auto info = make_bytecode_ptr<NFARepeatInfo>(len);
auto info = make_zeroed_bytecode_ptr<NFARepeatInfo>(len);
char *info_ptr = (char *)info.get();
// Collect state space info.
@ -2297,7 +2297,7 @@ struct Factory {
size_t nfaSize = sizeof(NFA) + offset;
DEBUG_PRINTF("nfa size %zu\n", nfaSize);
auto nfa = make_bytecode_ptr<NFA>(nfaSize);
auto nfa = make_zeroed_bytecode_ptr<NFA>(nfaSize);
assert(nfa); // otherwise we would have thrown std::bad_alloc
implNFA_t *limex = (implNFA_t *)getMutableImplNfa(nfa.get());

View File

@ -496,7 +496,7 @@ bytecode_ptr<NFA> mcclellanCompile16(dfa_info &info, const CompileContext &cc,
accel_offset -= sizeof(NFA); /* adj accel offset to be relative to m */
assert(ISALIGNED_N(accel_offset, alignof(union AccelAux)));
auto nfa = make_bytecode_ptr<NFA>(total_size);
auto nfa = make_zeroed_bytecode_ptr<NFA>(total_size);
char *nfa_base = (char *)nfa.get();
populateBasicInfo(sizeof(u16), info, total_size, aux_offset, accel_offset,
@ -715,7 +715,7 @@ bytecode_ptr<NFA> mcclellanCompile8(dfa_info &info, const CompileContext &cc,
accel_offset -= sizeof(NFA); /* adj accel offset to be relative to m */
assert(ISALIGNED_N(accel_offset, alignof(union AccelAux)));
auto nfa = bytecode_ptr<NFA>(total_size);
auto nfa = make_zeroed_bytecode_ptr<NFA>(total_size);
char *nfa_base = (char *)nfa.get();
mcclellan *m = (mcclellan *)getMutableImplNfa(nfa.get());

View File

@ -872,7 +872,7 @@ bytecode_ptr<NFA> mcshengCompile16(dfa_info &info, dstate_id_t sheng_end,
accel_offset -= sizeof(NFA); /* adj accel offset to be relative to m */
assert(ISALIGNED_N(accel_offset, alignof(union AccelAux)));
auto nfa = make_bytecode_ptr<NFA>(total_size);
auto nfa = make_zeroed_bytecode_ptr<NFA>(total_size);
mcsheng *m = (mcsheng *)getMutableImplNfa(nfa.get());
populateBasicInfo(sizeof(u16), info, total_size, aux_offset, accel_offset,
@ -998,7 +998,7 @@ bytecode_ptr<NFA> mcshengCompile8(dfa_info &info, dstate_id_t sheng_end,
accel_offset -= sizeof(NFA); /* adj accel offset to be relative to m */
assert(ISALIGNED_N(accel_offset, alignof(union AccelAux)));
auto nfa = make_bytecode_ptr<NFA>(total_size);
auto nfa = make_zeroed_bytecode_ptr<NFA>(total_size);
mcsheng *m = (mcsheng *)getMutableImplNfa(nfa.get());
allocateImplId8(info, sheng_end, accel_escape_info, &m->accel_limit_8,

View File

@ -343,7 +343,7 @@ bytecode_ptr<NFA> mpvCompile(const vector<raw_puff> &puffs_in,
DEBUG_PRINTF("%u puffs, len = %u\n", puffette_count, len);
auto nfa = make_bytecode_ptr<NFA>(len);
auto nfa = make_zeroed_bytecode_ptr<NFA>(len);
mpv_puffette *pa_base = (mpv_puffette *)
((char *)nfa.get() + sizeof(NFA) + sizeof(mpv)

View File

@ -507,7 +507,7 @@ bytecode_ptr<NFA> shengCompile(raw_dfa &raw, const CompileContext &cc,
DEBUG_PRINTF("NFA: %u, aux: %u, reports: %u, accel: %u, total: %u\n",
nfa_size, total_aux, total_reports, total_accel, total_size);
auto nfa = make_bytecode_ptr<NFA>(total_size);
auto nfa = make_zeroed_bytecode_ptr<NFA>(total_size);
populateBasicInfo(nfa.get(), info, accelInfo, nfa_size, reports_offset,
accel_offset, total_size, total_size - sizeof(NFA));

View File

@ -134,7 +134,7 @@ buildTamarama(const TamaInfo &tamaInfo, const u32 queue,
// use subSize as a sentinel value for no active subengines,
// so add one to subSize here
u32 activeIdxSize = calcPackedBytes(subSize + 1);
auto nfa = make_bytecode_ptr<NFA>(total_size);
auto nfa = make_zeroed_bytecode_ptr<NFA>(total_size);
nfa->type = verify_u8(TAMARAMA_NFA);
nfa->length = verify_u32(total_size);
nfa->queueIndex = queue;

View File

@ -137,7 +137,7 @@ bytecode_ptr<NFA> makeLbrNfa(NFAEngineType nfa_type, enum RepeatType rtype,
}
size_t len = sizeof(NFA) + sizeof(LbrStruct) + sizeof(RepeatInfo) +
tableLen + sizeof(u64a);
auto nfa = make_bytecode_ptr<NFA>(len);
auto nfa = make_zeroed_bytecode_ptr<NFA>(len);
nfa->type = verify_u8(nfa_type);
nfa->length = verify_u32(len);
return nfa;

View File

@ -891,7 +891,8 @@ buildAnchoredMatcher(RoseBuildImpl &build, const vector<LitFragment> &fragments,
throw ResourceLimitError();
}
auto atable = make_bytecode_ptr<anchored_matcher_info>(total_size, 64);
auto atable =
make_zeroed_bytecode_ptr<anchored_matcher_info>(total_size, 64);
char *curr = (char *)atable.get();
u32 state_offset = 0;

View File

@ -5612,7 +5612,7 @@ bytecode_ptr<RoseEngine> addSmallWriteEngine(const RoseBuildImpl &build,
const size_t smwrOffset = ROUNDUP_CL(mainSize);
const size_t newSize = smwrOffset + smallWriteSize;
auto rose2 = make_bytecode_ptr<RoseEngine>(newSize, 64);
auto rose2 = make_zeroed_bytecode_ptr<RoseEngine>(newSize, 64);
char *ptr = (char *)rose2.get();
memcpy(ptr, rose.get(), mainSize);
memcpy(ptr + smwrOffset, smwr_engine.get(), smallWriteSize);
@ -5958,7 +5958,7 @@ bytecode_ptr<RoseEngine> RoseBuildImpl::buildFinalEngine(u32 minWidth) {
proto.size = currOffset;
// Time to allocate the real RoseEngine structure, at cacheline alignment.
auto engine = make_bytecode_ptr<RoseEngine>(currOffset, 64);
auto engine = make_zeroed_bytecode_ptr<RoseEngine>(currOffset, 64);
assert(engine); // will have thrown bad_alloc otherwise.
// Copy in our prototype engine data.

View File

@ -504,7 +504,7 @@ bytecode_ptr<SmallWriteEngine> SmallWriteBuildImpl::build(u32 roseQuality) {
}
u32 size = sizeof(SmallWriteEngine) + nfa->length;
auto smwr = make_bytecode_ptr<SmallWriteEngine>(size);
auto smwr = make_zeroed_bytecode_ptr<SmallWriteEngine>(size);
smwr->size = size;
smwr->start_offset = start_offset;

View File

@ -63,7 +63,6 @@ public:
if (!ptr) {
throw std::bad_alloc();
}
std::memset(ptr.get(), 0, bytes);
}
bytecode_ptr(std::nullptr_t) {}
@ -122,12 +121,27 @@ private:
size_t alignment = 0; //!< Alignment of memory region in bytes.
};
/**
* \brief Constructs a bytecode_ptr<T> with the given size and alignment.
*/
template<typename T>
inline bytecode_ptr<T> make_bytecode_ptr(size_t size,
size_t align = alignof(T)) {
return bytecode_ptr<T>(size, align);
}
/**
* \brief Constructs a bytecode_ptr<T> with the given size and alignment and
* fills the memory region with zeroes.
*/
template<typename T>
inline bytecode_ptr<T> make_zeroed_bytecode_ptr(size_t size,
size_t align = alignof(T)) {
auto ptr = make_bytecode_ptr<T>(size, align);
std::memset(ptr.get(), 0, size);
return ptr;
}
} // namespace ue2
#endif // UTIL_BYTECODE_PTR_H