Changeset 4510


Ignore:
Timestamp:
Feb 18, 2015, 3:56:47 PM (4 years ago)
Author:
nmedfort
Message:

Many memory deallocation fixes.

Location:
icGREP/icgrep-devel/icgrep
Files:
22 edited

Legend:

Unmodified
Added
Removed
  • icGREP/icgrep-devel/icgrep/CMakeLists.txt

    r4508 r4510  
    8282    # target_link_libraries (RegExpADT ${Boost_LIBRARIES})
    8383ENDIF()
    84 
    85 #set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -g -fsanitize=address")
    8684
    8785#Check compiler support for 0x / 11
     
    253251
    254252SET(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS} -O3 -DNDEBUG")
     253SET(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS} -g -fsanitize=address")
    255254
    256255add_test(
  • icGREP/icgrep-devel/icgrep/compiler.cpp

    r4438 r4510  
    117117    }
    118118
    119     PabloBlock & main = PabloBlock::Create();
     119    SymbolGenerator symbolGenerator;
     120    PabloBlock & main = PabloBlock::Create(symbolGenerator);
    120121
    121122    CC_Compiler cc_compiler(main, encoding);
  • icGREP/icgrep-devel/icgrep/pablo/codegenstate.cpp

    r4443 r4510  
    378378}
    379379
    380 If * PabloBlock::createIf(PabloAST * condition, std::vector<Assign *> && definedVars, PabloBlock & body) {
     380If * PabloBlock::createIf(PabloAST * condition, std::initializer_list<Assign *> definedVars, PabloBlock & body) {
    381381    assert (condition);
    382382    return insertAtInsertionPoint(new If(condition, std::move(definedVars), body, this));
     
    390390/// CONSTRUCTOR
    391391
    392 PabloBlock::PabloBlock()
     392PabloBlock::PabloBlock(SymbolGenerator & symbolGenerator)
    393393: PabloAST(PabloAST::ClassTypeId::Block)
    394394, mZeroes(new Zeroes())
    395395, mOnes(new Ones())
    396 , mSymbolGenerator(new SymbolGenerator())
     396, mSymbolGenerator(symbolGenerator)
    397397, mPredecessor(nullptr)
    398398{
     
    411411
    412412PabloBlock::~PabloBlock() {
    413     if (mPredecessor == nullptr) {
    414         delete mSymbolGenerator;
    415     }   
    416 }
    417 
    418 }
     413
     414}
     415
     416}
  • icGREP/icgrep-devel/icgrep/pablo/codegenstate.h

    r4443 r4510  
    7070    }
    7171
    72     inline static PabloBlock & Create() {
    73         return *(new PabloBlock());
     72    inline static PabloBlock & Create(SymbolGenerator & symbolGenerator) {
     73        return *(new PabloBlock(symbolGenerator));
    7474    }
    7575
     
    142142    PabloAST * createSel(PabloAST * condition, PabloAST * trueExpr, PabloAST * falseExpr, const std::string prefix);
    143143
    144     If * createIf(PabloAST * condition, std::vector<Assign *> && definedVars, PabloBlock & body);
     144    If * createIf(PabloAST * condition, std::initializer_list<Assign *> definedVars, PabloBlock & body);
    145145
    146146    While * createWhile(PabloAST * condition, PabloBlock & body);
     
    155155
    156156    inline String * getName(const std::string name, const bool generated = true) const {
    157         return mSymbolGenerator->get(name, generated);
     157        return mSymbolGenerator.get(name, generated);
    158158    }
    159159
    160160    inline String * makeName(const std::string prefix, const bool generated = true) const {
    161         return mSymbolGenerator->make(prefix, generated);
     161        return mSymbolGenerator.make(prefix, generated);
    162162    }
    163163
    164164    inline Integer * getInteger(Integer::integer_t value) {
    165         return mSymbolGenerator->getInteger(value);
     165        return mSymbolGenerator.getInteger(value);
    166166    }
    167167
     
    169169
    170170protected:
    171     PabloBlock();
     171    PabloBlock(SymbolGenerator & symbolGenerator);
    172172
    173173    PabloBlock(PabloBlock * predecessor);
     
    186186    Zeroes * const                                      mZeroes;
    187187    Ones * const                                        mOnes;
    188     SymbolGenerator * const                             mSymbolGenerator;
     188    SymbolGenerator &                                   mSymbolGenerator;
    189189    PabloBlock *                                        mPredecessor;
    190190};
  • icGREP/icgrep-devel/icgrep/pablo/optimizers/pablo_simplifier.cpp

    r4443 r4510  
    5555            // the Assign's expression directly.
    5656            if (assign->superfluous()) {
    57                 stmt = (stmt->getNumUses() == 0) ? assign->eraseFromParent() : assign->replaceWith(assign->getExpr());
     57                if (stmt->getNumUses() == 0) {
     58                    stmt = assign->eraseFromParent();
     59                }
     60                else {
     61                    stmt = assign->replaceWith(assign->getExpr());
     62                }
    5863                continue;
    5964            }
  • icGREP/icgrep-devel/icgrep/pablo/pabloAST.cpp

    r4443 r4510  
    88#include <pablo/codegenstate.h>
    99#include <llvm/Support/Compiler.h>
    10 
     10#ifndef NDEBUG
     11#include <queue>
     12#endif
    1113namespace pablo {
    1214
    1315PabloAST::Allocator PabloAST::mAllocator;
     16PabloAST::VectorAllocator PabloAST::mVectorAllocator;
    1417
    1518/*
     
    2427    assert (expr1 && expr2);
    2528    if (expr1->getClassTypeId() == expr2->getClassTypeId()) {
    26         if ((isa<const Zeroes>(expr1)) || (isa<const Ones>(expr1))) {
     29        if ((isa<Zeroes>(expr1)) || (isa<Ones>(expr1))) {
    2730            return true;
    2831        }
     
    8083}
    8184
    82 void PabloAST::replaceAllUsesWith(PabloAST * expr) {
     85void PabloAST::replaceAllUsesWith(PabloAST * expr) {   
     86    Statement * user[mUsers.size()];
     87    Users::size_type users = 0;
     88    for (PabloAST * u : mUsers) {
     89        if (isa<Statement>(u)) {
     90            user[users++] = cast<Statement>(u);
     91        }
     92    }
     93    mUsers.clear();
    8394    assert (expr);
    84     Users Q;
    85     Q.swap(mUsers);
    86     for (PabloAST * user : Q) {
    87         if (isa<Statement>(user)) {
    88             cast<Statement>(user)->replaceUsesOfWith(this, expr);
    89         }
    90     }
    91     Q.clear();
    92 }
     95    for (auto i = 0; i != users; ++i) {
     96        user[i]->replaceUsesOfWith(this, expr);
     97    }
     98}
     99
     100
    93101
    94102void Statement::setOperand(const unsigned index, PabloAST * const value) {
    95103    assert (index < getNumOperands());
     104    assert (noRecursiveOperand(value));
    96105    if (LLVM_UNLIKELY(getOperand(index) == value)) {
    97106        return;
     
    206215}
    207216
     217#ifndef NDEBUG
     218    bool Statement::noRecursiveOperand(const PabloAST * const operand) {
     219        if (operand && isa<Statement>(operand)) {
     220            std::queue<const Statement *> Q;
     221            Q.push(cast<Statement>(operand));
     222            while (!Q.empty()) {
     223                const Statement * stmt = Q.front();
     224                if (stmt == this) {
     225                    return false;
     226                }
     227                Q.pop();
     228                for (auto i = 0; i != stmt->getNumOperands(); ++i) {
     229                    const PabloAST * op = stmt->getOperand(i);
     230                    if (isa<Statement>(op)) {
     231                        Q.push(cast<Statement>(op));
     232                    }
     233                }
     234            }
     235        }
     236        return true;
     237    }
     238#endif
     239
    208240void StatementList::insert(Statement * const statement) {
    209241    if (LLVM_UNLIKELY(mInsertionPoint == nullptr)) {
  • icGREP/icgrep-devel/icgrep/pablo/pabloAST.h

    r4444 r4510  
    3131public:
    3232
    33     using Users = std::vector<PabloAST*>;
     33    using Allocator = SlabAllocator<u_int8_t>;
     34    using VectorAllocator = SlabAllocator<PabloAST *>;
     35    using Users = std::vector<PabloAST*, VectorAllocator>;
    3436    using user_iterator = Users::iterator;
    3537    using const_user_iterator = Users::const_iterator;
    3638
    37     typedef SlabAllocator<PabloAST *> Allocator;
     39
    3840    enum class ClassTypeId : unsigned {
    3941        Advance
     
    101103    inline PabloAST(const ClassTypeId id)
    102104    : mClassTypeId(id)
     105    , mUsers(mVectorAllocator)
    103106    {
    104107
     
    114117    inline void removeUser(PabloAST * user) {
    115118        assert (user);
     119        if (mUsers.empty()) {
     120            return;
     121        }
    116122        auto pos = std::lower_bound(mUsers.begin(), mUsers.end(), user);
    117123        if (LLVM_UNLIKELY(pos == mUsers.end() || *pos != user)) {
     
    127133    const ClassTypeId       mClassTypeId;
    128134    Users                   mUsers;
     135    static VectorAllocator  mVectorAllocator;
    129136};
    130137
     
    207214    , mParent(parent)
    208215    , mOperands(operands.size())
    209     , mOperand(mAllocator.allocate(mOperands * sizeof(PabloAST *)))
     216    , mOperand(reinterpret_cast<PabloAST**>(mAllocator.allocate(mOperands * sizeof(PabloAST *))))
    210217    {
    211218        unsigned i = 0;
     
    218225        mName = name;
    219226    }
     227#ifndef NDEBUG
     228    bool noRecursiveOperand(const PabloAST * const operand);
     229#endif
    220230protected:   
    221231    const String *              mName;
  • icGREP/icgrep-devel/icgrep/pablo/pablo_compiler.cpp

    r4438 r4510  
    5858#include <llvm/Support/MemoryBuffer.h>
    5959#include <llvm/IR/IRBuilder.h>
    60 #include "llvm/Support/CommandLine.h"
     60#include <llvm/Support/CommandLine.h>
     61#include <llvm/ADT/Twine.h>
    6162#include <iostream>
    6263
     
    9394, mOutputAddrPtr(nullptr)
    9495, mMaxNestingDepth(0)
     96, mPrintRegisterFunction(nullptr)
    9597{
    9698    //Create the jit execution engine.up
     
    103105PabloCompiler::~PabloCompiler()
    104106{
    105     delete mMod;
     107    // delete mMod;
    106108}
    107109   
     
    158160        Value* indices[] = {b.getInt64(0), b.getInt32(i)};
    159161        Value * gep = b.CreateGEP(mBasisBitsAddr, indices);
    160         LoadInst * basisBit = b.CreateAlignedLoad(gep, BLOCK_SIZE/8, false, mBasisBits[i]->getName()->str());
     162        LoadInst * basisBit = b.CreateAlignedLoad(gep, BLOCK_SIZE/8, false, mBasisBits[i]->getName()->to_string());
    161163        mMarkerMap.insert(std::make_pair(mBasisBits[i], basisBit));
    162164    }
     
    190192    #endif
    191193
    192     //Use the pass manager to run optimizations on the function.
    193     FunctionPassManager fpm(mMod);
    194  #ifdef USE_LLVM_3_5
    195     mMod->setDataLayout(mExecutionEngine->getDataLayout());
    196     // Set up the optimizer pipeline.  Start with registering info about how the target lays out data structures.
    197     fpm.add(new DataLayoutPass(mMod));
    198 #endif
    199 #ifdef USE_LLVM_3_4
    200     fpm.add(new DataLayout(*mExecutionEngine->getDataLayout()));
    201 #endif
    202     fpm.doInitialization();
    203     fpm.run(*mFunction);
    204 
    205194    mExecutionEngine->finalizeObject();
    206195
     
    264253{
    265254    //This function can be used for testing to print the contents of a register from JIT'd code to the terminal window.
    266     mFunc_print_register = mMod->getOrInsertFunction("wrapped_print_register", Type::getVoidTy(getGlobalContext()), mBitBlockType, NULL);
    267     mExecutionEngine->addGlobalMapping(cast<GlobalValue>(mFunc_print_register), (void *)&wrapped_print_register);
     255    mPrintRegisterFunction = mMod->getOrInsertFunction("wrapped_print_register", Type::getVoidTy(getGlobalContext()), mBitBlockType, NULL);
     256    mExecutionEngine->addGlobalMapping(cast<GlobalValue>(mPrintRegisterFunction), (void *)&wrapped_print_register);
    268257    // to call->  b.CreateCall(mFunc_print_register, unicode_category);
    269258
     
    463452        const String * callee = mapping.first;
    464453        //std::cerr << callee->str() << " to be declared\n";
    465         auto ei = mExternalMap.find(callee->str());
     454        auto ei = mExternalMap.find(callee->value());
    466455        if (ei != mExternalMap.end()) {
    467456            void * fn_ptr = ei->second;
    468457            //std::cerr << "Ptr found:" <<  std::hex << ((intptr_t) fn_ptr) << std::endl;
    469             Value * externalValue = mMod->getOrInsertFunction(callee->str(), mBitBlockType, mBasisBitsInputPtr, NULL);
     458            Value * externalValue = mMod->getOrInsertFunction(callee->value(), mBitBlockType, mBasisBitsInputPtr, NULL);
    470459            if (LLVM_UNLIKELY(externalValue == nullptr)) {
    471                 throw std::runtime_error("Could not create static method call for external function \"" + callee->str() + "\"");
     460                throw std::runtime_error("Could not create static method call for external function \"" + callee->to_string() + "\"");
    472461            }
    473462            mExecutionEngine->addGlobalMapping(cast<GlobalValue>(externalValue), fn_ptr);
     
    475464        }
    476465        else {
    477             throw std::runtime_error("External function \"" + callee->str() + "\" not installed");
     466            throw std::runtime_error("External function \"" + callee->to_string() + "\" not installed");
    478467        }
    479468    }
     
    607596        //End Block
    608597        IRBuilder<> bEnd(ifEndBlock);
    609         for (const Statement * a : ifStatement->getDefined()) {
    610             PHINode * phi = bEnd.CreatePHI(mBitBlockType, 2, a->getName()->str());
    611             auto f = mMarkerMap.find(a);
     598        for (const PabloAST * node : ifStatement->getDefined()) {
     599            const Assign * assign = cast<Assign>(node);
     600            PHINode * phi = bEnd.CreatePHI(mBitBlockType, 2, assign->getName()->value());
     601            auto f = mMarkerMap.find(assign);
    612602            assert (f != mMarkerMap.end());
    613603            phi->addIncoming(mZeroInitializer, ifEntryBlock);
    614604            phi->addIncoming(f->second, mBasicBlock);
    615             mMarkerMap[a] = phi;
     605            mMarkerMap[assign] = phi;
    616606        }
    617607        // Create the phi Node for the summary variable.
     
    698688        // and for any Next nodes in the loop body
    699689        for (const Next * n : nextNodes) {
    700             PHINode * phi = bCond.CreatePHI(mBitBlockType, 2, n->getName()->str());
     690            PHINode * phi = bCond.CreatePHI(mBitBlockType, 2, n->getName()->value());
    701691            auto f = mMarkerMap.find(n->getInitial());
    702692            assert (f != mMarkerMap.end());
     
    754744            auto ci = mCalleeMap.find(call->getCallee());
    755745            if (LLVM_UNLIKELY(ci == mCalleeMap.end())) {
    756                 throw std::runtime_error("Unexpected error locating static function for \"" + call->getCallee()->str() + "\"");
     746                throw std::runtime_error("Unexpected error locating static function for \"" + call->getCallee()->to_string() + "\"");
    757747            }
    758748            mi = mMarkerMap.insert(std::make_pair(call, b.CreateCall(ci->second, mBasisBitsAddr))).first;
  • icGREP/icgrep-devel/icgrep/pablo/pablo_compiler.h

    r4433 r4510  
    2323#include <map>
    2424#include <algorithm>
     25#include <unordered_map>
     26#include <pablo/pe_string.h>
    2527#include <llvm/ADT/Twine.h>
    26 #include <unordered_map>
    27 #include <string>
    2828
    2929namespace llvm {
     
    107107
    108108
    109     ASTToValueMap                 mMarkerMap;
     109    ASTToValueMap                       mMarkerMap;
    110110    CarryQueueVector                    mCarryQueueVector;
    111111    std::vector<int>                    mCarryQueueSummaryIdx;
     
    146146    StringToValueMap                    mCalleeMap;
    147147
    148     Constant * mFunc_print_register;
     148    Constant *                          mPrintRegisterFunction;
    149149};
    150150
  • icGREP/icgrep-devel/icgrep/pablo/pe_string.h

    r4433 r4510  
    33
    44#include <pablo/pabloAST.h>
     5#include <llvm/ADT/StringRef.h>
     6#include <llvm/ADT/Twine.h>
     7#include <llvm/ADT/SmallVector.h>
    58#include <string>
     9#include <ostream>
    610
    711namespace pablo {
     
    913class String : public PabloAST {
    1014    friend class SymbolGenerator;
     15    friend std::ostream & operator<< (std::ostream& stream, const String & string);
    1116public:
     17    using StringAllocator = SlabAllocator<char>;
     18    using Value = StringRef;
     19
    1220    static inline bool classof(const PabloAST * e) {
    1321        return e->getClassTypeId() == ClassTypeId::String;
     
    1927
    2028    }
    21     inline const std::string & str() const {
     29    inline const StringRef & value() const {
    2230        return mValue;
    2331    }
    24     inline std::string str() {
     32    inline std::string to_string() const {
     33        return mValue.str();
     34    }
     35    inline StringRef value() {
    2536        return mValue;
    2637    }
     
    3243    }
    3344protected:
    34     String(const std::string && value, const bool generated) noexcept
     45    String(const std::string & value, const bool generated) noexcept
    3546    : PabloAST(ClassTypeId::String)
    36     , mValue(value)
     47    , mValue(duplicate(value))
    3748    , mGenerated(generated)
    3849    {
    3950
    4051    }
     52    inline const char * duplicate(const std::string & value) {
     53        char * string = reinterpret_cast<char*>(mAllocator.allocate(value.length() + 1));
     54        std::memcpy(string, value.c_str(), value.length());
     55        string[value.length()] = '\0';
     56        return string;
     57    }
    4158private:
    42     const std::string   mValue;
    43     const bool          mGenerated;
     59    const StringRef         mValue;
     60    const bool              mGenerated;
    4461};
     62
     63inline std::ostream & operator <<(std::ostream & stream, const String & string) {
     64    stream << string.value().data();
     65    return stream;
     66}
    4567
    4668}
  • icGREP/icgrep-devel/icgrep/pablo/pe_zeroes.h

    r4410 r4510  
    1616    friend class PabloBlock;
    1717public:
    18     static inline bool classof(const PabloAST * e) {
    19         return e->getClassTypeId() == ClassTypeId::Zeroes;
     18    static inline bool classof(const PabloAST * expr) {
     19        assert (expr);
     20        return expr->getClassTypeId() == ClassTypeId::Zeroes;
    2021    }
    2122    static inline bool classof(const void *) {
  • icGREP/icgrep-devel/icgrep/pablo/printer_pablos.cpp

    r4486 r4510  
    6464            strm << "Output[" << std::to_string(an->getOutputIndex()) << "].";
    6565        }
    66         strm << an->getName()->str() << " = ";
     66        strm << an->getName() << " = ";
    6767        print(an->getExpr(), strm);
    6868    }
    6969    else if (const Next * next = dyn_cast<const Next>(stmt)) {       
    70         strm << "Next(" << next->getName()->str() << ") = ";
     70        strm << "Next(" << next->getName() << ") = ";
    7171        print(next->getExpr(), strm);
    7272    }
     
    8585    else if (const Call * pablo_call = dyn_cast<const Call>(stmt)) {
    8686        print(pablo_call, strm);
    87         strm << " = " << pablo_call->getCallee()->str() << "()";
     87        strm << " = " << pablo_call->getCallee() << "()";
    8888    }
    8989    else if (const And * pablo_and = dyn_cast<const And>(stmt)) {
     
    165165    }
    166166    else if (const Var * var = dyn_cast<const Var>(expr)) {
    167         strm << var->getName()->str();
     167        strm << var->getName();
    168168    }
    169169    else if (const Statement * stmt = dyn_cast<Statement>(expr)) {
    170         strm << stmt->getName()->str();
     170        strm << stmt->getName();
    171171    }
    172172    else {
  • icGREP/icgrep-devel/icgrep/pablo/ps_if.cpp

    r4419 r4510  
    55namespace pablo {
    66
    7 If::If(PabloAST * expr, DefinedVars && definedVars, PabloBlock & body, PabloBlock * parent)
     7If::If(PabloAST * expr, std::initializer_list<Assign *> && definedVars, PabloBlock & body, PabloBlock * parent)
    88: Statement(ClassTypeId::If, {expr}, nullptr, parent)
    99, mBody(body)
    10 , mDefined(std::move(definedVars))
     10, mDefined(definedVars.begin(), definedVars.end(), mVectorAllocator)
    1111, mCarryCount(0)
    1212, mAdvanceCount(0)
  • icGREP/icgrep-devel/icgrep/pablo/ps_if.h

    r4432 r4510  
    1818    friend class PabloBlock;
    1919public:
    20     using DefinedVars = std::vector<Assign *>;
     20    using DefinedVars = std::vector<PabloAST *, VectorAllocator>;
    2121
    2222    static inline bool classof(const PabloAST * e) {
     
    5454    }
    5555protected:
    56     If(PabloAST * expr, DefinedVars && definedVars, PabloBlock & body, PabloBlock * parent);
     56    If(PabloAST * expr, std::initializer_list<Assign *> && definedVars, PabloBlock & body, PabloBlock * parent);
    5757private:
    5858    PabloBlock &    mBody;
  • icGREP/icgrep-devel/icgrep/pablo/symbol_generator.cpp

    r4433 r4510  
    2121    String * result;
    2222    if (f == mStringMap.end()) {
    23         result = new String(std::move(name), generated);
    24         mStringMap.insert(std::make_pair(name, result));
     23        result = new String(name, generated);
     24        mStringMap.insert(std::make_pair(std::move(name), result));
    2525    }
    2626    else {
     
    5858
    5959SymbolGenerator::~SymbolGenerator() {
    60     for (auto itr : mStringMap) {
    61         delete itr.second;
    62     }
    63     for (auto itr : mIntegerMap) {
    64         delete itr.second;
    65     }
     60
    6661}
    6762
  • icGREP/icgrep-devel/icgrep/pablo/symbol_generator.h

    r4433 r4510  
    1111#include <string>
    1212#include <unordered_map>
     13#include <llvm/ADT/Twine.h>
     14#include <llvm/ADT/StringMap.h>
     15#include <llvm/Support/StringPool.h>
    1316
    1417namespace pablo {
     
    1922class SymbolGenerator {
    2023    friend class PabloBlock;
     24    using Twine = llvm::Twine;
    2125public:
    2226    typedef u_int64_t integer_t;
     
    2428    String * make(const std::string prefix, const bool generated = true);
    2529    Integer * getInteger(const integer_t value);
     30    SymbolGenerator();
    2631    ~SymbolGenerator();
    27 protected:
    28     SymbolGenerator();   
    2932private:
    30     std::unordered_map<std::string, unsigned>   mPrefixMap;   
     33    std::unordered_map<std::string, integer_t>  mPrefixMap;
    3134    std::unordered_map<std::string, String *>   mStringMap;
    3235    std::unordered_map<integer_t, Integer *>    mIntegerMap;
  • icGREP/icgrep-devel/icgrep/re/re_cc.cpp

    r4432 r4510  
    1111
    1212namespace re {
     13CC::CharSetAllocator CC::mCharSetAllocator;
    1314
    1415CC::CC(const CC * cc1, const CC * cc2)
    1516: RE(ClassTypeId::CC)
    16 , mSparseCharSet(cc1->cbegin(), cc1->cend()) {
     17, mSparseCharSet(cc1->cbegin(), cc1->cend(), mCharSetAllocator) {
    1718    for (const CharSetItem & i : cc2->mSparseCharSet) {
    1819        insert_range(i.lo_codepoint, i.hi_codepoint);
     
    2223CC::CC(const CC & cc)
    2324: RE(ClassTypeId::CC)
    24 , mSparseCharSet(cc.cbegin(), cc.cend()) {
     25, mSparseCharSet(cc.cbegin(), cc.cend(), mCharSetAllocator) {
    2526
    2627}
    2728
    28 std::string CC::canonicalName(CC_type t) const {
     29std::string CC::canonicalName(const CC_type type) const {
    2930    std::stringstream name;
    3031    name << std::hex;
    31     if ((t == ByteClass) && (mSparseCharSet.back().hi_codepoint >= 0x80)) {
     32    if ((type == ByteClass) && (mSparseCharSet.back().hi_codepoint >= 0x80)) {
    3233      name << "BC";
    3334    }
    34     else name << "CC";
     35    else {
     36        name << "CC";
     37    }
    3538    char separator = '_';
    3639    for (const CharSetItem & i : mSparseCharSet) {
     
    4043        }
    4144        else {
    42             name << i.lo_codepoint << "-" << i.hi_codepoint;
     45            name << i.lo_codepoint << '-' << i.hi_codepoint;
    4346        }
    4447        separator = ',';
  • icGREP/icgrep-devel/icgrep/re/re_cc.h

    r4432 r4510  
    1212#include <string>
    1313#include <vector>
     14#include <slab_allocator.h>
    1415
    1516namespace re {
     
    2324    CodePointType hi_codepoint;
    2425};
    25 
    26 typedef std::vector<CharSetItem> CharSetVector;
    2726
    2827enum CC_type {UnicodeClass, ByteClass};
     
    3837    }
    3938
     39    using CharSetAllocator = SlabAllocator<CharSetItem>;
     40    using CharSetVector = std::vector<CharSetItem, CharSetAllocator>;
     41
    4042    typedef CharSetVector::iterator                 iterator;
    4143    typedef CharSetVector::const_iterator           const_iterator;
     
    4648    static const CodePointType UNICODE_MAX = 0x10FFFF;
    4749
    48     std::string canonicalName(CC_type t) const;
     50    std::string canonicalName(const CC_type type) const;
    4951
    5052    CodePointType max_codepoint();
     
    122124    }
    123125    inline CC()
    124     : RE(ClassTypeId::CC) {
     126    : RE(ClassTypeId::CC)
     127    , mSparseCharSet(mCharSetAllocator) {
    125128
    126129    }
    127130    CC(const CC & cc);
    128131    inline CC(const CodePointType codepoint)
    129     : RE(ClassTypeId::CC) {
     132    : RE(ClassTypeId::CC)
     133    , mSparseCharSet(mCharSetAllocator) {
    130134        insert(codepoint);
    131135    }
    132136    inline CC(const CodePointType lo_codepoint, const CodePointType hi_codepoint)
    133     : RE(ClassTypeId::CC) {
     137    : RE(ClassTypeId::CC)
     138    , mSparseCharSet(mCharSetAllocator) {
    134139        insert_range(lo_codepoint, hi_codepoint);
    135140    }
     
    137142private:   
    138143    CharSetVector mSparseCharSet;
     144    static CharSetAllocator mCharSetAllocator;
    139145};
    140146
  • icGREP/icgrep-devel/icgrep/re/re_compiler.cpp

    r4509 r4510  
    9595    PabloAST * cr1 = crb.createAdvance(CR, 1, "cr1");
    9696    Assign * acrlf = crb.createAssign("crlf", crb.createAnd(cr1, LF));
    97     mPB.createIf(CR, std::move(std::vector<Assign *>{acrlf}), crb);
     97    mPB.createIf(CR, {acrlf}, crb);
    9898    mCRLF = acrlf;
    9999
     
    110110    Assign * u8scope22 = it2.createAssign("u8scope22", it2.createAdvance(u8pfx2, 1));
    111111    Assign * NEL = it2.createAssign("NEL", it2.createAnd(it2.createAdvance(ccc.compileCC(makeCC(0xC2), it2), 1), ccc.compileCC(makeCC(0x85), it2)));
    112     it.createIf(u8pfx2, std::move(std::vector<Assign *>{u8scope22, NEL}), it2);
     112    it.createIf(u8pfx2, {u8scope22, NEL}, it2);
    113113   
    114114    //
     
    123123    PabloAST * ED_invalid = it3.createAnd(it3.createAdvance(ccc.compileCC(makeCC(0xED), it3), 1), ccc.compileCC(makeCC(0xA0, 0xBF), it3));
    124124    Assign * EX_invalid = it3.createAssign("EX_invalid", it3.createOr(E0_invalid, ED_invalid));
    125     it.createIf(u8pfx3, std::move(std::vector<Assign *>{u8scope32, u8scope3X, LS_PS, EX_invalid}), it3);
     125    it.createIf(u8pfx3, {u8scope32, u8scope3X, LS_PS, EX_invalid}, it3);
    126126 
    127127    //
     
    136136    PabloAST * F4_invalid = it4.createAnd(it4.createAdvance(ccc.compileCC(makeCC(0xF4), it4), 1), ccc.compileCC(makeCC(0x90, 0xBF), it4));
    137137    Assign * FX_invalid = it4.createAssign("FX_invalid", it4.createOr(F0_invalid, F4_invalid));
    138     it.createIf(u8pfx4, std::move(std::vector<Assign *>{u8scope4nonfinal, u8scope4X, FX_invalid}), it4);
     138    it.createIf(u8pfx4, {u8scope4nonfinal, u8scope4X, FX_invalid}, it4);
    139139
    140140    //
     
    156156   
    157157    Assign * NEL_LS_PS = it.createAssign("NEL_LS_PS", it.createOr(NEL, LS_PS));
    158     mPB.createIf(u8pfx, std::move(std::vector<Assign *>{u8invalid, valid_pfx, mNonFinal, NEL_LS_PS}), it);
     158    mPB.createIf(u8pfx, {u8invalid, valid_pfx, mNonFinal, NEL_LS_PS}, it);
    159159   
    160160    PabloAST * LB_chars = mPB.createOr(LF_VT_FF_CR, NEL_LS_PS);
     
    298298        MarkerType m1 = processSeqTail(current, end, 0, marker, nested);
    299299        Assign * m1a = nested.createAssign("m", markerVar(m1));
    300         pb.createIf(markerVar(marker), std::move(std::vector<Assign *>{m1a}), nested);
     300        pb.createIf(markerVar(marker), {m1a}, nested);
    301301        return makeMarker(m1.pos, m1a);
    302302    }
  • icGREP/icgrep-devel/icgrep/re/re_re.cpp

    r4272 r4510  
    33namespace re {
    44RE::Allocator RE::mAllocator;
     5RE::VectorAllocator RE::mVectorAllocator;
    56}
  • icGREP/icgrep-devel/icgrep/re/re_re.h

    r4432 r4510  
    3737class RE {
    3838public:
    39     typedef SlabAllocator<RE *> Allocator;
     39    using Allocator = SlabAllocator<u_int8_t>;
     40    using VectorAllocator = SlabAllocator<RE *>;
    4041    enum class ClassTypeId : unsigned {
    4142        Alt
     
    5859    }
    5960    inline static void release_memory() {
     61        mVectorAllocator.release_memory();
    6062        mAllocator.release_memory();
    6163    }
     
    6971
    7072    static Allocator mAllocator;
     73    static VectorAllocator mVectorAllocator;
    7174};
    7275
    73 class Vector : public RE, public std::vector<RE*> {
     76class Vector : public RE, public std::vector<RE*, RE::VectorAllocator> {
    7477public:
     78
    7579    virtual ~Vector() {
    7680    }
     
    7882    inline Vector(const ClassTypeId id)
    7983    : RE(id)
    80     , std::vector<RE*>()
     84    , std::vector<RE*, RE::VectorAllocator>(RE::mVectorAllocator)
    8185    {
    8286
     
    8488    inline Vector(const ClassTypeId id, const iterator begin, const iterator end)
    8589    : RE(id)
    86     , std::vector<RE*>(begin, end) {
     90    , std::vector<RE*, RE::VectorAllocator>(begin, end, RE::mVectorAllocator) {
    8791
    8892    }
    8993};
    90 
    91 //class Pair : public RE {
    92 //protected:
    93 //    inline Pair(const ClassTypeId id)
    94 //    : RE(id)
    95 //    , _lh(nullptr)
    96 //    , _rh(nullptr)
    97 //    {
    98 
    99 //    }
    100 //    inline Pair(const ClassTypeId id, const RE * lh, const RE * rh)
    101 //    : RE(id)
    102 //    , _lh(lh)
    103 //    , _rh(rh)
    104 //    {
    105 
    106 //    }
    107 //    virtual ~Pair() {
    108 //    }
    109 //protected:
    110 //    const RE * _lh;
    111 //    const RE * _rh;
    112 //};
    113 
    114 //static Diff * makeDiff(const RE * lh, const RE * rh);
    115 
    116 //static Intersect * makeIntersect(const RE * lh, const RE * rh);
    117 
    118 //static Permute * makePermute();
    119 //static Permute * makePermute(Vector::iterator begin, Vector::iterator end);
    120 
    121 //static SymDiff * makeSymDiff(const RE * lh, const RE * rh);
    122 
    123 //static Union * makeUnion(const RE * lh, const RE * rh);
    12494
    12595}
  • icGREP/icgrep-devel/icgrep/slab_allocator.h

    r4432 r4510  
    22#define SLAB_ALLOCATOR_H
    33
    4 #include <cstddef>
    5 #include <cstdint>
    6 #include <new>
    7 #include <algorithm>
     4#include <llvm/Support/Allocator.h>
    85
    96template <typename T>
    10 class SlabAllocator {
     7class SlabAllocator : public std::allocator<T> {
    118public:
    129
    13     enum { BasePoolSize = 4096 };
    14 
     10    using LLVMAllocator = llvm::BumpPtrAllocator;
    1511    using value_type = T;
    1612    using pointer = T*;
     
    2723
    2824    inline pointer allocate(size_type n) noexcept {
    29         if (n > mSpaceAvail && !extend(n)) {
    30             exit(-1);
    31         }
    32         pointer ptr = reinterpret_cast<pointer>(mAllocationPtr);
    33         mAllocationPtr += n;
    34         mSpaceAvail -= n;
    35         return ptr;
     25        return reinterpret_cast<pointer>(mAllocator.Allocate(n * sizeof(T), sizeof(T)));
    3626    }
    3727
    3828    inline void deallocate(pointer p, size_type n) noexcept {
    39         /* do nothing */
     29        mAllocator.Deallocate(p);
    4030    }
    4131
    42     void release_memory();
     32    inline size_type max_size() const {
     33        return std::numeric_limits<size_type>::max();
     34    }
    4335
    44     SlabAllocator();
    45     ~SlabAllocator();
     36    inline void release_memory() {
     37        mAllocator.Reset();
     38    }
    4639
    47 protected:
    48     bool extend(const size_type n) noexcept;
     40    static inline LLVMAllocator & get_allocator() {
     41        return mAllocator;
     42    }
     43
     44    inline bool operator==(SlabAllocator<T> const&) { return true; }
     45    inline bool operator!=(SlabAllocator<T> const&) { return false; }
     46
     47    inline SlabAllocator() {}
     48    inline SlabAllocator(const SlabAllocator &) {}
     49    inline ~SlabAllocator() { release_memory(); }
     50
    4951private:
    50     struct Chunk {
    51         Chunk *     prev;
    52         uint8_t     space[BasePoolSize];
    53     };
    54     size_type       mSpaceAvail;
    55     uint8_t *       mAllocationPtr;
    56     Chunk *         mCurrentChunk;
    57     size_type       mTotalSize;
    58     Chunk           mInitialChunk;
     52    static LLVMAllocator mAllocator;
    5953};
    6054
    6155template <typename T>
    62 SlabAllocator<T>::SlabAllocator()
    63 : mSpaceAvail(BasePoolSize)
    64 , mAllocationPtr(mInitialChunk.space)
    65 , mCurrentChunk(&mInitialChunk)
    66 , mTotalSize(BasePoolSize)
    67 {
    68     mInitialChunk.prev = nullptr;
    69 }
    70 
    71 template <typename T>
    72 SlabAllocator<T>::~SlabAllocator() {
    73     release_memory();
    74 }
    75 
    76 template <typename T>
    77 void SlabAllocator<T>::release_memory() {
    78     while (mCurrentChunk != &mInitialChunk) {
    79         Chunk * n = mCurrentChunk;
    80         mCurrentChunk = n->prev;
    81         free(n);
    82     }
    83     mSpaceAvail = BasePoolSize;
    84     mAllocationPtr = mInitialChunk.space;
    85 }
    86 
    87 template <typename T>
    88 bool SlabAllocator<T>::extend(const size_type n) noexcept {
    89     const size_type size = std::max<size_type>(n, mTotalSize) * 2;
    90     Chunk * newChunk = (Chunk *)malloc(sizeof(Chunk) + size - BasePoolSize);
    91     if (newChunk == nullptr) {
    92         return false;
    93     }
    94     newChunk->prev = mCurrentChunk;
    95     mTotalSize += size;
    96     mCurrentChunk = newChunk;
    97     mAllocationPtr = newChunk->space;
    98     mSpaceAvail = size;
    99     return true;
    100 }
     56llvm::BumpPtrAllocator SlabAllocator<T>::mAllocator;
    10157
    10258#endif // SLAB_ALLOCATOR_H
Note: See TracChangeset for help on using the changeset viewer.