diff --git a/src/fdr/fdr_compile.cpp b/src/fdr/fdr_compile.cpp index 885c2c87..f8d1bd0e 100644 --- a/src/fdr/fdr_compile.cpp +++ b/src/fdr/fdr_compile.cpp @@ -81,7 +81,7 @@ private: void dumpMasks(const u8 *defaultMask); #endif void setupTab(); - aligned_unique_ptr setupFDR(pair link); + aligned_unique_ptr setupFDR(pair, size_t> &link); void createInitialState(FDR *fdr); public: @@ -90,7 +90,7 @@ public: : eng(eng_in), tab(eng_in.getTabSizeBytes()), lits(lits_in), make_small(make_small_in) {} - aligned_unique_ptr build(pair link); + aligned_unique_ptr build(pair, size_t> &link); }; u8 *FDRCompiler::tabIndexToMask(u32 indexInTable) { @@ -141,7 +141,8 @@ void FDRCompiler::createInitialState(FDR *fdr) { } } -aligned_unique_ptr FDRCompiler::setupFDR(pair link) { +aligned_unique_ptr +FDRCompiler::setupFDR(pair, size_t> &link) { size_t tabSize = eng.getTabSizeBytes(); auto floodControlTmp = setupFDRFloodControl(lits, eng); @@ -189,8 +190,7 @@ aligned_unique_ptr FDRCompiler::setupFDR(pair link) { if (link.first) { fdr->link = verify_u32(ptr - fdr_base); - memcpy(ptr, link.first, link.second); - aligned_free(link.first); + memcpy(ptr, link.first.get(), link.second); } else { fdr->link = 0; } @@ -498,7 +498,8 @@ void FDRCompiler::setupTab() { #endif } -aligned_unique_ptr FDRCompiler::build(pair link) { +aligned_unique_ptr +FDRCompiler::build(pair, size_t> &link) { assignStringsToBuckets(); setupTab(); return setupFDR(link); @@ -511,16 +512,15 @@ aligned_unique_ptr fdrBuildTableInternal(const vector &lits, bool make_small, const target_t &target, const Grey &grey, u32 hint, hwlmStreamingControl *stream_control) { - pair link(nullptr, 0); + pair, size_t> link(nullptr, 0); if (stream_control) { - link = fdrBuildTableStreaming(lits, stream_control); + link = fdrBuildTableStreaming(lits, *stream_control); } DEBUG_PRINTF("cpu has %s\n", target.has_avx2() ? "avx2" : "no-avx2"); if (grey.fdrAllowTeddy) { - aligned_unique_ptr fdr - = teddyBuildTableHinted(lits, make_small, hint, target, link); + auto fdr = teddyBuildTableHinted(lits, make_small, hint, target, link); if (fdr) { DEBUG_PRINTF("build with teddy succeeded\n"); return fdr; diff --git a/src/fdr/fdr_compile_internal.h b/src/fdr/fdr_compile_internal.h index ac6d1257..9b0c323f 100644 --- a/src/fdr/fdr_compile_internal.h +++ b/src/fdr/fdr_compile_internal.h @@ -70,9 +70,9 @@ std::pair, size_t> setupFDRFloodControl(const std::vector &lits, const EngineDescription &eng); -std::pair +std::pair, size_t> fdrBuildTableStreaming(const std::vector &lits, - hwlmStreamingControl *stream_control); + hwlmStreamingControl &stream_control); static constexpr u32 HINT_INVALID = 0xffffffff; diff --git a/src/fdr/fdr_streaming_compile.cpp b/src/fdr/fdr_streaming_compile.cpp index 34536eec..3422d74c 100644 --- a/src/fdr/fdr_streaming_compile.cpp +++ b/src/fdr/fdr_streaming_compile.cpp @@ -306,24 +306,24 @@ size_t maxMaskLen(const vector &lits) { return rv; } -pair +pair, size_t> fdrBuildTableStreaming(const vector &lits, - hwlmStreamingControl *stream_control) { + hwlmStreamingControl &stream_control) { // refuse to compile if we are forced to have smaller than minimum // history required for long-literal support, full stop // otherwise, choose the maximum of the preferred history quantity // (currently a fairly extravagant 32) or the already used history - // quantity - subject to the limitation of stream_control->history_max + // quantity - subject to the limitation of stream_control.history_max const size_t MIN_HISTORY_REQUIRED = 32; - if (MIN_HISTORY_REQUIRED > stream_control->history_max) { + if (MIN_HISTORY_REQUIRED > stream_control.history_max) { throw std::logic_error("Cannot set history to minimum history required"); } size_t max_len = - MIN(stream_control->history_max, - MAX(MIN_HISTORY_REQUIRED, stream_control->history_min)); + MIN(stream_control.history_max, + MAX(MIN_HISTORY_REQUIRED, stream_control.history_min)); assert(max_len >= MIN_HISTORY_REQUIRED); size_t max_mask_len = maxMaskLen(lits); @@ -334,9 +334,9 @@ fdrBuildTableStreaming(const vector &lits, // we want enough history to manage the longest literal and the longest // mask. - stream_control->literal_history_required = + stream_control.literal_history_required = max(maxLen(lits), max_mask_len) - 1; - stream_control->literal_stream_state_required = 0; + stream_control.literal_stream_state_required = 0; return make_pair(nullptr, size_t{0}); } @@ -381,11 +381,11 @@ fdrBuildTableStreaming(const vector &lits, streamBits[CASELESS] = lg2(roundUpToPowerOfTwo(positions[CASELESS] + 2)); u32 tot_state_bytes = (streamBits[CASEFUL] + streamBits[CASELESS] + 7) / 8; - u8 * secondaryTable = (u8 *)aligned_zmalloc(tabSize); + auto secondaryTable = aligned_zmalloc_unique(tabSize); assert(secondaryTable); // otherwise would have thrown std::bad_alloc // then fill it in - u8 * ptr = secondaryTable; + u8 * ptr = secondaryTable.get(); FDRSTableHeader * header = (FDRSTableHeader *)ptr; // fill in header header->pseudoEngineID = (u32)0xffffffff; @@ -411,7 +411,7 @@ fdrBuildTableStreaming(const vector &lits, e = long_lits.end(); i != e; ++i) { u32 entry = verify_u32(i - long_lits.begin()); - u32 offset = verify_u32(ptr - secondaryTable); + u32 offset = verify_u32(ptr - secondaryTable.get()); // point the table entry to the string location litTabPtr[entry].offset = offset; @@ -425,10 +425,10 @@ fdrBuildTableStreaming(const vector &lits, } // fill in final lit table entry with current ptr (serves as end value) - litTabPtr[long_lits.size()].offset = verify_u32(ptr - secondaryTable); + litTabPtr[long_lits.size()].offset = verify_u32(ptr - secondaryTable.get()); // fill hash tables - ptr = secondaryTable + htOffset[CASEFUL]; + ptr = secondaryTable.get() + htOffset[CASEFUL]; for (u32 m = CASEFUL; m < MAX_MODES; ++m) { fillHashes(long_lits, max_len, (FDRSHashEntry *)ptr, hashEntries[m], (MODES)m, litToOffsetVal); @@ -436,9 +436,9 @@ fdrBuildTableStreaming(const vector &lits, } // tell the world what we did - stream_control->literal_history_required = max_len; - stream_control->literal_stream_state_required = tot_state_bytes; - return make_pair(secondaryTable, tabSize); + stream_control.literal_history_required = max_len; + stream_control.literal_stream_state_required = tot_state_bytes; + return make_pair(move(secondaryTable), tabSize); } } // namespace ue2 diff --git a/src/fdr/teddy_compile.cpp b/src/fdr/teddy_compile.cpp index 21f5c901..21bdb409 100644 --- a/src/fdr/teddy_compile.cpp +++ b/src/fdr/teddy_compile.cpp @@ -74,7 +74,7 @@ public: const TeddyEngineDescription &eng_in, bool make_small_in) : eng(eng_in), lits(lits_in), make_small(make_small_in) {} - aligned_unique_ptr build(pair link); + aligned_unique_ptr build(pair, size_t> &link); bool pack(map > &bucketToLits); }; @@ -281,7 +281,8 @@ bool TeddyCompiler::pack(map TeddyCompiler::build(pair link) { +aligned_unique_ptr +TeddyCompiler::build(pair, size_t> &link) { if (lits.size() > eng.getNumBuckets() * TEDDY_BUCKET_LOAD) { DEBUG_PRINTF("too many literals: %zu\n", lits.size()); return nullptr; @@ -342,8 +343,7 @@ aligned_unique_ptr TeddyCompiler::build(pair link) { if (link.first) { teddy->link = verify_u32(ptr - teddy_base); - memcpy(ptr, link.first, link.second); - aligned_free(link.first); + memcpy(ptr, link.first.get(), link.second); } else { teddy->link = 0; } @@ -436,10 +436,10 @@ aligned_unique_ptr TeddyCompiler::build(pair link) { } // namespace -aligned_unique_ptr teddyBuildTableHinted(const vector &lits, - bool make_small, u32 hint, - const target_t &target, - pair link) { +aligned_unique_ptr +teddyBuildTableHinted(const vector &lits, bool make_small, + u32 hint, const target_t &target, + pair, size_t> &link) { unique_ptr des; if (hint == HINT_INVALID) { des = chooseTeddyEngine(target, lits); diff --git a/src/fdr/teddy_compile.h b/src/fdr/teddy_compile.h index fba6a3d1..276c1347 100644 --- a/src/fdr/teddy_compile.h +++ b/src/fdr/teddy_compile.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2015, Intel Corporation + * Copyright (c) 2015-2016, Intel Corporation * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: @@ -49,7 +49,7 @@ struct hwlmLiteral; ue2::aligned_unique_ptr teddyBuildTableHinted(const std::vector &lits, bool make_small, u32 hint, const target_t &target, - std::pair link); + std::pair, size_t> &link); } // namespace ue2