Museum

Home

Lab Overview

Retrotechnology Articles

Online Manuals

⇒ sma(3X) — OSF1 1.0

Media Vault

Software Library

Restoration Projects

Artifacts Sought

Related Articles

shget(2)

shctl(2)

shmat(2)

mutex(3X)

malloc(3)

SMA(3X)  —  Subroutines

NAME

Sma - Shared memory allocator

SYNOPSIS


#include <codelibs/sma.h>
 class Sma
{
public:
Sma(char ∗base, int static_size = 0, int size = 0);
int alloc(int size);
void free(int offset);
};
 CC ... -lcodelibs
  

DESCRIPTION

This class is used for allocating and deallocating blocks of memory within a shared memory segment.  The constructor for this class takes three optional arguments.  The first is the base address of the shared memory segment.  This should be the value that is returned by shmat(2). The static_size parameter is the number of bytes that should be left untouched at the beginning of the shared memory segment. 

The size parameter is the number of bytes in the shared memory segment that should be made available for allocated blocks.  The shared memory segment of the specified size will be initialized and made ready for allocation if the size value is greater than zero.  A non-zero size should only be specified by the process that creates the shared memory segment.  A value of zero for size should be used by all other processes to inhibit the initialization.  The size value should be at least 16 bytes in size.  It is also bumped up to 16 if it is smaller than 16 and greater than 0.  Typically, size is at least as big as the page size of the machine to be useful. 

alloc returns the byte offset from the shared memory base where a block of size bytes is located.  If the allocator is unable to find a block of memory large enough to hold size bytes then zero (0) will be returned as the offset.  If the allocator has not been initialized then -1 is returned.  Offsets returned by alloc are always a multiple of eight (8). 

free causes the block of memory at offset from the base of the shared memory segment to placed back in the free block pool. This makes the block available for future allocations.  If the block to be freed is adjacent to another already free block then the two blocks will be joined into one larger free block.  Offset MUST be a value that has been returned by alloc.

No provisions are made for syncronizing multiple processes that can potentially try to allocate or free blocks at the same time.  It is up to the user of this class to guarantee such syncronization.  The Mutex(3X) class should make this task easier.

FATAL ERRORS

The Sma constructor will abort with an error message if the base address of the shared memory segment itself is not properly aligned.  There’s really nothing we can do at this point anyway. 

NOTES

If the size parameter is less than the minimum allowed, it is set to the minimum segment size, which is 16 bytes. 

SEE ALSO

shget(2), shctl(2), shmat(2), mutex(3X), malloc(3).

  —  codelibs  —  C++

Typewritten Software • bear@typewritten.org • Edmonds, WA 98026