Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

An example Analysis of zmalloc function of Redis

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Most people do not understand the knowledge points of this "Redis's zmalloc function instance Analysis" article, so the editor summarizes the following, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "Redis zmalloc function example Analysis" article.

Let's go directly to the custom zmalloc function in the Redis source code (not the latest version). This function is used in exactly the same way as conventional functions such as malloc, but the difference lies in its internal implementation details.

Void * zmalloc (size_t size) {

/ / allocate memory

Void * ptr = malloc (size + PREFIX_SIZE)

/ / failed to allocate and throw an exception

If (! ptr) zmalloc_oom_handler (size)

/ / can the system use the "malloc_size" function?

# ifdef HAVE_MALLOC_SIZE

Update_zmalloc_stat_alloc (zmalloc_size (ptr))

Return ptr

# else

/ / Save the actual size of the allocation data in the data field

* ((size_t*) ptr) = size

/ / calculate the aligned memory usage and update the "used_memory" variable

Update_zmalloc_stat_alloc (size + PREFIX_SIZE)

/ / returns the initial position of the data body

Return (char*) ptr + PREFIX_SIZE

# endif

}

In fact, the malloc function in the standard library has been able to automatically align the allocated memory, so the main purpose of the zmalloc method here is to accurately calculate the amount of memory allocated for each data store. Each time memory is allocated, zmalloc adds an additional memory space of PREFIX_SIZE size to the amount of data memory allocated. This PREFIX_SIZE macro represents the maximum memory addressing space (size_t) of the current system, depending on the type of specific system. Here we can abbreviate the PREFIX_SIZE-sized space as the "header" part of a storage unit.

The memory cell structure of the first version of Redis

As shown in the figure above, through the statement * ((size_t*) ptr) = size;, Redis stores the actual allocated data block size in the first PREFIX_SIZE of the currently allocated memory block, that is, in the data header, while the binary data entities are actually stored in the later "size" size memory space. In this case, a function called update_zmalloc_stat_alloc maintains a global variable named used_memory, which accumulates the amount of memory allocated each time. The function returns an offset pointer at the end, pointing to the body part of the data that is currently allocated memory. The implementation details of the update_zmalloc_stat_alloc function are as follows.

# define update_zmalloc_stat_alloc (_ n) do {

Size_t _ n = (_ n)

/ / Manual memory completion

If (_ n & (sizeof (long)-1)) _ n + = sizeof (long)-(_ n & (sizeof (long)-1))

AtomicIncr (used_memory, _ n)

} while (0)

The key point to note here is _ n + = sizeof (long)-(_ n & (sizeof (long)-1)); this line. The whole macro function first determines whether the memory allocated this time is an integral multiple of the sizeof (long) size (64-bit machine corresponds to 8-byte memory alignment; 32-bit machine corresponds to 4-byte memory alignment), if not, then add the corresponding placeholder space after the data segment to meet the memory alignment requirement (4x8 bytes) through the statement we gave earlier. The final atomicIncr function is used to update the global used_memory variable value while ensuring thread safety.

In this version of Redis, the process of memory release and memory allocation is just the opposite. The code shown below is the implementation details of the corresponding "zfree" function. First of all, the function points to the head address of the data field containing the actual size of the data block through the (char*) ptr-PREFIX_SIZE statement (moving to the low address of memory), and then obtains the real memory size allocated by the data block (excluding the memory alignment area) through the * ((size_t*) realptr) statement. Finally, the value of the global variable used_memory is updated through the update_zmalloc_stat_free function, and the memory is freed.

Void zfree (void * ptr) {

# ifndef HAVE_MALLOC_SIZE

Void * realptr

Size_t oldsize

# endif

If (ptr = = NULL) return

# ifdef HAVE_MALLOC_SIZE

Update_zmalloc_stat_free (zmalloc_size (ptr))

Free (ptr)

# else

Realptr = (char*) ptr-PREFIX_SIZE

Oldsize = * (size_t*) realptr)

Update_zmalloc_stat_free (oldsize+PREFIX_SIZE)

Free (realptr)

# endif

}

As shown below, if we look at the implementation details of the update_zmalloc_stat_free function again, you will find that it is similar to the previous update_zmalloc_stat_alloc function. By calculating the size of memory bytes that need to be replenished and subtracting the memory space of the corresponding size from the used_memory variable, the accurate calculation of memory space utilization can be realized.

# define update_zmalloc_stat_free (_ n) do {\

Size_t _ n = (_ n);\

If (_ n & (sizeof (long)-1)) _ n + = sizeof (long)-(_ n & (sizeof (long)-1));\

AtomicDecr (used_memory,__n);\

} while (0)

The above is about the content of this article "zmalloc function instance Analysis of Redis". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report