acl/lib_acl_cpp/include/acl_cpp/stdlib/dbuf_pool.hpp

375 lines
8.4 KiB
C++
Raw Normal View History

#pragma once
2015-01-26 19:58:02 +08:00
#include "acl_cpp/acl_cpp_define.hpp"
2015-11-09 22:11:21 +08:00
#include <vector>
struct ACL_DBUF_POOL;
namespace acl
{
2015-01-26 19:58:02 +08:00
/**
2015-11-09 22:11:21 +08:00
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><EFBFBD><EFBFBD>
* <EFBFBD>һ<EFBFBD><EFBFBD><EFBFBD>Ե<EFBFBD><EFBFBD>ͷţ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʺ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҪƵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һЩ<EFBFBD><EFBFBD>С<EFBFBD><EFBFBD><EFBFBD>ȵ<EFBFBD>С<EFBFBD>ڴ<EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD>ã<EFBFBD>
2015-01-26 19:58:02 +08:00
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD>װ<EFBFBD><EFBFBD> lib_acl <EFBFBD>е<EFBFBD> ACL_DBUF_POOL <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
2015-11-09 22:11:21 +08:00
2015-01-26 19:58:02 +08:00
class ACL_CPP_API dbuf_pool
{
public:
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̬<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
dbuf_pool();
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD>̬<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD> destroy
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ٶ<EFBFBD>̬<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
void destroy();
/**
2015-11-09 22:11:21 +08:00
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD> new/delete <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ dbuf_pool <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҳ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϣ<EFBFBD>
* <EFBFBD>Ӷ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> malloc/free <EFBFBD>Ĵ<EFBFBD><EFBFBD><EFBFBD>
2015-11-09 22:11:21 +08:00
* @param size {size_t} <EFBFBD>ɱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> dbuf_pool <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ij<EFBFBD><EFBFBD>ȴ<EFBFBD>С
* @param nblock {size_t} <EFBFBD>ڲ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD><EFBFBD>ڴ<EFBFBD><EFBFBD>4096<EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD><EFBFBD><EFBFBD>
*/
2015-11-09 22:11:21 +08:00
void *operator new(size_t size, size_t nblock = 2);
#if defined(_WIN32) || defined(_WIN64)
void operator delete(void* ptr, size_t);
#else
void operator delete(void* ptr);
2015-11-09 22:11:21 +08:00
#endif
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><EFBFBD>ص<EFBFBD>״̬<EFBFBD>Ա<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ظ<EFBFBD>ʹ<EFBFBD>ø<EFBFBD><EFBFBD>ڴ<EFBFBD><EFBFBD>ض<EFBFBD><EFBFBD><EFBFBD>
2015-11-09 22:11:21 +08:00
* @param reserve {size_t} <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ > 0<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><EFBFBD><EFBFBD>С<EFBFBD><EFBFBD>
* <EFBFBD>ô<EFBFBD>С<EFBFBD><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD>ڵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ѿ<EFBFBD><EFBFBD>ڸ<EFBFBD><EFBFBD>ڴ<EFBFBD><EFBFBD>ض<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ĵ<EFBFBD>С
* @return {bool} <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƿ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>򷵻<EFBFBD> false
*/
2015-11-09 22:11:21 +08:00
bool dbuf_reset(size_t reserve = 0);
2015-01-26 19:58:02 +08:00
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȵ<EFBFBD><EFBFBD>ڴ<EFBFBD>
2015-11-09 22:11:21 +08:00
* @param len {size_t} <EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD>ȣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><EFBFBD>Ƚ<EFBFBD>Сʱ(С<EFBFBD>ڹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* <EFBFBD>е<EFBFBD> block_size)ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> dbuf_pool <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ϣ<EFBFBD>
* <EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><EFBFBD>ϴ<EFBFBD>ʱ<EFBFBD><EFBFBD>ֱ<EFBFBD><EFBFBD>ʹ<EFBFBD><EFBFBD> malloc <EFBFBD><EFBFBD><EFBFBD>з<EFBFBD><EFBFBD><EFBFBD>
2015-01-26 19:58:02 +08:00
* @return {void*} <EFBFBD>·<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><EFBFBD><EFBFBD>ַ
*/
void* dbuf_alloc(size_t len);
2015-01-26 19:58:02 +08:00
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȵ<EFBFBD><EFBFBD>ڴ<EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param len {size_t} <EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><EFBFBD>
* @return {void*} <EFBFBD>·<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><EFBFBD><EFBFBD>ַ
*/
void* dbuf_calloc(size_t len);
2015-01-26 19:58:02 +08:00
/**
2015-11-09 22:11:21 +08:00
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̬<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>µ<EFBFBD><EFBFBD>ڴ<EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>и<EFBFBD><EFBFBD>ƣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> strdup
2015-01-26 19:58:02 +08:00
* @param s {const char*} Դ<EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD>
* @return {char*} <EFBFBD>¸<EFBFBD><EFBFBD>Ƶ<EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ
*/
char* dbuf_strdup(const char* s);
2015-01-26 19:58:02 +08:00
2015-11-09 22:11:21 +08:00
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̬<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>µ<EFBFBD><EFBFBD>ڴ<EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>и<EFBFBD><EFBFBD>ƣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> strdup
* @param s {const char*} Դ<EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD>
* @param len {size_t} <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>󳤶<EFBFBD>
* @return {char*} <EFBFBD>¸<EFBFBD><EFBFBD>Ƶ<EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ
*/
char* dbuf_strndup(const char* s, size_t len);
2015-01-26 19:58:02 +08:00
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݶ<EFBFBD>̬<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݽ<EFBFBD><EFBFBD>и<EFBFBD><EFBFBD><EFBFBD>
2015-11-09 22:11:21 +08:00
* @param addr {const void*} Դ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><EFBFBD><EFBFBD>ַ
2015-01-26 19:58:02 +08:00
* @param len {size_t} Դ<EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><EFBFBD><EFBFBD>
* @return {void*} <EFBFBD>¸<EFBFBD><EFBFBD>Ƶ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵ<EFBFBD>ַ
*/
2015-11-09 22:11:21 +08:00
void* dbuf_memdup(const void* addr, size_t len);
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><EFBFBD>ط<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD>
* @param addr {const void*} <EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><EFBFBD>ط<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><EFBFBD><EFBFBD>ַ
* @return {bool} <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><EFBFBD>ط<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷŶ<EFBFBD><EFBFBD>Σ<EFBFBD><EFBFBD>򷵻<EFBFBD> false
*/
bool dbuf_free(const void* addr);
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><EFBFBD>ط<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ij<EFBFBD>ε<EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> dbuf_reset ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǰ<EFBFBD>ͷŵ<EFBFBD>
* @param addr {const void*} <EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><EFBFBD>ط<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><EFBFBD><EFBFBD>ַ
* @return {bool} <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><EFBFBD>ط<EFBFBD><EFBFBD><EFBFBD>򷵻<EFBFBD> false
*/
bool dbuf_keep(const void* addr);
/**
* ȡ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><EFBFBD>ط<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ij<EFBFBD>ε<EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD>Ա<EFBFBD><EFBFBD>ڵ<EFBFBD><EFBFBD><EFBFBD> dbuf_reset ʱ<EFBFBD><EFBFBD><EFBFBD>ͷŵ<EFBFBD>
* @param addr {const void*} <EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><EFBFBD>ط<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><EFBFBD><EFBFBD>ַ
* @return {bool} <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><EFBFBD>ط<EFBFBD><EFBFBD><EFBFBD>򷵻<EFBFBD> false
*/
bool dbuf_unkeep(const void* addr);
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD> ACL_DBUF_POOL <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ա<EFBFBD><EFBFBD>ڲ<EFBFBD><EFBFBD><EFBFBD> C <EFBFBD>ӿڵ<EFBFBD><EFBFBD>ڴ<EFBFBD><EFBFBD>ض<EFBFBD><EFBFBD><EFBFBD>
* @return {ACL_DBUF_POOL*}
*/
ACL_DBUF_POOL *get_dbuf()
{
return pool_;
}
private:
ACL_DBUF_POOL* pool_;
size_t mysize_;
2015-11-09 22:11:21 +08:00
public:
~dbuf_pool();
};
2015-11-09 22:11:21 +08:00
/**
* sample:
* void test()
* {
* acl::dbuf_pool* dbuf = new acl::dbuf_pool;
* for (int i = 0; i < 1000; i++)
* {
* char* ptr = dbuf->dbuf_strdup("hello world!");
* printf("%s\r\n", p);
* }
* dbuf->destroy();
*
* // <20><><EFBFBD><EFBFBD> dbuf <20><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>ָ<EFBFBD><D6B8><EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><DAB2><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><DAB4><EFBFBD><EFBFBD><EFBFBD>λ<EFBFBD><CEBB>
* dbuf = new(8) acl::dbuf_pool;
* for (int i = 0; i < 1000; i++)
* {
* ptr = dbuf->dbuf_strdup("hello world!");
* printf("%s\r\n", p);
* }
*
* // <20><><EFBFBD><EFBFBD> dbuf <20><><EFBFBD><EFBFBD>
* dbuf->destroy();
* }
*
*/
//////////////////////////////////////////////////////////////////////////////
class dbuf_guard;
/**
* <EFBFBD>ڻ<EFBFBD>ڴ<EFBFBD><EFBFBD>ض<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϸ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ķ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
class ACL_CPP_API dbuf_obj
{
public:
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param guard {dbuf_guard*} <EFBFBD>ò<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǿ<EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* dbuf_guard <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͳһ<EFBFBD><EFBFBD><EFBFBD>٣<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ò<EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD>գ<EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD><EFBFBD>Ӧ
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD> dbuf_guard::push_back <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͳһ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
dbuf_obj(dbuf_guard* guard = NULL);
virtual ~dbuf_obj() {}
private:
friend class dbuf_guard;
// <20>ñ<EFBFBD><C3B1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> dbuf_guard <20><><EFBFBD><EFBFBD>ʹ<EFBFBD>ã<EFBFBD><C3A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӱ<EFBFBD>ȫ<EFBFBD><C8AB>
int nrefer_;
2015-11-09 22:11:21 +08:00
};
/**
* <EFBFBD><EFBFBD>ڴ<EFBFBD><EFBFBD>ع<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɸ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> dbuf_pool <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϸ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ķ<EFBFBD><EFBFBD>󣬵<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD>dbuf_pool <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͷš<EFBFBD>
*/
class ACL_CPP_API dbuf_guard
{
public:
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param dbuf {dbuf_pool*} <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><EFBFBD>ض<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǿ<EFBFBD>ʱ<EFBFBD><EFBFBD>dbuf <EFBFBD><EFBFBD><EFBFBD><EFBFBD> dbuf_guard
* <EFBFBD>ӹܣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ<EFBFBD>գ<EFBFBD><EFBFBD>򱾹<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><EFBFBD> dbuf_pool <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @param nblock {size_t} <EFBFBD><EFBFBD> dbuf <EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ϊ NULL ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* dbuf_pool <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڴ<EFBFBD><EFBFBD><EFBFBD>(4096)<EFBFBD>ı<EFBFBD><EFBFBD><EFBFBD>
*/
dbuf_guard(dbuf_pool* dbuf = NULL, size_t nblock = 2);
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ڲ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ɹ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> dbuf_pool <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
~dbuf_guard();
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD> dbuf_pool::dbuf_reset
* @param reserve {size_t}
* @return {bool}
*/
bool dbuf_reset(size_t reserve = 0)
{
return dbuf_->dbuf_reset(reserve);
}
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD> dbuf_pool::dbuf_alloc
* @param len {size_t}
* @return {void*}
*/
void* dbuf_alloc(size_t len)
{
return dbuf_->dbuf_alloc(len);
}
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD> dbuf_pool::dbuf_calloc
* @param len {size_t}
* @return {void*}
*/
void* dbuf_calloc(size_t len)
{
return dbuf_->dbuf_calloc(len);
}
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD> dbuf_pool::dbuf_strdup
* @param s {const char*}
* @return {char*}
*/
char* dbuf_strdup(const char* s)
{
return dbuf_->dbuf_strdup(s);
}
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD> dbuf_pool::dbuf_strndup
* @param s {const char*}
* @param len {size_t}
* @return {char*}
*/
char* dbuf_strndup(const char* s, size_t len)
{
return dbuf_->dbuf_strndup(s, len);
}
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD> dbuf_pool::dbuf_memdup
* @param addr {const void*}
* @param len {size_t}
* @return {void*}
*/
void* dbuf_memdup(const void* addr, size_t len)
{
return dbuf_->dbuf_memdup(addr, len);
}
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD> dbuf_pool::dbuf_free
* @param addr {const void*}
* @return {bool}
*/
bool dbuf_free(const void* addr)
{
return dbuf_->dbuf_free(addr);
}
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD> dbuf_pool::dbuf_keep
* @param addr {const void*}
* @return {bool}
*/
bool dbuf_keep(const void* addr)
{
return dbuf_->dbuf_keep(addr);
}
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD> dbuf_pool::dbuf_unkeep
* @param addr {const void*}
* @return {bool}
*/
bool dbuf_unkeep(const void* addr)
{
return dbuf_->dbuf_unkeep(addr);
}
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD> dbuf_pool <EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @return {acl::dbuf_pool&}
*/
acl::dbuf_pool& get_dbuf() const
{
return *dbuf_;
}
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֶ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ñ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> dbuf_pool <EFBFBD>Ϸ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> dbuf_obj <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>󽻸<EFBFBD>
* dbuf_guard <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ͳһ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ٹ<EFBFBD><EFBFBD><EFBFBD>
* @param obj {dbuf_obj*}
* @return {int} <EFBFBD><EFBFBD><EFBFBD><EFBFBD> obj <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӻ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> dbuf_obj <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>е<EFBFBD><EFBFBD>±<EFBFBD>λ<EFBFBD>ã<EFBFBD>
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ < 0 <EFBFBD><EFBFBD>˵<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ķ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ѿ<EFBFBD><EFBFBD><EFBFBD> dbuf_guard <EFBFBD>ļ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2015-11-09 22:11:21 +08:00
*/
int push_back(dbuf_obj* obj);
/**
* <EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD>ǰ<EFBFBD>ڴ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>й<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ķ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* @return {size_t}
*/
size_t size() const
{
return objs_.size();
}
/**
* <EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD>ǰ<EFBFBD>ڴ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>й<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ķ<EFBFBD><EFBFBD>󼯺<EFBFBD>
* @return {std::vector<dbuf_obj*>&}
*/
const std::vector<dbuf_obj*>& get_objs() const
{
return objs_;
}
/**
* <EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD><EFBFBD><EFBFBD>±<EFBFBD><EFBFBD>Ķ<EFBFBD><EFBFBD><EFBFBD>
* @param pos {size_t} ָ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>±<EFBFBD>λ<EFBFBD>ã<EFBFBD><EFBFBD><EFBFBD>ӦԽ<EFBFBD><EFBFBD>
* @return {dbuf_obj*} <EFBFBD><EFBFBD><EFBFBD>±<EFBFBD>λ<EFBFBD><EFBFBD>Խ<EFBFBD><EFBFBD>ʱ<EFBFBD><EFBFBD><EFBFBD><EFBFBD> NULL
*/
dbuf_obj* operator[](size_t pos) const;
private:
dbuf_pool* dbuf_;
std::vector<dbuf_obj*> objs_;
};
/**
* sample:
* // <20>̳<EFBFBD> acl::dbuf_obj <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* class myobj : public acl::dbuf_obj
* {
* public:
* // <20><> guard <20><><EFBFBD>󴫵ݸ<F3B4ABB5><DDB8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>󣬻<EFBFBD><F3A3ACBB><EFBFBD><E0BDAB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> guard <20>Ķ<EFBFBD><C4B6>󼯺<EFBFBD><F3BCAFBA><EFBFBD>
* myobj(acl::dbuf_guard* guard) : dbuf_obj(guard) {}
*
* void doit()
* {
* printf("hello world!\r\n");
* }
*
* private:
* ~myobj() {}
* };
*
* void test()
* {
* acl::dbuf_guard guard;
*
* // <20><> dbuf_guard <20><><EFBFBD><EFBFBD><EFBFBD>ϴ<EFBFBD><CFB4><EFBFBD><EFBFBD><EFBFBD>̬ 100 <20><> myobj <20><><EFBFBD><EFBFBD>
* for (int i = 0; i < 100; i++)
* {
* // <20><> guard <20><><EFBFBD><EFBFBD><EFBFBD>ϴ<EFBFBD><CFB4><EFBFBD><EFBFBD><EFBFBD>̬ myobj <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҽ<EFBFBD> guard <20><>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
* myobj* obj = new (guard.dbuf_alloc(sizeof(myobj))) myobj(&guard);
* obj->doit();
* }
*
* // <20><> guard <20><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><E6B4B4><EFBFBD>Ķ<EFBFBD>̬<EFBFBD><CCAC><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD>
* }
*/
} // namespace acl