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 struct _page *next;
00101 size_t used;
00102 } *page;
00103
00104 protected:
00114 virtual void* first(size_t size);
00115
00123 virtual void* alloc(size_t size);
00124
00134 char* first(char *str);
00135
00145 char* alloc(const char *str);
00146
00156 MemPager(size_t pagesize = 4096);
00157
00161 void purge(void);
00162
00166 void clean(void);
00167
00171 virtual ~MemPager();
00172
00173 public:
00180 inline int getPages(void)
00181 {return pages;};
00182 };
00183
00193 class __EXPORT StackPager : protected MemPager
00194 {
00195 private:
00196 typedef struct frame {
00197 struct frame *next;
00198 char data[1];
00199 } frame_t;
00200
00201 frame_t *stack;
00202
00203 public:
00209 StackPager(size_t pagesize);
00210
00218 void *push(const void *object, size_t size);
00219
00226 void *push(const char *string);
00227
00233 void *pull(void);
00234
00238 void purge(void);
00239 };
00240
00249 class __EXPORT SharedMemPager : public MemPager, public Mutex
00250 {
00251 protected:
00258 SharedMemPager(size_t pagesize = 4096, const char *name = NULL);
00259
00263 void purge(void);
00264
00271 void* first(size_t size);
00272
00279 void* alloc(size_t size);
00280 };
00281
00282 __EXPORT void endKeydata(void);
00283
00351 class __EXPORT Keydata : protected MemPager
00352 {
00353 public:
00354 #ifdef CCXX_PACKED
00355 #pragma pack(1)
00356 #endif
00357
00358 struct Keyval {
00359 Keyval *next;
00360 char val[1];
00361 };
00362
00363 struct Keysym {
00364 Keysym *next;
00365 Keyval *data;
00366 const char **list;
00367 short count;
00368 char sym[1];
00369 };
00370
00371 struct Define {
00372 char *keyword;
00373 char *value;
00374 };
00375
00376 #ifdef CCXX_PACKED
00377 #pragma pack()
00378 #endif
00379
00380 private:
00381 static std::ifstream *cfgFile;
00382 static char lastpath[KEYDATA_PATH_SIZE + 1];
00383 static int count;
00384 static int sequence;
00385
00386 int link;
00387
00388 Keysym *keys[KEYDATA_INDEX_SIZE];
00389
00396 unsigned getIndex(const char *sym);
00397
00398 protected:
00399 Keysym* getSymbol(const char *sym, bool create);
00400
00401 public:
00413 void load(const char *keypath);
00414
00428 void loadPrefix(const char *prefix, const char *keypath);
00429
00439 void loadFile(const char *filepath, const char *keys = NULL, const char *pre = NULL);
00440
00449 void load(Define *pairs);
00450
00454 Keydata();
00455
00463 Keydata(const char *keypath);
00464
00472 Keydata(Define *pairs, const char *keypath = NULL);
00473
00479 virtual ~Keydata();
00480
00488 void unlink(void);
00489
00498 int getCount(const char *sym);
00499
00507 const char* getFirst(const char *sym);
00508
00516 const char* getLast(const char *sym);
00517
00524 bool isKey(const char *sym);
00525
00533 const char *getString(const char *sym, const char *def = NULL);
00534
00542 long getLong(const char *sym, long def = 0);
00543
00550 bool getBool(const char *key);
00551
00559 double getDouble(const char *key, double def = 0.);
00560
00569 unsigned getIndex(char **data, unsigned max);
00570
00577 unsigned getCount(void);
00578
00587 void setValue(const char *sym, const char *data);
00588
00596 const char * const* getList(const char *sym);
00597
00604 void clrValue(const char *sym);
00605
00610 inline const char *operator[](const char *keyword)
00611 {return getLast(keyword);};
00612
00616 static void end(void);
00617
00622 friend inline void endKeydata(void)
00623 {Keydata::end();};
00624 };
00625
00633 class __EXPORT MemPagerObject
00634 {
00635 public:
00642 inline void *operator new(size_t size, MemPager &pager)
00643 {return pager.alloc(size);};
00644
00651 inline void *operator new[](size_t size, MemPager &pager)
00652 {return pager.alloc(size);};
00653
00657 inline void operator delete(void *) {};
00658
00662 inline void operator delete[](void *) {};
00663 };
00664
00673 class __EXPORT Assoc
00674 {
00675 private:
00676 struct entry {
00677 const char *id;
00678 entry *next;
00679 void *data;
00680 };
00681
00682 entry *entries[KEYDATA_INDEX_SIZE];
00683
00684 protected:
00685 Assoc();
00686 virtual ~Assoc();
00687
00688 void clear(void);
00689
00690 virtual void *getMemory(size_t size) = 0;
00691
00692 public:
00693 void *getPointer(const char *id) const;
00694 void setPointer(const char *id, void *data);
00695 };
00696
00707 class __EXPORT Runlist : public Mutex
00708 {
00709 private:
00710 Runable *first, *last;
00711
00712 protected:
00713 unsigned limit, used;
00714 void check(void);
00715
00716 public:
00722 Runlist(unsigned count = 1);
00723
00732 bool add(Runable *run);
00733
00740 void del(Runable *run);
00741
00747 void set(unsigned limit);
00748 };
00749
00756 class __EXPORT Runable
00757 {
00758 private:
00759 friend class Runlist;
00760 Runlist *list;
00761 Runable *next, *prev;
00762
00763 protected:
00764 Runable();
00765 virtual ~Runable();
00766
00771 virtual void ready(void) = 0;
00772
00773 public:
00780 bool starting(Runlist *list);
00781
00787 void stoping(void);
00788 };
00789
00790 #ifdef CCXX_NAMESPACES
00791 }
00792 #endif
00793
00794 #endif
00795