1 /*
2  * include/haproxy/pool-t.h
3  * Memory pools configuration and type definitions.
4  *
5  * Copyright (C) 2000-2020 Willy Tarreau - w@1wt.eu
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation, version 2.1
10  * exclusively.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
20  */
21 
22 #ifndef _HAPROXY_POOL_T_H
23 #define _HAPROXY_POOL_T_H
24 
25 #include <haproxy/api-t.h>
26 #include <haproxy/list-t.h>
27 #include <haproxy/thread-t.h>
28 
29 /* On architectures supporting threads and double-word CAS, we can implement
30  * lock-less memory pools. This isn't supported for debugging modes however.
31  */
32 #if defined(USE_THREAD) && defined(HA_HAVE_CAS_DW) && !defined(DEBUG_NO_LOCKLESS_POOLS) && !defined(DEBUG_UAF) && !defined(DEBUG_FAIL_ALLOC)
33 #define CONFIG_HAP_LOCKLESS_POOLS
34 #endif
35 
36 /* On architectures supporting threads we can amortize the locking cost using
37  * local pools.
38  */
39 #if defined(USE_THREAD) && !defined(DEBUG_NO_LOCAL_POOLS) && !defined(DEBUG_UAF) && !defined(DEBUG_FAIL_ALLOC)
40 #define CONFIG_HAP_LOCAL_POOLS
41 #endif
42 
43 /* On modern architectures with many threads, a fast memory allocator, and
44  * local pools, the global pools with their single list can be way slower than
45  * the standard allocator which already has its own per-thread arenas. In this
46  * case we disable global pools. The global pools may still be enforced
47  * using CONFIG_HAP_GLOBAL_POOLS though.
48  */
49 #if defined(USE_THREAD) && defined(HA_HAVE_FAST_MALLOC) && defined(CONFIG_HAP_LOCAL_POOLS) && !defined(CONFIG_HAP_GLOBAL_POOLS)
50 #define CONFIG_HAP_NO_GLOBAL_POOLS
51 #endif
52 
53 #define MEM_F_SHARED	0x1
54 #define MEM_F_EXACT	0x2
55 
56 /* By default, free objects are linked by a pointer stored at the beginning of
57  * the memory area. When DEBUG_MEMORY_POOLS is set, the allocated area is
58  * inflated by the size of a pointer so that the link is placed at the end
59  * of the objects. Hence free objects in pools remain intact. In addition,
60  * this location is used to keep a pointer to the pool the object was
61  * allocated from, and verify it's freed into the appropriate one.
62  */
63 #ifdef DEBUG_MEMORY_POOLS
64 #define POOL_EXTRA (sizeof(void *))
65 #define POOL_LINK(pool, item) (void **)(((char *)(item)) + ((pool)->size))
66 #else
67 #define POOL_EXTRA (0)
68 #define POOL_LINK(pool, item) ((void **)(item))
69 #endif
70 
71 /* A special pointer for the pool's free_list that indicates someone is
72  * currently manipulating it. Serves as a short-lived lock.
73  */
74 #define POOL_BUSY ((void *)1)
75 
76 #ifndef MAX_BASE_POOLS
77 #define MAX_BASE_POOLS 64
78 #endif
79 
80 #define POOL_AVG_SAMPLES 1024
81 
82 
83 struct pool_cache_head {
84 	struct list list;    /* head of objects in this pool */
85 	size_t size;         /* size of an object */
86 	unsigned int count;  /* number of objects in this pool */
87 };
88 
89 struct pool_cache_item {
90 	struct list by_pool; /* link to objects in this pool */
91 	struct list by_lru;  /* link to objects by LRU order */
92 };
93 
94 struct pool_head {
95 	void **free_list;
96 	__decl_thread(HA_SPINLOCK_T lock); /* the spin lock */
97 	unsigned int used;	/* how many chunks are currently in use */
98 	unsigned int needed_avg;/* floating indicator between used and allocated */
99 	unsigned int allocated;	/* how many chunks have been allocated */
100 	unsigned int limit;	/* hard limit on the number of chunks */
101 	unsigned int minavail;	/* how many chunks are expected to be used */
102 	unsigned int size;	/* chunk size */
103 	unsigned int flags;	/* MEM_F_* */
104 	unsigned int users;	/* number of pools sharing this zone */
105 	unsigned int failed;	/* failed allocations */
106 	/* 32-bit hole here */
107 	struct list list;	/* list of all known pools */
108 	char name[12];		/* name of the pool */
109 } __attribute__((aligned(64)));
110 
111 #endif /* _HAPROXY_POOL_T_H */
112 
113 /*
114  * Local variables:
115  *  c-indent-level: 8
116  *  c-basic-offset: 8
117  * End:
118  */
119