bsl_cachedpoolappend.h 3.6 KB
Newer Older
W
wangguibao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190
/***************************************************************************
 * 
 * Copyright (c) 2010 Baidu.com, Inc. All Rights Reserved
 * bsl_cachedpoolappend.h 2010/01/11 21:03:46 sun_xiao Exp 
 * 
 **************************************************************************/
 
 
 
/**
 * @file bsl_cachedpoolappend.h
 * @author sun_xiao(com@baidu.com)
 * @date 2010/01/11 21:03:46
 * @brief 
 *  
 **/



#ifndef  __BSL_ADJUSTCACHEDPOOL_H_
#define  __BSL_ADJUSTCACHEDPOOL_H_

#include "bsl_pool.h"

namespace bsl
{
class cached_mempool_append : public mempool
{
private:
    unsigned int _seg_size;
    unsigned int _seg_num;
    unsigned int _seg_usingnum;
    void * _head;
    unsigned int _head_len;
    void * _curr_seg;
    void * _pre_seg;
    char * _free_space;
    unsigned int _buff_freesize;

    void * _bigbuf_head;

    int _outer_tag;
public:
    inline cached_mempool_append()
    {
        _seg_size = 0;
        _seg_num = 0;
	    _seg_usingnum = 0;
        _head = 0;
        _head_len = 0;
        _free_space = 0;
	    _curr_seg = 0;
        _pre_seg = 0;
	    _buff_freesize = 0;
        
	    _bigbuf_head = 0;
	    _outer_tag = 0;

    }

    /**
     * @brief 
     *
     * @param [in] init_size   : int ʼС
     * @param [in] seg_size   : int 
     * @return  int 
     * @retval   
     * @see 
     * @author sun_xiao
     * @date 2010/01/12 17:25:34
    **/
    inline int create (int init_size, int seg_size)
    {
        destroy();
        if (init_size <= 0 || seg_size <= 0) {
	        return -1;
	    }

		if (NULL != _head) {
		    return -2;
		}
		_head = ::malloc(init_size + sizeof(void*));
		if (NULL == _head) {
		    return -3;
		}
		*(void **)_head = NULL;
        _curr_seg = _head;
        _pre_seg = _head;
		_head_len = init_size + sizeof(void*);
		_seg_size = seg_size + sizeof(void*);

		_free_space = (char *)_head + sizeof(void*);
		_buff_freesize = _head_len - sizeof(void *);
        _seg_num = 1;
        _seg_usingnum = 0;
		return 0;
	}

	inline int create(void *head, int head_size, int seg_size)
	{
        destroy();
		if (NULL == head || head_size <= (int)sizeof(void *) || seg_size <= 0) {
		    return -1;
		}
		_head = head;
		*(void **)_head = NULL;
        _curr_seg = _head;
        _pre_seg = _head;
		_head_len = head_size;
		_seg_size = seg_size + sizeof(void *);

		_free_space = (char *)_head + sizeof(void *);
		_buff_freesize = _head_len - sizeof(void *);
        _seg_num = 1;
        _seg_usingnum = 0;

		_outer_tag = 1;
		return 0;
    }

    virtual void * malloc(size_t size);

    virtual inline void free(void *, size_t)
    {
    }

    inline void clear()
    {
        while(_bigbuf_head)
        {
            void * tmp = *(void **)_bigbuf_head;
            ::free(_bigbuf_head);
            _bigbuf_head = tmp;
        }
	    _seg_usingnum = 0;
	    _free_space = (char *)_head + sizeof(void*);
	    _buff_freesize = _head_len - sizeof(void*);
	    _curr_seg = _head;
        _pre_seg = _head;
    }

    inline void destroy()
    {    
        while(_bigbuf_head)
        {
            void * tmp = *(void **)_bigbuf_head;
            ::free(_bigbuf_head);
            _bigbuf_head = tmp;
        }

	    void *tmp = 0;
	    if (0 == _outer_tag) {
	        tmp = _head;
	    } else {
	        tmp = *(void **)_head;
	    }
	    while (tmp) {
	        void *tmp1 = *(void **)tmp;
	        ::free(tmp);
	        tmp = tmp1;
	    }
	    _head = 0;
        _outer_tag = 0;

    }

    inline virtual ~cached_mempool_append()
    {
        destroy();
    }
};
}















#endif  //__BSL_ADJUSTCACHEDPOOL_H_

/* vim: set ts=4 sw=4 sts=4 tw=100 */