ucommon
misc.h
Go to the documentation of this file.
1 // Copyright (C) 2001-2005 Open Source Telecom Corporation.
2 // Copyright (C) 2006-2014 David Sugar, Tycho Softworks.
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU Lesser General Public License
15 // along with this program. If not, see <http://www.gnu.org/licenses/>.
16 //
17 // As a special exception, you may use this file as part of a free software
18 // library without restriction. Specifically, if other files instantiate
19 // templates or use macros or inline functions from this file, or you compile
20 // this file and link it with other files to produce an executable, this
21 // file does not by itself cause the resulting executable to be covered by
22 // the GNU General Public License. This exception does not however
23 // invalidate any other reasons why the executable file might be covered by
24 // the GNU General Public License.
25 //
26 // This exception applies only to the code released under the name GNU
27 // Common C++. If you copy code from other releases into a copy of GNU
28 // Common C++, as the General Public License permits, the exception does
29 // not apply to the code that you add in this way. To avoid misleading
30 // anyone as to the status of such modified files, you must delete
31 // this exception notice from them.
32 //
33 // If you write modifications of your own for GNU Common C++, it is your choice
34 // whether to permit this exception to apply to your modifications.
35 // If you do not wish that, delete this exception notice.
36 //
37 
43 #ifndef COMMONCPP_MISC_H_
44 #define COMMONCPP_MISC_H_
45 
46 #ifndef COMMONCPP_CONFIG_H_
47 #include <commoncpp/config.h>
48 #endif
49 
50 #define KEYDATA_INDEX_SIZE 97
51 #define KEYDATA_PAGER_SIZE 512
52 #if defined(PATH_MAX)
53 #if PATH_MAX > 512
54 #define KEYDATA_PATH_SIZE 512
55 #else
56 #define KEYDATA_PATH_SIZE PATH_MAX
57 #endif
58 #else
59 #define KEYDATA_PATH_SIZE 256
60 #endif
61 
62 NAMESPACE_COMMONCPP
63 
64 class __EXPORT MemPager : private ucommon::memalloc
65 {
66 public:
67  inline MemPager(size_t pagesize = 4096) : ucommon::memalloc(pagesize) {};
68 
69  inline void *alloc(size_t size)
70  {return _alloc(size);}
71 
72  char *alloc(const char *str);
73 
74  inline char *first(const char *str)
75  {return alloc(str);}
76 
77  inline void *first(size_t size)
78  {return _alloc(size);}
79 
80  inline int getPages(void)
81  {return pages();}
82 
83  inline void purge(void)
84  {memalloc::purge();}
85 };
86 
95 class __EXPORT SharedMemPager : public MemPager, public Mutex
96 {
97 protected:
104  SharedMemPager(size_t pagesize = 4096);
108  void purge(void);
109 
116  void* alloc(size_t size);
117 
118  inline void *first(size_t size)
119  {return alloc(size);}
120 };
121 
122 
131 class __EXPORT Assoc
132 {
133 private:
134  struct entry {
135  const char *id;
136  entry *next;
137  void *data;
138  };
139 
140  entry *entries[KEYDATA_INDEX_SIZE];
141 
142 protected:
143  Assoc();
144  virtual ~Assoc();
145 
146  void clear(void);
147 
148  virtual void *getMemory(size_t size) = 0;
149 
150 public:
151  void *getPointer(const char *id) const;
152  void setPointer(const char *id, void *data);
153 };
154 
155 END_NAMESPACE
156 #endif
Common namespace for all ucommon objects.
Definition: access.h:46
This class is used to associate (object) pointers with named strings.
Definition: misc.h:131
unsigned pages(void) const
Get the number of pages that have been allocated from the real heap.
Definition: memory.h:108
void purge(void)
Purge all allocated memory and heap pages immediately.
virtual void * _alloc(size_t size)
Allocate memory from the pager heap.
A memory protocol pager for private heap manager.
Definition: memory.h:60
The shared mempager uses a mutex to protect key access methods.
Definition: misc.h:95