aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/hyperscan/src/som/slot_manager_internal.h
blob: 7e1fecc7e65adc81a58621a21e372a8302539cb8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
 * Copyright (c) 2015-2017, Intel Corporation
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *  * Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *  * Neither the name of Intel Corporation nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef SLOT_MANAGER_INTERNAL_H
#define SLOT_MANAGER_INTERNAL_H

#include "nfagraph/ng.h"
#include "nfagraph/ng_is_equal.h"
#include "util/charreach.h"
#include "ue2common.h"

#include <memory>
#include <unordered_map>
#include <unordered_set>
#include <vector>

namespace ue2 {

struct InitialResetEntry {
    InitialResetEntry(std::shared_ptr<const NGHolder> sent_in,
                      std::shared_ptr<const NGHolder> body_in,
                      const std::unordered_map<NFAVertex, u32> &body_regions_in,
                      u32 sent_region_in, u32 first_bad_region_in)
        : sent(sent_in), body(body_in), body_regions(body_regions_in),
          sent_region(sent_region_in), first_bad_region(first_bad_region_in) {}

    std::shared_ptr<const NGHolder> sent;
    std::shared_ptr<const NGHolder> body;
    std::unordered_map<NFAVertex, u32> body_regions;
    u32 sent_region;
    u32 first_bad_region; /* ~0U if it must cover the whole g */
};

struct InitialResetInfo {
    explicit InitialResetInfo(u32 slot_in) : slot(slot_in) {}

    std::vector<InitialResetEntry> entries;
    u32 slot;
};

struct SlotCacheEntry {
    // We store our own copy of the prefix so we control its lifetime. A
    // pointer is used so that this entry can be placed in STL containers, as
    // NGHolder is not copy-constructible.
    SlotCacheEntry(const NGHolder &prefix_in, const CharReach &escapes_in,
                   u32 parent_in, bool is_reset_in, u32 slot_in);

    std::unique_ptr<const NGHolder> prefix;
    CharReach escapes;
    u32 parent_slot;
    bool is_reset;
    u32 slot;
};

struct SlotEntryHasher {
    size_t operator()(const SlotCacheEntry &e) const;
};

struct SlotEntryEqual {
    bool operator()(const SlotCacheEntry &a, const SlotCacheEntry &b) const;
};

struct SlotCache {
    typedef std::unordered_set<SlotCacheEntry, SlotEntryHasher,
                               SlotEntryEqual> CacheStore;

    void insert(const NGHolder &prefix, const CharReach &escapes,
                u32 parent_slot, bool is_reset, u32 slot);

    const SlotCacheEntry *find(const NGHolder &prefix, const CharReach &escapes,
                               u32 parent_slot, bool is_reset);

    CacheStore store;

    std::unordered_set<std::shared_ptr<const NGHolder>, NGHolderHasher,
                  NGHolderEqual> initial_prefixes;
    std::vector<InitialResetInfo> initial_resets;
};

} // namespace ue2

#endif