00001 #ifndef AUTO_ENLARGE_BUFFER_H 00002 #define AUTO_ENLARGE_BUFFER_H 00003 00004 #include <stdlib.h> 00005 #include <string.h> //for memcpy 00006 00007 class AutoEnlargeBuffer 00008 { 00009 public : 00010 00011 inline AutoEnlargeBuffer(int size = 128*1024); 00012 00013 inline ~AutoEnlargeBuffer(); 00014 00015 inline void copy(void *src, int size); 00016 00017 inline int size() { return m_size; } 00018 inline void *data() { return m_buffer; } 00019 00020 00021 private : 00022 00023 int m_SIZE; 00024 int m_size; 00025 void* m_buffer; 00026 }; 00027 00028 inline AutoEnlargeBuffer::AutoEnlargeBuffer(int size) 00029 : m_SIZE( size ), 00030 m_size( 0 ) 00031 { 00032 m_buffer = malloc(size); 00033 } 00034 00035 inline AutoEnlargeBuffer::~AutoEnlargeBuffer() 00036 { 00037 free( m_buffer ); 00038 } 00039 00040 inline void AutoEnlargeBuffer::copy(void *src, int size) 00041 { 00042 if ( size > m_SIZE ) { 00043 do { 00044 m_SIZE *= 2; 00045 } 00046 while ( size > m_SIZE ); 00047 00048 free( m_buffer ); 00049 m_buffer = malloc(m_SIZE); 00050 } 00051 00052 m_size = size; 00053 memcpy(m_buffer, src, size); 00054 } 00055 00056 #endif