36 #include <sys/types.h>
53 _identity(new
astring(identity)),
57 bool first_use =
false;
63 _the_memory = shm_open(special_filename(
identity).s(), flag, mode);
68 _the_memory = shm_open(special_filename(
identity).s(), flag, mode);
72 printf(
"error allocating shared segment for %s, was told %s.\n",
81 int ret = ftruncate(_the_memory,
size);
84 printf(
"error truncating shared segment for %s, was told %s.",
90#elif defined(__WIN32__)
91 _the_memory = ::CreateFileMapping((
HANDLE)-1, NULL, PAGE_READWRITE,
94 first_use = (err != ERROR_ALREADY_EXISTS);
102 #pragma message("simulating shared memory since unknown for this platform.")
103 if (!_bogus_shared_space().length()) {
104 _bogus_shared_space().reset(
size);
107 _the_memory = _bogus_shared_space().access();
112 abyte *contents = locked_grab_memory();
118 memset(contents, 0,
size);
119 locked_release_memory(contents);
128 close(
int(_the_memory));
129 shm_unlink(special_filename(
identity()).s());
131#elif defined(__WIN32__)
132 ::CloseHandle(_the_memory);
148 shared_file =
astring(
"/tmp_") +
"sharmem_" + shared_file;
164 for (
int i = 0; i < max_compare; i++)
165 if (contents[i] != 0)
return false;
172 return locked_grab_memory();
177 locked_release_memory(to_unlock);
181abyte *shared_memory::locked_grab_memory()
185 if (!_the_memory) return to_return;
188 MAP_SHARED,
int(_the_memory), 0);
189#elif defined(__WIN32__)
190 to_return = (
abyte *)::MapViewOfFile((
HANDLE)_the_memory, FILE_MAP_ALL_ACCESS,
193 to_return = (
abyte *)_the_memory;
198 throw(
astring(
astring(class_name()) +
"::" + func +
": no data was accessible in shared space."));
203void shared_memory::locked_release_memory(
abyte * &to_unlock)
205 if (!_the_memory || !to_unlock)
return;
207 munmap(to_unlock, _size);
208#elif defined(__WIN32__)
209 ::UnmapViewOfFile(to_unlock);
const basis::astring & identity() const
provides a peek at the name that this chunk was constructed with.
shared_memory(int size, const char *identity)
a shared chunk of the "size" specified will be created.
int size() const
returns the size of the shared chunk of memory.
static basis::astring unique_shared_mem_identifier(int sequencer)
returns a unique identifier for a shared memory chunk.
void unlock(basis::abyte *&to_unlock)
returns control of the shared memory so others can access it.
basis::abyte * lock()
locks the shared memory and returns a pointer to the storage.
bool first_usage(basis::abyte *locked_memory, int max_compare)
returns true if the "locked_memory" was just created.
virtual ~shared_memory()
cleans up the shared bit of memory as far as we're concerned.
a_sprintf is a specialization of astring that provides printf style support.
Provides a dynamically resizable ASCII character string.
static basis::astring application_name()
returns the full name of the current application.
static basis::un_int process_id()
returns the process id for this task, if that's relevant on the OS.
static void detooth_filename(basis::astring &to_clean, char replacement='_')
takes any known illegal file system characters out of "to_clean".
static basis::astring system_error_text(basis::un_int error_to_show)
returns the OS's string form of the "error_to_show".
static basis::un_int system_error()
gets the most recent system error reported on this thread.
An inter-process synchronization primitive.
bool lock(locking_methods how=ENDLESS_WAIT)
grab the lock, if possible.
void unlock()
releases a previously acquired lock.
#define NULL_POINTER
The value representing a pointer to nothing.
#define FUNCDEF(func_in)
FUNCDEF sets the name of a function (and plugs it into the callstack).
Implements an application lock to ensure only one is running at once.
The guards collection helps in testing preconditions and reporting errors.
void WHACK(contents *&ptr)
deletion with clearing of the pointer.
unsigned char abyte
A fairly important unit which is seldom defined...
unsigned int un_int
Abbreviated name for unsigned integers.
bool negative(const type &a)
negative returns true if "a" is less than zero.
A platform independent way to obtain the timestamp of a file.
A logger that sends to the console screen using the standard output device.
A dynamic container class that holds any kind of object via pointers.
Support for unicode builds.
Aids in achievement of platform independence.