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