1/*
2 * Copyright (c) 2015-2017, Intel Corporation
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * * Redistributions of source code must retain the above copyright notice,
8 * this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of Intel Corporation nor the names of its contributors
13 * may be used to endorse or promote products derived from this software
14 * without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/**
30 * \file
31 * \brief SOM Slot Manager.
32 */
33
34#ifndef SLOT_MANAGER_H
35#define SLOT_MANAGER_H
36
37#include "ue2common.h"
38#include "nfagraph/ng_holder.h"
39#include "util/bytecode_ptr.h"
40#include "util/noncopyable.h"
41
42#include <deque>
43#include <memory>
44#include <unordered_map>
45
46struct NFA;
47
48namespace ue2 {
49
50class CharReach;
51class NGHolder;
52struct Grey;
53struct SlotCache;
54
55/** \brief SOM slot manager. Used to hand out SOM slots and track their
56 * relationships during SOM construction. Also stores reverse NFAs used for
57 * SOM. */
58class SomSlotManager : noncopyable {
59public:
60 explicit SomSlotManager(u8 precision);
61 ~SomSlotManager();
62
63 /** \brief Sentinel value used to specify that a slot has no parent. */
64 static constexpr u32 NO_PARENT = ~0;
65
66 u32 getSomSlot(const NGHolder &prefix, const CharReach &escapes,
67 bool is_reset, u32 parent_slot);
68
69 /** prefix must be acting as a resetting sentinel and should be a dag (if
70 * not how are we establish som?) */
71 u32 getInitialResetSomSlot(const NGHolder &prefix, const NGHolder &g,
72 const std::unordered_map<NFAVertex, u32> &region_map,
73 u32 last_sent_region,
74 bool *prefix_already_implemented);
75
76 u32 getPrivateSomSlot(void);
77
78 void rollbackSomTo(u32 num);
79
80 u32 numSomSlots() const;
81
82 const std::deque<bytecode_ptr<NFA>> &getRevNfas() const {
83 return rev_nfas;
84 }
85
86 u32 addRevNfa(bytecode_ptr<NFA> nfa, u32 maxWidth);
87
88 u32 somHistoryRequired() const { return historyRequired; }
89
90 u32 somPrecision() const { return precision; }
91
92 void somPrecision(u32 p) {
93 precision = p;
94 }
95
96private:
97 u32 nextSomSlot;
98 std::unique_ptr<SlotCache> cache;
99
100 /** \brief Reverse NFAs used for SOM support. */
101 std::deque<bytecode_ptr<NFA>> rev_nfas;
102
103 /** \brief In streaming mode, the amount of history we've committed to
104 * using for SOM rev NFAs. */
105 u32 historyRequired;
106
107 /** \brief Number of bytes of SOM precision requested by the user, zero if
108 * not in SOM mode. */
109 u32 precision;
110
111#ifdef DUMP_SUPPORT
112 friend void dumpSomSlotManager(const SomSlotManager &ssm, const Grey &grey);
113#endif
114};
115
116} // namespace ue2
117
118#endif
119