FBB::SharedMemory(3bobcat)
β¨ NAME β¨
FBB::SharedMemory β Shared Memory memory structure. And yes bestie, this is the manual for FBB::SharedMemory(3bobcat), so buckle up. π
πΈ SYNOPSIS πΈ
#include <bobcat/sharedmemory>
Linking option: -lbobcat
π¦ DESCRIPTION π¦
The class FBB::SharedMemory implements a usable interface to a shared memory segment made available by FBB::SharedSegment and monitored by FBB::SharedPos. It is the main building block for FBB::SharedBuf, defining the deviceβ to which FBB::SharedBuf interfaces. All shared memory related I/O should be performed by FBB::SharedMemory objects, which are true objects, not themselves residing in shared memory.
An FBB::SharedMemory object defines, connects to and manages access to shared memory, encapsulating all raw shared memory operations. In addition to the class FBB::SharedMemory the header file bobcat/sharedmemory also defines a struct SharedEnum__ defining enum SizeUnit within the namespace FBB.
The requested amount of shared memory is always a lower bound to the maximum amount of shared memory that eventually may become available. When defining a SharedMemory object not all of its potentially available shared memory is immediately allocated. Shared memory will be allocated by the SharedMemory object once needed (up to a calculated maximum).
As a fictitious example: assume 100 kB of memory is requested. The SharedMemory object then maintains a table of, e.g., 10 entries, each entry controlling access to a shared memory block of 10 kB. These 10 kB blocks arenβt immediately allocated, but become available once the program reads from or writes to addresses located in these data blocks. Newly allocated data blocks are initialized to 0-bytes.
Caveat, bestie, listen up: when constructing a shared memory segment make sure the segmentβs ID is stored at a retrievable location. This allows other processes to access the shared segment. The shared segment ID is also required to delete a shared memory segment. If the shared segment ID is lost, the memory occupied by the shared memory segment remains inaccessible (although they can be retrieved and removed by additional means, likeipcs(1)andipcrm(1)). The memberidreturns the ID of the shared memory currently monitored by anFBB::SharedMemoryobject. π
π NAMESPACE π
FBB
All constructors, members, operators and manipulators, mentioned in this man-page, are defined in the namespace FBB.
π INHERITS FROM π
FBB::SharedEnum__
The struct SharedEnum__ is a wrapper struct around enum SizeUnit, which is available through inheritance in several FBB::Shared* classes, and offers symbolic constants defining standard memory sizes. The enum SizeUnit defines the following symbolic constants:
kB, representing 1024 (2**10) bytes of memory;MB, representing 1048576 (2**20) bytes of memory;GB, representing 1073741824 (2**30) bytes of memory
π CONSTRUCTORS, DESTRUCTOR π
SharedMemory()β The default constructor defines an empty stub, which cannot immediately be used. As theSharedMemoryclass supports move assignment, empty stubs can easily be (re)configured at any time after their construction. Itβs giving βblank canvas, but make it move-assignable.β β¨SharedMemory(size_t maxSize, SizeUnit sizeUnit, size_t access = 0600)β This constructor creates a shared memory segment having a capacity of at leastmaxSize * sizeUnitbytes. The shared memoryβs access rights are defined by theaccessparameter, using the well-known (chmod(1)) octal values to define access rights for the owner, the group and others. If construction succeeds the shared memory is ready for use. If construction fails, anFBB::Exceptionis thrown. No cap, those octal perms are doing the most. πSharedMemory(int id)β This constructor connects to a shared memory segment having IDid. If construction succeeds the shared memory is ready for use. If construction fails (e.g., no shared memory segment having IDidexists), anFBB::Exceptionis thrown. Like sliding into someone elseβs shared-memory DMs, but with anint. π~SharedMemory()β The destructor detaches any attached shared memory segments from theFBB::SharedMemoryobject. If the shared memory segment is currently locked by theFBB::SharedMemoryobject, the lock is removed. Cleanup era. π§
Copy and move constructors (and assignment operators) are not available.
β OVERLOADED OPERATORS β
std::ostream &operator<<(std::ostream &out, SharedMemory const &sharedMemory)β The overloaded insertion operator inserts information about theSharedMemoryobject into the providedostreamobject. The IDs of the shared segments, their sizes, the maximum number of shared memory segments, the number of bytes that can be read from the shared memory, and its actual storage capacity, etc., are displayed. Basically a whole vibe report for your shared memory. πSharedMemory &operator=(SharedMemory &&rhs)β The overloaded move assignment operator is available. It is used to (re)define the shared memory segment anFBB::SharedMemoryobject is interfacing with. Reinventing yourself, but for shared memory segments. π
β¨ MEMBER FUNCTIONS β¨
okay bestie, here's the whole roster of member functions, and yes we're going through every single one because you deserve the full tea π
size_t blockOffset() constβ returns the offset within the shared segment data block that matches whatoffsetgives you back.0is returned if theSharedMemoryobject hasn't been connected to a shared memory block yet (or if the offset just happens to be sitting at the block's offset0). no cap.void clear()β first, the shared memory gets locked. then all shared data segments are deleted, and the shared memory's own data gets reset to show it's completely empty. after that, the shared memory segment is unlocked again. whenclearreturns, theFBB::SharedMemoryobject is effectively re-initialized, withoffsetandnReadableboth returning0. slay. anFBB::Exceptionis thrown if theSharedMemoryobject hasn't been connected to a shared memory block yet.size_t dataSegmentSize() constβ returns the size (in bytes) of the shared memory data block.0is returned if theSharedMemoryobject hasn't been connected to a shared memory block yet.int get()β first theFBB::SharedMemoryobject callslockto lock the shared memory segment. next, the character atoffsetis retrieved andoffsetgets incremented. thenunlockis called, and the retrieved character is returned. ifoffsetis at least equal tonReadable,EOFis immediately returned. anFBB::Exceptionis thrown if theSharedMemoryobject hasn't been connected to a shared memory block yet.int id() constβ returns the ID of the shared memory segment. afterkill,idreturns-1, which means the shared segment can't be used anymore (btwoperator=can be used to re-initialize theFBB::SharedMemoryobject if you need to).SharedType *install(std::streamsize *offset, Params &&...params)β this one was implemented as a member template, usingtypename SharedTypeandtypename ...Params, which allows perfect forwarding of arguments toSharedType's constructor. it installs aSharedTypeobject atSharedMemory's first available offset: either at the currentoffset, or (ifSharedType's size is too big to fit in the current data block atoffset) at the first byte of the nextSharedSegmentshared data block.
the actual offset where the SharedType object is installed is returned in *offset, unless a nullptr is passed as install's first argument. a pointer to the installed SharedType is returned, with shmem.offset pointing just beyond SharedType's last byte.
the SharedType object is installed using placement new. for example, here's how you'd install a SharedMutex(3bobcat) object at the first possible location of SharedMemory shmem:
std::streamsize offset;
FBB::SharedMutex *mutexPtr = shmem.install<FBB::SharedMutex>(&offset);
if the installed object needs to be destroyed, its destructor must be explicitly called. for example, to destroy the Mutex pointed at by mutexPtr, use mutexPtr->~SharedMutex(). an FBB::Exception is thrown if shmem couldn't install the object in its shared memory data blocks.
void kill()β without locking the shared memory, all shared memory controlled by theSharedMemoryobject is deleted. theSharedMemoryobject is unusable after returning fromkill, and itsidmember returns-1. nothing happens if this member is called when theSharedMemoryobject hasn't been connected to a shared memory block yet.std::streamsize maxOffset() constβ returns the maximum possible offset that can be used with the shared memory segment. the membersoffsetandnReadablenever exceed the value returned bymaxOffset.0is returned if theSharedMemoryobject hasn't been connected to a shared memory block yet.std::streamsize nReadable() constβ returns the number of characters (bytes) that can be read from the beginning of the shared memory.0is returned if theSharedMemoryobject hasn't been connected to a shared memory block yet.std::streamsize offset() constβ returns the offset within the shared memory segment (so, relative to the segment'sios::begposition).0is returned if theSharedMemoryobject hasn't been connected to a shared memory block yet (or if the offset just happens to be at the shared memory's offset0).char *ptr()β returns0ifoffset() == maxOffset(). otherwise it returns a pointer to the character at indexoffsetwithin the shared memory segment. anFBB::Exceptionis thrown if theSharedMemoryobject hasn't been connected to a shared memory block yet.int put(int ch)β after locking the appropriate shared data segment,chis written at positionoffset, incrementingoffsetafterwards. ifch == EOF,EOFis immediately returned. anFBB::Exceptionis thrown if theSharedMemoryobject hasn't been connected to a shared memory block yet.int read(Type *value)β this member was implemented as a member template. it calls the next member, castingType *tochar *, and usingsizeof(Type)as its second argument. the number of bytes actually read is returned. this member returns-1if initiallyoffsetwas at least equal tonReadable. anFBB::Exceptionis thrown if theSharedMemoryobject hasn't been connected to a shared memory block yet.int read(char *data, std::streamsize len)β while locking the appropriate shared data segment(s), at mostlenbytes are read from the shared memory, starting atoffset. the bytes read from shared memory are stored atdata. the number of bytes actually read is returned. this member returns-1if initiallyoffsetwas at least equal tonReadable. anFBB::Exceptionis thrown if theSharedMemoryobject hasn't been connected to a shared memory block yet.int read(std::ios::off_type offset, Type *value, std::ios::seekdir origin = std::ios::beg)β this member was implemented as a member template. after changing theSharedMemory's offset tooffset(relative toorigin), it calls the firstreadmember, passing itvalue. the number of bytes actually read is returned. this member returns-1if initiallyoffsetwas at least equal tonReadable. anFBB::Exceptionis thrown if theSharedMemoryobject hasn't been connected to a shared memory block yet.void remove()β the shared memory is locked, after which all shared memory controlled by theFBB::SharedMemoryobject is deleted. theFBB::SharedMemoryobject is unusable after returning fromremove. anFBB::Exceptionis thrown if theSharedMemoryobject hasn't been connected to a shared memory block yet.std::ios::pos_type seek(std::ios::off_type offset, std::ios::seekdir origin = std::ios::beg)β moves the offset position relative to way. the value-1is returned when seeking before offset0or beyondmaxOffset, otherwise the offset relative to the begin location of the shared memory (i.e.,offsetfororigin == ios::beg) is returned.0is returned if theSharedMemoryobject hasn't been connected to a shared memory block yet (or if the offset just happens to be at the shared memory's offset0).std::streamsize showmanyc() constβ returns the number of characters that can be read from the current shared segment data block. this member interrogates the number of readable characters in the shared memory segment. heads up: this number may change while this member is being executed. in order to receive a stable return value, calling functions should have obtained a lock on the shared memory segment before calling this member.0is returned if theSharedMemoryobject hasn't been connected to a shared memory block yet (or if no characters can currently be read).void swap(SharedMemory &other)β swaps the current andotherFBB::SharedMemoryobjects. iconic.bool truncate(std::streamsize offset)β ifoffsetis not exceeding the value returned bynReadable,nReadableis changed tooffsetandtrueis returned. otherwisefalseis returned, and the value returned bynReadableis not changed. anFBB::Exceptionis thrown if theSharedMemoryobject hasn't been connected to a shared memory block yet.int write(Type const *value)β this member was implemented as a member template. it calls the next member, castingType const *tochar const *, and usingsizeof(Type)as its second argument. the number of bytes actually written is returned. this member returns-1if initiallyoffsetwas at least equal tomaxOffset. anFBB::Exceptionis thrown if theSharedMemoryobject hasn't been connected to a shared memory block yet.int write(char const *data, std::streamsize len)β theFBB::SharedMemoryobject callslockto lock the shared memory, and writes at mostlenbytes into the shared memory, starting atoffset. next,unlockis called. the number of bytes actually written is returned. the member function returns-1if initiallyoffsetis equal tomaxOffset. anFBB::Exceptionis thrown if theSharedMemoryobject hasn't been connected to a shared memory block yet.int write(std::ios::off_type offset, Type const *value, std::ios::seekdir origin = std::ios::beg)β this member was implemented as a member template. after changing theSharedMemory's offset tooffset(relative toorigin), it calls the firstwritemember, passing itvalue. the number of bytes actually written is returned. this member returns-1if initiallyoffsetwas at least equal tomaxOffset. anFBB::Exceptionis thrown if theSharedMemoryobject hasn't been connected to a shared memory block yet.
πΈ EXAMPLE πΈ
see the sharedstream(3bobcat) man page. that's the vibe. π
π FILES π
bobcat/sharedmemoryβ defines the class interface
π SEE ALSO
ok bestie, when you're done here and you want MORE reading (and honestly, same), go check these out π:
bobcat(7)chmod(1)ipcs(1)ipcrm(1)isharedstream(3bobcat)osharedstream(3bobcat)sharedblock(3bobcat)sharedcondition(3bobcat)sharedmutex(3bobcat)sharedpos(3bobcat)sharedreadme(7bobcat)sharedsegment(3bobcat)sharedstream(3bobcat)sharedbuf(3bobcat)
it's giving "shared everything" fr fr πΈ
π BUGS
None Reported.
literally flawless behavior?? no cap, nothing reported. the vibe check has been passed π
π BOBCAT PROJECT FILES
- https://fbb-git.gitlab.io/bobcat/ β the gitlab project page, aka where the whole thing lives π
Debian Bobcat project files, for the Debian girlies π:
libbobcat6β the debian package containing the shared library, changelog and copyright notelibbobcat-devβ the debian package containing the static library, headers, manual pages, and developer info
π BOBCAT
Bobcat is an acronym of `Brokkenβs Own Base Classes And Templates'.
likeβ¦ the naming? iconic. ate and left no crumbs β¨
Β©οΈ COPYRIGHT
This is free software, distributed under the terms of the GNU General Public License (GPL).
free as in freedom bestie, go wild π¦
π AUTHOR
Frank B. Brokken (f.b.brokken@rug.nl).
the main character who wrote all of this π
and that's a wrap, bestie!! this whole page came from the libbobcat-dev_6.07.01 source, dated 2005-2025 β a whole two decades of shared memory excellence, we love to see it π«Ά now go make some segments and slay π
π
π¦ where this page comes from
- debian trixie: libbobcat-dev 6.07.01-2 Β· GPL-3.0-or-later WITH OpenSSL exception
- ubuntu questing: libbobcat-dev 6.07.01-2
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms