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
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 Keyval *next;
00363 char val[1];
00364 };
00365
00366 struct Keysym {
00367 Keysym *next;
00368 Keyval *data;
00369 const char **list;
00370 short count;
00371 char sym[1];
00372 };
00373
00374 struct Define {
00375 char *keyword;
00376 char *value;
00377 };
00378
00379 #ifdef CCXX_PACKED
00380 #pragma pack()
00381 #endif
00382
00383 private:
00384 static std::ifstream *cfgFile;
00385 static char lastpath[KEYDATA_PATH_SIZE + 1];
00386 static int count;
00387 static int sequence;
00388
00389 int link;
00390
00391 Keysym *keys[KEYDATA_INDEX_SIZE];
00392
00399 unsigned getIndex(const char *sym);
00400
00401 protected:
00402 Keysym* getSymbol(const char *sym, bool create);
00403
00404 public:
00416 void load(const char *keypath);
00417
00431 void loadPrefix(const char *prefix, const char *keypath);
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
00466 Keydata(const char *keypath);
00467
00475 Keydata(Define *pairs, const char *keypath = NULL);
00476
00482 virtual ~Keydata();
00483
00491 void unlink(void);
00492
00501 int getCount(const char *sym);
00502
00510 const char* getFirst(const char *sym);
00511
00519 const char* getLast(const char *sym);
00520
00527 bool isKey(const char *sym);
00528
00536 const char *getString(const char *sym, const char *def = NULL);
00537
00545 long getLong(const char *sym, long def = 0);
00546
00553 bool getBool(const char *key);
00554
00562 double getDouble(const char *key, double def = 0.);
00563
00572 unsigned getIndex(char **data, unsigned max);
00573
00580 unsigned getCount(void);
00581
00590 void setValue(const char *sym, const char *data);
00591
00599 const char * const* getList(const char *sym);
00600
00607 void clrValue(const char *sym);
00608
00613 inline const char *operator[](const char *keyword)
00614 {return getLast(keyword);};
00615
00619 static void end(void);
00620
00625 friend inline void endKeydata(void)
00626 {Keydata::end();};
00627 };
00628
00636 class __EXPORT MemPagerObject
00637 {
00638 public:
00645 inline void *operator new(size_t size, MemPager &pager)
00646 {return pager.alloc(size);};
00647
00654 inline void *operator new[](size_t size, MemPager &pager)
00655 {return pager.alloc(size);};
00656
00660 inline void operator delete(void *) {};
00661
00665 inline void operator delete[](void *) {};
00666 };
00667
00676 class __EXPORT Assoc
00677 {
00678 private:
00679 struct entry {
00680 const char *id;
00681 entry *next;
00682 void *data;
00683 };
00684
00685 entry *entries[KEYDATA_INDEX_SIZE];
00686
00687 protected:
00688 Assoc();
00689 virtual ~Assoc();
00690
00691 void clear(void);
00692
00693 virtual void *getMemory(size_t size) = 0;
00694
00695 public:
00696 void *getPointer(const char *id) const;
00697 void setPointer(const char *id, void *data);
00698 };
00699
00710 class __EXPORT Runlist : public Mutex
00711 {
00712 private:
00713 Runable *first, *last;
00714
00715 protected:
00716 unsigned limit, used;
00717 void check(void);
00718
00719 public:
00725 Runlist(unsigned count = 1);
00726
00735 bool add(Runable *run);
00736
00743 void del(Runable *run);
00744
00750 void set(unsigned limit);
00751 };
00752
00759 class __EXPORT Runable
00760 {
00761 private:
00762 friend class Runlist;
00763 Runlist *list;
00764 Runable *next, *prev;
00765
00766 protected:
00767 Runable();
00768 virtual ~Runable();
00769
00774 virtual void ready(void) = 0;
00775
00776 public:
00783 bool starting(Runlist *list);
00784
00790 void stoping(void);
00791 };
00792
00793 #ifdef CCXX_NAMESPACES
00794 }
00795 #endif
00796
00797 #endif
00798