• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            曠野的呼聲

            路漫漫其修遠兮 吾將上下而求索

            常用鏈接

            統(tǒng)計

            最新評論

            【日記】最近干了點嘛?

                    現(xiàn)在已經(jīng)是大學(xué)生涯的最后階段了,回首過去的四年真是充滿感慨。由于畢業(yè)設(shè)計早早完事以至于現(xiàn)在略顯無聊,呵呵。但是一直都沒閑著,最近在研究內(nèi)存管理方面的東西,參考了不少的書,有IBM出的那本《C++應(yīng)用程序性能與優(yōu)化》、《深入解析Windows操作系統(tǒng)》、《Windows核心編程》等等。目的就是想對Windows的內(nèi)存管理策略以及Windows下面使用C++編程如何更好的使用內(nèi)存有更深入和更清醒的認(rèn)識。另外在這些積累之后可能會研究一下內(nèi)存池技術(shù)。
                    前兩天為老師做一個Demo,中間寫了一些簡單的輔助代碼,沒什么用,發(fā)這兒留個紀(jì)念,聊勝于無。

            //////////////////////////////////////////////////////////////////////////
            // 版權(quán)所有 董波
            //////////////////////////////////////////////////////////////////////////
            // File :  Buffer.h
            // Author: Dongbo
            // Date:   2009.1.14
            // Description: 對動態(tài)分配內(nèi)存的包裝以提供更高更好的安全性
            //////////////////////////////////////////////////////////////////////////
            #ifndef _DB_BUFFER_H_
            #define _DB_BUFFER_H_
            #include 
            <cassert>

            /*
            適用情況:
            需要動態(tài)分配內(nèi)存,且需要兼容原有API的時候!此時auto_ptr并不適用!
            */

            namespace db
            {
                
            // 基本實現(xiàn)細節(jié)
                namespace detail
                
            {
                    
            // Base_Buffer
                    template < typename _Ty >
                    
            class Base_Buffer
                    
            {
                    
            private:
                        Base_Buffer( 
            const Base_Buffer<_Ty>& );
                        Base_Buffer
            & operator = ( const Base_Buffer<_Ty>& );
                    
            public:
                        Base_Buffer( unsigned uCount ):m_pTmp(
            0),m_uCount( uCount )
                        
            {
                            
            if0 != uCount )
                            
            {
                                m_pTmp 
            = new _Ty[uCount];
                            }
              
                        }

                        
            ~Base_Buffer()
                        
            {
                            
            if0 != m_pTmp )
                            
            {
                                delete []m_pTmp;
                                m_pTmp 
            = 0;
                            }

                        }

                        _Ty
            * GetBufPtr()
                        
            {
                            assert( 
            0 != m_pTmp );
                            
            return m_pTmp;
                        }

                        
            const _Ty* GetBufPtr() const
                        
            {
                            assert( 
            0 != m_pTmp );
                            
            return m_pTmp;
                        }

                        unsigned  GetCount() 
            const
                        
            {
                            
            return m_uCount;
                        }

                        
            // 2009.1.20添加 增加重設(shè)功能
                        void ReSize( unsigned uSize )
                        
            {
                            
            if0 != uSize )
                            
            {
                                
            if0 != m_pTmp )
                                
            {
                                    delete []m_pTmp;
                                    m_pTmp 
            = NULL;
                                }

                                m_pTmp 
            = new _Ty[ uSize ]; 
                                m_uCount 
            = uSize;
                            }
                
                        }

                    
            protected:
                        _Ty
            *               m_pTmp;
                        unsigned           m_uCount;
                    }
            ;
                }
             
                template 
            < typename _Ty >
                
            class Buffer : public detail::Base_Buffer< _Ty >
                
            {
                
            public:
                    Buffer( unsigned uCount ) : detail::Base_Buffer
            <_Ty>(uCount)
                    
            {
                    }

                }
            ;

                
            class ByteBuffer : public Buffer<char>
                
            {
                
            public:
                    ByteBuffer( unsigned uSize ) : Buffer
            <char>(uSize)
                    
            {        }
                
            public:
                    unsigned  GetBytesNum() 
            const
                    
            {
                        
            return Buffer<char>::m_uCount;
                    }

                }
            ;
                typedef ByteBuffer CharBuffer;
            }

            endif // #ifndef _DB_BUFFER_H_  


            //////////////////////////////////////////////////////////////////////////
            // 版權(quán)所有 董波
            //////////////////////////////////////////////////////////////////////////
            // File :  string_cast.h
            // Author: Dongbo
            // Date:   2009.1.16
            // Description: char字符串與wchar_t字符串之間的轉(zhuǎn)換
            //////////////////////////////////////////////////////////////////////////
            #ifndef _DB_STRING_CAST_H_
            #define _DB_STRING_CAST_H_


            #include 
            <string>
            #include 
            <cstdlib>
            #include 
            "Buffer.h"

            namespace db
            {
                
            // 幾乎從來不用,用于擴展!
                template < typename _Ty > 
                
            class string_cast 
                

                
            private
                    string_cast(); 
                    template 
            < typename _Dummy > 
                    string_cast
            & operator = ( const string_cast<_Dummy>& ); 
                    template 
            < typename _Dummy > 
                    string_cast( 
            const string_cast<_Dummy>& ); 
                }


                
            // 到wchar_t字符串的轉(zhuǎn)換
                template <> 
                
            class string_cast< wchar_t > 
                

                
            private
                    
            // 避免產(chǎn)生一些奇怪的語法!
                    string_cast(); 
                    template 
            < typename _Dummy > 
                    string_cast
            & operator = ( const string_cast<_Dummy>& ); 
                    template 
            < typename _Dummy > 
                    string_cast( 
            const string_cast<_Dummy>& ); 
                
            public
                    template 
            < typename _TCHAR > 
                    string_cast( 
            const _TCHAR* str ) 
                    

                        
            if( NULL == str ) 
                        

                            
            throw std::bad_cast( "目標(biāo)串為空" ); 
                        }
             
                        m_strBuf 
            = str;
                    }
             

                    template 
            <> 
                    string_cast( 
            const char* str ) 
                    

                        
            // 檢查指針狀態(tài)
                        if( NULL == str ) 
                        

                            
            throw std::bad_cast( "目標(biāo)串為空" ); 
                        }
             

                        
            // 獲取長度以創(chuàng)建緩沖區(qū)
                        unsigned iLength = strlen( str ) + 1

                        Buffer
            <wchar_t> buffer( iLength ); 

                        
            // 修改現(xiàn)場以支持中文
                        setlocale( LC_CTYPE, "chs" ); 

                        
            // 轉(zhuǎn)換
                        size_t iSize = 0
            #if _MSC_VER > 1310
                        mbstowcs_s( 
            &iSize, buffer.GetBufPtr(), iLength, str, iLength ); 
            #else
                        mbstowcs( buffer.GetBufPtr(), str, iLength );
            #endif 

                        
            // 還原現(xiàn)場
                        setlocale( LC_CTYPE, "" ); 

                        
            // 基本錯誤檢查
                        if( (iSize<<1< iLength ) 
                        

                            
            throw std::bad_cast( "轉(zhuǎn)換未完成" ); 
                        }
             

                        
            // 拷貝到字符串中
                        m_strBuf.assign( buffer.GetBufPtr() ); 
                    }
             

                    
            // 獲取結(jié)果!
                    operator std::wstring() const 
                    

                        
            return m_strBuf; 
                    }
             

                
            public:
                    
            const std::wstring& ToWstr() const
                    
            {
                        
            return m_strBuf;
                    }

                
            protected
                    std::wstring m_strBuf; 
                }


                
            // 向string的轉(zhuǎn)換
                template<> 
                
            class string_cast< char > 
                

                
            private
                    string_cast(); 
                    template 
            < typename _Dummy > 
                    string_cast
            & operator = ( const string_cast<_Dummy>& ); 
                    template 
            < typename _Dummy > 
                    string_cast( 
            const string_cast<_Dummy>& ); 
                
            public
                    template 
            < typename _TCHAR > 
                    string_cast( 
            const _TCHAR* str ) 
                    

                        
            if( NULL == str ) 
                        

                            
            throw std::bad_cast( "目標(biāo)串為空" ); 
                        }
             
                        m_strBuf 
            = str;
                    }
             

                    template 
            <> 
                    string_cast( 
            const wchar_t* str ) 
                    

                        
            if( NULL == str ) 
                        

                            
            throw std::bad_cast( "目標(biāo)串為空" ); 
                        }
             

                        unsigned iLength 
            = ( wcslen( str ) + 1 )<<1

                        CharBuffer buffer( iLength ); 

                        
            // 修改現(xiàn)場以支持中文
                        setlocale( LC_CTYPE, "chs" ); 

                        size_t iSize 
            = 0
            #if _MSC_VER > 1310
                        wcstombs_s( 
            &iSize, buffer.GetBufPtr(), iLength, str, iLength ); 
            #else
                        wcstombs( buffer.GetBufPtr(), str, iLength );
            #endif

                        setlocale( LC_CTYPE, 
            "" );
                        
            if( (iSize<<1< iLength ) 
                        

                            
            throw std::bad_cast( "轉(zhuǎn)換未完成" ); 
                        }
             

                        m_strBuf.assign( buffer.GetBufPtr() ); 
                    }
             

                    
            operator std::string() const 
                    

                        
            return m_strBuf; 
                    }
             
                
            public:
                    
            const std::string& ToStr() const
                    
            {
                        
            return m_strBuf;
                    }

                
            protected
                    std::
            string m_strBuf; 
                }
            ;


            }

            #endif // #ifndef _DB_STRING_CAST_H_


            上面是兩個字符串轉(zhuǎn)化的程序,其實還可以優(yōu)化的,只不過,我很懶的,呵呵。

            #pragma once

            //////////////////////////////////////////////////////////////////////////
            // FILE : DynLib.h
            // Author : Dongbo
            // Created: 2009.5.13
            // Desc : Encapsulation for dynamic library! 
            //////////////////////////////////////////////////////////////////////////

            #include 
            <string>

            #include 
            <Windows.h>
            #include 
            <tchar.h>

            namespace db
            {
                
            class CDynLib
                
            {
                
            public:
                    typedef       std::basic_string
            < TCHAR >    string_type;
                    typedef       HMODULE                       handle_type;

                
            public:
                    CDynLib( 
            const string_type& strDynLibName );
                    
            ~CDynLib();
                
            public:
                    
            void       Load();
                    
            void       UnLoad();

                    
            const string_type& GetDynLibName() const;
                    
            void*      GetProc( const std::string& strSymbol ) const;

                
            public:
                    
            static string_type  GetSysErrorMsg( unsigned uCode );
                
            protected:
                    string_type            m_strDynLibName;
                    handle_type            m_hHandle;
                }
            ;

                inline 
            const CDynLib::string_type& CDynLib::GetDynLibName() const
                
            {
                    
            return this->m_strDynLibName;
                }

            }


            //////////////////////////////////////////////////////////////////////////
            // FILE : DynLib.cpp
            // Author : Dongbo
            // Created: 2009.5.13
            // Desc : Encapsulation for dynamic library! 
            //////////////////////////////////////////////////////////////////////////

            #ifdef __DB_USE_MFC_APPLICATION__
            #include 
            "stdafx.h"
            #endif // #ifdef __DB_USE_MFC_APPLICATION__

            #include 
            "DynLib.h"

            #include 
            "string_cast.h"

            namespace db
            {
                CDynLib::CDynLib( 
            const string_type& strDynLibName ) : \
                    m_strDynLibName( strDynLibName ), m_hHandle( 
            0 )
                
            {
                }


                CDynLib::
            ~CDynLib()
                
            {
                }


                CDynLib::string_type CDynLib::GetSysErrorMsg( unsigned uCode )
                
            {
                    
            // 這里的uCode被定義為從GetLastError獲得的返回值
                    LPVOID lpMsgBuf;
                    ::FormatMessage(
                        FORMAT_MESSAGE_ALLOCATE_BUFFER 
            | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
                        NULL,
                        uCode,
                        MAKELANGID( LANG_NEUTRAL, SUBLANG_DEFAULT ), 
                        (LPTSTR)
            &lpMsgBuf,
                        
            0,
                        NULL 
                        );

                    string_type strMsg( (string_type::value_type
            *)lpMsgBuf );
                    ::LocalFree( lpMsgBuf );

                    
            return strMsg;
                }


                
            void CDynLib::Load()
                
            {
                    m_hHandle 
            = ::LoadLibrary( this->m_strDynLibName.c_str() );

                    
            if0 == m_hHandle )
                    
            {
                        
            // 因為runtime_error不能接受wchar_t的字符串
                        
            // 當(dāng)系統(tǒng)使用的是char的時候,string_cast只起墊片的作用
                        throw std::runtime_error( 
                            
            "加載動態(tài)鏈接庫:" + 
                            (db::string_cast
            <char>this->m_strDynLibName.c_str() )).ToStr() + 
                            
            "失敗,信息:" + 
                            (db::string_cast
            <char>( CDynLib::GetSysErrorMsg( ::GetLastError() ).c_str() )).ToStr() );
                    }

                }


                
            void CDynLib::UnLoad()
                
            {
                    
            if0 != m_hHandle )
                    
            {
                        
            // 忽略卸載的錯誤
                        ::FreeLibrary( m_hHandle );
                        m_hHandle 
            = 0;
                    }

                }


                
            void* CDynLib::GetProc( const std::string& strSymbol ) const
                
            {
                    assert( m_hHandle 
            != 0 );

                    
            return (void*)::GetProcAddress( this->m_hHandle, strSymbol.c_str() );
                }

            }


            #pragma once

            //////////////////////////////////////////////////////////////////////////
            // FILE : Singleton.h
            // Author : Dongbo
            // Created: 2009.5.13
            // Desc : Singleton template
            //////////////////////////////////////////////////////////////////////////

            #include 
            <cassert>

            namespace db
            {
                template 
            < typename T >
                
            class Singleton
                
            {
                
            protected:
                    
            static T ms_Singleton;

                
            protected:
                    Singleton()
                    
            {
                    }


                    
            ~Singleton()
                    

                    }


                
            public:
                    
            static T& GetSingleton()
                    
            {
                        
            return ms_Singleton; 
                    }

                    
            static T* GetSingletonPtr()
                    
            {
                        
            return &ms_Singleton;
                    }

                }
            ;
            }


            #pragma once

            //////////////////////////////////////////////////////////////////////////
            // FILE : DynLibManager.h
            // Author : Dongbo
            // Created: 2009.5.13
            // Desc : A Manager for dynlib
            //////////////////////////////////////////////////////////////////////////

            #include 
            "Singleton.h"
            #include 
            "DynLib.h"

            #include 
            <map>

            namespace db
            {
                
            class CDynLibManager : public db::Singleton< CDynLibManager >
                
            {
                
            public:
                    typedef db::Singleton
            < CDynLibManager >         base_class;
                    typedef db::CDynLib                             dynlib_type;
                    typedef dynlib_type::string_type                string_type;
                    typedef std::map
            < string_type, dynlib_type* >   dynlib_container;

                    typedef CDynLibManager                          my_type;
                    typedef CDynLibManager
            &                         my_reference;
                    typedef CDynLibManager
            *                         my_pointer;

                    friend 
            class base_class;
                
            protected:
                    CDynLibManager();
                    
            ~CDynLibManager();
                
            public:
                    
            bool  RegisterDynLib( const string_type& strDynLibName );
                    
            void  UnRegisterDynLib( const string_type& strDynLibName );

                    
            const dynlib_type&   GetDynLib( const string_type& strDynLibName ) const;        

                
            protected:
                    dynlib_container             m_lstDynlibs;
                }
            ;
            }


            //////////////////////////////////////////////////////////////////////////
            // FILE : DynLibManager.cpp
            // Author : Dongbo
            // Created: 2009.5.13
            // Desc : A Manager for dynlib
            //////////////////////////////////////////////////////////////////////////

            #ifdef __DB_USE_MFC_APPLICATION__
            #include 
            "stdafx.h"
            #endif // #ifdef __DB_USE_MFC_APPLICATION__

            #include 
            "DynLibManager.h"

            namespace db
            {
                template
            <>
                CDynLibManager Singleton
            <CDynLibManager>::ms_Singleton;

                CDynLibManager::CDynLibManager()
                
            {
                }


                CDynLibManager::
            ~CDynLibManager()
                
            {
                    
            for( dynlib_container::iterator it = m_lstDynlibs.begin();
                        it 
            != m_lstDynlibs.end();
                        
            ++it )
                    
            {
                        
            if( it->second )
                        
            {
                            it
            ->second->UnLoad();
                            delete it
            ->second;
                        }
                        
                    }


                    m_lstDynlibs.clear();
                }


                
            bool CDynLibManager::RegisterDynLib( const string_type& strDynLibName )
                
            {
                    
            if( m_lstDynlibs.find( strDynLibName ) != m_lstDynlibs.end() )
                    
            {
                        
            return true;
                    }

                    
            else
                    
            {
                        
            // 重新載入
                        dynlib_type* pTmp = new dynlib_type(strDynLibName);
                        
            if!pTmp )
                        
            {
                            
            return false;
                        }


                        
            try
                        
            {
                            pTmp
            ->Load();
                        }

                        
            catch( std::runtime_error& 
            #if defined(DEBUG) || defined(_DEBUG)
                            e 
            #endif // #if defined(DEBUG) || defined(_DEBUG)
                            )
                        
            {
                            delete pTmp;
            #if defined(DEBUG) || defined(_DEBUG)
                            
            throw e;
            #else
                            
            return false;
            #endif // #if defined(DEBUG) || defined(_DEBUG)
                            
                        }
                        

                        m_lstDynlibs.insert( std::make_pair( strDynLibName, pTmp ) );

                        
            return true;
                    }

                }


                
            void CDynLibManager::UnRegisterDynLib( const string_type& strDynLibName )
                
            {
                    dynlib_container::iterator pos 
            = m_lstDynlibs.find( strDynLibName );
                    
            if( pos != m_lstDynlibs.end() )
                    
            {
                        
            if( pos->second )
                        
            {
                            pos
            ->second->UnLoad();
                            delete pos
            ->second;
                        }


                        m_lstDynlibs.erase( pos );
                    }

                }


                
            const CDynLibManager::dynlib_type& CDynLibManager::GetDynLib( const string_type& strDynLibName ) const
                
            {
                    dynlib_container::const_iterator pos 
            = m_lstDynlibs.find( strDynLibName );
                    
            if( pos != m_lstDynlibs.end() && pos->second )
                    
            {
                        
            return *pos->second;
                    }

                    
            else
                    
            {
                        
            throw std::runtime_error( "查找失敗!該庫可能尚未注冊!" );
                    }

                }

            }


                   在程序中我使用這個單件Manager來管理所有的動態(tài)載入的dll,目前還沒遇到什么問題,當(dāng)然不能說它沒問題,哈哈。

            for example:

            #include <iostream>

            using namespace std;
            #include 
            <tchar.h>
            #include 
            "DynLibManager.h"

            int main()
            {
                
            using namespace db;

                
            try
                
            {
                    CDynLibManager::GetSingletonPtr()
            ->RegisterDynLib( _T("data.dll") );

                    
            void *= CDynLibManager::GetSingletonPtr()->GetDynLib( _T("data.dll") ).GetProc( "QueryDataBaseInterface" );

                    
            if( p != NULL )
                    
            {
                        cout
            <<"加載成功!"<<endl;
                    }

                    
            else
                    
            {
                        cout
            <<"加載失敗!"<<endl;
                    }

                }

                
            catch( std::runtime_error& e )
                
            {
                    cout
            << e.what() << endl;
                }

                

                
            return 0;
            }



            代碼包:http://ishare.iask.sina.com.cn/f/5180214.html


                   學(xué)生生涯快結(jié)束了,大學(xué)也快結(jié)束了,我對未來充滿了向往。我渴望新的生活,渴望新的挑戰(zhàn),渴望結(jié)識新的朋友!有朋自遠方來不亦悅乎?

            posted on 2009-05-24 19:03 董波 閱讀(546) 評論(0)  編輯 收藏 引用


            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            久久天天婷婷五月俺也去| 九九久久99综合一区二区| 久久精品国产亚洲5555| 久久精品中文字幕第23页| 亚洲AⅤ优女AV综合久久久| 偷窥少妇久久久久久久久| 久久天堂AV综合合色蜜桃网 | 开心久久婷婷综合中文字幕| 久久亚洲高清综合| 色综合久久无码五十路人妻| 99久久伊人精品综合观看| 久久99这里只有精品国产| 国产美女久久久| 精品国产乱码久久久久软件| 久久久九九有精品国产| 国内精品久久国产| 国产精品综合久久第一页| 亚洲午夜久久久久久噜噜噜| 国产精品美女久久久久AV福利| 亚洲AV无码久久精品色欲| 久久久免费观成人影院| 久久超碰97人人做人人爱| 久久综合九色综合久99| 国产午夜精品理论片久久影视| 久久亚洲AV无码精品色午夜麻豆| 免费观看久久精彩视频 | 亚洲va中文字幕无码久久不卡| 精品一久久香蕉国产线看播放| 无码AV中文字幕久久专区| 日本久久久久久久久久| 国产精品综合久久第一页| 久久综合丁香激情久久| 精品蜜臀久久久久99网站| 伊人久久大香线焦AV综合影院 | 国产精品无码久久久久久| 久久久久久曰本AV免费免费| 久久er国产精品免费观看8| 成人午夜精品久久久久久久小说| 2021精品国产综合久久| 久久亚洲中文字幕精品有坂深雪 | 久久国产精品久久精品国产|