• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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_MEM_FREE_OBJECT_SET_H
17 #define ECMASCRIPT_MEM_FREE_OBJECT_SET_H
18 
19 #include <cstdint>
20 
21 #include "libpandabase/macros.h"
22 
23 namespace panda::ecmascript {
24 using SetType = int32_t;
25 
26 class FreeObject;
27 
28 class FreeObjectSet {
29 public:
FreeObjectSet(SetType type)30     FreeObjectSet(SetType type) : setType_(type)
31     {
32         Rebuild();
33     }
34     ~FreeObjectSet() = default;
35 
Empty()36     inline bool Empty() const
37     {
38         return available_ == 0;
39     }
40 
Available()41     inline size_t Available() const
42     {
43         return available_;
44     }
45 
46     void Free(uintptr_t begin, size_t size);
47 
48     void Rebuild();
49 
50     FreeObject *LookupSmallFreeObject(size_t size);
51     FreeObject *LookupLargeFreeObject(size_t size);
52     FreeObject *ObtainSmallFreeObject(size_t size);
53     FreeObject *ObtainLargeFreeObject(size_t size);
54 
55     NO_COPY_SEMANTIC(FreeObjectSet);
56     NO_MOVE_SEMANTIC(FreeObjectSet);
57 
58     static constexpr SetType INVALID_SET_TYPE = -1;
59 
60 private:
61     FreeObjectSet *next_ = nullptr;
62     FreeObjectSet *prev_ = nullptr;
63     SetType setType_ = INVALID_SET_TYPE;
64     size_t available_ = 0;
65     bool isAdded_ = false;
66     FreeObject *freeObject_ = nullptr;
67 
68     friend class FreeObjectList;
69 };
70 }  // namespace panda::ecmascript
71 #endif  // ECMASCRIPT_MEM_FREE_OBJECT_SET_H
72