Redis Source Analysis (25)---zmalloc memory allocation implementation

Source: Internet
Author: User
Tags safe mode

Time passed quickly, after 1 months of time to learn, I Redis source code analysis has been more than half of the previous study, I mainly for the Redis tool class code to learn. The next few days I'm going to learn about the implementation of some of the encapsulated classes in Redis code that can be used throughout the Redis system. For example, I'm going to analyze the implementation of the memory package in Zmalloc. First put aside the Redis memory function library does not say, in pure C language, memory allocation function has malloc,free,relloc these 3 functions, familiar with C language programming students will not be unfamiliar. But here Redis code writer, in the Redis system to the allocation of memory to do a small package. I can only say that is a small encapsulation, the core of the call method is still the C language of these 3 functions. Let's take a look at some of the APIs that are define in the zmalloc.h header file:

void *zmalloc (size_t size); /* Call Zmalloc Request size space
/void *zcalloc (size_t size); * Call system function Calloc function request space
/void *zrealloc (void *ptr, Size_ t size); /* The original memory resizes the space to size/
void zfree (void *ptr);//Free space method, and update used_memory value
/char *zstrdup (const char *s);//String complex The method * *
size_t zmalloc_used_memory (void);/* Gets the memory size currently occupied/
void zmalloc_enable_thread_safeness (void);/* Whether to set the thread safe mode
/void Zmalloc_set_oom_handler (*oom_handler) (size_t);/* Customizable set memory overflow processing method * *
float ZMALLOC_ Get_fragmentation_ratio (size_t RSS); /* The ratio of the given size to the size of the used memory * *
size_t zmalloc_get_rss (void);
size_t zmalloc_get_private_dirty (void); /* Get private dirty data size
/void Zlibc_free (void *ptr);  /* Original System Free Release method * *
Here are a few more concepts and variables:

static size_t used_memory = 0;
static int zmalloc_thread_safe = 0;
pthread_mutex_t Used_memory_mutex = Pthread_mutex_initializer;
The first used_memory, see the meaning we also know that this is the system has used how much memory size, in the global only maintain such a variable size, indicating that the author would like to analyze the current use of memory, the second Zmalloc_thread_safe, This refers to the thread-safe mode state, the following mutext, which is the service side, which appears in the operating system. According to this, we probably know, Redis in the code malloc and so on, will be based on the size of the creation, will update the used_memory, and the operation of the pattern will be wired safe and uneasy to the pattern of distinction.

/* In the use of memory space, the lock control
/#define UPDATE_ZMALLOC_STAT_ADD (__n) do {\
    pthread_mutex_lock (&used_memory_ Mutex); \
    Used_memory + = (__n); \
    Pthread_mutex_unlock (&used_memory_mutex); \
} while (0)
The above function operation is a thread safe mode when an operation, through the lock operation for the Used_memory control, is to control this variable, to avoid the value of the possibility of dirty data.

/* Call Zmalloc space to request size
/void *zmalloc (size_t size) {
	//actual call or malloc function
    void *ptr = malloc (size+prefix_ SIZE);
	
	If the result of the request is null, the oom is invoked, and the Oom processing method if
    (!ptr) zmalloc_oom_handler (size) is called;
#ifdef have_malloc_size
	//update used_memory size
    update_zmalloc_stat_alloc (Zmalloc_size (PTR));
    return ptr;
#else
    * ((size_t*) ptr) = size;
    Update_zmalloc_stat_alloc (size+prefix_size);
    Return (char*) ptr+prefix_size;
#endif
}
Zmalloc implementation, call or malloc C language method, do a oom exception processing, and then update the size. Inside the Update_zmalloc_stat_alloc method:

/* Request new _n size of memory, divided into thread-safe, and thread-unsafe mode */
#define UPDATE_ZMALLOC_STAT_ALLOC (__n) do {\
    size_t _n = (__n); \
    if ( _n& (sizeof (long)-1)) _n + = sizeof (long)-(_n& (sizeof (long)-1)); \
    if (zmalloc_thread_safe) {\
        update_zmalloc_stat_add (_n); \
    } else {\
        used_memory + = _n; \
    } \
} while (0)
The same zfree () operation is the reverse operation above, call the free method, the value of the used_memory, do a reduced operation. In the Apil also appeared the Zcalloc method, the following function code I analyze this function and malloc what is the difference between:

/* Call system function Calloc function request space
/void *zcalloc (size_t size) {//calloc is the
	same as malloc, but the parameters are different
	//void *calloc (size_t numelements,size_t sizeofelement), numelements * Sizeofelement is the size of the final memory
	//Location Here is the request for a block size of size+prefix_size space
    void *ptr = Calloc (1, size+prefix_size);

    if (!ptr) zmalloc_oom_handler (size);
#ifdef have_malloc_size
    update_zmalloc_stat_alloc (Zmalloc_size (PTR));
    return ptr;
#else
    * ((size_t*) ptr) = size;
    Update_zmalloc_stat_alloc (size+prefix_size);
    Return (char*) ptr+prefix_size;
#endif
}
In these methods, the author is very user-friendly to open a number of APIs to user calls, for example, in order to improve efficiency, can not open safe mode ah;

/* Whether to set thread safe mode
/void zmalloc_enable_thread_safeness (void) {
    zmalloc_thread_safe = 1;
}
Or customize a more reasonable memory overflow processing function, more to meet the needs of the system:

/* Customizable set memory overflow processing method
/void Zmalloc_set_oom_handler (*oom_handler) (size_t) {
    Zmalloc_oom_handler = oom_ handler;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.