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 
27 #include <cstddef>
28 
29 namespace fawkes {
30 
31 /** @class BlackBoardSharedMemoryHeader <blackboard/shmem/header.h>
32  * BlackBoard Shared Memory Header.
33  * This class is used identify BlackBoard shared memory headers and
34  * to interact with the management data in the shared memory segment.
35  * The basic options stored in the header is a version identifier
36  * and pointers to the list heads of the free and allocated chunk
37  * lists.
38  *
39  * @author Tim Niemueller
40  * @see SharedMemoryHeader
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  */
49 BlackBoardSharedMemoryHeader::BlackBoardSharedMemoryHeader(size_t data_size, unsigned int version)
50 {
51  _data_size = data_size;
52  _version = version;
53  data = NULL;
54 }
55 
56 /** Constructor.
57  * @param version The BB version to store in the shared memory segment to prevent
58  * conflicts with older software.
59  */
61 {
62  _data_size = 0;
63  _version = version;
64  data = NULL;
65 }
66 
67 /** Copy constructor.
68  * @param h header to copy
69  */
71 {
72  _data_size = h->_data_size;
73  _version = h->_version;
74  data = h->data;
75 }
76 
77 /** Set SharedMemory instance.
78  * This is needed for address conversion and must be set right after the constructor
79  * call of SharedMemory!
80  * @param shmem SharedMemory segment used for this header
81  */
82 void
84 {
85  this->shmem = shmem;
86 }
87 
88 /** Destructor */
90 {
91 }
92 
95 {
96  return new BlackBoardSharedMemoryHeader(this);
97 }
98 
99 /** Check if the given shared memory segment is a Fawkes BB segment
100  * @param memptr Ptr to the segment
101  * @return true if the version matches, false otherwise
102  */
103 bool
105 {
106  BlackBoardSharedMemoryHeaderData *md = (BlackBoardSharedMemoryHeaderData *)memptr;
107  return (_version == md->version);
108 }
109 
110 /** Check for equality of headers.
111  * First checks if passed SharedMemoryHeader is an instance of
112  * BlackBoardSharedMemoryHeader. If not returns false, otherwise it compares
113  * version, data size and data pointer. If all match returns true,
114  * false if any of them differs.
115  * @param s shared memory header to compare to
116  * @return true if the two instances identify the very same shared memory segments,
117  * false otherwise
118  */
119 bool
121 {
122  const BlackBoardSharedMemoryHeader *h = dynamic_cast<const BlackBoardSharedMemoryHeader *>(&s);
123  if (!h) {
124  return false;
125  } else {
126  return ((_version == h->_version) && (_data_size == h->_data_size) && (data == h->data));
127  }
128 }
129 
130 /** Get the size of the header data.
131  * @return size of the header data
132  */
133 size_t
135 {
136  return sizeof(BlackBoardSharedMemoryHeaderData);
137 }
138 
139 /** Initialize shared memory segment
140  * This copies basic management header data into the shared memory segment.
141  * Basically sets the version and list heads to NULL
142  * @param memptr pointer to the memory
143  */
144 void
146 {
147  data = (BlackBoardSharedMemoryHeaderData *)memptr;
148  data->version = _version;
149  data->shm_addr = memptr;
150  data->free_list_head = NULL;
151  data->alloc_list_head = NULL;
152 }
153 
154 /** Set data of this header
155  * Sets the internal pointer to the shared memory header data
156  * to the data retrieved from the shared memory segment.
157  * @param memptr pointer to the memory
158  */
159 void
161 {
162  data = (BlackBoardSharedMemoryHeaderData *)memptr;
163 }
164 
165 void
167 {
168  data = NULL;
169 }
170 
171 /** Data segment size.
172  * @return size of the data segment without header
173  */
174 size_t
176 {
177  return _data_size;
178 }
179 
180 /** Get the head of the free chunks list.
181  * @return pointer to the free list head, local pointer, already transformed,
182  * you can use this without further conversion.
183  */
184 chunk_list_t *
186 {
187  return (chunk_list_t *)shmem->ptr(data->free_list_head);
188 }
189 
190 /** Get the head of the allocated chunks list.
191  * @return pointer to the allocated list head, local pointer, already transformed,
192  * you can use this without further conversion.
193  */
194 chunk_list_t *
196 {
197  return (chunk_list_t *)shmem->ptr(data->alloc_list_head);
198 }
199 
200 /** Set the head of the free chunks list.
201  * @param flh pointer to the new free list head, must be a pointer to the local
202  * shared memory segment. Will be transformed to a shared memory address.
203  */
204 void
206 {
207  data->free_list_head = (chunk_list_t *)shmem->addr(flh);
208 }
209 
210 /** Set the head of the allocated chunks list.
211  * @param alh pointer to the new allocated list head, must be a pointer to the local
212  * shared memory segment. Will be transformed to a shared memory address.
213  */
214 void
216 {
217  data->alloc_list_head = (chunk_list_t *)shmem->addr(alh);
218 }
219 
220 /** Get BlackBoard version.
221  * @return BlackBoard version
222  */
223 unsigned int
225 {
226  return data->version;
227 }
228 
229 } // end namespace fawkes
virtual size_t size()
Get the size of the header data.
Definition: header.cpp:134
unsigned int version() const
Get BlackBoard version.
Definition: header.cpp:224
chunk_list_t * alloc_list_head()
Get the head of the allocated chunks list.
Definition: header.cpp:195
void set_shared_memory(SharedMemory *shmem)
Set SharedMemory instance.
Definition: header.cpp:83
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:160
Fawkes library namespace.
virtual ~BlackBoardSharedMemoryHeader()
Destructor.
Definition: header.cpp:89
virtual bool operator==(const fawkes::SharedMemoryHeader &s) const
Check for equality of headers.
Definition: header.cpp:120
chunk_list_t * free_list_head()
Get the head of the free chunks list.
Definition: header.cpp:185
void set_alloc_list_head(chunk_list_t *alh)
Set the head of the allocated chunks list.
Definition: header.cpp:215
virtual void reset()
Reset information previously set with set().
Definition: header.cpp:166
virtual SharedMemoryHeader * clone() const
Clone this shared memory header.
Definition: header.cpp:94
void * addr(void *ptr) const
Get an address from a real pointer.
Definition: shm.cpp:690
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:205
void * ptr(void *addr) const
Get the real pointer to the data based on an address.
Definition: shm.cpp:659
virtual size_t data_size()
Data segment size.
Definition: header.cpp:175
virtual bool matches(void *memptr)
Check if the given shared memory segment is a Fawkes BB segment.
Definition: header.cpp:104
Shared memory segment.
Definition: shm.h:52
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:145
BlackBoardSharedMemoryHeader(unsigned int version)
Constructor.
Definition: header.cpp:60
Interface for shared memory header.
Definition: shm.h:33