• <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>

            自己實現的一個ACE內存分配器

            針對我的前兩篇文章《基于ACE實現的一個內存池》和《基于ACE實現的一個內存池-續篇》后,發現緩存ACE_Message_Block的時候還是不太方便,然后干脆實現了ACE_Allocator接口,代碼如下,利用這個分配器的ACE_Message_Block將會很快貼出來。

            //MemPoolAllocator.h
            /**
             *    @date 2007.10.29
             *  @author PeakGao <peakgao163@163.com>
             
            */

            #ifndef OM_MEMPOOLALLOCATOR_H
            #define OM_MEMPOOLALLOCATOR_H

            #include 
            <ace/pre.h>

            //#include <ace/ACE_export.h>

            #if !defined (ACE_LACKS_PRAGMA_ONCE)
            # pragma once
            #endif /* ACE_LACKS_PRAGMA_ONCE */

            #include 
            <ace/Malloc_Base.h>

            #if defined (ACE_HAS_MALLOC_STATS)
            #if defined (ACE_HAS_THREADS)
            #include 
            "ace/Process_Mutex.h"
            #define ACE_PROCESS_MUTEX ACE_Process_Mutex
            #else
            #include 
            "ace/SV_Semaphore_Simple.h"
            #define ACE_PROCESS_MUTEX ACE_SV_Semaphore_Simple
            #endif /* ACE_HAS_THREADS */

            #endif /* ACE_HAS_MALLOC_STATS */

            #include 
            "MemPoolT.h"


            namespace om{

            class My_Allocator : public ACE_Allocator, public CachePool
            {
            public:
              
            /// These methods are defined.
              virtual void *malloc (size_t nbytes);
              
            virtual void *calloc (size_t nbytes, char initial_value = '\0');
              
            virtual void *calloc (size_t n_elem, size_t elem_size, char initial_value = '\0');
              
            virtual void free (void *ptr);

              
            /// These methods are no-ops.
              virtual int remove (void);
              
            virtual int bind (const char *name, void *pointer, int duplicates = 0);
              
            virtual int trybind (const char *name, void *&pointer);
              
            virtual int find (const char *name, void *&pointer);
              
            virtual int find (const char *name);
              
            virtual int unbind (const char *name);
              
            virtual int unbind (const char *name, void *&pointer);
              
            virtual int sync (ssize_t len = -1int flags = MS_SYNC);
              
            virtual int sync (void *addr, size_t len, int flags = MS_SYNC);
              
            virtual int protect (ssize_t len = -1int prot = PROT_RDWR);
              
            virtual int protect (void *addr, size_t len, int prot = PROT_RDWR);
            #if defined (ACE_HAS_MALLOC_STATS)
              
            virtual void print_stats (voidconst;
            #endif /* ACE_HAS_MALLOC_STATS */
              
            virtual void dump (voidconst;

            private:
              
            // DO NOT ADD ANY STATE (DATA MEMBERS) TO THIS CLASS!!!!  See the
              
            // <ACE_Allocator::instance> implementation for explanation.
            }
            ;


            #include 
            /**/ <ace/post.h>

            }
             // namespace om

            #endif // OM_MEMPOOLALLOCATOR_H


            // MemPoolAllocator.cpp
            /**
             *    @date 2007.10.29
             *  @author PeakGao <peakgao163@163.com>
             
            */

             
            #include 
            "MemPoolAllocator.h"
            #include 
            <ace/OS_NS_string.h>

            namespace om{

                
            void *
                My_Allocator::malloc (size_t nbytes)
                
            {
                    
            if (nbytes > 0 && nbytes <= CachePool::getBlockSize())
                      
            return CachePool::alloc();

                  
            return NULL;
                }


                
            void *
                My_Allocator::calloc (size_t nbytes,
                                           
            char initial_value)
                
            {
                  
            void *ptr = malloc(nbytes);
                  
            if (!ptr)
                      
            return NULL;

                  ACE_OS::memset (ptr, initial_value, nbytes);
                  
            return (void *) ptr;
                }


                
            void *
                My_Allocator::calloc (size_t n_elem, size_t elem_size, 
            char initial_value)
                
            {
                  
            return My_Allocator::calloc (n_elem * elem_size, initial_value);
                }


                
            void
                My_Allocator::free (
            void *ptr)
                
            {
                    CachePool::free(ptr);
                }


                
            int
                My_Allocator::remove (
            void)
                
            {
                  ACE_NOTSUP_RETURN (
            -1);
                }


                
            int
                My_Allocator::bind (
            const char *void *int)
                
            {
                  ACE_NOTSUP_RETURN (
            -1);
                }


                
            int
                My_Allocator::trybind (
            const char *void *&)
                
            {
                  ACE_NOTSUP_RETURN (
            -1);
                }


                
            int
                My_Allocator::find (
            const char *void *&)
                
            {
                  ACE_NOTSUP_RETURN (
            -1);
                }


                
            int
                My_Allocator::find (
            const char *)
                
            {
                  ACE_NOTSUP_RETURN (
            -1);
                }


                
            int
                My_Allocator::unbind (
            const char *)
                
            {
                  ACE_NOTSUP_RETURN (
            -1);
                }


                
            int
                My_Allocator::unbind (
            const char *void *&)
                
            {
                  ACE_NOTSUP_RETURN (
            -1);
                }


                
            int
                My_Allocator::sync (ssize_t, 
            int)
                
            {
                  ACE_NOTSUP_RETURN (
            -1);
                }


                
            int
                My_Allocator::sync (
            void *, size_t, int)
                
            {
                  ACE_NOTSUP_RETURN (
            -1);
                }


                
            int
                My_Allocator::protect (ssize_t, 
            int)
                
            {
                  ACE_NOTSUP_RETURN (
            -1);
                }


                
            int
                My_Allocator::protect (
            void *, size_t, int)
                
            {
                  ACE_NOTSUP_RETURN (
            -1);
                }


                
            #if defined (ACE_HAS_MALLOC_STATS)
                
            void
                My_Allocator::print_stats (
            voidconst
                
            {
                }

                
            #endif /* ACE_HAS_MALLOC_STATS */

                
            void
                My_Allocator::dump (
            voidconst
                
            {
                
            #if defined (ACE_HAS_DUMP)
                
            #endif /* ACE_HAS_DUMP */
                }



            }
             // namespace om

            posted on 2007-10-29 12:48 PeakGao 閱讀(1968) 評論(1)  編輯 收藏 引用 所屬分類: C++技術

            評論

            # re: 自己實現的一個ACE內存分配器 2008-03-14 23:27 happychui

            1 class My_Allocator 繼承CachePool時必須要模版實例化才行,所以必須加上<ACE_SYNCH_MUTEX>
            2 因為ACE_SYNCH_MUTEX需要頭文件 Task.h所以加上了。
              回復  更多評論   

            <2011年5月>
            24252627282930
            1234567
            891011121314
            15161718192021
            22232425262728
            2930311234

            導航

            統計

            常用鏈接

            留言簿(9)

            隨筆分類(67)

            隨筆檔案(65)

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            久久精品国产第一区二区三区| 国产精品狼人久久久久影院| 天堂久久天堂AV色综合| 99久久国产热无码精品免费| 国内精品久久久久久野外| 天天综合久久久网| 亚洲国产天堂久久久久久| 久久亚洲春色中文字幕久久久| 国产成人久久精品区一区二区| 久久精品国产一区二区| 久久综合狠狠综合久久综合88| 夜夜亚洲天天久久| 久久亚洲国产成人精品性色| 亚洲成人精品久久| 亚洲色欲久久久综合网东京热| 97精品伊人久久久大香线蕉| 狠狠色丁香久久婷婷综合| 久久精品国产欧美日韩| 久久婷婷激情综合色综合俺也去| 久久夜色撩人精品国产| 大伊人青草狠狠久久| 久久国语露脸国产精品电影| 人妻系列无码专区久久五月天| www性久久久com| 97精品依人久久久大香线蕉97| 久久福利片| 国产巨作麻豆欧美亚洲综合久久| 97久久精品无码一区二区| 久久AV无码精品人妻糸列| 久久亚洲国产最新网站| 久久精品成人| 久久久久国产一区二区| 国产成人无码精品久久久久免费| 国产一区二区精品久久| 国产精品久久久久影院嫩草| 午夜精品久久久久久中宇| 久久久久亚洲av综合波多野结衣| 国产69精品久久久久观看软件| 2019久久久高清456| 无码人妻精品一区二区三区久久 | 久久久久综合网久久|