00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00044 #ifndef CCXX_MISC_H_
00045 #define CCXX_MISC_H_
00046
00047 #ifndef CCXX_MISSING_H_
00048 #include <cc++/missing.h>
00049 #endif
00050
00051 #ifndef CCXX_THREAD_H_
00052 #include <cc++/thread.h>
00053 #endif
00054
00055 #define KEYDATA_INDEX_SIZE 97
00056 #define KEYDATA_PAGER_SIZE 512
00057
00058 #if defined(PATH_MAX)
00059 #if PATH_MAX > 512
00060 #define KEYDATA_PATH_SIZE 512
00061 #else
00062 #define KEYDATA_PATH_SIZE PATH_MAX
00063 #endif
00064 #else
00065 #define KEYDATA_PATH_SIZE 256
00066 #endif
00067
00068 #ifdef CCXX_NAMESPACES
00069 namespace ost {
00070 #endif
00071
00072 class __EXPORT Runlist;
00073 class __EXPORT Runable;
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 void clean(void);
00168
00172 virtual ~MemPager();
00173
00174 public:
00181 inline int getPages(void)
00182 {return pages;};
00183 };
00184
00194 class __EXPORT StackPager : protected MemPager
00195 {
00196 private:
00197 typedef struct frame
00198 {
00199 struct frame *next;
00200 char data[1];
00201 } frame_t;
00202
00203 frame_t *stack;
00204
00205 public:
00211 StackPager(size_t pagesize);
00212
00220 void *push(const void *object, size_t size);
00221
00228 void *push(const char *string);
00229
00235 void *pull(void);
00236
00240 void purge(void);
00241 };
00242
00251 class __EXPORT SharedMemPager : public MemPager, public Mutex
00252 {
00253 protected:
00260 SharedMemPager(size_t pagesize = 4096, const char *name = NULL);
00261
00265 void purge(void);
00266
00273 void* first(size_t size);
00274
00281 void* alloc(size_t size);
00282 };
00283
00352 void endKeydata(void);
00353
00354 class __EXPORT Keydata : protected MemPager
00355 {
00356 public:
00357 #ifdef CCXX_PACKED
00358 #pragma pack(1)
00359 #endif
00360
00361 struct Keyval
00362 {
00363 Keyval *next;
00364 char val[1];
00365 };
00366
00367 struct Keysym
00368 {
00369 Keysym *next;
00370 Keyval *data;
00371 const char **list;
00372 short count;
00373 char sym[1];
00374 };
00375
00376 struct Define
00377 {
00378 char *keyword;
00379 char *value;
00380 };
00381
00382 #ifdef CCXX_PACKED
00383 #pragma pack()
00384 #endif
00385
00386 private:
00387 static std::ifstream *cfgFile;
00388 static char lastpath[KEYDATA_PATH_SIZE + 1];
00389 static int count;
00390 static int sequence;
00391
00392 int link;
00393
00394 Keysym *keys[KEYDATA_INDEX_SIZE];
00395
00402 unsigned getIndex(const char *sym);
00403
00404 protected:
00405 Keysym* getSymbol(const char *sym, bool create);
00406
00407 public:
00419 void load(const char *keypath);
00420
00434 void loadPrefix(const char *prefix, const char *keypath);
00435
00445 void loadFile(const char *filepath, const char *keys = NULL, const char *pre = NULL);
00446
00455 void load(Define *pairs);
00456
00460 Keydata();
00461
00469 Keydata(const char *keypath);
00470
00478 Keydata(Define *pairs, const char *keypath = NULL);
00479
00485 virtual ~Keydata();
00486
00494 void unlink(void);
00495
00504 int getCount(const char *sym);
00505
00513 const char* getFirst(const char *sym);
00514
00522 const char* getLast(const char *sym);
00523
00530 bool isKey(const char *sym);
00531
00539 const char *getString(const char *sym, const char *def = NULL);
00540
00548 long getLong(const char *sym, long def = 0);
00549
00556 bool getBool(const char *key);
00557
00565 double getDouble(const char *key, double def = 0.);
00566
00575 unsigned getIndex(char **data, unsigned max);
00576
00583 unsigned getCount(void);
00584
00593 void setValue(const char *sym, const char *data);
00594
00602 const char * const* getList(const char *sym);
00603
00610 void clrValue(const char *sym);
00611
00616 inline const char *operator[](const char *keyword)
00617 {return getLast(keyword);};
00618
00622 static void end(void);
00623
00628 friend inline void endKeydata(void)
00629 {Keydata::end();};
00630 };
00631
00639 class __EXPORT MemPagerObject
00640 {
00641 public:
00648 inline void *operator new(size_t size, MemPager &pager)
00649 {return pager.alloc(size);};
00650
00657 inline void *operator new[](size_t size, MemPager &pager)
00658 {return pager.alloc(size);};
00659
00663 inline void operator delete(void *) {};
00664
00668 inline void operator delete[](void *) {};
00669 };
00670
00679 class __EXPORT Assoc
00680 {
00681 private:
00682 class __EXPORT entry
00683 {
00684 public:
00685 const char *id;
00686 entry *next;
00687 void *data;
00688 };
00689
00690 entry *entries[KEYDATA_INDEX_SIZE];
00691
00692 protected:
00693 Assoc();
00694 virtual ~Assoc();
00695
00696 void clear(void);
00697
00698 virtual void *getMemory(size_t size) = 0;
00699
00700 public:
00701 void *getPointer(const char *id);
00702 void setPointer(const char *id, void *data);
00703 };
00704
00715 class __EXPORT Runlist : public Mutex
00716 {
00717 private:
00718 Runable *first, *last;
00719
00720 protected:
00721 unsigned limit, used;
00722 void check(void);
00723
00724 public:
00730 Runlist(unsigned count = 1);
00731
00740 bool add(Runable *run);
00741
00748 void del(Runable *run);
00749
00755 void set(unsigned limit);
00756 };
00757
00764 class __EXPORT Runable
00765 {
00766 private:
00767 friend class __EXPORT Runlist;
00768 Runlist *list;
00769 Runable *next, *prev;
00770
00771 protected:
00772 Runable();
00773 virtual ~Runable();
00774
00779 virtual void ready(void) = 0;
00780
00781 public:
00788 bool starting(Runlist *list);
00789
00795 void stoping(void);
00796 };
00797
00798 #ifdef CCXX_NAMESPACES
00799 }
00800 #endif
00801
00802 #endif
00803