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
00349 class __EXPORT Keydata : protected MemPager
00350 {
00351 public:
00352 #ifdef CCXX_PACKED
00353 #pragma pack(1)
00354 #endif
00355
00356 struct Keyval {
00357 Keyval *next;
00358 char val[1];
00359 };
00360
00361 struct Keysym {
00362 Keysym *next;
00363 Keyval *data;
00364 const char **list;
00365 short count;
00366 char sym[1];
00367 };
00368
00369 struct Define {
00370 char *keyword;
00371 char *value;
00372 };
00373
00374 #ifdef CCXX_PACKED
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:
00411 void load(const char *keypath);
00412
00426 void loadPrefix(const char *prefix, const char *keypath);
00427
00437 void loadFile(const char *filepath, const char *keys = NULL, const char *pre = NULL);
00438
00447 void load(Define *pairs);
00448
00452 Keydata();
00453
00461 Keydata(const char *keypath);
00462
00470 Keydata(Define *pairs, const char *keypath = NULL);
00471
00477 virtual ~Keydata();
00478
00486 void unlink(void);
00487
00496 int getCount(const char *sym);
00497
00505 const char* getFirst(const char *sym);
00506
00514 const char* getLast(const char *sym);
00515
00522 bool isKey(const char *sym);
00523
00531 const char *getString(const char *sym, const char *def = NULL);
00532
00540 long getLong(const char *sym, long def = 0);
00541
00548 bool getBool(const char *key);
00549
00557 double getDouble(const char *key, double def = 0.);
00558
00567 unsigned getIndex(char **data, unsigned max);
00568
00575 unsigned getCount(void);
00576
00585 void setValue(const char *sym, const char *data);
00586
00594 const char * const* getList(const char *sym);
00595
00602 void clrValue(const char *sym);
00603
00608 inline const char *operator[](const char *keyword)
00609 {return getLast(keyword);};
00610
00614 static void end(void);
00615
00620 friend inline void endKeydata(void)
00621 {Keydata::end();};
00622 };
00623
00631 class __EXPORT MemPagerObject
00632 {
00633 public:
00640 inline void *operator new(size_t size, MemPager &pager)
00641 {return pager.alloc(size);};
00642
00649 inline void *operator new[](size_t size, MemPager &pager)
00650 {return pager.alloc(size);};
00651
00655 inline void operator delete(void *) {};
00656
00660 inline void operator delete[](void *) {};
00661 };
00662
00671 class __EXPORT Assoc
00672 {
00673 private:
00674 struct entry {
00675 const char *id;
00676 entry *next;
00677 void *data;
00678 };
00679
00680 entry *entries[KEYDATA_INDEX_SIZE];
00681
00682 protected:
00683 Assoc();
00684 virtual ~Assoc();
00685
00686 void clear(void);
00687
00688 virtual void *getMemory(size_t size) = 0;
00689
00690 public:
00691 void *getPointer(const char *id) const;
00692 void setPointer(const char *id, void *data);
00693 };
00694
00705 class __EXPORT Runlist : public Mutex
00706 {
00707 private:
00708 Runable *first, *last;
00709
00710 protected:
00711 unsigned limit, used;
00712 void check(void);
00713
00714 public:
00720 Runlist(unsigned count = 1);
00721
00730 bool add(Runable *run);
00731
00738 void del(Runable *run);
00739
00745 void set(unsigned limit);
00746 };
00747
00754 class __EXPORT Runable
00755 {
00756 private:
00757 friend class __EXPORT Runlist;
00758 Runlist *list;
00759 Runable *next, *prev;
00760
00761 protected:
00762 Runable();
00763 virtual ~Runable();
00764
00769 virtual void ready(void) = 0;
00770
00771 public:
00778 bool starting(Runlist *list);
00779
00785 void stoping(void);
00786 };
00787
00788 #ifdef CCXX_NAMESPACES
00789 }
00790 #endif
00791
00792 #endif
00793