New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Sync ReadLimiter #201
Sync ReadLimiter #201
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,7 @@ mod primitive; | |
pub mod layout; | ||
mod mask; | ||
pub mod units; | ||
mod read_limiter; | ||
mod zero; | ||
|
||
#[cfg(test)] | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
// Copyright (c) 2013-2015 Sandstorm Development Group, Inc. and contributors | ||
// Licensed under the MIT License: | ||
// | ||
// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
// of this software and associated documentation files (the "Software"), to deal | ||
// in the Software without restriction, including without limitation the rights | ||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
// copies of the Software, and to permit persons to whom the Software is | ||
// furnished to do so, subject to the following conditions: | ||
// | ||
// The above copyright notice and this permission notice shall be included in | ||
// all copies or substantial portions of the Software. | ||
// | ||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
// THE SOFTWARE. | ||
|
||
#[cfg(feature = "sync_reader")] | ||
pub use sync::ReadLimiter; | ||
|
||
#[cfg(feature = "sync_reader")] | ||
mod sync { | ||
use crate::{Error, Result}; | ||
use core::sync::atomic::{AtomicUsize, AtomicBool, Ordering}; | ||
|
||
pub struct ReadLimiter { | ||
pub limit: AtomicUsize, | ||
pub limit_reached: AtomicBool, | ||
} | ||
|
||
impl ReadLimiter { | ||
pub fn new(limit: u64) -> ReadLimiter { | ||
if limit > core::usize::MAX as u64 { | ||
panic!("traversal_limit_in_words cannot be bigger than core::usize::MAX") | ||
} | ||
|
||
ReadLimiter { | ||
limit: AtomicUsize::new(limit as usize), | ||
limit_reached: AtomicBool::new(false), | ||
} | ||
} | ||
|
||
#[inline] | ||
pub fn can_read(&self, amount: usize) -> Result<()> { | ||
let limit_reached = self.limit_reached.load(Ordering::Relaxed); | ||
if limit_reached { | ||
return Err(Error::failed(format!("read limit exceeded"))); | ||
} | ||
|
||
let prev_limit = self.limit.fetch_sub(amount, Ordering::Relaxed); | ||
if prev_limit == amount { | ||
self.limit_reached.store(true, Ordering::Relaxed); | ||
} else if prev_limit < amount { | ||
self.limit_reached.store(true, Ordering::Relaxed); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the goal is to cause future reads to fail too, then I think we could do
here and not need to worry about adding a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Won't this just resets the value to what it was right before the If we want to use a single variable, we could set the value to 0 here when fully consumed (since the new value after |
||
return Err(Error::failed(format!("read limit exceeded"))); | ||
} | ||
|
||
Ok(()) | ||
} | ||
} | ||
} | ||
|
||
#[cfg(not(feature = "sync_reader"))] | ||
pub use unsync::ReadLimiter; | ||
|
||
#[cfg(not(feature = "sync_reader"))] | ||
mod unsync { | ||
use crate::{Error, Result}; | ||
use core::cell::Cell; | ||
|
||
pub struct ReadLimiter { | ||
pub limit: Cell<u64>, | ||
} | ||
|
||
impl ReadLimiter { | ||
pub fn new(limit: u64) -> ReadLimiter { | ||
ReadLimiter { | ||
limit: Cell::new(limit), | ||
} | ||
} | ||
|
||
#[inline] | ||
pub fn can_read(&self, amount: usize) -> Result<()> { | ||
let amount = amount as u64; | ||
let current = self.limit.get(); | ||
if amount > current { | ||
Err(Error::failed(format!("read limit exceeded"))) | ||
} else { | ||
self.limit.set(current - amount); | ||
Ok(()) | ||
} | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this branch necessary? When
prev_limit == amount
, the current limit will be 0, and therefore we'll get an error on the next attempt to read.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right, it's not necessary