Fawkes API  Fawkes Development Version
header.cpp
1 
2 /***************************************************************************
3  * bb_shmem_header.cpp - BlackBoard shared memory header
4  *
5  * Created: Thu Oct 19 14:21:07 2006 (Anne's 25th Birthday)
6  * Copyright 2006 Tim Niemueller [www.niemueller.de]
7  *
8  ****************************************************************************/
9 
10 /* This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version. A runtime exception applies to
14  * this software (see LICENSE.GPL_WRE file mentioned below for details).
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Library General Public License for more details.
20  *
21  * Read the full text in the LICENSE.GPL_WRE file in the doc directory.
22  */
23 
24 #include <blackboard/shmem/header.h>
25 #include <utils/ipc/shm.h>
26 #include <cstddef>
27 
28 namespace fawkes {
29 
30 /** @class BlackBoardSharedMemoryHeader <blackboard/shmem/header.h>
31  * BlackBoard Shared Memory Header.
32  * This class is used identify BlackBoard shared memory headers and
33  * to interact with the management data in the shared memory segment.
34  * The basic options stored in the header is a version identifier
35  * and pointers to the list heads of the free and allocated chunk
36  * lists.
37  *
38  * @author Tim Niemueller
39  * @see SharedMemoryHeader
40  */
41 
42 
43 /** Constructor
44  * @param data_size the size of the shared memory segment without the header
45  * that should be allocated.
46  * @param version The BB version to store in the shared memory segment to prevent
47  * conflicts with older software.
48  */
50  unsigned int version)
51 {
52  _data_size = data_size;
53  _version = version;
54  data = NULL;
55 }
56 
57 
58 /** Constructor.
59  * @param version The BB version to store in the shared memory segment to prevent
60  * conflicts with older software.
61  */
63 {
64  _data_size = 0;
65  _version = version;
66  data = NULL;
67 }
68 
69 
70 /** Copy constructor.
71  * @param h header to copy
72  */
74 {
75  _data_size = h->_data_size;
76  _version = h->_version;
77  data = h->data;
78 }
79 
80 
81 /** Set SharedMemory instance.
82  * This is needed for address conversion and must be set right after the constructor
83  * call of SharedMemory!
84  * @param shmem SharedMemory segment used for this header
85  */
86 void
88 {
89  this->shmem = shmem;
90 }
91 
92 
93 /** Destructor */
95 {
96 }
97 
98 
99 
102 {
103  return new BlackBoardSharedMemoryHeader(this);
104 }
105 
106 
107 /** Check if the given shared memory segment is a Fawkes BB segment
108  * @param memptr Ptr to the segment
109  * @return true if the version matches, false otherwise
110  */
111 bool
113 {
114  BlackBoardSharedMemoryHeaderData *md = (BlackBoardSharedMemoryHeaderData *)memptr;
115  return (_version == md->version);
116 }
117 
118 
119 /** Check for equality of headers.
120  * First checks if passed SharedMemoryHeader is an instance of
121  * BlackBoardSharedMemoryHeader. If not returns false, otherwise it compares
122  * version, data size and data pointer. If all match returns true,
123  * false if any of them differs.
124  * @param s shared memory header to compare to
125  * @return true if the two instances identify the very same shared memory segments,
126  * false otherwise
127  */
128 bool
130 {
131  const BlackBoardSharedMemoryHeader *h = dynamic_cast<const BlackBoardSharedMemoryHeader *>(&s);
132  if ( ! h ) {
133  return false;
134  } else {
135  return ( (_version == h->_version) &&
136  (_data_size == h->_data_size) &&
137  (data == h->data) );
138  }
139 }
140 
141 /** Get the size of the header data.
142  * @return size of the header data
143  */
144 size_t
146 {
147  return sizeof(BlackBoardSharedMemoryHeaderData);
148 }
149 
150 
151 /** Initialize shared memory segment
152  * This copies basic management header data into the shared memory segment.
153  * Basically sets the version and list heads to NULL
154  * @param memptr pointer to the memory
155  */
156 void
158 {
159  data = (BlackBoardSharedMemoryHeaderData *)memptr;
160  data->version = _version;
161  data->shm_addr = memptr;
162  data->free_list_head = NULL;
163  data->alloc_list_head = NULL;
164 }
165 
166 
167 /** Set data of this header
168  * Sets the internal pointer to the shared memory header data
169  * to the data retrieved from the shared memory segment.
170  * @param memptr pointer to the memory
171  */
172 void
174 {
175  data = (BlackBoardSharedMemoryHeaderData *)memptr;
176 }
177 
178 
179 void
181 {
182  data = NULL;
183 }
184 
185 
186 /** Data segment size.
187  * @return size of the data segment without header
188  */
189 size_t
191 {
192  return _data_size;
193 }
194 
195 
196 /** Get the head of the free chunks list.
197  * @return pointer to the free list head, local pointer, already transformed,
198  * you can use this without further conversion.
199  */
200 chunk_list_t *
202 {
203  return (chunk_list_t *)shmem->ptr(data->free_list_head);
204 }
205 
206 
207 /** Get the head of the allocated chunks list.
208  * @return pointer to the allocated list head, local pointer, already transformed,
209  * you can use this without further conversion.
210  */
211 chunk_list_t *
213 {
214  return (chunk_list_t *)shmem->ptr(data->alloc_list_head);
215 }
216 
217 
218 /** Set the head of the free chunks list.
219  * @param flh pointer to the new free list head, must be a pointer to the local
220  * shared memory segment. Will be transformed to a shared memory address.
221  */
222 void
224 {
225  data->free_list_head = (chunk_list_t *)shmem->addr(flh);
226 }
227 
228 
229 /** Set the head of the allocated chunks list.
230  * @param alh pointer to the new allocated list head, must be a pointer to the local
231  * shared memory segment. Will be transformed to a shared memory address.
232  */
233 void
235 {
236  data->alloc_list_head = (chunk_list_t *)shmem->addr(alh);
237 }
238 
239 
240 /** Get BlackBoard version.
241  * @return BlackBoard version
242  */
243 unsigned int
245 {
246  return data->version;
247 }
248 
249 } // end namespace fawkes
virtual size_t size()
Get the size of the header data.
Definition: header.cpp:145
unsigned int version() const
Get BlackBoard version.
Definition: header.cpp:244
chunk_list_t * alloc_list_head()
Get the head of the allocated chunks list.
Definition: header.cpp:212
void set_shared_memory(SharedMemory *shmem)
Set SharedMemory instance.
Definition: header.cpp:87
virtual void set(void *memptr)
Set data of this header Sets the internal pointer to the shared memory header data to the data retrie...
Definition: header.cpp:173
Fawkes library namespace.
virtual ~BlackBoardSharedMemoryHeader()
Destructor.
Definition: header.cpp:94
virtual bool operator==(const fawkes::SharedMemoryHeader &s) const
Check for equality of headers.
Definition: header.cpp:129
chunk_list_t * free_list_head()
Get the head of the free chunks list.
Definition: header.cpp:201
void set_alloc_list_head(chunk_list_t *alh)
Set the head of the allocated chunks list.
Definition: header.cpp:234
virtual void reset()
Reset information previously set with set().
Definition: header.cpp:180
virtual SharedMemoryHeader * clone() const
Clone this shared memory header.
Definition: header.cpp:101
void * addr(void *ptr) const
Get an address from a real pointer.
Definition: shm.cpp:637
Chunk lists as stored in BlackBoard shared memory segment.
void set_free_list_head(chunk_list_t *flh)
Set the head of the free chunks list.
Definition: header.cpp:223
void * ptr(void *addr) const
Get the real pointer to the data based on an address.
Definition: shm.cpp:606
virtual size_t data_size()
Data segment size.
Definition: header.cpp:190
virtual bool matches(void *memptr)
Check if the given shared memory segment is a Fawkes BB segment.
Definition: header.cpp:112
Shared memory segment.
Definition: shm.h:49
BlackBoard Shared Memory Header.
Definition: header.h:34
virtual void initialize(void *memptr)
Initialize shared memory segment This copies basic management header data into the shared memory segm...
Definition: header.cpp:157
BlackBoardSharedMemoryHeader(unsigned int version)
Constructor.
Definition: header.cpp:62
Interface for shared memory header.
Definition: shm.h:33