| 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 | #ifndef SLOT_MANAGER_INTERNAL_H |
| 30 | #define SLOT_MANAGER_INTERNAL_H |
| 31 | |
| 32 | #include "nfagraph/ng.h" |
| 33 | #include "nfagraph/ng_is_equal.h" |
| 34 | #include "util/charreach.h" |
| 35 | #include "ue2common.h" |
| 36 | |
| 37 | #include <memory> |
| 38 | #include <unordered_map> |
| 39 | #include <unordered_set> |
| 40 | #include <vector> |
| 41 | |
| 42 | namespace ue2 { |
| 43 | |
| 44 | struct InitialResetEntry { |
| 45 | InitialResetEntry(std::shared_ptr<const NGHolder> sent_in, |
| 46 | std::shared_ptr<const NGHolder> body_in, |
| 47 | const std::unordered_map<NFAVertex, u32> &body_regions_in, |
| 48 | u32 sent_region_in, u32 first_bad_region_in) |
| 49 | : sent(sent_in), body(body_in), body_regions(body_regions_in), |
| 50 | sent_region(sent_region_in), first_bad_region(first_bad_region_in) {} |
| 51 | |
| 52 | std::shared_ptr<const NGHolder> sent; |
| 53 | std::shared_ptr<const NGHolder> body; |
| 54 | std::unordered_map<NFAVertex, u32> body_regions; |
| 55 | u32 sent_region; |
| 56 | u32 first_bad_region; /* ~0U if it must cover the whole g */ |
| 57 | }; |
| 58 | |
| 59 | struct InitialResetInfo { |
| 60 | explicit InitialResetInfo(u32 slot_in) : slot(slot_in) {} |
| 61 | |
| 62 | std::vector<InitialResetEntry> entries; |
| 63 | u32 slot; |
| 64 | }; |
| 65 | |
| 66 | struct SlotCacheEntry { |
| 67 | // We store our own copy of the prefix so we control its lifetime. A |
| 68 | // pointer is used so that this entry can be placed in STL containers, as |
| 69 | // NGHolder is not copy-constructible. |
| 70 | SlotCacheEntry(const NGHolder &prefix_in, const CharReach &escapes_in, |
| 71 | u32 parent_in, bool is_reset_in, u32 slot_in); |
| 72 | |
| 73 | std::unique_ptr<const NGHolder> prefix; |
| 74 | CharReach escapes; |
| 75 | u32 parent_slot; |
| 76 | bool is_reset; |
| 77 | u32 slot; |
| 78 | }; |
| 79 | |
| 80 | struct SlotEntryHasher { |
| 81 | size_t operator()(const SlotCacheEntry &e) const; |
| 82 | }; |
| 83 | |
| 84 | struct SlotEntryEqual { |
| 85 | bool operator()(const SlotCacheEntry &a, const SlotCacheEntry &b) const; |
| 86 | }; |
| 87 | |
| 88 | struct SlotCache { |
| 89 | typedef std::unordered_set<SlotCacheEntry, SlotEntryHasher, |
| 90 | SlotEntryEqual> CacheStore; |
| 91 | |
| 92 | void insert(const NGHolder &prefix, const CharReach &escapes, |
| 93 | u32 parent_slot, bool is_reset, u32 slot); |
| 94 | |
| 95 | const SlotCacheEntry *find(const NGHolder &prefix, const CharReach &escapes, |
| 96 | u32 parent_slot, bool is_reset); |
| 97 | |
| 98 | CacheStore store; |
| 99 | |
| 100 | std::unordered_set<std::shared_ptr<const NGHolder>, NGHolderHasher, |
| 101 | NGHolderEqual> initial_prefixes; |
| 102 | std::vector<InitialResetInfo> initial_resets; |
| 103 | }; |
| 104 | |
| 105 | } // namespace ue2 |
| 106 | |
| 107 | #endif |
| 108 | |