ici::doc::pod3::sdr(3)

πŸ“¦ version 3.2.1, in ubuntu jammy

πŸ’– sdr β€” Simple Data Recorder library πŸ’–

okay so first things first: this page lives in section 3 of the manual, under ici::doc::pod3::sdr(3) β€” ICI library functions, iykyk. And bestie, it's a long one, so buckle up. 🌸


✨ SYNOPSIS ✨

#include "sdr.h"

[see below for available functions]

that's it, that's the whole synopsis. the rest is vibes and functions, and the functions are listed below, so keep scrolling. πŸ’…


πŸ¦‹ DESCRIPTION πŸ¦‹

SDR is a library of functions that support the use of an abstract data recording device called an "SDR" ("simple data recorder") for persistent storage of data. The SDR abstraction insulates software not only from the specific characteristics of any single data storage device but also from some kinds of persistent data storage and retrieval chores. The underlying principle? An SDR provides standardized support for user data organization at object granularity, with direct access to persistent user data objects, rather than supporting user data organization only at "file" granularity and requiring the user to implement access to the data objects accreted within those files. no cap. 🫢

The SDR library is designed to provide some of the same kinds of directory services as a file system, plus support for complex data structures that give you way more operational flexibility than files. (As an example of this flexibility, consider how much easier and faster it is to delete a given element from the middle of a linked list than it is to delete a range of bytes from the middle of a text file. like, it's not even close. 😭) The intent is to enable the software developer to take maximum advantage of the high speed and direct byte addressability of a non-volatile flat address space in the management of persistent data. The SDR equivalent of a "record" of data is simply a block of nominally persistent memory allocated from this address space. The SDR equivalent of a "file" is a collection object. Like files, collections can have names, can be located by name within persistent storage, and can impose structure on the data items they encompass. But β€” as discussed later β€” SDR collection objects can impose structures other than the strict FIFO accretion of records or bytes that characterizes a file. iconic behavior honestly. ✨

The notional data recorder managed by the SDR library takes the form of a single array of randomly accessible, contiguous, nominally persistent memory locations called a heap. Physically, the heap may be implemented as:

  • a region of shared memory,
  • a single file of predefined size, or
  • both β€” that is, the heap may be a region of shared memory that is automatically mirrored in a file.

SDR services that manage SDR data are provided in several layers, each of which relies on the services implemented at lower levels:

  • At the highest level, a cataloguing service enables retrieval of persistent objects by name.
  • Services that manage three types of persistent data collections are provided for use both by applications and by the cataloguing service: linked lists, self-delimiting tables (which function as arrays that remember their own dimensions), and self-delimiting strings (short character arrays that remember their lengths, for speedier retrieval).
  • Basic SDR heap space management services, analogous to malloc() and free(), enable the creation and destruction of objects of arbitrary type.
  • Farther down the service stack are memcpy-like low-level functions for reading from and writing to the heap.
  • Protection of SDR data integrity across a series of reads and writes is provided by a transaction mechanism.

SDR persistent data are referenced in application code by Object values and Address values, both of which are simply displacements (offsets) within SDR address space. The difference between the two is that an Object is always the address of a block of heap space returned by some call to sdr_malloc(), while an Address can refer to any byte in the address space. That is, an Address is the SDR functional equivalent of a C pointer in DRAM, and some Addresses point to Objects. πŸ’«

Before using SDR services, the services must be loaded to the target machine and initialized by invoking the sdr_initialize() function, and the management profiles of one or more SDR's must be loaded by invoking the sdr_load_profile() function. These steps are normally performed only once, at application load time.

An application gains access to an SDR by passing the name of the SDR to the sdr_start_using() function, which returns an Sdr pointer. Most other SDR library functions take an Sdr pointer as first argument.

⚠️ All writing to an SDR heap must occur during a transaction that was initiated by the task issuing the write. no sneaky side-writes, bestie.

Transactions are single-threaded; if task B wants to start a transaction while a transaction begun by task A is still in progress, it must wait until A's transaction is either ended or cancelled. A transaction is begun by calling sdr_begin_xn(). The current transaction is normally ended by calling the sdr_end_xn() function, which returns an error return code value in the event that any serious SDR-related processing error was encountered in the course of the transaction. Transactions may safely be nested, provided that every level of transaction activity that is begun is properly ended. periodt. πŸŽ€

The current transaction may instead be cancelled by calling sdr_cancel_xn(), which is normally used to indicate that some sort of serious SDR-related processing error has been encountered. Canceling a transaction reverses all SDR update activity performed up to that point within the scope of the transaction β€” and, if the canceled transaction is an inner, nested transaction, all SDR update activity performed within the scope of every outer transaction encompassing that transaction and every other transaction nested within any of those outer transactions β€” provided the SDR was configured for transaction reversibility. When an SDR is configured for reversibility, all heap write operations performed during a transaction are recorded in a log file that is retained until the end of the transaction. Each log file entry notes the location at which the write operation was performed, the length of data written, and the content of the overwritten heap bytes prior to the write operation. Canceling the transaction causes the log entries to be read and processed in reverse order, restoring all overwritten data. Ending the transaction, on the other hand, simply causes the log to be discarded. πŸ’€

If a log file exists at the time that the profile for an SDR is loaded (typically during application initialization), the transaction that was being logged is automatically canceled and reversed. This ensures that, for example, a power failure that occurs in the middle of a transaction will never wreck the SDR's data integrity: either all updates issued during a given transaction are reflected in the current database content or none are. it's giving "all or nothing" and honestly we're obsessed. 🫢

As a further measure to protect SDR data integrity, an SDR may additionally be configured for object bounding. When an SDR is configured to be "bounded", every heap write operation is restricted to the extent of a single object allocated from heap space; that is, it's impossible to overwrite part of one object by writing beyond the end of another. To enable the library to enforce this mechanism, application code is prohibited from writing anywhere but within the extent of an object that either:

  • (a) was allocated from managed heap space during the same transaction (directly or indirectly via some collection management function), or
  • (b) was staged β€” identified as an update target β€” during the same transaction (again, either directly or via some collection management function).

Note that both transaction reversibility and object bounding consume processing cycles and inhibit performance to some degree. Determining the right balance between operational safety and processing speed is left to the user. your call bestie, i don't make the rules πŸ’…

Note also that, since SDR transactions are single-threaded, they can additionally be used as a general mechanism for simply implementing "critical sections" in software that is already using SDR for other purposes: the beginning of a transaction marks the start of code that can't be executed concurrently by multiple tasks. To support this use of the SDR transaction mechanism, the additional transaction termination function sdr_exit_xn() is provided. sdr_exit_xn() simply ends a transaction without either signaling an error or checking for errors. Like sdr_cancel_xn(), sdr_exit_xn() has no return value; unlike sdr_cancel_xn(), it assures that ending an inner, nested transaction does not cause the outer transaction to be aborted and backed out.

😭 But this capability must be used carefully: the protection of SDR data integrity requires that transactions which are ended by sdr_exit_xn() must not encompass any SDR update activity whatsoever. not a single byte. fr fr.

The heap space management functions of the SDR library are adapted directly from the Personal Space Management (psm) function library. The manual page for psm(3) explains the algorithms used and the rationale behind them. The principal difference between PSM memory management and SDR heap management is that, for performance reasons, SDR reserves the "small pool" for its own use only; all user data space is allocated from the "large pool", via the sdr_malloc() function.


🌈 RETURN VALUES AND ERROR HANDLING 🌈

Whenever an SDR function call fails, a diagnostic message explaining the failure of the function is recorded in the error message pool managed by the "platform" system (see the discussion of putErrmsg() in platform(3)). 🫢

The failure of any function invoked in the course of an SDR transaction causes all subsequent SDR activity in that transaction to fail immediately. This can streamline SDR application code somewhat: it may not be necessary to check the return value of every SDR function call executed during a transaction. If the sdr_end_xn() call returns zero, all updates performed during the transaction must have succeeded. slay. ✨


✨ SYSTEM ADMINISTRATION FUNCTIONS ✨

int sdr_initialize(int wmSize, char *wmPtr, int wmKey, char *wmName)

okay bestie, listen up. This is the origin story of your SDR system. You must call sdr_initialize() once every time the computer reboots, before you call any other SDR library function. No exceptions. πŸ’…

It attaches to a pool of shared memory managed by PSM (peep psm(3) for the tea) that makes SDR library operations possible. Now, if your SDR system needs to share a common pool of shared memory with one or more other systems, you gotta put the key of that shared memory segment in wmKey and the PSM partition name associated with that memory segment in wmName. Otherwise, wmKey must be zero and wmName must be NULL, which makes sdr_initialize() assign default values. Easy.

If a shared memory segment identified by the effective value of wmKey already exists, then wmSize can be zero and wmPtr is ignored. Otherwise, you must provide the size of the shared memory pool in wmSize, and a new shared memory segment gets created based on wmPtr:

  • if wmPtr is NULL, then wmSize bytes of shared memory are dynamically acquired, allocated, and assigned to the newly created shared memory segment;
  • otherwise, the memory located at wmPtr is assumed to have been pre-allocated and is merely assigned to the newly created shared memory segment.

sdr_initialize() also creates a semaphore to serialize access to the SDR system's private array of SDR profiles. Iconic.

Returns 0 on success, -1 on any failure. 🫢


void sdr_wm_usage(PsmUsageSummary *summary)

Loads summary with a snapshot of the usage of the SDR system's private working memory. To print the snapshot, use psm_report(). (See psm(3).) ✨


void sdr_shutdown( )

Ends all access to all SDRs (check sdr_stop_using()), detaches from the SDR system's working memory (releasing the memory if it was dynamically allocated by sdr_initialize()), and destroys the SDR system's private semaphore. After sdr_shutdown(), you must call sdr_initialize() again before any call to any other SDR library function. No cap. πŸ’€


✨ DATABASE ADMINISTRATION FUNCTIONS ✨

int sdr_load_profile(char *name, int configFlags, long heapWords, int memKey, char *pathName, char *restartCmd, unsigned int restartLatency)

Loads the profile for an SDR into the system's private list of SDR profiles. Here's the thing: SDRs themselves are persistent, but SDR profiles are not. So for an application to access an SDR, sdr_load_profile() must have been called to load the profile of the SDR since the last invocation of sdr_initialize(). Periodt.

  • name is the name of the SDR, required for any subsequent sdr_start_using() call.
  • configFlags specifies the configuration of the SDR, the bitwise "or" of some combination of the following:
    • SDR_IN_DRAM β€” SDR is implemented as a region of shared memory.
    • SDR_IN_FILE β€” SDR is implemented as a file.
    • SDR_REVERSIBLE β€” SDR transactions are logged and are reversed if canceled.
    • SDR_BOUNDED β€” Heap updates are not allowed to cross object boundaries.
  • heapWords specifies the size of the heap in words; word size depends on machine architecture, i.e., a word is 4 bytes on a 32-bit machine, 8 bytes on a 64-bit machine. Note that each SDR prepends to the heap a "map" of predefined, fixed size. The total amount of space occupied by an SDR in memory and/or in a file is the sum of the size of the map plus the product of word size and heapWords.
  • memKey is ignored if configFlags does not include SDR_IN_DRAM. It should normally be SM_NO_KEY, causing the shared memory region for the SDR to be allocated dynamically and shared using a dynamically selected shared memory key. If specified, memKey must be a shared memory key identifying a pre-allocated region of shared memory whose length is equal to the total SDR size, shared via the indicated key.
  • pathName is ignored if configFlags includes neither SDR_REVERSIBLE nor SDR_IN_FILE. It is the fully qualified name of the directory into which the SDR's log file and/or database file will be written. The name of the log file (if any) will be "<sdrname>.sdrlog". The name of the database file (if any) will be "<sdrname>.sdr"; this file will be automatically created and filled with zeros if it does not exist at the time the SDR's profile is loaded.
  • If a cleanup task must be run whenever a transaction is reversed, the command to execute this task must be provided in restartCmd and the number of seconds to wait for this task to finish before resuming operations must be provided in restartLatency. If restartCmd is NULL or restartLatency is zero then no cleanup task will be run upon transaction reversal.

Returns 0 on success, -1 on any error.


int sdr_reload_profile(char *name, int configFlags, long heapWords, int memKey, char *pathName, char *restartCmd, unsigned int restartLatency)

Use this when the state of an SDR is thought to be inconsistent, maybe because a program crashed while it had a transaction open. It unloads the profile for the SDR, forcing the reversal of any transaction that is currently in progress when the SDR's profile is re-loaded. Then it calls sdr_load_profile() to re-load the profile for the SDR. Same return values as sdr_load_profile. πŸ”₯


Sdr sdr_start_using(char *name)

Locates SDR profile by name and returns a handle that can be used for all functions that operate on that SDR. On any failure, returns NULL. πŸ™ˆ


char *sdr_name(Sdr sdr)

Returns the name of the sdr. 🌸


long sdr_heap_size(Sdr sdr)

Returns the total size of the SDR heap, in bytes. πŸ’–


void sdr_stop_using(Sdr sdr)

Terminates access to the SDR via this handle. Other users of the SDR are not affected. Frees the Sdr object. πŸŽ€


void sdr_abort(Sdr sdr)

Terminates the task. In flight configuration, also terminates all use of the SDR system by all tasks. πŸ’€


void sdr_destroy(Sdr sdr)

Ends all access to this SDR, unloads the SDR's profile, and erases the SDR from memory and file system. πŸ‘‘


✨ DATABASE TRANSACTION FUNCTIONS ✨

void sdr_begin_xn(Sdr sdr)

Initiates a transaction. Note that transactions are single-threaded; any task that calls sdr_begin_xn() is suspended until all previously requested transactions have been ended or canceled. So don't be impatient, bestie. ⏳


int sdr_in_xn(Sdr sdr)

Returns 1 if called in the course of a transaction, 0 otherwise. Simple as that. ✨


void sdr_exit_xn(Sdr sdr)

Simply abandons the current transaction, ceasing the calling task's lock on ION. Must not be used if any database modifications were performed during the transaction; sdr_end_xn() must be called instead, to commit those modifications. Don't mess this up. 🚫


void sdr_cancel_xn(Sdr sdr)

Cancels the current transaction. If reversibility is enabled for the SDR, canceling a transaction reverses all heap modifications performed during that transaction. πŸ”„


int sdr_end_xn(Sdr sdr)

Ends the current transaction. Returns 0 if the transaction completed without any error; returns -1 if any operation performed in the course of the transaction failed, in which case the transaction was automatically canceled. 😭


✨ DATABASE I/O FUNCTIONS ✨

void sdr_read(Sdr sdr, char *into, Address from, int length)

Copies length characters at from (a location in the indicated SDR) to the memory location given by into. The data are copied from the shared memory region in which the SDR resides, if any; otherwise they are read from the file in which the SDR resides. πŸ“–


void sdr_peek(sdr, variable, from)

sdr_peek() is a macro that uses sdr_read() to load variable from the indicated address in the SDR database; the size of variable is used as the number of bytes to copy. πŸ‘€


void sdr_write(Sdr sdr, Address into, char *from, int length)

Copies length characters at from (a location in memory) to the SDR heap location given by into. Can only be performed during a transaction, and if the SDR is configured for object bounding then heap locations into through (into + (length - 1)) must be within the extent of some object that was either allocated or staged within the same transaction. The data are copied both to the shared memory region in which the SDR resides, if any, and also to the file in which the SDR resides, if any. ✍️


void sdr_poke(sdr, into, variable)

sdr_poke() is a macro that uses sdr_write() to store variable at the indicated address in the SDR database; the size of variable is used as the number of bytes to copy. πŸ’…


char *sdr_pointer(Sdr sdr, Address address)

Returns a pointer to the indicated location in the heap β€” a "heap pointer" β€” or NULL if the indicated address is invalid. NOTE that this function cannot be used if the SDR does not reside in a shared memory region.

Providing an alternative to using sdr_read() to retrieve objects into local memory, sdr_pointer() can help make SDR-based applications run very quickly, but it must be used WITH GREAT CAUTION! Never use a direct pointer into the heap when not within a transaction, because you will have no assurance at any time that the object pointed to by that pointer has not changed (or is even still there). And NEVER de-reference a heap pointer in order to write directly into the heap: this makes transaction reversal impossible. Whenever writing to the SDR, always use sdr_write(). ⚠️


Address sdr_address(Sdr sdr, char *pointer)

Returns the address within the SDR heap of the indicated location, which must be (or be derived from) a heap pointer as returned by sdr_pointer(). Returns zero if the indicated location is not greater than the start of the heap mirror. NOTE that this function cannot be used if the SDR does not reside in a shared memory region. 🧭


void sdr_get(sdr, variable, heap_pointer)

sdr_get() is a macro that uses sdr_read() to load variable from the SDR address given by heap_pointer; heap_pointer must be (or be derived from) a heap pointer as returned by sdr_pointer(). The size of variable is used as the number of bytes to copy. πŸ“


void sdr_set(sdr, heap_pointer, variable)

sdr_set() is a macro that uses sdr_write() to store variable at the SDR address given by heap_pointer; heap_pointer must be (or be derived from) a heap pointer as returned by sdr_pointer(). The size of variable is used as the number of bytes to copy. 🧚


πŸŽ€ HEAP SPACE MANAGEMENT FUNCTIONS πŸŽ€

okay bestie, this is the part where we actually get stuff into and out of the heap. buckle up πŸ’–

  • Object sdr_malloc(Sdr sdr, unsigned long size) β€” grabs a block of space out of the indicated SDR's heap. size is how big the block is gonna be, and heads up: the max is 1/2 of the maximum address space size (so like 2G on a 32-bit machine, no cap). returns the block address if it ate, and zero if the block just couldn't be allocated 😭
  • Object sdr_insert(Sdr sdr, char *from, unsigned long size) β€” calls sdr_malloc() to get a block of size bytes, and if that works out, it calls sdr_write() to copy size bytes of data from memory at from into the fresh block. returns the block address on success, zero if allocation flopped.
  • Object sdr_stow(sdr, variable) β€” this one's a macro, not a function, okay? it uses sdr_insert() to insert a copy of variable into the database, and it takes variable's size as the number of bytes to copy. cute, right? 🫢
  • int sdr_object_length(Sdr sdr, Object object) β€” tells you how many bytes of heap space got allocated to the application data sitting at object.
  • void sdr_free(Sdr sdr, Object object) β€” frees the heap space that object is occupying so it can be re-allocated later. bye girl πŸ‘‹
  • void sdr_stage(Sdr sdr, char *into, Object from, int length) β€” kinda like sdr_read(), it copies length characters from from (a spot in the heap of the indicated SDR) to the memory location into. but unlike sdr_get(), sdr_stage() demands that from be the address of some allocated object, not just any old location in the heap. and when you call it inside a transaction, it notifies the SDR library that this object might get updated later in the transaction β€” which lets the library grab the object's size for later, so it can validate any attempts to write into a location inside that object. if length is zero, SDR privately retrieves the object's size but copies none of its content into memory. sneaky but valid ✨
  • long sdr_unused(Sdr sdr) β€” returns the number of bytes of heap space that hasn't been allocated to either the large or the small objects pool yet.
  • void sdr_usage(Sdr sdr, SdrUsageSummary *summary) β€” loads that SdrUsageSummary structure with a lil snapshot of the SDR's usage status. and SdrUsageSummary is defined as:
typedef struct
{
        char            sdrName[MAX_SDR_NAME + 1];
        unsigned int    sdrSize;
        unsigned int    smallPoolSize;
        unsigned int    smallPoolFreeBlockCount[SMALL_SIZES];
        unsigned int    smallPoolFree;
        unsigned int    smallPoolAllocated;
        unsigned int    largePoolSize;
        unsigned int    largePoolFreeBlockCount[LARGE_ORDERS];
        unsigned int    largePoolFree;
        unsigned int    largePoolAllocated;
        unsigned int    unusedSize;
} SdrUsageSummary;
  • void sdr_report(SdrUsageSummary *summary) β€” sends a printed summary of the SDR's usage status straight to stdout. receipts, but make it a report πŸ’…
  • int sdr_heap_depleted(Sdr sdr) β€” a Boolean girlie: returns 1 if the total available space in the SDR's heap (that's small pool free + large pool free + unused) is less than 1/16 of the total size of the heap. otherwise it returns zero. basically it's the "we're running out of room" alarm 🚨

πŸ” HEAP SPACE USAGE TRACING πŸ”

so here's the tea: if SDR_TRACE is defined when the SDR source code gets compiled, the system bakes in built-in support for simple tracing of SDR heap space usage. heap space allocations get logged, and heap space deallocations get matched up to those logged allocations, "closing" them. this is so helpful for sniffing out heap space leaks and other kinds of SDR heap access problems, no cap πŸ•΅οΈβ€β™€οΈ

  • int sdr_start_trace(Sdr sdr, int traceLogSize, char *traceLogAddress) β€” kicks off an episode of SDR heap space usage tracing. traceLogSize is how many bytes of shared memory to use for logging trace activity, and the frequency with which "closed" trace log events have to be deleted varies inversely with how much memory you gave the trace log (iykyk). traceLogAddress is normally NULL, which makes the trace system dynamically allocate traceLogSize bytes of shared memory for trace logging; but if it's non-NULL, it's gotta point to traceLogSize bytes of shared memory that the application already pre-allocated for exactly this. returns 0 on success, -1 on any failure.
  • void sdr_print_trace(Sdr sdr, int verbose) β€” prints a cumulative trace report and a current usage report for sdr. if verbose is zero, only exceptions get printed β€” notably trace log events that are still open, aka potential SDR heap space leaks πŸ‘€ β€” otherwise all the activity in the trace log gets printed.
  • void sdr_clear_trace(Sdr sdr) β€” deletes all the closed trace log events from the log, freeing up memory for more tracing. spring cleaning 🌸
  • void sdr_stop_trace(Sdr sdr) β€” ends the current episode of SDR heap space usage tracing. and if the shared memory used for the trace log was allocated by sdr_start_trace(), it releases that shared memory too. that's closure, bestie ✨

πŸ“‡ CATALOGUE FUNCTIONS πŸ“‡

the SDR catalogue functions keep the catalogue of the names, types, and addresses of objects within an SDR. the catalogue service comes with functions for creating, deleting and finding catalogue entries, plus one for navigating through catalogue entries sequentially. it's giving librarian energy πŸ“š

  • void sdr_catlg(Sdr sdr, char *name, int type, Object object) β€” associates object with name in the indicated SDR's catalogue and notes the type that was declared for this object. type is optional and means literally nothing beyond whatever the application decides it means.
heads up: the SDR catalogue is flat, not hierarchical like a directory tree, and all names must be unique. also the length of name is limited to 15 characters. keep it short, babe.
  • Object sdr_find(Sdr sdr, char *name, int *type) β€” finds the Object associated with name in the indicated SDR's catalogue and returns its address; it also reports the catalogued type of the object in *type if type is non-NULL. returns zero if nothing's currently catalogued under this name πŸ˜”
  • void sdr_uncatlg(Sdr sdr, char *name) β€” dissociates from name whatever object in the indicated SDR's catalogue is currently hanging out under that name.
  • Object sdr_read_catlg(Sdr sdr, char *name, int *type, Object *object, Object previous_entry) β€” this is the one for navigating catalogue entries sequentially. if previous_entry is zero, it reads the first entry in the indicated SDR's catalogue; otherwise it reads the next catalogue entry following the one at previous_entry. either way, it returns zero if no such catalogue entry exists; otherwise it copies that entry's name, type, and catalogued object address into name, *type, and *object, and then returns the address of the catalogue entry β€” which you can totally use as previous_entry in a later call to sdr_read_catlg(). chain them together, it's a whole vibe πŸ¦‹

πŸ‘‘ USER'S GUIDE πŸ‘‘

Compiling an SDR application

just make sure you "#include "sdr.h"" at the top of every source file that has any SDR function calls in it. that's it, that's the step.

for UNIX applications, link with -lsdr.

Loading an SDR application (VxWorks)

ld < "libsdr.o"

once the library is loaded, you can start loading SDR applications. slay πŸ’…


🌈 SEE ALSO 🌈

sdrlist(3), sdrstring(3), sdrtable(3)


and that's a wrap on this whole page, bestie!! this one's from perl v5.24.1, dated 2016-07-07, and it's the ici::doc::pod3::sdr(3) man page β€” iconic, timeless, still valid. go forth and manage that heap. mwah πŸ’–πŸŽ€βœ¨

πŸ“¦ where this page comes from