Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages | Examples

misc.h

Go to the documentation of this file.
00001 // Copyright (C) 1999-2003 Open Source Telecom Corporation. 00002 // 00003 // This program is free software; you can redistribute it and/or modify 00004 // it under the terms of the GNU General Public License as published by 00005 // the Free Software Foundation; either version 2 of the License, or 00006 // (at your option) any later version. 00007 // 00008 // This program is distributed in the hope that it will be useful, 00009 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00011 // GNU General Public License for more details. 00012 // 00013 // You should have received a copy of the GNU General Public License 00014 // along with this program; if not, write to the Free Software 00015 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00016 // 00017 // As a special exception to the GNU General Public License, permission is 00018 // granted for additional uses of the text contained in its release 00019 // of Common C++. 00020 // 00021 // The exception is that, if you link the Common C++ library with other 00022 // files to produce an executable, this does not by itself cause the 00023 // resulting executable to be covered by the GNU General Public License. 00024 // Your use of that executable is in no way restricted on account of 00025 // linking the Common C++ library code into it. 00026 // 00027 // This exception does not however invalidate any other reasons why 00028 // the executable file might be covered by the GNU General Public License. 00029 // 00030 // This exception applies only to the code released under the 00031 // name Common C++. If you copy code from other releases into a copy of 00032 // Common C++, as the General Public License permits, the exception does 00033 // not apply to the code that you add in this way. To avoid misleading 00034 // anyone as to the status of such modified files, you must delete 00035 // this exception notice from them. 00036 // 00037 // If you write modifications of your own for Common C++, it is your choice 00038 // whether to permit this exception to apply to your modifications. 00039 // If you do not wish that, delete this exception notice. 00040 00047 #ifndef CCXX_MISC_H_ 00048 #define CCXX_MISC_H_ 00049 00050 #ifndef CCXX_MISSING_H_ 00051 #include <cc++/missing.h> 00052 #endif 00053 00054 #ifndef CCXX_THREAD_H_ 00055 #include <cc++/thread.h> 00056 #endif 00057 00058 #define KEYDATA_INDEX_SIZE 97 00059 #define KEYDATA_PAGER_SIZE 512 00060 00061 #if defined(PATH_MAX) 00062 #if PATH_MAX > 512 00063 #define KEYDATA_PATH_SIZE 512 00064 #else 00065 #define KEYDATA_PATH_SIZE PATH_MAX 00066 #endif 00067 #else 00068 #define KEYDATA_PATH_SIZE 256 00069 #endif 00070 00071 #ifdef CCXX_NAMESPACES 00072 namespace ost { 00073 #endif 00074 00090 class __EXPORT MemPager 00091 { 00092 private: 00093 friend class String; 00094 friend class MemPagerObject; 00095 00096 size_t pagesize; 00097 unsigned int pages; 00098 00099 struct _page 00100 { 00101 struct _page *next; 00102 size_t used; 00103 } *page; 00104 00105 protected: 00115 virtual void* first(size_t size); 00116 00124 virtual void* alloc(size_t size); 00125 00135 char* first(char *str); 00136 00146 char* alloc(const char *str); 00147 00157 MemPager(size_t pagesize = 4096); 00158 00162 void purge(void); 00163 00167 virtual ~MemPager(); 00168 00169 public: 00176 inline int getPages(void) 00177 {return pages;}; 00178 }; 00179 00189 class __EXPORT StackPager : protected MemPager 00190 { 00191 private: 00192 typedef struct frame 00193 { 00194 struct frame *next; 00195 char data[1]; 00196 } frame_t; 00197 00198 frame_t *stack; 00199 00200 public: 00206 StackPager(size_t pagesize); 00207 00215 void *push(const void *object, size_t len); 00216 00223 void *push(const char *string); 00224 00230 void *pull(void); 00231 00235 void purge(void); 00236 }; 00237 00246 class __EXPORT SharedMemPager : public MemPager, public Mutex 00247 { 00248 protected: 00255 SharedMemPager(size_t pagesize = 4096, const char *name = NULL); 00256 00260 void purge(void); 00261 00268 void* first(size_t size); 00269 00276 void* alloc(size_t size); 00277 }; 00278 00346 class __EXPORT Keydata : protected MemPager 00347 { 00348 public: 00349 #if defined(__GNUC__) || !defined(__hpux) 00350 #pragma pack(1) 00351 #endif 00352 00353 struct Keyval 00354 { 00355 Keyval *next; 00356 char val[1]; 00357 }; 00358 00359 struct Keysym 00360 { 00361 Keysym *next; 00362 Keyval *data; 00363 const char **list; 00364 short count; 00365 char sym[1]; 00366 }; 00367 00368 struct Define 00369 { 00370 char *keyword; 00371 char *value; 00372 }; 00373 00374 #if defined(__GNUC__) || !defined(__hpux) 00375 #pragma pack() 00376 #endif 00377 00378 private: 00379 static std::ifstream *cfgFile; 00380 static char lastpath[KEYDATA_PATH_SIZE + 1]; 00381 static int count; 00382 static int sequence; 00383 00384 int link; 00385 00386 Keysym *keys[KEYDATA_INDEX_SIZE]; 00387 00394 unsigned getIndex(const char *sym); 00395 00396 protected: 00397 Keysym* getSymbol(const char *sym, bool create); 00398 00399 public: 00412 void load(const char *keypath, 00413 const char *environment = "CONFIG_KEYDATA"); 00414 00430 void loadPrefix(const char *prefix, 00431 const char *keypath, const char *environment = "CONFIG_KEYDATA"); 00432 00442 void loadFile(const char *filepath, const char *keys = NULL, const char *pre = NULL); 00443 00452 void load(Define *pairs); 00453 00457 Keydata(); 00458 00467 Keydata(const char *keypath, const char *environment="CONFIG_KEYDATA"); 00468 00474 virtual ~Keydata(); 00475 00483 void unlink(void); 00484 00493 int getCount(const char *sym); 00494 00502 const char* getFirst(const char *sym); 00503 00511 const char* getLast(const char *sym); 00512 00521 unsigned getIndex(char **data, unsigned max); 00522 00529 unsigned getCount(void); 00530 00539 void setValue(const char *sym, const char *data); 00540 00548 const char * const* getList(const char *sym); 00549 00556 void clrValue(const char *sym); 00557 00562 inline const char *operator[](const char *keyword) 00563 {return getLast(keyword);}; 00564 00568 static void end(void); 00569 00574 friend inline void endKeydata(void) 00575 {Keydata::end();}; 00576 }; 00577 00585 class __EXPORT MemPagerObject 00586 { 00587 public: 00594 inline void *operator new(size_t size, MemPager &pager) 00595 {return pager.alloc(size);}; 00596 00603 inline void *operator new[](size_t size, MemPager &pager) 00604 {return pager.alloc(size);}; 00605 00609 inline void operator delete(void *) {}; 00610 00614 inline void operator delete[](void *) {}; 00615 }; 00616 00617 #ifdef CCXX_NAMESPACES 00618 } 00619 #endif 00620 00621 #endif 00622

Generated on Fri Jan 21 13:36:02 2005 for GNU CommonC++ by doxygen 1.3.8