|
| 1 | +// Copyright (c) 2013-2015 Sandstorm Development Group, Inc. and contributors |
| 2 | +// Licensed under the MIT License: |
| 3 | +// |
| 4 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 | +// of this software and associated documentation files (the "Software"), to deal |
| 6 | +// in the Software without restriction, including without limitation the rights |
| 7 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | +// copies of the Software, and to permit persons to whom the Software is |
| 9 | +// furnished to do so, subject to the following conditions: |
| 10 | +// |
| 11 | +// The above copyright notice and this permission notice shall be included in |
| 12 | +// all copies or substantial portions of the Software. |
| 13 | +// |
| 14 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 19 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 20 | +// THE SOFTWARE. |
| 21 | + |
| 22 | +#[cfg(feature = "sync_reader")] |
| 23 | +pub use sync::ReadLimiter; |
| 24 | + |
| 25 | +#[cfg(feature = "sync_reader")] |
| 26 | +mod sync { |
| 27 | + use crate::{Error, Result}; |
| 28 | + use core::sync::atomic::{AtomicUsize, Ordering}; |
| 29 | + |
| 30 | + pub struct ReadLimiter { |
| 31 | + pub limit: usize, |
| 32 | + pub read: AtomicUsize, |
| 33 | + } |
| 34 | + |
| 35 | + impl ReadLimiter { |
| 36 | + pub fn new(limit: u64) -> ReadLimiter { |
| 37 | + if limit > core::usize::MAX as u64 { |
| 38 | + panic!("traversal_limit_in_words cannot be bigger than core::usize::MAX") |
| 39 | + } |
| 40 | + |
| 41 | + ReadLimiter { |
| 42 | + limit: limit as usize, |
| 43 | + read: AtomicUsize::new(0), |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + #[inline] |
| 48 | + pub fn can_read(&self, amount: usize) -> Result<()> { |
| 49 | + let read = self.read.load(Ordering::Relaxed) + amount; |
| 50 | + |
| 51 | + if read > self.limit { |
| 52 | + Err(Error::failed(format!("read limit exceeded"))) |
| 53 | + } else { |
| 54 | + self.read.fetch_add(amount, Ordering::Relaxed); |
| 55 | + Ok(()) |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +#[cfg(not(feature = "sync_reader"))] |
| 62 | +pub use unsync::ReadLimiter; |
| 63 | + |
| 64 | +#[cfg(not(feature = "sync_reader"))] |
| 65 | +mod unsync { |
| 66 | + use core::cell::Cell; |
| 67 | + use crate::{Error, Result}; |
| 68 | + |
| 69 | + pub struct ReadLimiter { |
| 70 | + pub limit: Cell<u64>, |
| 71 | + } |
| 72 | + |
| 73 | + impl ReadLimiter { |
| 74 | + pub fn new(limit: u64) -> ReadLimiter { |
| 75 | + ReadLimiter { |
| 76 | + limit: Cell::new(limit), |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + #[inline] |
| 81 | + pub fn can_read(&self, amount: usize) -> Result<()> { |
| 82 | + let amount = amount as u64; |
| 83 | + let current = self.limit.get(); |
| 84 | + if amount > current { |
| 85 | + Err(Error::failed(format!("read limit exceeded"))) |
| 86 | + } else { |
| 87 | + self.limit.set(current - amount); |
| 88 | + Ok(()) |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | +} |
0 commit comments