Annotation of parser3/src/include/pa_pool.h, revision 1.3

1.1       paf         1: /*
1.3     ! paf         2:   $Id: parser.C,v 1.3 2001/01/26 15:31:56 paf Exp $
        !             3: */
        !             4: 
        !             5: /*
1.1       paf         6: 
                      7:        String                          Chunk0
                      8:        ======                          ========
1.2       paf         9:        head--------->[ptr, size]
1.1       paf        10:        append_here-------->[ptr, size]
1.2       paf        11:        link_row      ........
1.1       paf        12:                        .                       .
                     13:                        .                       [ptr, size]
                     14:                        ...........>[link to the next chunk]
                     15: 
                     16: */
                     17: 
                     18: #ifndef PA_POOL_H
                     19: #define PA_POOL_H
                     20: 
                     21: #include <stddef.h>
                     22: 
                     23: class Pool;
                     24: 
                     25: class String {
1.2       paf        26: public:
                     27:        enum {
                     28:                CR_PREALLOCATED_COUNT=5,
                     29:                CR_GROW_PERCENT=60
                     30:        };
                     31: 
                     32: private:
1.1       paf        33:        friend Pool;
                     34: 
                     35:        // the pool I'm allocated on
                     36:        Pool *pool;
                     37: 
1.2       paf        38:        // last chank allocated count cache
                     39:        int curr_chunk_rows;
1.1       paf        40:        struct Chunk {
1.2       paf        41:                // the number of rows per chunk
                     42:                int count;
1.1       paf        43:                union Row {
                     44:                        // chunk item
                     45:                        struct {
                     46:                                char *ptr;  // pointer to the start of string fragment
                     47:                                size_t size;  // length of the fragment
                     48:                        } item;
                     49:                        Chunk *link;  // link to the next chunk in chain
1.2       paf        50:                } first[CR_PREALLOCATED_COUNT];
1.1       paf        51:                // next rows are here
1.2       paf        52:                Chunk *preallocated_link;
1.1       paf        53:        }
1.2       paf        54:                head;  // the head chunk of the chunk chain
1.1       paf        55: 
                     56:        // next append would write to this record
                     57:        Chunk::Row *append_here;
                     58:        
                     59:        // the address of place where lies address 
                     60:        // of the link to the next chunk to allocate
1.2       paf        61:        Chunk::Row *link_row;
1.1       paf        62: 
                     63:        // new&constructors made private to enforce factory manufacturing at pool
                     64:        void *operator new(size_t size, Pool *apool);
                     65: 
1.2       paf        66:        void construct(Pool *apool);
                     67:        String(Pool *apool) { 
                     68:                construct(apool); 
1.1       paf        69:        }
                     70:        String(Pool *apool, char *src) {
1.2       paf        71:                construct(apool);
1.1       paf        72:                *this+=src;
                     73:        }
                     74: 
                     75:        bool chunk_is_full() {
1.2       paf        76:                return append_here == link_row;
1.1       paf        77:        }
                     78:        void expand();
                     79: 
                     80: public:
                     81: 
                     82:        size_t size();
                     83:        char *c_str();
                     84:        String& operator += (char *src);
                     85: };
                     86: 
                     87: class Pool {
                     88: public:
                     89:        Pool();
                     90:        ~Pool();
                     91:     void *alloc(size_t size);
                     92:     void *calloc(size_t size);
                     93: 
                     94:        String *makeString() { 
                     95:                return new(this) String(this);
                     96:        }
                     97:        String *makeString(char *src) {
                     98:                return new(this) String(this, src);
                     99:        }
                    100: };
                    101: 
                    102: #endif

E-mail: