Annotation of parser3/src/targets/isapi/pool_storage.h, revision 1.1

1.1     ! paf         1: /** @file
        !             2:        Parser: ISAPI: pool storage class decl.
        !             3: 
        !             4:        Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
        !             5: 
        !             6:        Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
        !             7: 
        !             8:        $Id: pa_pool.h,v 1.42 2001/03/22 17:13:43 paf Exp $
        !             9: */
        !            10: 
        !            11: #ifndef PA_POOL_STORAGE_H
        !            12: #define PA_POOL_STORAGE_H
        !            13: 
        !            14: #include "pa_config_includes.h"
        !            15: 
        !            16: /**
        !            17:        Dumb pool allocations accounter
        !            18:        
        !            19:        @todo implement at least simple suballocations
        !            20: */
        !            21: class Pool_storage {
        !            22: public:
        !            23:        
        !            24:        void **ptrs;
        !            25:        size_t size;
        !            26:        size_t used;
        !            27: 
        !            28:        Pool_storage(size_t preallocate=10*0x400) : 
        !            29:                ptrs((void **)::malloc(preallocate*sizeof(void *))),
        !            30:                size(0),
        !            31:                used(0) {
        !            32:                if(ptrs) // successfully preallocated?
        !            33:                        size=preallocate;
        !            34:        }
        !            35: 
        !            36:        ~Pool_storage() {
        !            37:                return;
        !            38:                for(size_t i=0; i<used; i++)
        !            39:                        free(ptrs[i]);
        !            40:                free(ptrs);
        !            41:        }
        !            42: 
        !            43:        void *malloc(size_t size) {
        !            44:                if(full())
        !            45:                        if(!expand())
        !            46:                                return 0;
        !            47: 
        !            48:                return ptrs[used++]=::malloc(size);
        !            49:        }
        !            50:        void *calloc(size_t size) {
        !            51:                if(full())
        !            52:                        if(!expand())
        !            53:                                return 0;
        !            54: 
        !            55:                return ptrs[used++]=::calloc(size, 1);
        !            56:        }
        !            57: 
        !            58: private:
        !            59: 
        !            60:        bool full() { return used==size; }
        !            61:        bool expand() {
        !            62:                size_t new_size=size*2;
        !            63:                void **new_ptrs=(void **)::realloc(ptrs, new_size*sizeof(void *));
        !            64:                if(new_ptrs) {
        !            65:                        ptrs=new_ptrs;
        !            66:                        size=new_size;
        !            67:                        return true;
        !            68:                } else
        !            69:                        return false;                   
        !            70:        }
        !            71: };
        !            72: 
        !            73: 
        !            74: #endif

E-mail: