mz_timely_util/containers.rs
1// Copyright Materialize, Inc. and contributors. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License in the LICENSE file at the
6// root of this repository, or online at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16//! Reusable containers.
17
18pub mod stack;
19
20pub(crate) use alloc::alloc_aligned_zeroed;
21pub use alloc::{enable_columnar_lgalloc, set_enable_columnar_lgalloc};
22
23mod alloc {
24 use mz_ore::region::Region;
25
26 /// Allocate a region of memory with a capacity of at least `len` that is properly aligned
27 /// and zeroed. The memory in Regions is always aligned to its content type.
28 #[inline]
29 pub(crate) fn alloc_aligned_zeroed<T: bytemuck::AnyBitPattern>(len: usize) -> Region<T> {
30 if enable_columnar_lgalloc() {
31 Region::new_auto_zeroed(len)
32 } else {
33 Region::new_heap_zeroed(len)
34 }
35 }
36
37 thread_local! {
38 static ENABLE_COLUMNAR_LGALLOC: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
39 }
40
41 /// Returns `true` if columnar allocations should come from lgalloc.
42 #[inline]
43 pub fn enable_columnar_lgalloc() -> bool {
44 ENABLE_COLUMNAR_LGALLOC.get()
45 }
46
47 /// Set whether columnar allocations should come from lgalloc. Applies to future allocations.
48 pub fn set_enable_columnar_lgalloc(enabled: bool) {
49 ENABLE_COLUMNAR_LGALLOC.set(enabled);
50 }
51}