Skip to content

Files

Latest commit

d651028 · May 1, 2025

History

History
1089 lines (950 loc) · 32.8 KB

zmalloc.c

File metadata and controls

1089 lines (950 loc) · 32.8 KB
antirezoranagradevnexenyossigosundbmadolsonenjoy-binbinK-JomattstaShooterIT
Mar 22, 2009
Mar 22, 2009
1
/* zmalloc - total amount of allocated memory aware version of malloc()
2
*
Mar 20, 2024
Mar 20, 2024
3
* Copyright (c) 2009-Present, Redis Ltd.
Mar 22, 2009
Mar 22, 2009
4
* All rights reserved.
5
*
May 1, 2025
May 1, 2025
6
* Licensed under your choice of (a) the Redis Source Available License 2.0
7
* (RSALv2); or (b) the Server Side Public License v1 (SSPLv1); or (c) the
8
* GNU Affero General Public License v3 (AGPLv3).
Mar 22, 2009
Mar 22, 2009
9
*/
10
Aug 4, 2021
Aug 4, 2021
11
#include "fmacros.h"
12
#include "config.h"
13
#include "solarisfixes.h"
14
Oct 23, 2009
Oct 23, 2009
15
#include <stdio.h>
Mar 22, 2009
Mar 22, 2009
16
#include <stdlib.h>
May 30, 2018
May 30, 2018
17
#include <stdint.h>
Dec 13, 2020
Dec 13, 2020
18
#include <unistd.h>
Mar 27, 2012
Mar 27, 2012
19
Aug 4, 2021
Aug 4, 2021
20
#ifdef __linux__
21
#include <sys/mman.h>
22
#endif
23
Mar 27, 2012
Mar 27, 2012
24
/* This function provide us access to the original libc free(). This is useful
25
* for instance to free results obtained by backtrace_symbols(). We need
26
* to define this function before including zmalloc.h that may shadow the
27
* free implementation if we use jemalloc or another non standard allocator. */
28
void zlibc_free(void *ptr) {
29
free(ptr);
30
}
31
Mar 22, 2009
Mar 22, 2009
32
#include <string.h>
Jan 9, 2011
Jan 9, 2011
33
#include "zmalloc.h"
Oct 1, 2015
Oct 1, 2015
34
#include "atomicvar.h"
Aug 19, 2024
Aug 19, 2024
35
#include "redisassert.h"
Jun 4, 2009
Jun 4, 2009
36
May 22, 2022
May 22, 2022
37
#define UNUSED(x) ((void)(x))
38
Oct 23, 2010
Oct 23, 2010
39
#ifdef HAVE_MALLOC_SIZE
40
#define PREFIX_SIZE (0)
41
#else
Feb 26, 2024
Feb 26, 2024
42
/* Use at least 8 bytes alignment on all systems. */
Jan 20, 2023
Jan 20, 2023
43
#if SIZE_MAX < 0xffffffffffffffffull
44
#define PREFIX_SIZE 8
Oct 27, 2009
Oct 27, 2009
45
#else
Oct 23, 2010
Oct 23, 2010
46
#define PREFIX_SIZE (sizeof(size_t))
47
#endif
Feb 22, 2021
Feb 22, 2021
48
#endif
49
Feb 25, 2021
Feb 25, 2021
50
/* When using the libc allocator, use a minimum allocation size to match the
51
* jemalloc behavior that doesn't return NULL in this case.
52
*/
53
#define MALLOC_MIN_SIZE(x) ((x) > 0 ? (x) : sizeof(long))
54
Oct 23, 2010
Oct 23, 2010
55
/* Explicitly override malloc/free etc when using tcmalloc. */
56
#if defined(USE_TCMALLOC)
57
#define malloc(size) tc_malloc(size)
58
#define calloc(count,size) tc_calloc(count,size)
59
#define realloc(ptr,size) tc_realloc(ptr,size)
60
#define free(ptr) tc_free(ptr)
Feb 26, 2024
Feb 26, 2024
61
/* Explicitly override malloc/free etc when using jemalloc. */
Jun 20, 2011
Jun 20, 2011
62
#elif defined(USE_JEMALLOC)
63
#define malloc(size) je_malloc(size)
64
#define calloc(count,size) je_calloc(count,size)
65
#define realloc(ptr,size) je_realloc(ptr,size)
66
#define free(ptr) je_free(ptr)
Dec 30, 2016
Dec 30, 2016
67
#define mallocx(size,flags) je_mallocx(size,flags)
Apr 16, 2024
Apr 16, 2024
68
#define rallocx(ptr,size,flags) je_rallocx(ptr,size,flags)
Dec 30, 2016
Dec 30, 2016
69
#define dallocx(ptr,flags) je_dallocx(ptr,flags)
Oct 27, 2009
Oct 27, 2009
70
#endif
71
Aug 19, 2024
Aug 19, 2024
72
#define MAX_THREADS 16 /* Keep it a power of 2 so we can use '&' instead of '%'. */
73
#define THREAD_MASK (MAX_THREADS - 1)
74
75
typedef struct used_memory_entry {
76
redisAtomic long long used_memory;
77
char padding[CACHE_LINE_SIZE - sizeof(long long)];
78
} used_memory_entry;
Jan 15, 2010
Jan 15, 2010
79
Aug 19, 2024
Aug 19, 2024
80
static __attribute__((aligned(CACHE_LINE_SIZE))) used_memory_entry used_memory[MAX_THREADS];
81
static redisAtomic size_t num_active_threads = 0;
82
static __thread long my_thread_index = -1;
83
84
static inline void init_my_thread_index(void) {
85
if (unlikely(my_thread_index == -1)) {
86
atomicGetIncr(num_active_threads, my_thread_index, 1);
87
my_thread_index &= THREAD_MASK;
88
}
89
}
90
91
static void update_zmalloc_stat_alloc(long long num) {
92
init_my_thread_index();
93
atomicIncr(used_memory[my_thread_index].used_memory, num);
94
}
95
96
static void update_zmalloc_stat_free(long long num) {
97
init_my_thread_index();
98
atomicDecr(used_memory[my_thread_index].used_memory, num);
99
}
Mar 22, 2009
Mar 22, 2009
100
Aug 24, 2012
Aug 24, 2012
101
static void zmalloc_default_oom(size_t size) {