当前位置: 首页 > news >正文

网站搭建工作软件开发设计文档示例

网站搭建工作,软件开发设计文档示例,培训网站建设,直播平台创建公会libevent 定制内存分配 默认情况下,libevent 使用 C 库的内存管理函数在堆上分配内存。通过提供 malloc、realloc和 free 的替代函数,可以让 libevent 使用其他的内存管理器。希望 libevent 使用一个更高效的分配器时;或者希望 libevent 使用一个工具分配器,以便检查内存泄漏时…libevent 定制内存分配 默认情况下,libevent 使用 C 库的内存管理函数在堆上分配内存。通过提供 malloc、realloc和 free 的替代函数,可以让 libevent 使用其他的内存管理器。希望 libevent 使用一个更高效的分配器时;或者希望 libevent 使用一个工具分配器,以便检查内存泄漏时,可能需要这样做。 libevent允许用户(库的使用者)定制自己的内存分配函数。 首先如果要定制自己的内存分配函数就得在一开始配置编译libevent库是不能加入--disable-malloc-replacement选项。默认情况下是没有这个选项的。如果加入了这个选项那么将会在生成的event-config.h中定义_EVENT_DISABLE_MM_REPLACEMENT这个宏。下面是libevent内存分配函数的声明(在mm-internal.h文件) /** Copyright (c) 2007-2012 Niels Provos and Nick Mathewson** Redistribution and use in source and binary forms, with or without* modification, are permitted provided that the following conditions* are met:* 1. Redistributions of source code must retain the above copyright* notice, this list of conditions and the following disclaimer.* 2. Redistributions in binary form must reproduce the above copyright* notice, this list of conditions and the following disclaimer in the* documentation and/or other materials provided with the distribution.* 3. The name of the author may not be used to endorse or promote products* derived from this software without specific prior written permission.** THIS SOFTWARE IS PROVIDED BY THE AUTHOR AS IS AND ANY EXPRESS OR* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*/ #ifndef MM_INTERNAL_H_INCLUDED_ #define MM_INTERNAL_H_INCLUDED_#include sys/types.h#ifdef __cplusplus extern C { #endif#ifndef EVENT__DISABLE_MM_REPLACEMENT /* Internal use only: Memory allocation functions. We give them nice short* mm_names for our own use, but make sure that the symbols have longer names* so they dont conflict with other libraries (like, say, libmm). *//** Allocate uninitialized memory.** return On success, return a pointer to sz newly allocated bytes.* On failure, set errno to ENOMEM and return NULL.* If the argument sz is 0, simply return NULL.*/ EVENT2_EXPORT_SYMBOL void *event_mm_malloc_(size_t sz);/** Allocate memory initialized to zero.** return On success, return a pointer to (count * size) newly allocated* bytes, initialized to zero.* On failure, or if the product would result in an integer overflow,* set errno to ENOMEM and return NULL.* If either arguments are 0, simply return NULL.*/ EVENT2_EXPORT_SYMBOL void *event_mm_calloc_(size_t count, size_t size);/** Duplicate a string.** return On success, return a pointer to a newly allocated duplicate* of a string.* Set errno to ENOMEM and return NULL if a memory allocation error* occurs (or would occur) in the process.* If the argument str is NULL, set errno to EINVAL and return NULL.*/ EVENT2_EXPORT_SYMBOL char *event_mm_strdup_(const char *str);EVENT2_EXPORT_SYMBOL void *event_mm_realloc_(void *p, size_t sz); EVENT2_EXPORT_SYMBOL void event_mm_free_(void *p); #define mm_malloc(sz) event_mm_malloc_(sz) #define mm_calloc(count, size) event_mm_calloc_((count), (size)) #define mm_strdup(s) event_mm_strdup_(s) #define mm_realloc(p, sz) event_mm_realloc_((p), (sz)) #define mm_free(p) event_mm_free_(p) #else #define mm_malloc(sz) malloc(sz) #define mm_calloc(n, sz) calloc((n), (sz)) #define mm_strdup(s) strdup(s) #define mm_realloc(p, sz) realloc((p), (sz)) #define mm_free(p) free(p) #endif#ifdef __cplusplus } #endif#endif 这些内存分配函数是给libevent使用的而非用户(从这些接口声明在mm-internal.h文件中就可以看到这一点)。libevent的其他函数要申请内存就调用mm_malloc之类的宏定义。如果一开始在配置的时候(event-config.h)就禁止用户定制自己的内存分配函数那么就把这些宏定义为C语言标准内存分配函数。 当然即使没有禁止如果用户没有定制自己的内存分配函数最终还是调用C语言的标准内存分配函数。这一点在event_mm_xxxx这些函数的实现上可以看到。 定制内存函数的声明在include/event2/event.h: #if !defined(EVENT__DISABLE_MM_REPLACEMENT) || defined(EVENT_IN_DOXYGEN_) /**Override the functions that Libevent uses for memory management.Usually, Libevent uses the standard libc functions malloc, realloc, andfree to allocate memory. Passing replacements for those functions toevent_set_mem_functions() overrides this behavior.Note that all memory returned from Libevent will be allocated by thereplacement functions rather than by malloc() and realloc(). Thus, if youhave replaced those functions, it will not be appropriate to free() memorythat you get from Libevent. Instead, you must use the free_fn replacementthat you provided.Note also that if you are going to call this function, you should do sobefore any call to any Libevent function that does allocation.Otherwise, those functions will allocate their memory using malloc(), butthen later free it using your provided free_fn.param malloc_fn A replacement for malloc.param realloc_fn A replacement for reallocparam free_fn A replacement for free.**/ EVENT2_EXPORT_SYMBOL void event_set_mem_functions(void *(*malloc_fn)(size_t sz),void *(*realloc_fn)(void *ptr, size_t sz),void (*free_fn)(void *ptr)); /** This definition is present if Libevent was built with support forevent_set_mem_functions() */ #define EVENT_SET_MEM_FUNCTIONS_IMPLEMENTED #endif其定义在event.c中 void event_set_mem_functions(void *(*malloc_fn)(size_t sz),void *(*realloc_fn)(void *ptr, size_t sz),void (*free_fn)(void *ptr)) {mm_malloc_fn_ malloc_fn;mm_realloc_fn_ realloc_fn;mm_free_fn_ free_fn; }定制自己的内存分配函数需要注意的一些地方 替换内存管理函数影响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函数分配的内存并且已经定制了malloc和realloc函数那么就应该使用定制的free函数释放。否则将会C语言标准库的free函数释放定制内存分配函数分配的内存这将发生错误。所以三者要么全部不定制要么全部定制。
http://www.pierceye.com/news/378045/

相关文章:

  • 淘宝客免费网站建设宝塔搭建wordpress主机地址
  • 可以看网站的浏览器wordpress+博客+简书
  • 游戏源码网站免费网站模板有哪些内容
  • 江西网站优化广东网站设计有名的公司
  • wordpress整合dplayer关键词优化举例
  • wordpress怎么设置跳站外链接番禺网站建设培训学校
  • 怎样建立网站平台新网站应该怎么做
  • 根据颜色找网站济南做网站公司排名
  • 面对面视频 网站开发网络科技加我qq是干嘛
  • 如何登录网站制作平台百度旧版本
  • 广东营销型网站建设报价定制商品的app
  • 网站导航常用关键字电子商务网站设计内容
  • 建设vip网站相关视频wordpress 修改用户头像
  • 考百度指数 某个关键词在某个行业网站上的wordpress与Wix对比
  • 机器人网站建设规划书福州网站制作怎样
  • 自己创建一个网站需要多少钱2023最建议买10款手机
  • 寻找富阳网站建设国内个人网站欣赏
  • 企业自建站城市建设模拟游戏官方网站
  • 网站建设数据库类型建立网站信息发布登记制度
  • it培训机构都有哪些seo推广教程seo推广技巧
  • 龙岩网站开发较好的公司wordpress屏蔽首页
  • 有没有做美食的网站深圳网站建站公司
  • 学校网站建设需求分析调研表网站右侧信息跟随左侧菜单栏变化
  • 家乡网站建设策划案邢台哪里建网站
  • 网站建设实习收获青岛网上房地产网站
  • 简述电子政务网站设计的技术企业邮箱是什么类型的账户
  • 深圳网站建设公司元嘉定网站开发
  • 佛山外贸网站建设平台上传网站安装教程
  • c2c网站建设实例德国网站建设
  • 建网站支持设备是什么意思佛山中小企业网站建设