1/*
2 * Copyright (c) 2021-2024 Huawei Device Co., Ltd.
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 at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#ifndef ECMASCRIPT_FREE_OBJECT_H
17#define ECMASCRIPT_FREE_OBJECT_H
18
19#include "ecmascript/js_hclass.h"
20#include "ecmascript/mem/barriers.h"
21#include "ecmascript/mem/tagged_object.h"
22
23#define INVALID_OBJECT ((FreeObject *) JSTaggedValue::NULL_POINTER)
24
25namespace panda::ecmascript {
26class BaseHeap;
27
28class FreeObject : public TaggedObject {
29public:
30    static FreeObject *Cast(uintptr_t object)
31    {
32        return reinterpret_cast<FreeObject *>(object);
33    }
34    static FreeObject *FillFreeObject(BaseHeap *heap, uintptr_t address, size_t size);
35
36    inline bool IsEmpty() const
37    {
38        return Available() == 0;
39    }
40
41    inline uintptr_t GetBegin() const
42    {
43        return reinterpret_cast<uintptr_t>(this);
44    }
45
46    inline uintptr_t GetEnd() const
47    {
48        return reinterpret_cast<uintptr_t>(this) + Available();
49    }
50
51    inline void SetAvailable(uint32_t size)
52    {
53        if (size >= SIZE) {
54            Barriers::SetPrimitive<JSTaggedType>(this, SIZE_OFFSET, JSTaggedValue(size).GetRawData());
55        }
56    }
57
58    uint32_t Available() const;
59
60    bool IsFreeObject() const;
61
62    // Before operating any freeobject, need to mark unpoison when is_asan is true.
63    void AsanUnPoisonFreeObject() const;
64
65    // After operating any freeobject, need to marked poison again when is_asan is true
66    void AsanPoisonFreeObject() const;
67
68    static constexpr size_t NEXT_OFFSET = TaggedObjectSize();
69    ACCESSORS_FIXED_SIZE_FIELD(Next, FreeObject *, JSTaggedType, NEXT_OFFSET, SIZE_OFFSET)
70    // TaggedArray visitor may be error while concurrent marking
71    ACCESSORS(Size, SIZE_OFFSET, SIZE)
72};
73
74static_assert((FreeObject::SIZE % static_cast<uint8_t>(MemAlignment::MEM_ALIGN_OBJECT)) == 0);
75}  // namespace panda::ecmascript
76
77#endif  // ECMASCRIPT_MEM_FREE_OBJECT_H
78