FBB::SharedSegment(3bobcat)
π FBB::SharedSegment π
okay bestie, welcome to the <bobcat/sharedsegment> era β this is the manual page for the shared memory data structure, straight out of section 3 of the Bobcat manuals β¨
π NAME π
FBB::SharedSegment β the Shared Memory data structure, no cap π
πΈ SYNOPSIS πΈ
#include <bobcat/sharedsegment>
Linking option: -lbobcat
π¦ DESCRIPTION π¦
okay so the class FBB::SharedSegment implements the shared memory data structure that Bobcat's shared memory classes use. Bobcat's SharedMemory class accesses or defines a shared memory segment, and it controls all its read and write operations.
here's the tea β β the amount of shared memory you request is always a lower bound to the maximum amount of shared memory that might eventually become available. So when you define a SharedSegment object, not all of its potentially available shared memory gets allocated right away. The SharedSegment object allocates shared memory once it's needed (up to a calculated maximum). it's giving lazy loading but make it memory π«Ά
as a fictitious example: say 100 kB of memory is requested, then the SharedSegment object maintains a table of, e.g., 10 entries, each one controlling access to a shared memory block of 10 kB. Those 10 kB blocks aren't immediately allocated β they become available once the program reads from or writes to addresses located in these data segments. iconic behaviour honestly.
so the class SharedSegment defines a gateway, controlling access to and allocating the required shared memory data segments. That table we mentioned consists of nBlocks SharedBlock (sharedblock(3bobcat)) values, offering mutexes and IDs of shared data segments. The mutexes control which process has access to a particular block of shared data memory, and the IDs are either -1 β meaning their shared memory data segment has not yet been allocated β or they contain the IDs of defined shared memory data segments. yesss the -1 is the "not yet" flag, remember that π
The class SharedSegment's sole responsibility is to offer the framework as described. When it's used by an FBB::SharedMemory object, different processes may gain access to different parts of the shared memory data without interfering with each other's read and write actions. teamwork makes the dream work fr fr π
β¨ NAMESPACE β¨
FBB
All constructors, members, operators and manipulators mentioned in this man-page are defined in the namespace FBB. periodt π
π§ INHERITS FROM π§
-
(yeah bestie, nothing. it inherits from nobody, it's self-made π)
π CONSTRUCTORS π
No publicly accessible constructors have been defined for SharedSegment. Instead there's a static member function create (peep below) that returns a pointer to a shared memory segment, in which a SharedSegment has been defined. so you don't just new this thing, you create it β¨
π₯ OVERLOADED OPERATORS π₯
std::ostream &operator<<(std::ostream &out, SharedSegment const &sharedData)β this overloaded insertion operator inserts basic statistics of the shared memory data into theostreamobject. What stats, you ask? Info about the IDs of the shared segments, their sizes, the maximum number of shared data segments, and the number of bytes that can be read from the shared memory. it's basically the receipts πFBB::SharedBlock &operator[](size_t idx)β returns table elementidxof the table ofFBB::SharedBlockblock IDs. > β οΈ the behavior of the program is undefined ifidxis or exceedsnBlocks(). so like, don't do that bestie, please π
Copy and move constructors (and assignment operators) are not available. not a thing. never was. π
π MEMBER FUNCTIONS π
size_t access() constβ returns the access rights of the shared memory segment as a number, which is usually interpreted as an octal value, using the well-known (chmod(1)) way to define the access rights for owner, group and others. octal rights, you know the vibe πvoid clear()β all the shared memory data blocks are unconditionally deleted andnReadablereturns0. and here's the spicy part: the shared memory data blocks are not locked prior to deleting them. After callingclear, all allocatedSharedSegment's shared memory segments have ceased to exist and can no longer be used. gone, reduced to atoms πvoid lock(size_t idx) constβ access to shared data segmentidxis locked. this member itself does not support recursive locking, so no nesting bestie πsize_t nBlocks() constβ returns the number of shared memory data blocks that can be used by theFBB::SharedSegmentobject.int newData(size_t idx)β returns the ID of a newly created shared memory data segment. That ID is also stored in the table of shared memory data segments that is maintained by theSharedSegmentobject.
An FBB::Exception is thrown if the shared memory data segment could not be allocated. π€
std::streamsize nReadable() constβ returns the number of characters (bytes) that can be read from the beginning of the shared memory.void nReadableLock() constβ when this member function returns, a lock has been obtained ofSharedSegment's mutex controlling access to the object's data member storing the number of characters that can be read from the shared memory controlled by theSharedSegmentobject. locked in πvoid nReadableUnlock() constβ releases the lock previously acquired bynReadableLock. and we're free π¦size_t segmentSize() constβ returns the size (in bytes) of the shared memory data blocks. TheSharedSegmentobject can accommodate at mostsegmentSize() * nBlocks()bytes. do the math bestie, that's your ceiling πbool truncate(std::streamsize offset)β after callingnReadableLock, ifoffsetis not exceeding the value returned bynReadable, thennReadableis changed tooffsetandtrueis returned. Otherwisefalseis returned, and the value returned bynReadablehas not been changed. Before returning,nReadableUnlockis called. so either it slays or it doesn't, no in-between πvoid unlock(size_t idx) constβ releases the lock on the shared memory data segmentidx. If the current process does not own the lock of shared memory data blockidx, nothing happens and the function immediately returns. no drama, it just leaves πΈvoid updateNreadable(std::streamsize offset)β the number of bytes that can be retrieved from the shared memory is updated tomax(nReadable(), offset). it only ever goes up, grow bestie grow π±
β STATIC MEMBER FUNCTIONS β
void *attach(int id)β returns the address of shared memory segmentid, mapped to the calling process's memory area.
An FBB::Exception is thrown if the shared memory data segment could not be attached. π
SharedSegment *create(int *id, size_t nBlocks, size_t segmentSize, size_t access)β returns a pointer to a newly createdSharedSegmentobject, defined in the computer's shared memory. yesss the birth of a segment π
The created shared memory's ID is stored at *id. The remaining arguments define the potential number of shared memory data blocks (nBlocks); the size of these data blocks (segmentSize); and the shared memory's access rights (access, using the well-known octal value representation as used by (chmod(1)) to define access rights for the owner, the group and others).
To return the shared segment to the operating system, deleteSegment should be used. don't ghost it, clean up properly π§Ή
An FBB::Exception is thrown if the shared memory data segment could not be created.
void deleteSegment(int id)β the shared memory segment having IDidis deleted. After callingdeleteSegment, shared memory segmentiddoesn't exist anymore. Theidcan be the shared memory ID of any segment for which the current user has write permissions.
AnFBB::Exceptionis thrown if shared memory data segmentidcould not be deleted.
Type *detach(Type *sharedPtr, bool requireOK = true)β this member is defined as a member template. It expects a pointer to a shared memory segment, previously mapped on the calling process's memory space byattach, and detaches it from the process's memory space, returning0.
By default, detaching the memory must succeed or an FBB::Exception is thrown. Throwing an exception on failure can be prevented by passing false as the member's second argument. so requireOK = true is the default, and false is your chill mode option π
π Note that detaching a memory segment does not destroy it. To return a shared segment to the operating system, deleteSegment should be used. detach β delete, write that down bestie ποΈ
size_t size(int id)β the size (in bytes) of shared memory data block having IDidis returned.
AnFBB::Exceptionis thrown if the size of segmentidcould not be determined.
π EXAMPLE π
See the sharedstream(3bobcat) man page. (go read it, it's the main character of the examples β¨)
π FILES π
bobcat/sharedsegment β defines the class interface
π« SEE ALSO π«
bobcat(7), chmod(1), isharedstream(3bobcat), osharedstream(3bobcat), sharedblock(3bobcat), sharedcondition(3bobcat), sharedmemory(3bobcat), sharedmutex(3bobcat), sharedpos(3bobcat), sharedreadme(7bobcat), sharedstream(3bobcat), sharedbuf(3bobcat)
the whole friend group, all linked up π«Ά
π BUGS π
None Reported. (we love a flawless queen π )
π BOBCAT PROJECT FILES π
https://fbb-git.gitlab.io/bobcat/: gitlab project page;
Debian Bobcat project files:
libbobcat6: debian package containing the shared library, changelog and copyright note;libbobcat-dev: 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. iykyk π±
πΈ COPYRIGHT πΈ
This is free software, distributed under the terms of the GNU General Public License (GPL). free as in freedom bestie π
β¨ AUTHOR β¨
Frank B. Brokken (f.b.brokken@rug.nl). the icon, the legend π
and that's a wrap on libbobcat-dev_6.07.01 π dated 2005-2025, page FBB::SharedSegment(3bobcat) β mwah, go forth and share your memory safely π
π¦β¨
π¦ 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