Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:54:47

0001 //----------------------------------*-C++-*----------------------------------//
0002 // Copyright 2020-2024 UT-Battelle, LLC, and other Celeritas developers.
0003 // See the top-level COPYRIGHT file for details.
0004 // SPDX-License-Identifier: (Apache-2.0 OR MIT)
0005 //---------------------------------------------------------------------------//
0006 //! \file corecel/data/StackAllocatorData.hh
0007 //---------------------------------------------------------------------------//
0008 #pragma once
0009 
0010 #include "corecel/Macros.hh"
0011 #include "corecel/Types.hh"
0012 
0013 #include "Collection.hh"
0014 #include "CollectionAlgorithms.hh"
0015 #include "CollectionBuilder.hh"
0016 
0017 namespace celeritas
0018 {
0019 //---------------------------------------------------------------------------//
0020 /*!
0021  * Storage for a stack and its dynamic size.
0022  */
0023 template<class T, Ownership W, MemSpace M>
0024 struct StackAllocatorData
0025 {
0026     celeritas::Collection<T, W, M> storage;  //!< Allocated capacity
0027     celeritas::Collection<size_type, W, M> size;  //!< Stored size
0028 
0029     //! Whether the data is assigned
0030     explicit CELER_FUNCTION operator bool() const
0031     {
0032         return !storage.empty() && !size.empty();
0033     }
0034 
0035     //! Total capacity of stack
0036     CELER_FUNCTION size_type capacity() const { return storage.size(); }
0037 
0038     //! Assign from another stack
0039     template<Ownership W2, MemSpace M2>
0040     StackAllocatorData& operator=(StackAllocatorData<T, W2, M2>& other)
0041     {
0042         CELER_EXPECT(other);
0043         storage = other.storage;
0044         size = other.size;
0045         return *this;
0046     }
0047 };
0048 
0049 //---------------------------------------------------------------------------//
0050 /*!
0051  * Resize a stack allocator in host code.
0052  */
0053 template<class T, MemSpace M>
0054 inline void
0055 resize(StackAllocatorData<T, Ownership::value, M>* data, size_type capacity)
0056 {
0057     CELER_EXPECT(capacity > 0);
0058     resize(&data->storage, capacity);
0059     resize(&data->size, 1);
0060     celeritas::fill(size_type(0), &data->size);
0061 }
0062 
0063 //---------------------------------------------------------------------------//
0064 }  // namespace celeritas