Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-15 08:55:04

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