ucommon
|
00001 // Copyright (C) 2001-2005 Open Source Telecom Corporation. 00002 // Copyright (C) 2006-2014 David Sugar, Tycho Softworks. 00003 // 00004 // This program is free software; you can redistribute it and/or modify 00005 // it under the terms of the GNU General Public License as published by 00006 // the Free Software Foundation; either version 2 of the License, or 00007 // (at your option) any later version. 00008 // 00009 // This program is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU Lesser General Public License 00015 // along with this program. If not, see <http://www.gnu.org/licenses/>. 00016 // 00017 // As a special exception, you may use this file as part of a free software 00018 // library without restriction. Specifically, if other files instantiate 00019 // templates or use macros or inline functions from this file, or you compile 00020 // this file and link it with other files to produce an executable, this 00021 // file does not by itself cause the resulting executable to be covered by 00022 // the GNU General Public License. This exception does not however 00023 // invalidate any other reasons why the executable file might be covered by 00024 // the GNU General Public License. 00025 // 00026 // This exception applies only to the code released under the name GNU 00027 // Common C++. If you copy code from other releases into a copy of GNU 00028 // Common C++, as the General Public License permits, the exception does 00029 // not apply to the code that you add in this way. To avoid misleading 00030 // anyone as to the status of such modified files, you must delete 00031 // this exception notice from them. 00032 // 00033 // If you write modifications of your own for GNU Common C++, it is your choice 00034 // whether to permit this exception to apply to your modifications. 00035 // If you do not wish that, delete this exception notice. 00036 // 00037 00043 #ifndef COMMONCPP_MISC_H_ 00044 #define COMMONCPP_MISC_H_ 00045 00046 #ifndef COMMONCPP_CONFIG_H_ 00047 #include <commoncpp/config.h> 00048 #endif 00049 00050 #define KEYDATA_INDEX_SIZE 97 00051 #define KEYDATA_PAGER_SIZE 512 00052 #if defined(PATH_MAX) 00053 #if PATH_MAX > 512 00054 #define KEYDATA_PATH_SIZE 512 00055 #else 00056 #define KEYDATA_PATH_SIZE PATH_MAX 00057 #endif 00058 #else 00059 #define KEYDATA_PATH_SIZE 256 00060 #endif 00061 00062 namespace ost { 00063 00064 class __EXPORT MemPager : private ucommon::memalloc 00065 { 00066 public: 00067 inline MemPager(size_t pagesize = 4096) : ucommon::memalloc(pagesize) {} 00068 00069 inline void *alloc(size_t size) 00070 {return _alloc(size);} 00071 00072 char *alloc(const char *str); 00073 00074 inline char *first(const char *str) 00075 {return alloc(str);} 00076 00077 inline void *first(size_t size) 00078 {return _alloc(size);} 00079 00080 inline int getPages(void) 00081 {return pages();} 00082 00083 inline void purge(void) 00084 {memalloc::purge();} 00085 }; 00086 00095 class __EXPORT SharedMemPager : public MemPager, public Mutex 00096 { 00097 protected: 00104 SharedMemPager(size_t pagesize = 4096); 00108 void purge(void); 00109 00116 void* alloc(size_t size); 00117 00118 inline void *first(size_t size) 00119 {return alloc(size);} 00120 }; 00121 00122 00131 class __EXPORT Assoc 00132 { 00133 private: 00134 struct entry { 00135 const char *id; 00136 entry *next; 00137 void *data; 00138 }; 00139 00140 entry *entries[KEYDATA_INDEX_SIZE]; 00141 00142 protected: 00143 Assoc(); 00144 virtual ~Assoc(); 00145 00146 void clear(void); 00147 00148 virtual void *getMemory(size_t size) = 0; 00149 00150 public: 00151 void *getPointer(const char *id) const; 00152 void setPointer(const char *id, void *data); 00153 }; 00154 00155 } // namespace ost 00156 #endif