home *** CD-ROM | disk | FTP | other *** search
/ Resource for Source: C/C++ / Resource for Source - C-C++.iso / misc_src / cslib16b / include / csbuff.h < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-01  |  7.0 KB  |  224 lines

  1. /***********************************************************************
  2.  
  3.                                       CSDB Library, Version 1.6.b 
  4.                                          Released: March 2nd 1995 
  5.  
  6.        Header file of the BUFFER class.
  7.        This class buffers the IO done by
  8.        the database classes.
  9.  
  10.                                            Copyright(c) 1994,1995 
  11.                                                            Combis 
  12.                                                   The Netherlands 
  13. ***********************************************************************/
  14.  
  15. #ifndef __CSBUFF_H
  16. #define __CSBUFF_H
  17.  
  18. #ifndef __CSEDSTR_H
  19. #include "csedstr.h"
  20. #endif
  21.  
  22. #ifndef __CSTABLE_H
  23. #include "cstable.h"
  24. #endif
  25.  
  26. #ifndef __CSTYPES_H
  27. #include "cstypes.h"
  28. #endif
  29.  
  30.  
  31. class BUFFER: public TABLE
  32. {
  33.  
  34.    STR     b_name;    // Name of the buffer system
  35.    int     is_open;
  36.  
  37. protected:
  38.  
  39. enum { ST_READ=0, ST_DIRTY=1};
  40.  
  41.    typedef struct
  42.    {
  43.       void *prev;
  44.       void *next;
  45.       void **key_add;
  46.       U32  key;
  47.       int  stat;
  48.       int  prior;
  49.    } bcp;        // Buffer Chain Pointers
  50.  
  51.    union
  52.    {
  53.      U32 l;
  54.      struct { U16 i1; U16 i2;} i;
  55.      struct { U8 c1; U8 c2; U8 c3; U8 c4; } c;
  56.    } bufbit;
  57.  
  58.  
  59.    U32     lbsc_k;      // Load Buffer Short Cut Key
  60.    int     lbsc_p;      // Load Buffer Short Cut Priority
  61.    int     lbsc_s;      // Load Buffer Short Cut Status
  62.    void *lbsc_a;      // Load Buffer Short Cut Address
  63.  
  64.    U32    *bupo;
  65.    U32    *bupn;
  66.    bcp    *buch;          // Buffer chains
  67.    U16    *buma;          // Max number of buffer in chain
  68.    U16    *buco;          // Number of buffers currently in chain
  69.  
  70.  
  71.    U32    reco;          // Number of reads from file
  72.    U32    wrco;          // Number of writes to file.
  73.    U32    fbco;          // Number of times a 'force-buffer' was needed.
  74.    U32    llco;          // Locate and Load count;
  75.  
  76.    U16    bs;          // Buffer Size
  77.    U16    nb;          // Maximum Number of Buffers
  78.    U16    nbm;          // Minimum Number of Buffers
  79.    U16    na;          // Number of buffers currently Allocated from OS
  80.    U16    bas;          // Buffer Allocation Size (bs+sizeof(bcp))
  81.  
  82.    U16    buf_lim_nr;   // Limits the index blocks down.
  83.    U16    headpagesize; // Size of the first page, containing the header.
  84.    S32    headoff;      // Offset to the first page in file. (Can be negative)
  85.    int    enoug_mem;    // Short-cut in memory allocation.
  86.  
  87.    int    echo_on;
  88.  
  89.    FILE *fp;
  90.  
  91.  
  92. protected:
  93.  
  94. ////////////////////////// Core functions //////////////////////////////////////
  95. //   Chain
  96.    void connect(bcp &left,bcp &right)    { left.next=&right; right.prev=&left; }
  97.    void connect(void *left,void *right) { connect(*(bcp *)left,*(bcp *)right); }
  98.    void end_ic(bcp &c,bcp *p)        { connect(*(bcp *)c.prev,*p); connect(*p,c); }
  99.    bcp *init_bcp(void *d,U32 key,void **add,int prior,int stat);
  100.  
  101. //   Buffers
  102.    void *i2b(void *p)           { return ((CSCHAR *)p-bs); }
  103.    void *b2i(void *p)           { return ((CSCHAR *)p+bs); }
  104.    void  force_buff(U32 key,void *& data,void ** addr,int prior,int stat);
  105.  
  106. //   I/O
  107.    U32     pos_infile(U32 pagnr)          { return headoff+pagnr*bs; }
  108.    void  file_2_buffer(U32 key,void *buf);
  109.    void  buffer_2_file(U32 key,void *buf);
  110.  
  111.  
  112.  
  113. public:
  114.  
  115.    void free_chain(bcp *p);
  116.    void init_allocs(void);
  117.    void init_vars(void);
  118.    void init_defaults(void);
  119.  
  120. ////////////////////////// Miscellaneous ///////////////////////////////////////
  121.    U32    debug_bb(void) { return bufbit.l; }
  122.    void set_num_page(U32 n);
  123.  
  124.    void zap2(void);
  125.    void overflow_buf(int prior);
  126.  
  127.    void buco_min(int prior)   { buco[prior]--; }
  128.    void buco_plus(int prior)
  129.    {
  130.      if(  buco[prior]==buma[prior]) overflow_buf(prior);
  131.      else buco[prior]++;
  132.    }
  133.  
  134.  
  135.  
  136. ////////////////////////////////////////////////////////////////////////////////
  137. ////////////////////////                ////////////////////////////
  138. ////////////////////////     P U B L I C        ////////////////////////////
  139. ////////////////////////                ////////////////////////////
  140. ////////////////////////////////////////////////////////////////////////////////
  141.  
  142.  
  143. public:
  144.  
  145.    ~BUFFER(void);
  146.    BUFFER(void);
  147.  
  148.    void echo_IO(int TF) { echo_on=TF; }
  149.  
  150. ////////////////////////// Setup functions /////////////////////////////////////
  151.  
  152.    void index_limit(U16  nr);
  153.    void header_page_size(U16  n);
  154.  
  155.    void buffer_size(U16  n);
  156.    void number_buff(U16  maxi,U16  mini=2);
  157.  
  158.    void number_buff_min(U16  mini);
  159.  
  160.    void name(CSCHAR *s)           { b_name=s; TABLE::name(s); }
  161.    CSCHAR *name(void)           { return (CSCHAR *)b_name; }
  162.  
  163. ////////////////////////// Open & Close ////////////////////////////////////////
  164.    void open(FILE *p);
  165.    int    open(void)  { return is_open;  }
  166.    void close(void);
  167.    void zap(void);
  168.    void empty(void);
  169.  
  170. ////////////////////////// Number & size of Buffers ////////////////////////////
  171.    U16    number_buff_max(void)       { return nb;  }
  172.    U16    number_buff_min(void)       { return nbm; }
  173.  
  174.  
  175. ////////////////////////// Writing back to disk ////////////////////////////////
  176.    void flush(void);
  177.    void save_all(void);
  178.  
  179. ////////////////////////// Locate & Load functions /////////////////////////////
  180.    void *locate_buff(U32 key,int prior,int rwl);
  181.    void *load_buff(U32 key,int prior,int rwl);
  182.  
  183.    void *locate_buff(U32 key)       { return locate_buff(key,1,ST_READ);  }
  184.    void *load_buff(U32 key)       { return load_buff(key,2,ST_READ);     }
  185.    void *locate_buff_d(U32 key)    { return locate_buff(key,3,ST_DIRTY); }
  186.    void *load_buff_d(U32 key)       { return load_buff(key,4,ST_DIRTY);     }
  187.    void *load_buff_ld(U32 key)       { return load_buff(key,31,ST_DIRTY);  }
  188.    void *locate_buff_ld(U32 key)   { return locate_buff(key,31,ST_DIRTY);}
  189.    void *load_buff_l(U32 key)       { return load_buff(key,31,ST_READ);     }
  190.    void *locate_buff_l(U32 key)    { return locate_buff(key,31,ST_READ); }
  191.  
  192.    int    in_ram(U32 key);
  193.    int    in_ram(U32 key,void * &buf);
  194.  
  195. ////////////////////////// Changing Status & Priority //////////////////////////
  196.    void change_stat(void *buf,int prior,int stat);
  197.    void change_stat(void *buf,int prior);
  198.    void change_stat(U32 key,int prior);
  199.    int    priority(void *buf)        { return ((bcp *)b2i(buf))->prior; }
  200.    void max_with_priority(int prior,U16  number) { buma[prior]=number; }
  201.    int    max_with_priority(int prior)         { return buma[prior]; }
  202.  
  203. ////////////////////////// Set Clean/Dirty flag ////////////////////////////////
  204.    int    is_dirty(void *buf)       { return ((bcp *)b2i(buf))->stat;   }
  205.    void dirty(void *buf)       { ((bcp *)b2i(buf))->stat=ST_DIRTY; }
  206.    void clean(void *buf)       { ((bcp *)b2i(buf))->stat=ST_READ;  }
  207.  
  208.    int    is_dirty(U32 key);
  209.    void dirty(U32 key)       { void *buf; if(in_ram(key,buf)) dirty(buf); }
  210.    void clean(U32 key)       { void *buf; if(in_ram(key,buf)) clean(buf); }
  211.  
  212.    int    has_been_updated(void)       { return (wrco>0); }
  213.  
  214. ////////////////////////// Report writing //////////////////////////////////////
  215.    int    report(CSCHAR *name,int sub=1);
  216.    void report(FILE *fipo,int sub);
  217.  
  218.  
  219. ////////////////////////// Background processing ///////////////////////////////
  220.    int    background(void);
  221.  
  222. };
  223. #endif
  224.