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 : private ucommon::memalloc
66 {
67 public:
68  inline MemPager(size_t pagesize = 4096) : ucommon::memalloc(pagesize) {}
69 
70  inline void *alloc(size_t size)
71  {return _alloc(size);}
72 
73  char *alloc(const char *str);
74 
75  inline char *first(const char *str)
76  {return alloc(str);}
77 
78  inline void *first(size_t size)
79  {return _alloc(size);}
80 
81  inline int getPages(void)
82  {return pages();}
83 
84  inline void purge(void)
85  {memalloc::purge();}
86 };
87 
96 class __EXPORT SharedMemPager : public MemPager, public Mutex
97 {
98 protected:
105  SharedMemPager(size_t pagesize = 4096);
109  void purge(void);
110 
117  void* alloc(size_t size);
118 
119  inline void *first(size_t size)
120  {return alloc(size);}
121 };
122 
123 
132 class __EXPORT Assoc
133 {
134 private:
135  struct entry {
136  const char *id;
137  entry *next;
138  void *data;
139  };
140 
141  entry *entries[KEYDATA_INDEX_SIZE];
142 
143 protected:
144  Assoc();
145  virtual ~Assoc();
146 
147  void clear(void);
148 
149  virtual void *getMemory(size_t size) = 0;
150 
151 public:
152  void *getPointer(const char *id) const;
153  void setPointer(const char *id, void *data);
154 };
155 
156 } // namespace ost
157 #endif
This class is used to associate (object) pointers with named strings.
Definition: misc.h:132
Definition: address.h:59
Common namespace for all ucommon objects.
Definition: access.h:47
A memory protocol pager for private heap manager.
Definition: memory.h:61
The shared mempager uses a mutex to protect key access methods.
Definition: misc.h:96