• <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 閱讀(1927) 評論(0)  編輯 收藏 引用 所屬分類: STL
            久久国产色AV免费观看| 日本久久中文字幕| 天天躁日日躁狠狠久久| 国产产无码乱码精品久久鸭| 69SEX久久精品国产麻豆| 久久99国产一区二区三区| 亚洲午夜精品久久久久久app| 欧美伊人久久大香线蕉综合| 亚洲va国产va天堂va久久| 亚洲国产天堂久久综合网站| 合区精品久久久中文字幕一区| 精品久久久无码人妻中文字幕豆芽| 国产日韩欧美久久| 日日躁夜夜躁狠狠久久AV| 精品久久久久久久久久久久久久久| 伊人久久精品无码av一区| 久久精品无码专区免费 | 久久99精品国产麻豆蜜芽| 久久精品国产亚洲AV忘忧草18| 国产激情久久久久影院老熟女免费| 国内精品伊人久久久久777| 久久99精品国产麻豆婷婷| 韩国三级大全久久网站| 亚洲国产精品无码久久久不卡| 久久久久香蕉视频| 久久免费精品一区二区| 久久亚洲国产成人精品性色| 久久婷婷五月综合色奶水99啪| 久久精品国产99久久香蕉| 国产精品免费久久久久电影网| 国产综合久久久久| 精品久久无码中文字幕| 久久亚洲中文字幕精品有坂深雪| 一级做a爰片久久毛片看看| 欧美精品丝袜久久久中文字幕| 国产成人精品久久| 国产福利电影一区二区三区久久久久成人精品综合 | 99久久99久久精品免费看蜜桃| 波多野结衣AV无码久久一区| 国内精品综合久久久40p| 亚洲欧美伊人久久综合一区二区|