<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # 9.3 內存管理回調設置 默認情況下,libevent 使用C 庫的內存管理函數在堆上分配內存。 通過提供malloc、realloc和free 的替代函數,可以讓libevent 使用其他的內存管理器。 希望libevent 使用一個更高效的分配器時;或者希望libevent 使用一個工具分配器,以便檢查內存泄漏時,可能需要這樣做。 ```java void event_set_mem_functions(void *(*malloc_fn)(size_t sz), void *(*realloc_fn)(void *ptr, size_t sz), void (*free_fn)(void *ptr)); ``` 這里有個替換libevent 分配器函數的示例,它可以計算已經分配的字節數。 實際應用中可能 需要添加鎖,以避免運行在多個線程中時發生錯誤。 ### 實例 ```cpp #include <event2/event.h> #include <sys/types.h> #include <stdlib.h> /* This union's purpose is to be as big as the largest of all the * types it contains. */ union alignment { size_t sz; void *ptr; double dbl; }; /* We need to make sure that everything we return is on the right alignment to hold anything, including a double. */ #define ALIGNMENT sizeof(union alignment) /* We need to do this cast-to-char* trick on our pointers to adjust them; doing arithmetic on a void* is not standard. */ #define OUTPTR(ptr) (((char*)ptr)+ALIGNMENT) #define INPTR(ptr) (((char*)ptr)-ALIGNMENT) static size_t total_allocated = 0; static void *replacement_malloc(size_t sz) { void *chunk = malloc(sz + ALIGNMENT); if (!chunk) return chunk; total_allocated += sz; *(size_t*)chunk = sz; return OUTPTR(chunk); } static void *replacement_realloc(void *ptr, size_t sz) { size_t old_size = 0; if (ptr) { ptr = INPTR(ptr); old_size = *(size_t*)ptr; } ptr = realloc(ptr, sz + ALIGNMENT); if (!ptr) return NULL; *(size_t*)ptr = sz; total_allocated = total_allocated - old_size + sz; return OUTPTR(ptr); } static void replacement_free(void *ptr) { ptr = INPTR(ptr); total_allocated -= *(size_t*)ptr; free(ptr); } void start_counting_bytes(void) { event_set_mem_functions(replacement_malloc, replacement_realloc, replacement_free); } ``` ### 注意 * 替換內存管理函數影響libevent 隨后的所有分配、調整大小和釋放內存操作。所以,必 須保證在調用任何其他libevent 函數之前進行替換。否則,libevent 可能用你的free 函 數釋放用C 庫的malloc 分配的內存。 * 你的malloc 和realloc 函數返回的內存塊應該具有和C 庫返回的內存塊一樣的地址對 齊。 * 你的realloc 函數應該正確處理realloc(NULL,sz)(也就是當作malloc(sz)處理) * 你的realloc 函數應該正確處理realloc(ptr,0)(也就是當作free(ptr)處理) * 你的free 函數不必處理free(NULL) * 你的malloc 函數不必處理malloc(0) * 如果在多個線程中使用libevent,替代的內存管理函數需要是線程安全的。 * libevent 將使用這些函數分配返回給你的內存。所以,如果要釋放由libevent 函數分配 和返回的內存,而你已經替換malloc 和realloc 函數,那么應該使用替代的free 函數。 event_set_mem_functions 函數聲明在<event2/event.h>中,在libevent 2.0.1-alpha 版本中 首次出現。 可以在禁止event_set_mem_functions 函數的配置下編譯libevent 。這時候使用 event_set_mem_functions 將不會編譯或者鏈接。 在2.0.2-alpha 及以后版本中,可以通過檢查是否定義了EVENT_SET_MEM_FUNCTIONS_IMPLEMENTED 宏來確定event_set_mem_functions 函數是否存在。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看