Fawkes API Fawkes Development Version
memory_manager.h
1
2/***************************************************************************
3 * memory_manager.h - BlackBoard memory manager
4 *
5 * Created: Sat Sep 23 16:00:56 2006 (INSITE 2006, Joburg, South Africa)
6 * Copyright 2006-2009 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#ifndef _BLACKBOARD_MEMORY_MANAGER_H_
25#define _BLACKBOARD_MEMORY_MANAGER_H_
26
27#include <sys/types.h>
28
29namespace fawkes {
30
31class BlackBoardSharedMemoryHeader;
32class BlackBoardInterfaceManager;
33class BlackBoardMemoryManager;
34class SharedMemory;
35class Mutex;
36class SemaphoreSet;
37
38// define our own list type std::list is way too fat
39/** Chunk lists as stored in BlackBoard shared memory segment.
40 * The data segment of a chunk follows directly after the header. So if c is a chunk_list_t
41 * pointer to a chunk then the data segment of that chunk can be accessed via
42 * (char *)c + sizeof(chunk_list_t).
43 */
45{
46 chunk_list_t *next; /**< offset to next element in list */
47 void * ptr; /**< pointer to data memory */
48 unsigned int size; /**< total size of chunk, including overhanging bytes,
49 * excluding header */
50 unsigned int overhang; /**< number of overhanging bytes in this chunk */
51};
52
53// May be added later if we want/need per chunk semaphores
54// int semset_key; /* key of semaphore for this chunk */
55// unsigned int reserved :16;/* reserved bytes */
56// unsigned int semset_sem : 8;/* semaphore number in semaphore set */
57
59{
61
62public:
63 BlackBoardMemoryManager(size_t memsize);
64 BlackBoardMemoryManager(size_t memsize,
65 unsigned int version,
66 bool use_shmem,
67 const char * shmem_token = "FawkesBlackBoard");
69
70 void *alloc(unsigned int num_bytes);
71 void free(void *chunk_ptr);
72
73 void check();
74
75 bool is_master() const;
76
77 unsigned int max_free_size() const;
78 unsigned int max_allocated_size() const;
79
80 unsigned int free_size() const;
81 unsigned int allocated_size() const;
82 unsigned int overhang_size() const;
83
84 unsigned int num_free_chunks() const;
85 unsigned int num_allocated_chunks() const;
86
87 unsigned int memory_size() const;
88 unsigned int version() const;
89
90 void print_free_chunks_info() const;
91 void print_allocated_chunks_info() const;
92 void print_performance_info() const;
93
94 void lock();
95 bool try_lock();
96 void unlock();
97
98 /*
99 void lock(void *ptr);
100 bool try_lock(void *ptr);
101 void unlock(void *ptr);
102 */
103
105 {
107
108 private:
111
112 public:
114 ChunkIterator(const ChunkIterator &it);
115 ChunkIterator &operator++(); // prefix
116 ChunkIterator operator++(int inc); // postfix
117 ChunkIterator &operator+(unsigned int i);
118 ChunkIterator &operator+=(unsigned int i);
119 bool operator==(const ChunkIterator &c) const;
120 bool operator!=(const ChunkIterator &c) const;
121 void * operator*() const;
123
124 unsigned int size() const;
125 unsigned int overhang() const;
126
127 private:
128 SharedMemory *shmem_;
129 chunk_list_t *cur_;
130 };
131
134
135private:
136 chunk_list_t *list_add(chunk_list_t *list, chunk_list_t *addel);
137 chunk_list_t *list_remove(chunk_list_t *list, chunk_list_t *rmel);
138 chunk_list_t *list_find_ptr(chunk_list_t *list, void *ptr);
139 unsigned int list_length(const chunk_list_t *list) const;
140 chunk_list_t *list_get_biggest(const chunk_list_t *list) const;
141 chunk_list_t *list_next(const chunk_list_t *list) const;
142
143 void cleanup_free_chunks();
144
145 void list_print_info(const chunk_list_t *list) const;
146
147 void *alloc_nolock(unsigned int num_bytes);
148
149private:
150 bool master_;
151
152 size_t memsize_;
153
154 // Mutex to be used for all list operations (alloc, free)
155 Mutex *mutex_;
156
157 // used for shmem
158 BlackBoardSharedMemoryHeader *shmem_header_;
159 SharedMemory * shmem_;
160
161 // Used for heap memory
162 void * memory_;
163 chunk_list_t *free_list_head_; /**< offset of the free chunks list head */
164 chunk_list_t *alloc_list_head_; /**< offset of the allocated chunks list head */
165};
166
167} // end namespace fawkes
168
169#endif
BlackBoard interface manager.
unsigned int size() const
Get size of data segment.
bool operator!=(const ChunkIterator &c) const
Check inequality of two iterators.
bool operator==(const ChunkIterator &c) const
Check equality of two iterators.
ChunkIterator & operator=(const ChunkIterator &c)
Assign iterator.
unsigned int overhang() const
Get number of overhanging bytes.
ChunkIterator & operator++()
Increment iterator.
ChunkIterator & operator+=(unsigned int i)
Advance by a certain amount.
void * operator*() const
Get memory pointer of chunk.
ChunkIterator & operator+(unsigned int i)
Advance by a certain amount.
BlackBoard memory manager.
unsigned int overhang_size() const
Get number of overhanging bytes.
unsigned int num_free_chunks() const
Get number of free chunks.
void * alloc(unsigned int num_bytes)
Allocate memory.
void print_free_chunks_info() const
Print out info about free chunks.
void print_allocated_chunks_info() const
Print out info about allocated chunks.
unsigned int memory_size() const
Get size of memory.
void free(void *chunk_ptr)
Free a memory chunk.
unsigned int max_free_size() const
Get maximum allocatable memory size.
ChunkIterator end()
Get end of chunk list.
void check()
Check memory consistency.
bool try_lock()
Try to lock memory.
void print_performance_info() const
Prints out performance info.
ChunkIterator begin()
Get first element for chunk iteration.
unsigned int allocated_size() const
Get total allocated memory.
unsigned int free_size() const
Get total free memory.
unsigned int version() const
Get BlackBoard version.
BlackBoardMemoryManager(size_t memsize)
Heap Memory Constructor.
unsigned int num_allocated_chunks() const
Get number of allocated chunks.
unsigned int max_allocated_size() const
Get maximum alloced memory size.
bool is_master() const
Check if this BB memory manager is the master.
BlackBoard Shared Memory Header.
Definition: header.h:35
Mutex mutual exclusion lock.
Definition: mutex.h:33
Shared memory segment.
Definition: shm.h:53
Fawkes library namespace.
Chunk lists as stored in BlackBoard shared memory segment.
unsigned int size
total size of chunk, including overhanging bytes, excluding header
chunk_list_t * next
offset to next element in list
unsigned int overhang
number of overhanging bytes in this chunk
void * ptr
pointer to data memory