| 1 |
/* RCSid $Id$ */
|
| 2 |
/*
|
| 3 |
* RdataShare.h
|
| 4 |
*
|
| 5 |
* Radiance classes for interprocess data sharing
|
| 6 |
*
|
| 7 |
* Errors end up calling error() from rterror.h, so
|
| 8 |
* checking return values is not normally needed.
|
| 9 |
*
|
| 10 |
* Created by Greg Ward on 10/14/2024.
|
| 11 |
*/
|
| 12 |
|
| 13 |
#ifndef RdataShare_h
|
| 14 |
#define RdataShare_h
|
| 15 |
|
| 16 |
#include <sys/types.h>
|
| 17 |
|
| 18 |
/// Channel creation and i/o flags
|
| 19 |
enum {RDSexcl=1, RDSextend=2, RDSread=4, RDSwrite=8};
|
| 20 |
|
| 21 |
/*
|
| 22 |
* When data sharing object is created, flag meanings are:
|
| 23 |
* RDSexcl = Create a new file, or fail if it exists
|
| 24 |
* RDSextend = Set data extent to match size parameter
|
| 25 |
* RDSread = Data may be read
|
| 26 |
* RDSwrite = Data may be written
|
| 27 |
*
|
| 28 |
* The RDSread flag should be given to GetMemory() if a data
|
| 29 |
* read operation is desired. Similarly, the RDSwrite flag
|
| 30 |
* should be given when writing data in ReleaseMemory().
|
| 31 |
* Memory-mapped files always read *and* write data if opened
|
| 32 |
* in the appropriate mode. (Beware that writing to read-only data
|
| 33 |
* or reading write-only data will generate a system fault.)
|
| 34 |
* The RDSextend flag may be given with RDSread in GetMemory()
|
| 35 |
* and RDSwrite in ReleaseMemory() to allow the file size to
|
| 36 |
* increase as needed for a given operation.
|
| 37 |
* In some implementations, the RDSexcl flag may guarantee
|
| 38 |
* data consistency by placing locks on the requested memory areas.
|
| 39 |
* In such cases, it may make sense to include the RDSwrite flag
|
| 40 |
* in GetMemory() calls if the intention is to write new results
|
| 41 |
* in ReleaseMemory(), so an exclusive lock will be held.
|
| 42 |
*/
|
| 43 |
|
| 44 |
/// Data share object types returned by GetType()
|
| 45 |
enum RDSType {RDSTanonMap=1, RDSTfileMap, RDSTfile,
|
| 46 |
RDSTcust1, RDSTcust2, RDSTcust3, RDSTcust4};
|
| 47 |
|
| 48 |
extern const char RDSnoname[];
|
| 49 |
|
| 50 |
/// Abstract base class for shared memory object
|
| 51 |
class RdataShare {
|
| 52 |
protected:
|
| 53 |
char * chName; // channel name
|
| 54 |
size_t osiz; // current object size
|
| 55 |
int mode; // open mode
|
| 56 |
public:
|
| 57 |
RdataShare() {
|
| 58 |
chName = NULL; osiz = 0; mode = 0;
|
| 59 |
}
|
| 60 |
virtual ~RdataShare();
|
| 61 |
/// Get channel name, or RDSnoname if anonymous
|
| 62 |
const char * GetName() const {
|
| 63 |
return chName ? chName : RDSnoname;
|
| 64 |
}
|
| 65 |
/// Get R/W flags
|
| 66 |
int GetMode() const {
|
| 67 |
return mode & (RDSread|RDSwrite);
|
| 68 |
}
|
| 69 |
/// Get current object size
|
| 70 |
size_t GetSize() const {
|
| 71 |
return osiz;
|
| 72 |
}
|
| 73 |
/// Return data sharing type
|
| 74 |
virtual RDSType GetType() const = 0;
|
| 75 |
/// Attempt to extend or shrink object (adjust if 0)
|
| 76 |
virtual size_t Resize(size_t new_siz = 0) = 0;
|
| 77 |
/// Get data buffer
|
| 78 |
virtual void * GetMemory(size_t offs, size_t len, int fl = RDSread) = 0;
|
| 79 |
/// Return data buffer
|
| 80 |
virtual bool ReleaseMemory(void *bp, int fl = RDSwrite) = 0;
|
| 81 |
};
|
| 82 |
|
| 83 |
/// Memory sharing implemented with mmap'ed file (or anonymous area)
|
| 84 |
class RdataShareMap : public RdataShare {
|
| 85 |
void * mmorg; // memory-mapped origin
|
| 86 |
int bufCount; // count of allocated buffers
|
| 87 |
public:
|
| 88 |
RdataShareMap(const char *name, int flags, size_t siz = 0);
|
| 89 |
virtual ~RdataShareMap();
|
| 90 |
RDSType GetType() const {
|
| 91 |
return chName ? RDSTfileMap : RDSTanonMap;
|
| 92 |
}
|
| 93 |
size_t Resize(size_t new_siz = 0);
|
| 94 |
void * GetMemory(size_t offs, size_t len, int fl);
|
| 95 |
bool ReleaseMemory(void *dp, int fl);
|
| 96 |
};
|
| 97 |
|
| 98 |
struct RDSbuffer; // Private struct for buffer list
|
| 99 |
|
| 100 |
/// Memory sharing implemented with simple file
|
| 101 |
class RdataShareFile : public RdataShare {
|
| 102 |
int fd; // open file descriptor
|
| 103 |
RDSbuffer * blist; // allocated buffer list
|
| 104 |
public:
|
| 105 |
RdataShareFile(const char *name, int flags, size_t siz = 0);
|
| 106 |
virtual ~RdataShareFile();
|
| 107 |
RDSType GetType() const {
|
| 108 |
return RDSTfile;
|
| 109 |
}
|
| 110 |
size_t Resize(size_t new_siz = 0);
|
| 111 |
void * GetMemory(size_t offs, size_t len, int fl);
|
| 112 |
bool ReleaseMemory(void *dp, int fl);
|
| 113 |
};
|
| 114 |
|
| 115 |
#endif // RdataShare_h
|