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 
29 namespace fawkes {
30 
31 class BlackBoardSharedMemoryHeader;
32 class BlackBoardInterfaceManager;
33 class BlackBoardMemoryManager;
34 class SharedMemory;
35 class Mutex;
36 class 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 
62 public:
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:
113  ChunkIterator();
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 
133  ChunkIterator end();
134 
135 private:
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 
149 private:
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
void print_performance_info() const
Prints out performance info.
unsigned int size() const
Get size of data segment.
ChunkIterator begin()
Get first element for chunk iteration.
unsigned int allocated_size() const
Get total allocated memory.
bool is_master() const
Check if this BB memory manager is the master.
void print_allocated_chunks_info() const
Print out info about allocated chunks.
Fawkes library namespace.
ChunkIterator end()
Get end of chunk list.
BlackBoard memory manager.
unsigned int memory_size() const
Get size of memory.
chunk_list_t * next
offset to next element in list
void * alloc(unsigned int num_bytes)
Allocate memory.
unsigned int size
total size of chunk, including overhanging bytes, excluding header
bool try_lock()
Try to lock memory.
Chunk lists as stored in BlackBoard shared memory segment.
unsigned int overhang
number of overhanging bytes in this chunk
BlackBoardMemoryManager(size_t memsize)
Heap Memory Constructor.
ChunkIterator & operator++()
Increment iterator.
unsigned int version() const
Get BlackBoard version.
ChunkIterator & operator+=(unsigned int i)
Advance by a certain amount.
void print_free_chunks_info() const
Print out info about free chunks.
unsigned int num_free_chunks() const
Get number of free chunks.
void * ptr
pointer to data memory
bool operator!=(const ChunkIterator &c) const
Check inequality of two iterators.
ChunkIterator & operator+(unsigned int i)
Advance by a certain amount.
void * operator*() const
Get memory pointer of chunk.
unsigned int free_size() const
Get total free memory.
Shared memory segment.
Definition: shm.h:52
unsigned int overhang() const
Get number of overhanging bytes.
BlackBoard Shared Memory Header.
Definition: header.h:34
void check()
Check memory consistency.
unsigned int max_allocated_size() const
Get maximum alloced memory size.
void free(void *chunk_ptr)
Free a memory chunk.
bool operator==(const ChunkIterator &c) const
Check equality of two iterators.
Mutex mutual exclusion lock.
Definition: mutex.h:32
unsigned int num_allocated_chunks() const
Get number of allocated chunks.
unsigned int max_free_size() const
Get maximum allocatable memory size.
BlackBoard interface manager.
ChunkIterator & operator=(const ChunkIterator &c)
Assign iterator.
unsigned int overhang_size() const
Get number of overhanging bytes.