/* SPDX-License-Identifier: Apache-2.0 */ /* * Copyright (C) 2014-2018 Intel Corporation * * This implementation is highly derived from ChromeOS: * platform2/camera/hal/intel/ipu3/common/SharedItemPool.cpp */ #include #include #include "shared_item_pool.h" namespace libcamera { LOG_DEFINE_CATEGORY(SharedItemPool) template SharedItemPool::SharedItemPool(const char *name) : allocated_(nullptr), capacity_(0), deleter_(this), poolName_(name), resetter_(nullptr) { } template SharedItemPool::~SharedItemPool() { deInit(); } template int SharedItemPool::init(int32_t capacity, void (*resetter)(ItemType *)) { if (capacity_ != 0) { LOG(SharedItemPool, Error) << "Pool initialized already"; return -ENOSYS; } std::lock_guard l(mutex_); resetter_ = resetter; capacity_ = capacity; available_.reserve(capacity); allocated_ = new ItemType[capacity]; for (int32_t i = 0; i < capacity; i++) available_.push_back(&allocated_[i]); LOG(SharedItemPool, Debug) << "Shared pool " << poolName_ << "init with " << capacity << " items"; return 0; } template bool SharedItemPool::isFull() { std::lock_guard l(mutex_); bool ret = (available_.size() == capacity_); return ret; } template int SharedItemPool::deInit() { std::lock_guard l(mutex_); if (capacity_ == 0) { LOG(SharedItemPool, Debug) << "Shared pool " << poolName_ << " isn't initialized or already de-initialized"; return 0; } if (available_.size() != capacity_) { LOG(SharedItemPool, Error) << "Not all items are returned " << "when destroying pool " << poolName_ << "(" << available_.size() << "/" << capacity_ << ")"; } delete[] allocated_; allocated_ = nullptr; available_.clear(); capacity_ = 0; LOG(SharedItemPool, Debug) << "Shared pool " << poolName_ << " deinit done"; return 0; } template int SharedItemPool::acquireItem(std::shared_ptr &item) { item.reset(); std::lock_guard l(mutex_); if (available_.empty()) { LOG(SharedItemPool, Error) << "Shared pool " << poolName_ << "is empty"; return -ENOSYS; } std::shared_ptr sh(available_[0], deleter_); available_.erase(available_.begin()); item = sh; LOG(SharedItemPool, Debug) << "Shared pool " << poolName_ << "acquire items " << sh.get(); return 0; } template size_t SharedItemPool::availableItems() { std::lock_guard l(mutex_); size_t ret = available_.size(); return ret; } template int SharedItemPool::_releaseItem(ItemType *item) { std::lock_guard l(mutex_); if (resetter_) resetter_(item); LOG(SharedItemPool, Debug) << "Shared pool " << poolName_ << "returning item " << item; available_.push_back(item); return 0; } template class SharedItemPool; template class SharedItemPool; } /* namespace libcamera */