• <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>
            隨筆-91  評論-137  文章-0  trackbacks-0
            作為一個山寨的STL,那么不得不提的是其中的allocator(空間配置器)。顧名思義,它是負責空間分配用的,下面代碼十分簡單,僅對C函數malloc和free進行了薄薄的一層封裝,同時給定了自定義函數free_handler用于在空間分配時候由于內存被占滿了而導致的分配失敗。

            這里值得注意的是:這個釋放函數的函數指針應該是由調用方來負責指定,并且它僅有一個參數表明至少需要釋放多少字節的內存。

            下面來看代碼,代碼非常簡單,應此這里就不逐一展開說明了。
            #ifndef _QLANGUAGE_LIBRARY_ALLOC_H_
            #define _QLANGUAGE_LIBRARY_ALLOC_H_

            #if 0
            #include 
            <new>
            #define __THROW_BAD_ALLOC throw std::bad_alloc
            #elif !defined(__THROW_BAD_ALLOC)
            #include 
            <iostream>
            #define __THROW_BAD_ALLOC std::cerr << "out of memory" << std::endl; exit(1)
            #endif

            namespace QLanguage
            {
                
            namespace Library
                
            {
                    template 
            <typename T>
                    
            class allocator
                    
            {
                    
            public:
                        allocator()
                        
            {
                        }


                        allocator(
            const allocator<T>&)
                        
            {
                        }


                        
            static T* allocate()
                        
            {
                            
            const size_t size = sizeof(T);
                            T
            * result = (T*)malloc(size);
                            
            while(result == nullptr)
                            
            {
                                
            if(free_handler) free_handler(size);
                                
            else __THROW_BAD_ALLOC;
                                result 
            = (T*)malloc(size);
                            }

                            
            return result;
                        }


                        
            static T* allocate(const size_t& n)
                        
            {
                            
            const size_t size = n * sizeof(T);
                            
            if(size <= 0throw "bad allocate size";
                            T
            * result = (T*)malloc(size);
                            
            while(result == nullptr)
                            
            {
                                
            if(free_handler) free_handler(size);
                                
            else __THROW_BAD_ALLOC;
                                result 
            = (T*)malloc(size);
                            }

                            
            return result;
                        }


                        
            static void deallocate(T* p)
                        
            {
                            free(p);
                        }


                        
            static void deallocate(T* p, const size_t&)
                        
            {
                            free(p);
                        }


                        
            static T* reallocate(T* p, const size_t& n)
                        
            {
                            
            const size_t size = n * sizeof(T);
                            
            if(size <= 0throw "bad reallocate size";
                            T
            * result = (T*)realloc(p, size);
                            
            while(result == nullptr)
                            
            {
                                
            if(free_handler) free_handler(size);
                                
            else __THROW_BAD_ALLOC;
                                result 
            = (T*)realloc(p, size);
                            }

                            
            return result;
                        }

                    
            public:
                        
            static void(*free_handler)(const size_t&);

                        
            static void set_handler(void(*h)(const size_t&))
                        
            {
                            free_handler 
            = h;
                        }

                    }
            ;

                    template 
            <typename T>
                    typename 
            void (*allocator<T>::free_handler)(const size_t&= nullptr;
                }

            }


            #endif

            完整代碼請到http://qlanguage.codeplex.com/下載
            posted on 2012-05-20 21:45 lwch 閱讀(1935) 評論(0)  編輯 收藏 引用 所屬分類: STL
            一本久久综合亚洲鲁鲁五月天| 久久影院午夜理论片无码| 国产精品久久久久久久人人看| 国产精品热久久毛片| 女人高潮久久久叫人喷水| 久久人人爽人人人人爽AV| 久久99久久99精品免视看动漫| 91精品国产乱码久久久久久 | 久久亚洲精品视频| 一本久道久久综合狠狠爱| 成人久久综合网| 伊人久久亚洲综合影院| 国产精品久久影院| 亚洲精品视频久久久| 国产精品欧美久久久天天影视| 无码精品久久一区二区三区| 久久国产免费观看精品3| 三级韩国一区久久二区综合| 国内精品久久久人妻中文字幕| 亚洲人成网站999久久久综合| 亚洲综合精品香蕉久久网| 欧美成a人片免费看久久| 99久久精品国产高清一区二区 | 久久精品人人做人人爽97 | 99精品国产免费久久久久久下载 | 久久综合日本熟妇| 久久精品一区二区国产| 色欲久久久天天天综合网| 日本亚洲色大成网站WWW久久| 精品久久久久久亚洲| 999久久久免费精品国产| 无码人妻精品一区二区三区久久久 | 久久免费的精品国产V∧| 思思久久99热只有频精品66| 久久93精品国产91久久综合| 94久久国产乱子伦精品免费 | 久久AV无码精品人妻糸列| 久久久久久久综合狠狠综合| 欧美久久一级内射wwwwww.| 久久精品亚洲欧美日韩久久| 国产高潮久久免费观看|