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 SharedMemory;
33 class Mutex;
34 class SemaphoreSet;
35 
36 // define our own list type std::list is way too fat
37 /** Chunk lists as stored in BlackBoard shared memory segment.
38  * The data segment of a chunk follows directly after the header. So if c is a chunk_list_t
39  * pointer to a chunk then the data segment of that chunk can be accessed via
40  * (char *)c + sizeof(chunk_list_t).
41  */
42 struct chunk_list_t {
43  chunk_list_t *next; /**< offset to next element in list */
44  void *ptr; /**< pointer to data memory */
45  unsigned int size; /**< total size of chunk, including overhanging bytes,
46  * excluding header */
47  unsigned int overhang; /**< number of overhanging bytes in this chunk */
48 };
49 
50 // May be added later if we want/need per chunk semaphores
51 // int semset_key; /* key of semaphore for this chunk */
52 // unsigned int reserved :16;/* reserved bytes */
53 // unsigned int semset_sem : 8;/* semaphore number in semaphore set */
54 
55 
57 {
58  friend class BlackBoardInterfaceManager;
59  public:
60  BlackBoardMemoryManager(size_t memsize);
61  BlackBoardMemoryManager(size_t memsize, unsigned int version,
62  bool use_shmem,
63  const char *shmem_token = "FawkesBlackBoard");
65 
66  void * alloc(unsigned int num_bytes);
67  void free(void *chunk_ptr);
68 
69  void check();
70 
71  bool is_master() const;
72 
73  unsigned int max_free_size() const;
74  unsigned int max_allocated_size() const;
75 
76  unsigned int free_size() const;
77  unsigned int allocated_size() const;
78  unsigned int overhang_size() const;
79 
80  unsigned int num_free_chunks() const;
81  unsigned int num_allocated_chunks() const;
82 
83  unsigned int memory_size() const;
84  unsigned int version() const;
85 
86  void print_free_chunks_info() const;
87  void print_allocated_chunks_info() const;
88  void print_performance_info() const;
89 
90  void lock();
91  bool try_lock();
92  void unlock();
93 
94  /*
95  void lock(void *ptr);
96  bool try_lock(void *ptr);
97  void unlock(void *ptr);
98  */
99 
101  {
102  friend class BlackBoardMemoryManager;
103  private:
106  public:
107  ChunkIterator();
108  ChunkIterator(const ChunkIterator &it);
109  ChunkIterator & operator++ (); // prefix
110  ChunkIterator operator++ (int inc); // postfix
111  ChunkIterator & operator+ (unsigned int i);
112  ChunkIterator & operator+= (unsigned int i);
113  bool operator== (const ChunkIterator & c) const;
114  bool operator!= (const ChunkIterator & c) const;
115  void * operator* () const;
116  ChunkIterator & operator= (const ChunkIterator & c);
117 
118  unsigned int size() const;
119  unsigned int overhang() const;
120 
121  private:
122  SharedMemory *__shmem;
123  chunk_list_t *__cur;
124  };
125 
126  ChunkIterator begin();
127  ChunkIterator end();
128 
129  private:
130  chunk_list_t * list_add(chunk_list_t *list, chunk_list_t *addel);
131  chunk_list_t * list_remove(chunk_list_t *list, chunk_list_t *rmel);
132  chunk_list_t * list_find_ptr(chunk_list_t *list, void *ptr);
133  unsigned int list_length(const chunk_list_t *list) const;
134  chunk_list_t * list_get_biggest(const chunk_list_t *list) const;
135  chunk_list_t * list_next(const chunk_list_t *list) const;
136 
137  void cleanup_free_chunks();
138 
139  void list_print_info(const chunk_list_t *list) const;
140 
141  void * alloc_nolock(unsigned int num_bytes);
142 
143  private:
144  bool __master;
145 
146  size_t __memsize;
147 
148  // Mutex to be used for all list operations (alloc, free)
149  Mutex *__mutex;
150 
151  // used for shmem
152  BlackBoardSharedMemoryHeader *__shmem_header;
153  SharedMemory *__shmem;
154 
155  // Used for heap memory
156  void *__memory;
157  chunk_list_t *__free_list_head; /**< offset of the free chunks list head */
158  chunk_list_t *__alloc_list_head; /**< offset of the allocated chunks list head */
159 
160 };
161 
162 } // end namespace fawkes
163 
164 #endif
Fawkes library namespace.
BlackBoard memory manager.
chunk_list_t * next
offset to next element in list
unsigned int size
total size of chunk, including overhanging bytes, excluding header
Chunk lists as stored in BlackBoard shared memory segment.
unsigned int overhang
number of overhanging bytes in this chunk
void * ptr
pointer to data memory
Shared memory segment.
Definition: shm.h:49
BlackBoard Shared Memory Header.
Definition: header.h:34
Mutex mutual exclusion lock.
Definition: mutex.h:32
BlackBoard interface manager.