14514f5e3Sopenharmony_ci/*
24514f5e3Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
34514f5e3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
44514f5e3Sopenharmony_ci * you may not use this file except in compliance with the License.
54514f5e3Sopenharmony_ci * You may obtain a copy of the License at
64514f5e3Sopenharmony_ci *
74514f5e3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
84514f5e3Sopenharmony_ci *
94514f5e3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
104514f5e3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
114514f5e3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
124514f5e3Sopenharmony_ci * See the License for the specific language governing permissions and
134514f5e3Sopenharmony_ci * limitations under the License.
144514f5e3Sopenharmony_ci */
154514f5e3Sopenharmony_ci
164514f5e3Sopenharmony_ci#ifndef ECMASCRIPT_MEM_CHUNK_ALLOCATOR_H
174514f5e3Sopenharmony_ci#define ECMASCRIPT_MEM_CHUNK_ALLOCATOR_H
184514f5e3Sopenharmony_ci
194514f5e3Sopenharmony_ci#include "ecmascript/mem/chunk.h"
204514f5e3Sopenharmony_ci
214514f5e3Sopenharmony_cinamespace panda::ecmascript {
224514f5e3Sopenharmony_citemplate<typename T>
234514f5e3Sopenharmony_ciclass ChunkAllocator {
244514f5e3Sopenharmony_cipublic:
254514f5e3Sopenharmony_ci    // used for std allocator
264514f5e3Sopenharmony_ci    using value_type = T;
274514f5e3Sopenharmony_ci    using pointer = T *;
284514f5e3Sopenharmony_ci    using reference = T &;
294514f5e3Sopenharmony_ci    using const_pointer = const T *;
304514f5e3Sopenharmony_ci    using const_reference = const T &;
314514f5e3Sopenharmony_ci    using size_type = size_t;
324514f5e3Sopenharmony_ci    using difference_type = ptrdiff_t;
334514f5e3Sopenharmony_ci
344514f5e3Sopenharmony_ci    template<typename U>
354514f5e3Sopenharmony_ci    struct Rebind {
364514f5e3Sopenharmony_ci        using other = ChunkAllocator<U>;
374514f5e3Sopenharmony_ci    };
384514f5e3Sopenharmony_ci
394514f5e3Sopenharmony_ci    template<typename U>
404514f5e3Sopenharmony_ci    using rebind = Rebind<U>;
414514f5e3Sopenharmony_ci
424514f5e3Sopenharmony_ci    explicit ChunkAllocator(Chunk *chunk) : chunk_(chunk) {}
434514f5e3Sopenharmony_ci
444514f5e3Sopenharmony_ci    template<typename U>
454514f5e3Sopenharmony_ci    explicit ChunkAllocator(const ChunkAllocator<U> &other) : chunk_(other.chunk_)
464514f5e3Sopenharmony_ci    {
474514f5e3Sopenharmony_ci    }
484514f5e3Sopenharmony_ci    template<typename U>
494514f5e3Sopenharmony_ci    friend class ChunkAllocator;
504514f5e3Sopenharmony_ci
514514f5e3Sopenharmony_ci    ChunkAllocator(const ChunkAllocator &) = default;
524514f5e3Sopenharmony_ci    ChunkAllocator &operator=(const ChunkAllocator &) = default;
534514f5e3Sopenharmony_ci    ChunkAllocator(ChunkAllocator &&other) noexcept
544514f5e3Sopenharmony_ci    {
554514f5e3Sopenharmony_ci        chunk_ = other.chunk_;
564514f5e3Sopenharmony_ci        other.chunk_ = nullptr;
574514f5e3Sopenharmony_ci    }
584514f5e3Sopenharmony_ci    ChunkAllocator &operator=(ChunkAllocator &&other) noexcept
594514f5e3Sopenharmony_ci    {
604514f5e3Sopenharmony_ci        chunk_ = other.chunk_;
614514f5e3Sopenharmony_ci        other.chunk_ = nullptr;
624514f5e3Sopenharmony_ci        return *this;
634514f5e3Sopenharmony_ci    }
644514f5e3Sopenharmony_ci    ~ChunkAllocator() = default;
654514f5e3Sopenharmony_ci
664514f5e3Sopenharmony_ci    // NOLINTNEXTLINE(readability-identifier-naming)
674514f5e3Sopenharmony_ci    size_type max_size() const
684514f5e3Sopenharmony_ci    {
694514f5e3Sopenharmony_ci        return static_cast<size_type>(-1) / sizeof(T);
704514f5e3Sopenharmony_ci    }
714514f5e3Sopenharmony_ci
724514f5e3Sopenharmony_ci    // NOLINTNEXTLINE(readability-identifier-naming)
734514f5e3Sopenharmony_ci    pointer address(reference x) const
744514f5e3Sopenharmony_ci    {
754514f5e3Sopenharmony_ci        return &x;
764514f5e3Sopenharmony_ci    }
774514f5e3Sopenharmony_ci    // NOLINTNEXTLINE(readability-identifier-naming)
784514f5e3Sopenharmony_ci    const_pointer address(const_reference x) const
794514f5e3Sopenharmony_ci    {
804514f5e3Sopenharmony_ci        return &x;
814514f5e3Sopenharmony_ci    }
824514f5e3Sopenharmony_ci
834514f5e3Sopenharmony_ci    // NOLINTNEXTLINE(readability-identifier-naming)
844514f5e3Sopenharmony_ci    pointer allocate(size_type n, [[maybe_unused]] const void *ptr = nullptr)
854514f5e3Sopenharmony_ci    {
864514f5e3Sopenharmony_ci        ASSERT(n <= max_size());
874514f5e3Sopenharmony_ci        return chunk_->NewArray<T>(n);
884514f5e3Sopenharmony_ci    }
894514f5e3Sopenharmony_ci
904514f5e3Sopenharmony_ci    // NOLINTNEXTLINE(readability-identifier-naming)
914514f5e3Sopenharmony_ci    void deallocate([[maybe_unused]] pointer p, [[maybe_unused]] size_type n) {}
924514f5e3Sopenharmony_ci
934514f5e3Sopenharmony_ci    template<typename U, typename... Args>
944514f5e3Sopenharmony_ci    void construct(U *p, Args &&... args)  // NOLINT(readability-identifier-naming)
954514f5e3Sopenharmony_ci    {
964514f5e3Sopenharmony_ci        ::new (static_cast<void *>(p)) U(std::forward<Args>(args)...);
974514f5e3Sopenharmony_ci    }
984514f5e3Sopenharmony_ci    template<typename U>
994514f5e3Sopenharmony_ci    void destroy(U *p)  // NOLINT(readability-identifier-naming)
1004514f5e3Sopenharmony_ci    {
1014514f5e3Sopenharmony_ci        if (p == nullptr) {
1024514f5e3Sopenharmony_ci            return;
1034514f5e3Sopenharmony_ci        }
1044514f5e3Sopenharmony_ci        p->~U();
1054514f5e3Sopenharmony_ci    }
1064514f5e3Sopenharmony_ci
1074514f5e3Sopenharmony_ci    bool operator==(ChunkAllocator const &other) const
1084514f5e3Sopenharmony_ci    {
1094514f5e3Sopenharmony_ci        return chunk_ == other.chunk_;
1104514f5e3Sopenharmony_ci    }
1114514f5e3Sopenharmony_ci    bool operator!=(ChunkAllocator const &other) const
1124514f5e3Sopenharmony_ci    {
1134514f5e3Sopenharmony_ci        return chunk_ != other.chunk_;
1144514f5e3Sopenharmony_ci    }
1154514f5e3Sopenharmony_ci
1164514f5e3Sopenharmony_ci    [[nodiscard]] void *Alloc(size_t size)
1174514f5e3Sopenharmony_ci    {
1184514f5e3Sopenharmony_ci        return chunk_->NewArray<uint8_t>(size);
1194514f5e3Sopenharmony_ci    }
1204514f5e3Sopenharmony_ci
1214514f5e3Sopenharmony_ci    [[nodiscard]] T *AllocArray(size_t size)
1224514f5e3Sopenharmony_ci    {
1234514f5e3Sopenharmony_ci        return chunk_->NewArray<T>(size);
1244514f5e3Sopenharmony_ci    }
1254514f5e3Sopenharmony_ci
1264514f5e3Sopenharmony_ci    void Delete(T *ptr)
1274514f5e3Sopenharmony_ci    {
1284514f5e3Sopenharmony_ci        if (ptr == nullptr) {
1294514f5e3Sopenharmony_ci            LOG_ECMA_MEM(FATAL) << "free nullptr";
1304514f5e3Sopenharmony_ci            UNREACHABLE();
1314514f5e3Sopenharmony_ci        }
1324514f5e3Sopenharmony_ci        // NOLINTNEXTLINE(readability-braces-around-statements,bugprone-suspicious-semicolon)
1334514f5e3Sopenharmony_ci        if constexpr (std::is_class_v<T>) {
1344514f5e3Sopenharmony_ci            ptr->~T();
1354514f5e3Sopenharmony_ci        }
1364514f5e3Sopenharmony_ci        Free(ptr);
1374514f5e3Sopenharmony_ci    }
1384514f5e3Sopenharmony_ci
1394514f5e3Sopenharmony_ci    void Free([[maybe_unused]] void *mem) {}
1404514f5e3Sopenharmony_ci
1414514f5e3Sopenharmony_ci    Chunk *chunk()
1424514f5e3Sopenharmony_ci    {
1434514f5e3Sopenharmony_ci        return chunk_;
1444514f5e3Sopenharmony_ci    }
1454514f5e3Sopenharmony_ci
1464514f5e3Sopenharmony_ciprivate:
1474514f5e3Sopenharmony_ci    Chunk *chunk_;
1484514f5e3Sopenharmony_ci};
1494514f5e3Sopenharmony_ci}  // namespace panda::ecmascript
1504514f5e3Sopenharmony_ci
1514514f5e3Sopenharmony_ci#endif  // ECMASCRIPT_MEM_CHUNK_ALLOCATOR_H
152