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