├── .gitignore ├── Cargo.toml ├── .github └── workflows │ └── ci.yml ├── LICENSE-MIT ├── Makefile ├── src ├── allocator.rs └── lib.rs ├── README.md └── LICENSE-APACHE /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /Cargo.lock 3 | /.idea 4 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "allocation-counter" 3 | version = "0.8.1" 4 | authors = ["Fredrik Fornwall "] 5 | categories = ["development-tools", "memory-management"] 6 | description = "Count the number of memory allocation of some code." 7 | edition = "2021" 8 | homepage = "https://github.com/fornwall/allocation-counter" 9 | keywords = ["allocator", "memory"] 10 | license = "MIT/Apache-2.0" 11 | readme = "README.md" 12 | repository = "https://github.com/fornwall/allocation-counter" 13 | -------------------------------------------------------------------------------- /.github/workflows/ci.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | on: push 3 | jobs: 4 | test: 5 | strategy: 6 | matrix: 7 | os: [ubuntu, macos, windows] 8 | runs-on: ${{ matrix.os }}-latest 9 | steps: 10 | - uses: actions/checkout@v4 11 | - uses: dtolnay/rust-toolchain@stable 12 | - run: make check 13 | 14 | test-32-bits: 15 | runs-on: ubuntu-latest 16 | steps: 17 | - uses: actions/checkout@v4 18 | - uses: dtolnay/rust-toolchain@stable 19 | with: 20 | targets: i686-unknown-linux-musl 21 | - run: cargo test --target i686-unknown-linux-musl 22 | -------------------------------------------------------------------------------- /LICENSE-MIT: -------------------------------------------------------------------------------- 1 | MIT/X Consortium License 2 | 3 | @ 2019-2020 Fedor Logachev 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a 6 | copy of this software and associated documentation files (the "Software"), 7 | to deal in the Software without restriction, including without limitation 8 | the rights to use, copy, modify, merge, publish, distribute, sublicense, 9 | and/or sell copies of the Software, and to permit persons to whom the 10 | Software is furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18 | THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 21 | DEALINGS IN THE SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | CARGO_COMMAND = cargo 2 | CLIPPY_PARAMS = -- \ 3 | -W clippy::cargo \ 4 | -W clippy::cast_lossless \ 5 | -W clippy::dbg_macro \ 6 | -W clippy::expect_used \ 7 | -W clippy::if_not_else \ 8 | -W clippy::items_after_statements \ 9 | -W clippy::large_stack_arrays \ 10 | -W clippy::linkedlist \ 11 | -W clippy::manual_filter_map \ 12 | -W clippy::match_same_arms \ 13 | -W clippy::needless_continue \ 14 | -W clippy::needless_pass_by_value \ 15 | -W clippy::nursery \ 16 | -W clippy::option_if_let_else \ 17 | -W clippy::print_stderr \ 18 | -W clippy::print_stdout \ 19 | -W clippy::redundant_closure_for_method_calls \ 20 | -W clippy::semicolon_if_nothing_returned \ 21 | -W clippy::similar_names \ 22 | -W clippy::single_match_else \ 23 | -W clippy::trivially_copy_pass_by_ref \ 24 | -W clippy::unnested_or_patterns \ 25 | -W clippy::unreadable-literal \ 26 | -W clippy::unseparated-literal-suffix \ 27 | -D warnings 28 | ifeq ($(CLIPPY_PEDANTIC),1) 29 | CLIPPY_PARAMS += -W clippy::pedantic 30 | endif 31 | 32 | check: 33 | $(CARGO_COMMAND) fmt --all 34 | $(CARGO_COMMAND) clippy --tests $(CLIPPY_PARAMS) 35 | $(CARGO_COMMAND) clippy --lib --bins $(CLIPPY_PARAMS) -D clippy::panic 36 | $(CARGO_COMMAND) test 37 | $(CARGO_COMMAND) test --release 38 | -------------------------------------------------------------------------------- /src/allocator.rs: -------------------------------------------------------------------------------- 1 | use std::alloc::{GlobalAlloc, Layout, System}; 2 | use std::cell::RefCell; 3 | 4 | pub const MAX_DEPTH: usize = 64; 5 | 6 | pub struct AllocationInfoStack { 7 | pub depth: u32, 8 | pub elements: [crate::AllocationInfo; MAX_DEPTH], 9 | } 10 | 11 | thread_local! { 12 | pub static ALLOCATIONS: RefCell = RefCell::new(AllocationInfoStack { 13 | depth: 0, 14 | elements: [crate::AllocationInfo::default(); MAX_DEPTH], 15 | }); 16 | } 17 | thread_local! { 18 | pub static DO_COUNT: RefCell = RefCell::new(0); 19 | } 20 | 21 | struct CountingAllocator; 22 | 23 | unsafe impl GlobalAlloc for CountingAllocator { 24 | unsafe fn alloc(&self, l: Layout) -> *mut u8 { 25 | DO_COUNT.with(|b| { 26 | if *b.borrow() == 0 { 27 | ALLOCATIONS.with(|info_stack| { 28 | let mut info_stack = info_stack.borrow_mut(); 29 | let depth = info_stack.depth; 30 | let info = &mut info_stack.elements[depth as usize]; 31 | 32 | info.count_total += 1; 33 | info.count_current += 1; 34 | if info.count_current > 0 { 35 | info.count_max = info.count_max.max(info.count_current as u64); 36 | } 37 | info.bytes_total += l.size() as u64; 38 | info.bytes_current += l.size() as i64; 39 | if info.bytes_current > 0 { 40 | info.bytes_max = info.bytes_max.max(info.bytes_current as u64); 41 | } 42 | }); 43 | } 44 | }); 45 | 46 | System.alloc(l) 47 | } 48 | 49 | unsafe fn dealloc(&self, ptr: *mut u8, l: Layout) { 50 | DO_COUNT.with(|b| { 51 | if *b.borrow() == 0 { 52 | ALLOCATIONS.with(|info_stack| { 53 | let mut info_stack = info_stack.borrow_mut(); 54 | let depth = info_stack.depth; 55 | let info = &mut info_stack.elements[depth as usize]; 56 | info.count_current -= 1; 57 | info.bytes_current -= l.size() as i64; 58 | }); 59 | } 60 | }); 61 | 62 | System.dealloc(ptr, l); 63 | } 64 | } 65 | 66 | #[global_allocator] 67 | static GLOBAL: CountingAllocator = CountingAllocator {}; 68 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Crates.io](https://img.shields.io/crates/v/allocation-counter.svg)](https://crates.io/crates/allocation-counter) 2 | [![Docs](https://docs.rs/allocation-counter/badge.svg)](https://docs.rs/allocation-counter/) 3 | [![Build](https://github.com/fornwall/allocation-counter/workflows/CI/badge.svg)](https://github.com/fornwall/allocation-counter/actions?query=workflow%3A%22CI%22) 4 | 5 | # allocation-counter 6 | Rust library to run code while counting allocations. Can be used to explore memory allocation usage, or assert that the desired amount of memory allocations is not exceeded in tests. 7 | 8 | It works by replacing the System allocator with a custom one which increases a thread local counter on each memory allocation before delegating to the normal system allocator. 9 | 10 | See the below example and the [crate documentation](https://docs.rs/allocation-counter/latest/allocation_counter/) for more information. 11 | 12 | # Example 13 | Add as a dependency - since including the trait replaces the global memory allocator, you most likely want it gated behind a feature: 14 | 15 | ```toml 16 | [features] 17 | count-allocations = ["allocation-counter"] 18 | 19 | [dependencies] 20 | allocation-counter = { version = "0", optional = true } 21 | ``` 22 | 23 | Tests can now be written to assert that the number of desired memory allocations are not exceeded: 24 | 25 | ```rust 26 | #[cfg(feature = "count-allocations")] 27 | #[test] 28 | pub fn no_memory_allocations() { 29 | // Verify that no memory allocations are made: 30 | let info = allocation_counter::measure(|| { 31 | code_that_should_not_allocate(); 32 | }); 33 | assert_eq!(info.count_total, 0); 34 | 35 | // Let's use a case where some allocations are expected. 36 | let info = allocation_counter::measure(|| { 37 | code_that_should_allocate_a_little(); 38 | }); 39 | 40 | // Using a lower bound can help track behaviour over time: 41 | assert!((500..600).contains(&info.count_total)); 42 | assert!((10_000..20_000).contains(&info.bytes_total)); 43 | 44 | // Limit peak memory usage: 45 | assert!((100..200).contains(&info.count_max)); 46 | assert!((1_000..2_000).contains(&info.bytes_max)); 47 | 48 | // We don't want any leaks: 49 | assert_eq!(0, info.count_current); 50 | assert_eq!(0, info.bytes_current); 51 | 52 | // It's possible to opt out of counting allocations 53 | // for certain parts of the code flow: 54 | let info = allocation_counter::measure(|| { 55 | code_that_should_not_allocate(); 56 | allocation_counter::opt_out(|| { 57 | external_code_that_should_not_be_tested(); 58 | }); 59 | code_that_should_not_allocate(); 60 | }); 61 | assert_eq!(0, info.count_total); 62 | } 63 | ``` 64 | 65 | Run the tests with the necessary feature enabled: 66 | 67 | ```sh 68 | cargo test --features count-allocations 69 | ``` 70 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | /*! 2 | This crate provides a method to measure memory allocations while running some code. 3 | 4 | It can be used either exploratory (obtaining insights in how much memory allocations 5 | are being made), or as a tool to assert desired allocation behaviour in tests. 6 | 7 | # Usage 8 | Add as a dependency - since including the trait replaces the global memory allocator, 9 | you most likely want it gated behind a feature. 10 | 11 | ```toml 12 | [features] 13 | count-allocations = ["allocation-counter"] 14 | 15 | [dependencies] 16 | allocation-counter = { version = "0", optional = true } 17 | ``` 18 | 19 | The [measure()] function is now available, which can measure memory allocations made 20 | when the supplied function or closure runs. 21 | 22 | Tests can be conditional on the feature: 23 | 24 | ``` 25 | #[cfg(feature = "count-allocations")] 26 | #[test] 27 | { 28 | // [...] 29 | } 30 | ``` 31 | 32 | The test code itself could look like: 33 | 34 | ```no_run 35 | # fn code_that_should_not_allocate() {} 36 | # fn code_that_should_allocate_a_little() {} 37 | # fn external_code_that_should_not_be_tested() {} 38 | // Verify that no memory allocations are made: 39 | let info = allocation_counter::measure(|| { 40 | code_that_should_not_allocate(); 41 | }); 42 | assert_eq!(info.count_total, 0); 43 | 44 | // Let's use a case where some allocations are expected. 45 | let info = allocation_counter::measure(|| { 46 | code_that_should_allocate_a_little(); 47 | }); 48 | 49 | // Using a lower bound can help track behaviour over time: 50 | assert!((500..600).contains(&info.count_total)); 51 | assert!((10_000..20_000).contains(&info.bytes_total)); 52 | 53 | // Limit peak memory usage: 54 | assert!((100..200).contains(&info.count_max)); 55 | assert!((1_000..2_000).contains(&info.bytes_max)); 56 | 57 | // We don't want any leaks: 58 | assert_eq!(0, info.count_current); 59 | assert_eq!(0, info.bytes_current); 60 | 61 | // It's possible to opt out of counting allocations 62 | // for certain parts of the code flow: 63 | let info = allocation_counter::measure(|| { 64 | code_that_should_not_allocate(); 65 | allocation_counter::opt_out(|| { 66 | external_code_that_should_not_be_tested(); 67 | }); 68 | code_that_should_not_allocate(); 69 | }); 70 | assert_eq!(0, info.count_total); 71 | ``` 72 | 73 | Run the tests with the necessary feature enabled. 74 | 75 | ```sh 76 | cargo test --features count-allocations 77 | ``` 78 | */ 79 | 80 | pub(crate) mod allocator; 81 | 82 | /// The allocation information obtained by a [measure()] call. 83 | #[derive(Clone, Copy, Default, Debug, PartialEq, Eq, Hash)] 84 | pub struct AllocationInfo { 85 | /// The total number of allocations made during a [measure()] call. 86 | pub count_total: u64, 87 | 88 | /// The current (net result) number of allocations during a [measure()] call. 89 | /// 90 | /// A non-zero value of this field means that the function did not deallocate all allocations, as shown below. 91 | /// 92 | /// ``` 93 | /// let info = allocation_counter::measure(|| { 94 | /// let b = std::hint::black_box(Box::new(1_u32)); 95 | /// std::mem::forget(b); 96 | /// }); 97 | /// assert_eq!(info.count_current, 1); 98 | /// ``` 99 | pub count_current: i64, 100 | 101 | /// The max number of allocations held during a point in time during a [measure()] call. 102 | pub count_max: u64, 103 | 104 | /// The total amount of bytes allocated during a [measure()] call. 105 | pub bytes_total: u64, 106 | 107 | /// The current (net result) amount of bytes allocated during a [measure()] call. 108 | /// 109 | /// A non-zero value of this field means that not all memory was deallocated, as shown below. 110 | /// 111 | /// ``` 112 | /// let info = allocation_counter::measure(|| { 113 | /// let b = std::hint::black_box(Box::new(1_u32)); 114 | /// std::mem::forget(b); 115 | /// }); 116 | /// assert_eq!(info.bytes_current, 4); 117 | /// ``` 118 | pub bytes_current: i64, 119 | 120 | /// The max amount of bytes allocated at one time during a [measure()] call. 121 | pub bytes_max: u64, 122 | } 123 | 124 | impl std::ops::AddAssign for AllocationInfo { 125 | fn add_assign(&mut self, other: Self) { 126 | self.count_total += other.count_total; 127 | self.count_current += other.count_current; 128 | self.count_max += other.count_max; 129 | self.bytes_total += other.bytes_total; 130 | self.bytes_current += other.bytes_current; 131 | self.bytes_max += other.bytes_max; 132 | } 133 | } 134 | 135 | /// Run a closure or function while measuring the performed memory allocations. 136 | /// 137 | /// Will only measure those allocations done by the current thread, so take care 138 | /// when interpreting the returned count for multithreaded code. 139 | /// 140 | /// Use [opt_out()] to opt of of counting allocations temporarily. 141 | /// 142 | /// Nested `measure()` calls are supported up to a max depth of 64. 143 | /// 144 | /// # Arguments 145 | /// 146 | /// - `run_while_measuring` - The code to run while measuring allocations 147 | /// 148 | /// # Examples 149 | /// 150 | /// ``` 151 | /// # fn code_that_should_not_allocate_memory() {} 152 | /// let actual = allocation_counter::measure(|| { 153 | /// "hello, world".to_string(); 154 | /// }); 155 | /// let expected = allocation_counter::AllocationInfo { 156 | /// count_total: 1, 157 | /// count_current: 0, 158 | /// count_max: 1, 159 | /// bytes_total: 12, 160 | /// bytes_current: 0, 161 | /// bytes_max: 12, 162 | /// }; 163 | /// assert_eq!(actual, expected); 164 | /// ``` 165 | pub fn measure(run_while_measuring: F) -> AllocationInfo { 166 | allocator::ALLOCATIONS.with(|info_stack| { 167 | let mut info_stack = info_stack.borrow_mut(); 168 | info_stack.depth += 1; 169 | assert!( 170 | (info_stack.depth as usize) < allocator::MAX_DEPTH, 171 | "Too deep allocation measuring nesting" 172 | ); 173 | let depth = info_stack.depth; 174 | info_stack.elements[depth as usize] = AllocationInfo::default(); 175 | }); 176 | 177 | run_while_measuring(); 178 | 179 | allocator::ALLOCATIONS.with(|info_stack| { 180 | let mut info_stack = info_stack.borrow_mut(); 181 | let depth = info_stack.depth; 182 | let popped = info_stack.elements[depth as usize]; 183 | info_stack.depth -= 1; 184 | let depth = info_stack.depth as usize; 185 | info_stack.elements[depth] += popped; 186 | popped 187 | }) 188 | } 189 | 190 | /// Opt out of counting allocations while running some code. 191 | /// 192 | /// Useful to avoid certain parts of the code flow that should not be counted. 193 | /// 194 | /// # Arguments 195 | /// 196 | /// - `run_while_not_counting` - The code to run while not counting allocations 197 | /// 198 | /// # Examples 199 | /// 200 | /// ``` 201 | /// # fn code_that_should_not_allocate() {} 202 | /// # fn external_code_that_should_not_be_tested() {} 203 | /// let info = allocation_counter::measure(|| { 204 | /// code_that_should_not_allocate(); 205 | /// allocation_counter::opt_out(|| { 206 | /// external_code_that_should_not_be_tested(); 207 | /// }); 208 | /// code_that_should_not_allocate(); 209 | /// }); 210 | /// assert_eq!(info.count_total, 0); 211 | /// ``` 212 | pub fn opt_out(run_while_not_counting: F) { 213 | allocator::DO_COUNT.with(|b| { 214 | *b.borrow_mut() += 1; 215 | run_while_not_counting(); 216 | *b.borrow_mut() -= 1; 217 | }); 218 | } 219 | 220 | #[test] 221 | fn test_measure() { 222 | let info = measure(|| { 223 | // Do nothing. 224 | }); 225 | assert_eq!(info.bytes_current, 0); 226 | assert_eq!(info.bytes_total, 0); 227 | assert_eq!(info.bytes_max, 0); 228 | assert_eq!(info.count_current, 0); 229 | assert_eq!(info.count_total, 0); 230 | assert_eq!(info.count_max, 0); 231 | 232 | let info = measure(|| { 233 | { 234 | let _a = std::hint::black_box(Box::new(1_u32)); 235 | } 236 | { 237 | let _b = std::hint::black_box(Box::new(1_u32)); 238 | } 239 | }); 240 | assert_eq!(info.bytes_current, 0); 241 | assert_eq!(info.bytes_total, 8); 242 | assert_eq!(info.bytes_max, 4); 243 | assert_eq!(info.count_current, 0); 244 | assert_eq!(info.count_total, 2); 245 | assert_eq!(info.count_max, 1); 246 | 247 | let info = measure(|| { 248 | { 249 | let _a = std::hint::black_box(Box::new(1_u32)); 250 | } 251 | let b = std::hint::black_box(Box::new(1_u32)); 252 | std::mem::forget(b); 253 | }); 254 | assert_eq!(info.bytes_current, 4); 255 | assert_eq!(info.bytes_total, 8); 256 | assert_eq!(info.bytes_max, 4); 257 | assert_eq!(info.count_current, 1); 258 | assert_eq!(info.count_total, 2); 259 | assert_eq!(info.count_max, 1); 260 | 261 | let info = measure(|| { 262 | let a = std::hint::black_box(Box::new(1_u32)); 263 | let b = std::hint::black_box(Box::new(1_u32)); 264 | let _c = std::hint::black_box(Box::new(*a + *b)); 265 | }); 266 | assert_eq!(info.bytes_current, 0); 267 | assert_eq!(info.bytes_total, 12); 268 | assert_eq!(info.bytes_max, 12); 269 | assert_eq!(info.count_current, 0); 270 | assert_eq!(info.count_total, 3); 271 | assert_eq!(info.count_max, 3); 272 | } 273 | 274 | #[test] 275 | fn test_opt_out() { 276 | let allocations = measure(|| { 277 | // Do nothing. 278 | }); 279 | assert_eq!(allocations.count_total, 0); 280 | 281 | let allocations = measure(|| { 282 | let v: Vec = vec![12]; 283 | assert_eq!(v.len(), 1); 284 | opt_out(|| { 285 | let v: Vec = vec![12]; 286 | assert_eq!(v.len(), 1); 287 | opt_out(|| { 288 | let v: Vec = vec![12]; 289 | assert_eq!(v.len(), 1); 290 | }); 291 | }); 292 | let v: Vec = vec![12]; 293 | assert_eq!(v.len(), 1); 294 | let v: Vec = vec![12]; 295 | assert_eq!(v.len(), 1); 296 | }); 297 | assert_eq!(allocations.count_total, 3); 298 | 299 | let info = measure(|| { 300 | opt_out(|| { 301 | let v: Vec = vec![12]; 302 | assert_eq!(v.len(), 1); 303 | }); 304 | }); 305 | assert_eq!(0, info.count_total); 306 | } 307 | 308 | #[test] 309 | fn test_nested_counting() { 310 | let info = measure(|| { 311 | let _a = std::hint::black_box(Box::new(1_u32)); 312 | let info = measure(|| { 313 | let _b = std::hint::black_box(Box::new(1_u32)); 314 | }); 315 | assert_eq!(info.bytes_current, 0); 316 | assert_eq!(info.bytes_total, 4); 317 | assert_eq!(info.bytes_max, 4); 318 | assert_eq!(info.count_current, 0); 319 | assert_eq!(info.count_total, 1); 320 | assert_eq!(info.count_max, 1); 321 | }); 322 | assert_eq!(info.bytes_current, 0); 323 | assert_eq!(info.bytes_total, 8); 324 | assert_eq!(info.bytes_max, 8); 325 | assert_eq!(info.count_current, 0); 326 | assert_eq!(info.count_total, 2); 327 | assert_eq!(info.count_max, 2); 328 | } 329 | -------------------------------------------------------------------------------- /LICENSE-APACHE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "[]" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright [yyyy] [name of copyright owner] 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | --------------------------------------------------------------------------------