source: icGREP/icgrep-devel/icgrep/grep/grep_engine.h @ 5952

Last change on this file since 5952 was 5952, checked in by cameron, 14 months ago

Use AlignedAllocator? when boost::align unavailable (Jenkins test server).

File size: 5.2 KB
RevLine 
[4324]1/*
[5476]2 *  Copyright (c) 2017 International Characters.
[4324]3 *  This software is licensed to the public under the Open Software License 3.0.
4 *  icgrep is a trademark of International Characters.
5 */
[5476]6#ifndef GREP_ENGINE_H
7#define GREP_ENGINE_H
8#include <grep_interface.h>
[5704]9#include <kernels/streamset.h>
[5804]10#include <cc/multiplex_CCs.h>
[5771]11#include <string>
[5267]12#include <vector>
[5695]13#include <sstream>
[5761]14#include <atomic>
[5464]15
[5267]16namespace re { class CC; }
17namespace re { class RE; }
[5473]18namespace llvm { namespace cl { class OptionCategory; } }
[5474]19class Driver;
[4324]20
[5474]21
[5473]22namespace grep {
[5892]23   
[5894]24enum class GrepRecordBreakKind {Null, LF, Unicode};
[5473]25
[5894]26class MatchAccumulator {
27public:
28    MatchAccumulator() {}
29    virtual void accumulate_match(const size_t lineNum, char * line_start, char * line_end) = 0;
30    virtual void finalize_match(char * buffer_end) {}  // default: no op
31};
32
[5913]33extern "C" void accumulate_match_wrapper(intptr_t accum_addr, const size_t lineNum, char * line_start, char * line_end);
[5894]34
[5913]35extern "C" void finalize_match_wrapper(intptr_t accum_addr, char * buffer_end);
[5894]36
[5945]37   
38#define MAX_SIMD_WIDTH_SUPPORTED 512
39#define INITIAL_CAPACITY 1024
40   
41class SearchableBuffer  {
42    SearchableBuffer();
43    void addSearchCandidate(char * string_ptr, size_t length);
[5952]44    size_t getCandidateCount() {return mEntries;}
[5945]45    ~SearchableBuffer();
46private:
47    static const unsigned BUFFER_ALIGNMENT = MAX_SIMD_WIDTH_SUPPORTED/8;
[5952]48    size_t mAllocated_capacity;
49    char * mBuffer_base;
50    alignas(BUFFER_ALIGNMENT) char mInitial_buffer[INITIAL_CAPACITY];
51    size_t mSpace_used;
52    size_t mEntries;
[5945]53};
54
[5894]55void grepBuffer(re::RE * pattern, const char * buffer, size_t bufferLength, MatchAccumulator * accum);
56
[5406]57class GrepEngine {
[5761]58    enum class FileStatus {Pending, GrepComplete, PrintComplete};
[4659]59public:
[4324]60
[5703]61    GrepEngine();
[5698]62    virtual ~GrepEngine();
[5695]63   
[5945]64    void setPreferMMap() {mPreferMMap = true;}
65   
66    void showFileNames() {mShowFileNames = true;}
67    void setStdinLabel(std::string lbl) {mStdinLabel = lbl;}
68    void showLineNumbers() {mShowLineNumbers = true;}
69    void setInitialTab() {mInitialTab = true;}
70
71    void setMaxCount(int m) {mMaxCount = m;}
72    void setInvertMatches() {mInvertMatches = true;}
73    void setCaseInsensitive()  {mCaseInsensitive = true;}
74
75    void suppressFileMessages() {mSuppressFileMessages = true;}
76
[5894]77    void setRecordBreak(GrepRecordBreakKind b);
[5703]78    void initFileResult(std::vector<std::string> & filenames);
[5913]79    void initREs(std::vector<re::RE *> & REs);
80    virtual void grepCodeGen();
[5703]81    bool searchAllFiles();
[5735]82    void * DoGrepThreadMethod();
83
[5700]84protected:
[5913]85    std::pair<parabix::StreamSetBuffer *, parabix::StreamSetBuffer *> grepPipeline(parabix::StreamSetBuffer * ByteStream);
[5704]86
87    virtual uint64_t doGrep(const std::string & fileName, const uint32_t fileIdx);
[5771]88    int32_t openFile(const std::string & fileName, std::ostringstream & msgstrm);
[5703]89
[5945]90    enum class EngineKind {QuietMode, MatchOnly, CountOnly, EmitMatches};
91    EngineKind mEngineKind;
92   
93    std::string linePrefix(std::string fileName);
94
95    bool mSuppressFileMessages;
96    bool mPreferMMap;
97    bool mShowFileNames;
98    std::string mStdinLabel;
99    bool mShowLineNumbers;
100    bool mInitialTab;
101    bool mCaseInsensitive;
102    bool mInvertMatches;
103    int mMaxCount;
104   
[5474]105    Driver * mGrepDriver;
[5695]106
[5761]107    std::atomic<unsigned> mNextFileToGrep;
108    std::atomic<unsigned> mNextFileToPrint;
[5695]109    std::vector<std::string> inputFiles;
[5771]110    std::vector<std::ostringstream> mResultStrs;
[5735]111    std::vector<FileStatus> mFileStatus;
[5771]112    bool grepMatchFound;
[5894]113    GrepRecordBreakKind mGrepRecordBreak;
[5913]114   
115    std::vector<re:: RE *> mREs;
116    std::set<re::Name *> mUnicodeProperties;
117    re::CC * mBreakCC;
[5804]118    std::unique_ptr<cc::MultiplexedAlphabet> mpx;
[5700]119    std::string mFileSuffix;
[5704]120    bool mMoveMatchesToEOL;
[5735]121    pthread_t mEngineThread;
[4324]122};
123
[5941]124
125//
126// The EmitMatches engine uses an EmitMatchesAccumulator object to concatenate together
127// matched lines.
128
129class EmitMatch : public MatchAccumulator {
130    friend class EmitMatchesEngine;
131public:
[5945]132    EmitMatch(std::string linePrefix, bool showLineNumbers, bool initialTab, std::ostringstream & strm) : mLinePrefix(linePrefix),
133        mShowLineNumbers(showLineNumbers),
134        mInitialTab(initialTab),
135        mLineCount(0),
136        mTerminated(true),
137        mResultStr(strm) {}
[5941]138    void accumulate_match(const size_t lineNum, char * line_start, char * line_end) override;
139    void finalize_match(char * buffer_end) override;
140protected:
141    std::string mLinePrefix;
[5945]142    bool mShowLineNumbers;
143    bool mInitialTab;
[5941]144    size_t mLineCount;
145    bool mTerminated;
146    std::ostringstream & mResultStr;
147};
148
[5703]149class EmitMatchesEngine : public GrepEngine {
[5698]150public:
[5703]151    EmitMatchesEngine();
[5913]152    void grepCodeGen() override;
[5700]153private:
154    uint64_t doGrep(const std::string & fileName, const uint32_t fileIdx) override;
[5698]155};
156
[5703]157class CountOnlyEngine : public GrepEngine {
[5700]158public:
[5703]159    CountOnlyEngine();
[5700]160private:
161    uint64_t doGrep(const std::string & fileName, const uint32_t fileIdx) override;
[5703]162};
163
164class MatchOnlyEngine : public GrepEngine {
165public:
[5945]166    MatchOnlyEngine(bool showFilesWithoutMatch, bool useNullSeparators);
[5703]167private:
168    uint64_t doGrep(const std::string & fileName, const uint32_t fileIdx) override;
[5704]169    unsigned mRequiredCount;
[5700]170};
[5704]171
172class QuietModeEngine : public GrepEngine {
173public:
174    QuietModeEngine();
175};
176
[5473]177}
[5033]178
[4324]179#endif
Note: See TracBrowser for help on using the repository browser.