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

            評論

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

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

            <2008年11月>
            2627282930311
            2345678
            9101112131415
            16171819202122
            23242526272829
            30123456

            導航

            統計

            常用鏈接

            留言簿(9)

            隨筆分類(67)

            隨筆檔案(65)

            搜索

            最新評論

            閱讀排行榜

            評論排行榜

            久久夜色精品国产亚洲av| 国产精品成人99久久久久 | 武侠古典久久婷婷狼人伊人| 久久精品九九亚洲精品天堂| 久久精品国产精品亚洲| 一日本道伊人久久综合影| 亚洲国产精品无码久久久秋霞2| 国产V亚洲V天堂无码久久久| 国产精品久久久久久久久免费| 久久亚洲美女精品国产精品| 精品久久久久久亚洲| 国产免费福利体检区久久| 中文国产成人精品久久亚洲精品AⅤ无码精品 | 中文字幕亚洲综合久久| 国产精品99久久精品爆乳| 久久综合鬼色88久久精品综合自在自线噜噜 | 无码人妻精品一区二区三区久久 | 久久久久久狠狠丁香| 亚洲v国产v天堂a无码久久| 思思久久好好热精品国产| 精品熟女少妇av免费久久| 亚洲午夜精品久久久久久app| 国产精品久久久久9999| 国产成人AV综合久久| 久久亚洲日韩精品一区二区三区| 久久精品无码av| 久久久久久午夜成人影院 | 国内高清久久久久久| 国产精品久久久天天影视香蕉| 2021久久精品免费观看| 国产精品99久久久久久猫咪 | 亚洲精品97久久中文字幕无码| 国产一区二区三区久久| 成人久久免费网站| 伊人伊成久久人综合网777| 久久免费高清视频| 久久国产精品久久| 中文精品久久久久国产网址| 日韩精品国产自在久久现线拍| 中文精品久久久久人妻不卡| 久久综合九色综合精品|