Libevent source code reading 2-memory management

Do not build high platforms on floating sand

The reading process is
mainly to refer to the libevent reference manual, and read the source code
link according to the module order introduced
http://www.cppblog.com/mysileng/archive/2013/02/04/197717.html

Memory operation

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;//内存释放
}#ifndef _EVENT_DISABLE_MM_REPLACEMENT
static void *(*_mm_malloc_fn)(size_t sz) = NULL;
static void *(*_mm_realloc_fn)(void *p, size_t sz) = NULL;
static void (*_mm_free_fn)(void *p) = NULL;
三个回调对象。

By calling this function, setting our custom memory operation function as a parameter can replace libevent's default memory operation slot function.
Memory specific operation function:

void *
event_mm_malloc_(size_t sz)//申请内存
{
    
    
	if (_mm_malloc_fn)
		return _mm_malloc_fn(sz);
	else
		return malloc(sz);//可以看到libevent默认使用
		//malloc函数来分配内存
}
void *
event_mm_calloc_(size_t count, size_t size)
{
    
    
	if (_mm_malloc_fn) {
    
    
		size_t sz = count * size;
		void *p = _mm_malloc_fn(sz);
		if (p)
			memset(p, 0, sz);
		return p;
	} else
		return calloc(count, size);
}
void *
event_mm_realloc_(void *ptr, size_t sz)
{
    
    
//重新调整之前分配的内存
	if (_mm_realloc_fn)
		return _mm_realloc_fn(ptr, sz);
	else
		return realloc(ptr, sz);
}
void
event_mm_free_(void *ptr)
{
    
    
	if (_mm_free_fn)
		_mm_free_fn(ptr);
	else
		free(ptr);
}

As you can see from the above code, if the user does not have a custom memory operation function, libevent uses C system functions to perform memory operations.
Recommendation: When using custom memory manipulation functions, consider synchronization in a multithreaded environment. So as not to make mistakes

Points to note when using custom memory operation functions

The replacement memory management function affects all subsequent allocation, resizing, and release operations of libevent. Therefore, you must make sure to replace it before calling any other libevent functions. Otherwise, libevent may use your free function to release the memory allocated with malloc of the C library.
The memory block returned by your malloc and realloc functions should have the same address alignment as the memory block returned by the C library.
Your realloc function should correctly handle realloc(NULL,sz) (that is, as malloc(sz)).
Your realloc function should correctly handle realloc(ptr,0) (that is, as free(ptr))
. The free function does not have to deal with free (NULL).
Your malloc function does not have to deal with malloc(0).
If you use libevent in multiple threads, the alternate memory management function needs to be thread-safe.
Libevent will use these functions to allocate the memory returned to you. So, if you want to release the memory allocated and returned by the libevent function, and you have replaced the malloc and realloc functions, you should use the alternative free function.

Guess you like

Origin blog.csdn.net/weixin_39308337/article/details/107589029