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

What is the redis memory allocation?

2025-01-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

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

This article introduces the relevant knowledge of "how redis memory allocation is". In the operation of actual cases, many people will encounter such a dilemma. Then let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Redis memory management encapsulates a series of malloc functions. Additional memory size information of the request is recorded.

# include

# include

Static size_t used_memory = 0

/ *

Allocate memory of sizeof (size_t) + size size. The first sizeof (size_t) bytes record the size of this allocation.

Record the total memory allocated and return the first address of the memory used to store the data, that is, across the sizeof (size_t) size of bytes

, /

Void * zmalloc (size_t size) {

Void * ptr = malloc (size+sizeof (size_t))

If (! ptr) return NULL

* ((size_t*) ptr) = size

Used_memory + = size+sizeof (size_t)

Return (char*) ptr+sizeof (size_t)

}

/ / reallocate memory. Ptr is the memory first address of the old data, and size is the memory size that needs to be shredded this time.

Void * zrealloc (void * ptr, size_t size) {

Void * realptr

Size_t oldsize

Void * newptr

/ / if ptr is empty, there is no old data. You can apply for a new piece of memory without data migration.

If (ptr = = NULL) return zmalloc (size)

/ / the amount of memory occupied by old data

Realptr = (char*) ptr-sizeof (size_t)

/ / get the memory size of the data part

Oldsize = * (size_t*) realptr)

/ / reallocate memory of size+sizeof (size_t) size using the memory address of the old data as the base address

Newptr = realloc (realptr,size+sizeof (size_t))

If (! newptr) return NULL

/ / memory size of recording data section

* ((size_t*) newptr) = size

/ / recalculate the total size of allocated memory. Sizeof (size_t) is still in use and does not need to be calculated.

Used_memory-= oldsize

Used_memory + = size

/ / return the first address of the memory where the data is stored

Return (char*) newptr+sizeof (size_t)

}

Void zfree (void * ptr) {

Void * realptr

Size_t oldsize

If (ptr = = NULL) return

/ / work out the real first address of memory

Realptr = (char*) ptr-sizeof (size_t)

Oldsize = * (size_t*) realptr)

/ / subtract the amount of memory freed

Used_memory-= oldsize+sizeof (size_t)

Free (realptr)

}

/ / copy string

Char * zstrdup (const char * s) {

Size_t l = strlen (s) + 1

Char * p = zmalloc (l)

Memcpy (pjorspen1)

Return p

}

Size_t zmalloc_used_memory (void) {

Return used_memory

}

This is the end of the content of "how redis memory allocation is". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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

Internet Technology

Wechat

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

12
Report