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 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:
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
00468 virtual ~Keydata();
00469
00477 void unlink(void);
00478
00487 int getCount(const char *sym);
00488
00496 const char* getFirst(const char *sym);
00497
00505 const char* getLast(const char *sym);
00506
00515 unsigned getIndex(char **data, unsigned max);
00516
00523 unsigned getCount(void);
00524
00533 void setValue(const char *sym, const char *data);
00534
00542 const char * const* getList(const char *sym);
00543
00550 void clrValue(const char *sym);
00551
00556 inline const char *operator[](const char *keyword)
00557 {return getLast(keyword);};
00558
00562 static void end(void);
00563
00568 friend inline void endKeydata(void)
00569 {Keydata::end();};
00570 };
00571
00579 class __EXPORT MemPagerObject
00580 {
00581 public:
00588 inline void *operator new(size_t size, MemPager &pager)
00589 {return pager.alloc(size);};
00590
00597 inline void *operator new[](size_t size, MemPager &pager)
00598 {return pager.alloc(size);};
00599
00603 inline void operator delete(void *) {};
00604
00608 inline void operator delete[](void *) {};
00609 };
00610
00619 class __EXPORT Assoc
00620 {
00621 private:
00622 class __EXPORT entry
00623 {
00624 public:
00625 const char *id;
00626 entry *next;
00627 void *data;
00628 };
00629
00630 entry *entries[KEYDATA_INDEX_SIZE];
00631
00632 protected:
00633 Assoc();
00634
00635 void clear(void);
00636
00637 virtual void *getMemory(size_t size) = 0;
00638
00639 public:
00640 void *getPointer(const char *id);
00641 void setPointer(const char *id, void *data);
00642 };
00643
00654 class __EXPORT Runlist : public Mutex
00655 {
00656 private:
00657 Runable *first, *last;
00658
00659 protected:
00660 unsigned limit, used;
00661 void check(void);
00662
00663 public:
00669 Runlist(unsigned count = 1);
00670
00679 bool add(Runable *run);
00680
00687 void del(Runable *run);
00688
00694 void set(unsigned limit);
00695 };
00696
00703 class __EXPORT Runable
00704 {
00705 private:
00706 friend class __EXPORT Runlist;
00707 Runlist *list;
00708 Runable *next, *prev;
00709
00710 protected:
00711 Runable();
00712 virtual ~Runable();
00713
00718 virtual void ready(void) = 0;
00719
00720 public:
00727 bool starting(Runlist *list);
00728
00734 void stoping(void);
00735 };
00736
00737 #ifdef CCXX_NAMESPACES
00738 }
00739 #endif
00740
00741 #endif
00742