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

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

E-mail: