Loading...
Searching...
No Matches
shared_mem_object.h
/* SPDX-License-Identifier: LGPL-2.1-or-later */
/*
* Copyright (C) 2023 Raspberry Pi Ltd
* Copyright (C) 2024 Andrei Konovalov
* Copyright (C) 2024 Dennis Bonke
*
* Helpers for shared memory allocations
*/
#pragma once
#include <stdint.h>
#include <string>
#include <sys/mman.h>
#include <type_traits>
#include <utility>
#include <libcamera/base/class.h>
#include <libcamera/base/shared_fd.h>
#include <libcamera/base/span.h>
namespace libcamera {
class SharedMem
{
public:
SharedMem();
SharedMem(const std::string &name, std::size_t size);
SharedMem(SharedMem &&rhs);
virtual ~SharedMem();
SharedMem &operator=(SharedMem &&rhs);
const SharedFD &fd() const
{
return fd_;
}
Span<uint8_t> mem() const
{
return mem_;
}
explicit operator bool() const
{
return !mem_.empty();
}
private:
LIBCAMERA_DISABLE_COPY(SharedMem)
SharedFD fd_;
Span<uint8_t> mem_;
};
template<class T, typename = std::enable_if_t<std::is_standard_layout<T>::value>>
class SharedMemObject : public SharedMem
{
public:
static constexpr std::size_t kSize = sizeof(T);
SharedMemObject()
: SharedMem(), obj_(nullptr)
{
}
template<class... Args>
SharedMemObject(const std::string &name, Args &&...args)
: SharedMem(name, kSize), obj_(nullptr)
{
if (mem().empty())
return;
obj_ = new (mem().data()) T(std::forward<Args>(args)...);
}
SharedMemObject(SharedMemObject<T> &&rhs)
: SharedMem(std::move(rhs))
{
this->obj_ = rhs.obj_;
rhs.obj_ = nullptr;
}
~SharedMemObject()
{
if (obj_)
obj_->~T();
}
SharedMemObject<T> &operator=(SharedMemObject<T> &&rhs)
{
SharedMem::operator=(std::move(rhs));
this->obj_ = rhs.obj_;
rhs.obj_ = nullptr;
return *this;
}
T *operator->()
{
return obj_;
}
const T *operator->() const
{
return obj_;
}
T &operator*()
{
return *obj_;
}
const T &operator*() const
{
return *obj_;
}
private:
LIBCAMERA_DISABLE_COPY(SharedMemObject)
T *obj_;
};
} /* namespace libcamera */
class.h
Utilities to help constructing class interfaces.
LIBCAMERA_DISABLE_COPY
#define LIBCAMERA_DISABLE_COPY(klass)
Disable copy construction and assignment of the klass.
Definition class.h:27
libcamera::SharedFD
RAII-style wrapper for file descriptors.
Definition shared_fd.h:17
libcamera::SharedMemObject
Helper class to allocate an object in shareable memory.
Definition shared_mem_object.h:60
libcamera::SharedMemObject::kSize
static constexpr std::size_t kSize
The size of the object stored in shared memory.
Definition shared_mem_object.h:62
libcamera::SharedMemObject::operator->
const T * operator->() const
Dereference the stored object.
Definition shared_mem_object.h:105
libcamera::SharedMemObject::SharedMemObject
SharedMemObject(const std::string &name, Args &&...args)
Construct a SharedMemObject.
Definition shared_mem_object.h:70
libcamera::SharedMemObject::operator*
const T & operator*() const
Dereference the stored object.
Definition shared_mem_object.h:115
libcamera::SharedMemObject::operator->
T * operator->()
Dereference the stored object.
Definition shared_mem_object.h:100
libcamera::SharedMemObject::SharedMemObject
SharedMemObject(SharedMemObject< T > &&rhs)
Move constructor for SharedMemObject.
Definition shared_mem_object.h:79
libcamera::SharedMemObject::operator*
T & operator*()
Dereference the stored object.
Definition shared_mem_object.h:110
libcamera::SharedMemObject::operator=
SharedMemObject< T > & operator=(SharedMemObject< T > &&rhs)
Move assignment operator for SharedMemObject.
Definition shared_mem_object.h:92
libcamera::SharedMemObject::~SharedMemObject
~SharedMemObject()
Destroy the SharedMemObject instance.
Definition shared_mem_object.h:86
libcamera::SharedMem
Helper class to allocate and manage memory shareable between processes.
Definition shared_mem_object.h:24
libcamera::SharedMem::~SharedMem
virtual ~SharedMem()
Destroy the SharedMem instance.
Definition shared_mem_object.cpp:100
libcamera::SharedMem::mem
Span< uint8_t > mem() const
Retrieve the underlying shared memory.
Definition shared_mem_object.h:40
libcamera::SharedMem::operator=
SharedMem & operator=(SharedMem &&rhs)
Move assignment operator for SharedMem.
Definition shared_mem_object.cpp:110
libcamera::SharedMem::fd
const SharedFD & fd() const
Retrieve the file descriptor for the underlying shared memory.
Definition shared_mem_object.h:35
libcamera
Top-level libcamera namespace.
Definition backtrace.h:17
shared_fd.h
File descriptor wrapper.