• <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 閱讀(1978) 評論(1)  編輯 收藏 引用 所屬分類: C++技術

            評論

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

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

            <2007年4月>
            25262728293031
            1234567
            891011121314
            15161718192021
            22232425262728
            293012345

            導航

            統計

            常用鏈接

            留言簿(9)

            隨筆分類(67)

            隨筆檔案(65)

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            99久久精品无码一区二区毛片| 久久久噜噜噜久久中文字幕色伊伊| 亚洲国产日韩欧美综合久久| 久久久久99精品成人片| 亚洲国产成人乱码精品女人久久久不卡| 国产三级观看久久| 99久久国产热无码精品免费 | 久久国产精品久久精品国产| 久久久久久久综合日本| 久久国产免费直播| 国产91久久综合| 色综合久久综合中文综合网| 久久无码精品一区二区三区| 欧美一区二区精品久久| 久久久久AV综合网成人| 亚洲精品国产美女久久久| 久久99精品国产99久久6男男| 2021国产精品午夜久久| 久久精品国产色蜜蜜麻豆| 无码超乳爆乳中文字幕久久| 日本精品一区二区久久久 | 99久久99久久精品国产| 丁香色欲久久久久久综合网| 久久99热这里只有精品国产| 久久精品国产72国产精福利| 97精品伊人久久大香线蕉app| 久久亚洲精品成人av无码网站| 久久久久久伊人高潮影院| 精品久久久久久国产免费了| 欧美日韩精品久久久久| 亚洲一区中文字幕久久| 高清免费久久午夜精品| 久久精品国产亚洲AV嫖农村妇女| 2021久久精品免费观看| 亚洲国产成人久久综合碰| 色综合久久中文字幕综合网| 久久强奷乱码老熟女| 久久久久亚洲爆乳少妇无| 久久精品成人免费观看97| 久久激情亚洲精品无码?V| 久久久WWW免费人成精品|