Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

What is the redis memory allocation?

Shulou Source: shulou.com Published: 2022-06-01 12:21:28 09月21日 Update

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!

Tags: Memory size data allocation address content bytes more knowledge parts storage utility learning next information functions dilemmas bases base addresses characters Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology Linux Docker Xiaomi Huawei