feisty meow concerns codebase  2.140
shared_memory.h
Go to the documentation of this file.
1 #ifndef SHARED_MEMORY_CLASS
2 #define SHARED_MEMORY_CLASS
3 
4 /*****************************************************************************\
5 * *
6 * Name : shared_memory *
7 * Author : Chris Koeritz *
8 * *
9 *******************************************************************************
10 * Copyright (c) 2002-$now By Author. This program is free software; you can *
11 * redistribute it and/or modify it under the terms of the GNU General Public *
12 * License as published by the Free Software Foundation; either version 2 of *
13 * the License or (at your option) any later version. This is online at: *
14 * http://www.fsf.org/copyleft/gpl.html *
15 * Please send any updates to: fred@gruntose.com *
16 \*****************************************************************************/
17 
18 #include <basis/contracts.h>
19 #include <filesystem/byte_filer.h>
20 #include <processes/rendezvous.h>
21 
22 namespace application {
23 
25 
30 class shared_memory : public virtual basis::root_object
31 {
32 public:
33  shared_memory(int size, const char *identity);
35 
41  virtual ~shared_memory();
43 
46  DEFINE_CLASS_NAME("shared_memory");
47 
48  bool valid() const { return _valid; }
50 
52  int size() const { return _size; }
54 
55  const basis::astring &identity() const;
57 
58  bool first_usage(basis::abyte *locked_memory, int max_compare);
60 
66  basis::abyte *lock();
68 
72  void unlock(basis::abyte * &to_unlock);
74 
76  static basis::astring unique_shared_mem_identifier(int sequencer);
78 
83 private:
84  processes::rendezvous *_locking;
85 #ifdef __UNIX__
86  int _the_memory;
87 #elif defined(__WIN32__)
88  void *_the_memory;
89 #endif
90  bool _valid;
91  basis::astring *_identity;
92  int _size;
93 
94  // these do the actual work of getting the memory.
95  basis::abyte *locked_grab_memory();
96  void locked_release_memory(basis::abyte * &to_unlock);
97 
98  static basis::astring special_filename(const basis::astring &identity);
100 
101  // forbidden.
102  shared_memory(const shared_memory &);
103  shared_memory &operator =(const shared_memory &);
104 };
105 
106 } //namespace.
107 
108 
109 #endif // outer guard.
110 
Implements storage for memory that can be shared between threads.
Definition: shared_memory.h:31
const basis::astring & identity() const
provides a peek at the name that this chunk was constructed with.
bool valid() const
this must be true for the shared_memory to be usable.
Definition: shared_memory.h:48
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.
Definition: shared_memory.h:51
static basis::astring unique_shared_mem_identifier(int sequencer)
returns a unique identifier for a shared memory chunk.
DEFINE_CLASS_NAME("shared_memory")
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.
Provides a dynamically resizable ASCII character string.
Definition: astring.h:35
An inter-process synchronization primitive.
Definition: rendezvous.h:34
Implements an application lock to ensure only one is running at once.
unsigned char abyte
A fairly important unit which is seldom defined...
Definition: definitions.h:51