1f0bfeaa8Sopenharmony_ci/*
2f0bfeaa8Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3f0bfeaa8Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4f0bfeaa8Sopenharmony_ci * you may not use this file except in compliance with the License.
5f0bfeaa8Sopenharmony_ci * You may obtain a copy of the License at
6f0bfeaa8Sopenharmony_ci *
7f0bfeaa8Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8f0bfeaa8Sopenharmony_ci *
9f0bfeaa8Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10f0bfeaa8Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11f0bfeaa8Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12f0bfeaa8Sopenharmony_ci * See the License for the specific language governing permissions and
13f0bfeaa8Sopenharmony_ci * limitations under the License.
14f0bfeaa8Sopenharmony_ci */
15f0bfeaa8Sopenharmony_ci
16f0bfeaa8Sopenharmony_ci#include "pm_smartptr_util.h"
17f0bfeaa8Sopenharmony_ci#include "purgeable_mem_builder.h"
18f0bfeaa8Sopenharmony_ci
19f0bfeaa8Sopenharmony_cinamespace OHOS {
20f0bfeaa8Sopenharmony_cinamespace PurgeableMem {
21f0bfeaa8Sopenharmony_ci#ifdef LOG_TAG
22f0bfeaa8Sopenharmony_ci#undef LOG_TAG
23f0bfeaa8Sopenharmony_ci#endif
24f0bfeaa8Sopenharmony_ci#define LOG_TAG "PurgeableMem: Builder"
25f0bfeaa8Sopenharmony_ci
26f0bfeaa8Sopenharmony_ciPurgeableMemBuilder::~PurgeableMemBuilder()
27f0bfeaa8Sopenharmony_ci{
28f0bfeaa8Sopenharmony_ci    if (nextBuilder_) {
29f0bfeaa8Sopenharmony_ci        nextBuilder_.reset();
30f0bfeaa8Sopenharmony_ci    }
31f0bfeaa8Sopenharmony_ci}
32f0bfeaa8Sopenharmony_ci
33f0bfeaa8Sopenharmony_civoid PurgeableMemBuilder::AppendBuilder(std::unique_ptr<PurgeableMemBuilder> builder)
34f0bfeaa8Sopenharmony_ci{
35f0bfeaa8Sopenharmony_ci    IF_NULL_LOG_ACTION(builder, "input builder is nullptr", return);
36f0bfeaa8Sopenharmony_ci    if (nextBuilder_) {
37f0bfeaa8Sopenharmony_ci        nextBuilder_->AppendBuilder(std::move(builder));
38f0bfeaa8Sopenharmony_ci    } else {
39f0bfeaa8Sopenharmony_ci        nextBuilder_ = std::move(builder);
40f0bfeaa8Sopenharmony_ci    }
41f0bfeaa8Sopenharmony_ci}
42f0bfeaa8Sopenharmony_ci
43f0bfeaa8Sopenharmony_cibool PurgeableMemBuilder::BuildAll(void *data, size_t size)
44f0bfeaa8Sopenharmony_ci{
45f0bfeaa8Sopenharmony_ci    if (!Build(data, size)) {
46f0bfeaa8Sopenharmony_ci        HILOG_ERROR(LOG_CORE, "%{public}s: build(0x%{public}llx, %{public}zu) fail",
47f0bfeaa8Sopenharmony_ci            __func__, (unsigned long long)data, size);
48f0bfeaa8Sopenharmony_ci        return false;
49f0bfeaa8Sopenharmony_ci    }
50f0bfeaa8Sopenharmony_ci    if (!nextBuilder_) {
51f0bfeaa8Sopenharmony_ci        return true;
52f0bfeaa8Sopenharmony_ci    }
53f0bfeaa8Sopenharmony_ci    return nextBuilder_->BuildAll(data, size);
54f0bfeaa8Sopenharmony_ci}
55f0bfeaa8Sopenharmony_ci} /* namespace PurgeableMem */
56f0bfeaa8Sopenharmony_ci} /* namespace OHOS */
57