2006-06-26 00:48:02 +00:00
|
|
|
/*
|
2014-12-23 12:51:28 +00:00
|
|
|
* include/common/memory.h
|
|
|
|
* Memory management definitions..
|
|
|
|
*
|
|
|
|
* Copyright (C) 2000-2014 Willy Tarreau - w@1wt.eu
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation, version 2.1
|
|
|
|
* exclusively.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
*/
|
2006-06-26 00:48:02 +00:00
|
|
|
|
2006-06-29 15:53:05 +00:00
|
|
|
#ifndef _COMMON_MEMORY_H
|
|
|
|
#define _COMMON_MEMORY_H
|
2006-06-26 00:48:02 +00:00
|
|
|
|
2017-11-22 14:47:29 +00:00
|
|
|
#include <sys/mman.h>
|
|
|
|
|
2006-06-26 00:48:02 +00:00
|
|
|
#include <stdlib.h>
|
2014-12-23 13:13:16 +00:00
|
|
|
#include <string.h>
|
2018-02-18 19:36:42 +00:00
|
|
|
#include <stdint.h>
|
2006-06-26 00:48:02 +00:00
|
|
|
|
2006-06-29 15:53:05 +00:00
|
|
|
#include <common/config.h>
|
2007-05-13 16:26:08 +00:00
|
|
|
#include <common/mini-clist.h>
|
2017-08-29 07:52:38 +00:00
|
|
|
#include <common/hathreads.h>
|
2006-06-26 00:48:02 +00:00
|
|
|
|
2015-10-28 11:04:02 +00:00
|
|
|
#ifndef DEBUG_DONT_SHARE_POOLS
|
2007-05-13 16:26:08 +00:00
|
|
|
#define MEM_F_SHARED 0x1
|
2015-10-28 11:04:02 +00:00
|
|
|
#else
|
|
|
|
#define MEM_F_SHARED 0
|
|
|
|
#endif
|
2016-01-25 01:19:13 +00:00
|
|
|
#define MEM_F_EXACT 0x2
|
2007-05-13 16:26:08 +00:00
|
|
|
|
2015-10-28 14:09:29 +00:00
|
|
|
/* reserve an extra void* at the end of a pool for linking */
|
|
|
|
#ifdef DEBUG_MEMORY_POOLS
|
|
|
|
#define POOL_EXTRA (sizeof(void *))
|
|
|
|
#define POOL_LINK(pool, item) (void **)(((char *)item) + (pool->size))
|
|
|
|
#else
|
|
|
|
#define POOL_EXTRA (0)
|
|
|
|
#define POOL_LINK(pool, item) ((void **)(item))
|
|
|
|
#endif
|
|
|
|
|
2018-10-16 05:58:39 +00:00
|
|
|
#define MAX_BASE_POOLS 32
|
|
|
|
|
2018-02-22 13:05:55 +00:00
|
|
|
#ifdef CONFIG_HAP_LOCKLESS_POOLS
|
2018-01-24 17:38:31 +00:00
|
|
|
struct pool_free_list {
|
|
|
|
void **free_list;
|
|
|
|
uintptr_t seq;
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2007-05-13 16:26:08 +00:00
|
|
|
struct pool_head {
|
2017-11-26 09:50:36 +00:00
|
|
|
void **free_list;
|
2018-02-22 13:05:55 +00:00
|
|
|
#ifdef CONFIG_HAP_LOCKLESS_POOLS
|
2018-01-24 17:38:31 +00:00
|
|
|
uintptr_t seq;
|
|
|
|
#else
|
|
|
|
__decl_hathreads(HA_SPINLOCK_T lock); /* the spin lock */
|
|
|
|
#endif
|
2007-05-13 16:26:08 +00:00
|
|
|
unsigned int used; /* how many chunks are currently in use */
|
|
|
|
unsigned int allocated; /* how many chunks have been allocated */
|
|
|
|
unsigned int limit; /* hard limit on the number of chunks */
|
|
|
|
unsigned int minavail; /* how many chunks are expected to be used */
|
|
|
|
unsigned int size; /* chunk size */
|
|
|
|
unsigned int flags; /* MEM_F_* */
|
2007-05-13 22:16:13 +00:00
|
|
|
unsigned int users; /* number of pools sharing this zone */
|
2015-10-28 15:24:21 +00:00
|
|
|
unsigned int failed; /* failed allocations */
|
2018-01-24 17:38:31 +00:00
|
|
|
struct list list; /* list of all known pools */
|
2007-05-13 22:16:13 +00:00
|
|
|
char name[12]; /* name of the pool */
|
2017-11-26 09:50:36 +00:00
|
|
|
} __attribute__((aligned(64)));
|
2007-05-13 16:26:08 +00:00
|
|
|
|
2018-10-16 05:58:39 +00:00
|
|
|
|
|
|
|
extern struct pool_head pool_base_start[MAX_BASE_POOLS];
|
|
|
|
extern unsigned int pool_base_count;
|
|
|
|
|
2015-10-08 12:12:13 +00:00
|
|
|
/* poison each newly allocated area with this byte if >= 0 */
|
|
|
|
extern int mem_poison_byte;
|
2007-05-13 16:26:08 +00:00
|
|
|
|
2014-12-03 14:25:28 +00:00
|
|
|
/* Allocates new entries for pool <pool> until there are at least <avail> + 1
|
|
|
|
* available, then returns the last one for immediate use, so that at least
|
|
|
|
* <avail> are left available in the pool upon return. NULL is returned if the
|
|
|
|
* last entry could not be allocated. It's important to note that at least one
|
|
|
|
* allocation is always performed even if there are enough entries in the pool.
|
|
|
|
* A call to the garbage collector is performed at most once in case malloc()
|
|
|
|
* returns an error, before returning NULL.
|
2007-05-13 16:26:08 +00:00
|
|
|
*/
|
2017-08-29 07:52:38 +00:00
|
|
|
void *__pool_refill_alloc(struct pool_head *pool, unsigned int avail);
|
2014-12-03 14:25:28 +00:00
|
|
|
void *pool_refill_alloc(struct pool_head *pool, unsigned int avail);
|
2007-05-13 16:26:08 +00:00
|
|
|
|
|
|
|
/* Try to find an existing shared pool with the same characteristics and
|
|
|
|
* returns it, otherwise creates this one. NULL is returned if no memory
|
|
|
|
* is available for a new creation.
|
|
|
|
*/
|
|
|
|
struct pool_head *create_pool(char *name, unsigned int size, unsigned int flags);
|
|
|
|
|
|
|
|
/* Dump statistics on pools usage.
|
|
|
|
*/
|
2014-01-28 15:49:56 +00:00
|
|
|
void dump_pools_to_trash();
|
2007-05-13 16:26:08 +00:00
|
|
|
void dump_pools(void);
|
2015-10-28 15:24:21 +00:00
|
|
|
int pool_total_failures();
|
|
|
|
unsigned long pool_total_allocated();
|
|
|
|
unsigned long pool_total_used();
|
2007-05-13 16:26:08 +00:00
|
|
|
|
2007-05-13 17:38:49 +00:00
|
|
|
/*
|
|
|
|
* This function frees whatever can be freed in pool <pool>.
|
|
|
|
*/
|
2017-11-24 16:34:44 +00:00
|
|
|
void pool_flush(struct pool_head *pool);
|
2007-05-13 17:38:49 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This function frees whatever can be freed in all pools, but respecting
|
|
|
|
* the minimum thresholds imposed by owners.
|
2017-08-29 07:52:38 +00:00
|
|
|
*
|
2017-11-24 16:34:44 +00:00
|
|
|
* <pool_ctx> is used when pool_gc is called to release resources to allocate
|
2017-08-29 07:52:38 +00:00
|
|
|
* an element in __pool_refill_alloc. It is important because <pool_ctx> is
|
|
|
|
* already locked, so we need to skip the lock here.
|
2007-05-13 17:38:49 +00:00
|
|
|
*/
|
2017-11-24 16:34:44 +00:00
|
|
|
void pool_gc(struct pool_head *pool_ctx);
|
2007-05-13 17:38:49 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This function destroys a pull by freeing it completely.
|
|
|
|
* This should be called only under extreme circumstances.
|
|
|
|
*/
|
2017-11-24 16:34:44 +00:00
|
|
|
void *pool_destroy(struct pool_head *pool);
|
2007-05-13 17:38:49 +00:00
|
|
|
|
2018-10-16 05:58:39 +00:00
|
|
|
/* returns the pool index for pool <pool>, or -1 if this pool has no index */
|
|
|
|
static inline ssize_t pool_get_index(const struct pool_head *pool)
|
|
|
|
{
|
|
|
|
size_t idx;
|
|
|
|
|
|
|
|
idx = pool - pool_base_start;
|
|
|
|
if (idx >= MAX_BASE_POOLS)
|
|
|
|
return -1;
|
|
|
|
return idx;
|
|
|
|
}
|
|
|
|
|
2018-02-22 13:05:55 +00:00
|
|
|
#ifdef CONFIG_HAP_LOCKLESS_POOLS
|
2018-01-24 17:38:31 +00:00
|
|
|
/*
|
|
|
|
* Returns a pointer to type <type> taken from the pool <pool_type> if
|
|
|
|
* available, otherwise returns NULL. No malloc() is attempted, and poisonning
|
|
|
|
* is never performed. The purpose is to get the fastest possible allocation.
|
|
|
|
*/
|
|
|
|
static inline void *__pool_get_first(struct pool_head *pool)
|
|
|
|
{
|
|
|
|
struct pool_free_list cmp, new;
|
|
|
|
|
|
|
|
cmp.seq = pool->seq;
|
|
|
|
__ha_barrier_load();
|
|
|
|
|
|
|
|
cmp.free_list = pool->free_list;
|
|
|
|
do {
|
|
|
|
if (cmp.free_list == NULL)
|
|
|
|
return NULL;
|
|
|
|
new.seq = cmp.seq + 1;
|
|
|
|
__ha_barrier_load();
|
|
|
|
new.free_list = *POOL_LINK(pool, cmp.free_list);
|
|
|
|
} while (__ha_cas_dw((void *)&pool->free_list, (void *)&cmp, (void *)&new) == 0);
|
2018-02-19 23:49:46 +00:00
|
|
|
|
2018-01-24 17:38:31 +00:00
|
|
|
HA_ATOMIC_ADD(&pool->used, 1);
|
|
|
|
#ifdef DEBUG_MEMORY_POOLS
|
|
|
|
/* keep track of where the element was allocated from */
|
|
|
|
*POOL_LINK(pool, cmp.free_list) = (void *)pool;
|
|
|
|
#endif
|
|
|
|
return cmp.free_list;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void *pool_get_first(struct pool_head *pool)
|
|
|
|
{
|
|
|
|
void *ret;
|
|
|
|
|
|
|
|
ret = __pool_get_first(pool);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Returns a pointer to type <type> taken from the pool <pool_type> or
|
|
|
|
* dynamically allocated. In the first case, <pool_type> is updated to point to
|
|
|
|
* the next element in the list. No memory poisonning is ever performed on the
|
|
|
|
* returned area.
|
|
|
|
*/
|
|
|
|
static inline void *pool_alloc_dirty(struct pool_head *pool)
|
|
|
|
{
|
|
|
|
void *p;
|
|
|
|
|
|
|
|
if ((p = __pool_get_first(pool)) == NULL)
|
|
|
|
p = __pool_refill_alloc(pool, 0);
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns a pointer to type <type> taken from the pool <pool_type> or
|
|
|
|
* dynamically allocated. In the first case, <pool_type> is updated to point to
|
|
|
|
* the next element in the list. Memory poisonning is performed if enabled.
|
|
|
|
*/
|
|
|
|
static inline void *pool_alloc(struct pool_head *pool)
|
|
|
|
{
|
|
|
|
void *p;
|
|
|
|
|
|
|
|
p = pool_alloc_dirty(pool);
|
|
|
|
#ifdef DEBUG_MEMORY_POOLS
|
|
|
|
if (p) {
|
|
|
|
/* keep track of where the element was allocated from */
|
|
|
|
*POOL_LINK(pool, p) = (void *)pool;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (p && mem_poison_byte >= 0) {
|
|
|
|
memset(p, mem_poison_byte, pool->size);
|
|
|
|
}
|
|
|
|
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2018-10-16 06:55:15 +00:00
|
|
|
/* Locklessly add item <ptr> to pool <pool>, then update the pool used count.
|
|
|
|
* Both the pool and the pointer must be valid. Use pool_free() for normal
|
|
|
|
* operations.
|
|
|
|
*/
|
|
|
|
static inline void __pool_free(struct pool_head *pool, void *ptr)
|
|
|
|
{
|
|
|
|
void *free_list = pool->free_list;
|
|
|
|
|
|
|
|
do {
|
|
|
|
*POOL_LINK(pool, ptr) = (void *)free_list;
|
|
|
|
__ha_barrier_store();
|
|
|
|
} while (!HA_ATOMIC_CAS(&pool->free_list, (void *)&free_list, ptr));
|
|
|
|
HA_ATOMIC_SUB(&pool->used, 1);
|
|
|
|
}
|
|
|
|
|
2018-01-24 17:38:31 +00:00
|
|
|
/*
|
|
|
|
* Puts a memory area back to the corresponding pool.
|
|
|
|
* Items are chained directly through a pointer that
|
|
|
|
* is written in the beginning of the memory area, so
|
|
|
|
* there's no need for any carrier cell. This implies
|
|
|
|
* that each memory area is at least as big as one
|
|
|
|
* pointer. Just like with the libc's free(), nothing
|
|
|
|
* is done if <ptr> is NULL.
|
|
|
|
*/
|
|
|
|
static inline void pool_free(struct pool_head *pool, void *ptr)
|
|
|
|
{
|
|
|
|
if (likely(ptr != NULL)) {
|
|
|
|
#ifdef DEBUG_MEMORY_POOLS
|
|
|
|
/* we'll get late corruption if we refill to the wrong pool or double-free */
|
|
|
|
if (*POOL_LINK(pool, ptr) != (void *)pool)
|
|
|
|
*(volatile int *)0 = 0;
|
|
|
|
#endif
|
2018-10-16 06:55:15 +00:00
|
|
|
__pool_free(pool, ptr);
|
2018-01-24 17:38:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-22 13:05:55 +00:00
|
|
|
#else /* CONFIG_HAP_LOCKLESS_POOLS */
|
2007-05-13 16:26:08 +00:00
|
|
|
/*
|
2014-12-08 15:35:23 +00:00
|
|
|
* Returns a pointer to type <type> taken from the pool <pool_type> if
|
|
|
|
* available, otherwise returns NULL. No malloc() is attempted, and poisonning
|
|
|
|
* is never performed. The purpose is to get the fastest possible allocation.
|
2007-05-13 16:26:08 +00:00
|
|
|
*/
|
2017-08-29 07:52:38 +00:00
|
|
|
static inline void *__pool_get_first(struct pool_head *pool)
|
2014-12-23 13:13:16 +00:00
|
|
|
{
|
|
|
|
void *p;
|
|
|
|
|
2014-12-08 15:35:23 +00:00
|
|
|
if ((p = pool->free_list) != NULL) {
|
2015-10-28 14:09:29 +00:00
|
|
|
pool->free_list = *POOL_LINK(pool, p);
|
2014-12-23 13:13:16 +00:00
|
|
|
pool->used++;
|
2015-10-28 14:23:51 +00:00
|
|
|
#ifdef DEBUG_MEMORY_POOLS
|
|
|
|
/* keep track of where the element was allocated from */
|
|
|
|
*POOL_LINK(pool, p) = (void *)pool;
|
|
|
|
#endif
|
2014-12-23 13:13:16 +00:00
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
2007-05-13 16:26:08 +00:00
|
|
|
|
2017-08-29 07:52:38 +00:00
|
|
|
static inline void *pool_get_first(struct pool_head *pool)
|
|
|
|
{
|
|
|
|
void *ret;
|
|
|
|
|
2017-11-07 09:42:54 +00:00
|
|
|
HA_SPIN_LOCK(POOL_LOCK, &pool->lock);
|
2017-08-29 07:52:38 +00:00
|
|
|
ret = __pool_get_first(pool);
|
2017-11-07 09:42:54 +00:00
|
|
|
HA_SPIN_UNLOCK(POOL_LOCK, &pool->lock);
|
2017-08-29 07:52:38 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2014-12-08 15:35:23 +00:00
|
|
|
/*
|
|
|
|
* Returns a pointer to type <type> taken from the pool <pool_type> or
|
|
|
|
* dynamically allocated. In the first case, <pool_type> is updated to point to
|
|
|
|
* the next element in the list. No memory poisonning is ever performed on the
|
|
|
|
* returned area.
|
|
|
|
*/
|
|
|
|
static inline void *pool_alloc_dirty(struct pool_head *pool)
|
|
|
|
{
|
|
|
|
void *p;
|
|
|
|
|
2017-11-07 09:42:54 +00:00
|
|
|
HA_SPIN_LOCK(POOL_LOCK, &pool->lock);
|
2017-08-29 07:52:38 +00:00
|
|
|
if ((p = __pool_get_first(pool)) == NULL)
|
|
|
|
p = __pool_refill_alloc(pool, 0);
|
2017-11-07 09:42:54 +00:00
|
|
|
HA_SPIN_UNLOCK(POOL_LOCK, &pool->lock);
|
2014-12-08 15:35:23 +00:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2017-11-22 14:47:29 +00:00
|
|
|
#ifndef DEBUG_UAF /* normal allocator */
|
|
|
|
|
2017-11-22 09:50:54 +00:00
|
|
|
/* allocates an area of size <size> and returns it. The semantics are similar
|
|
|
|
* to those of malloc().
|
|
|
|
*/
|
|
|
|
static inline void *pool_alloc_area(size_t size)
|
|
|
|
{
|
|
|
|
return malloc(size);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* frees an area <area> of size <size> allocated by pool_alloc_area(). The
|
|
|
|
* semantics are identical to free() except that the size is specified and
|
|
|
|
* may be ignored.
|
|
|
|
*/
|
|
|
|
static inline void pool_free_area(void *area, size_t __maybe_unused size)
|
|
|
|
{
|
|
|
|
free(area);
|
|
|
|
}
|
|
|
|
|
2017-11-22 14:47:29 +00:00
|
|
|
#else /* use-after-free detector */
|
|
|
|
|
|
|
|
/* allocates an area of size <size> and returns it. The semantics are similar
|
|
|
|
* to those of malloc(). However the allocation is rounded up to 4kB so that a
|
|
|
|
* full page is allocated. This ensures the object can be freed alone so that
|
|
|
|
* future dereferences are easily detected. The returned object is always
|
2018-02-22 13:14:23 +00:00
|
|
|
* 16-bytes aligned to avoid issues with unaligned structure objects. In case
|
|
|
|
* some padding is added, the area's start address is copied at the end of the
|
|
|
|
* padding to help detect underflows.
|
2017-11-22 14:47:29 +00:00
|
|
|
*/
|
|
|
|
static inline void *pool_alloc_area(size_t size)
|
|
|
|
{
|
|
|
|
size_t pad = (4096 - size) & 0xFF0;
|
2018-02-22 10:39:23 +00:00
|
|
|
void *ret;
|
2017-11-22 14:47:29 +00:00
|
|
|
|
2018-02-22 10:39:23 +00:00
|
|
|
ret = mmap(NULL, (size + 4095) & -4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, 0, 0);
|
2018-02-22 13:14:23 +00:00
|
|
|
if (ret == MAP_FAILED)
|
|
|
|
return NULL;
|
|
|
|
if (pad >= sizeof(void *))
|
|
|
|
*(void **)(ret + pad - sizeof(void *)) = ret + pad;
|
|
|
|
return ret + pad;
|
2017-11-22 14:47:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* frees an area <area> of size <size> allocated by pool_alloc_area(). The
|
|
|
|
* semantics are identical to free() except that the size must absolutely match
|
2018-02-22 13:14:23 +00:00
|
|
|
* the one passed to pool_alloc_area(). In case some padding is added, the
|
|
|
|
* area's start address is compared to the one at the end of the padding, and
|
|
|
|
* a segfault is triggered if they don't match, indicating an underflow.
|
2017-11-22 14:47:29 +00:00
|
|
|
*/
|
|
|
|
static inline void pool_free_area(void *area, size_t size)
|
|
|
|
{
|
|
|
|
size_t pad = (4096 - size) & 0xFF0;
|
|
|
|
|
2018-02-22 13:14:23 +00:00
|
|
|
if (pad >= sizeof(void *) && *(void **)(area - sizeof(void *)) != area)
|
|
|
|
*(volatile int *)0 = 0;
|
|
|
|
|
2017-11-22 14:47:29 +00:00
|
|
|
munmap(area - pad, (size + 4095) & -4096);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* DEBUG_UAF */
|
|
|
|
|
2014-12-08 15:35:23 +00:00
|
|
|
/*
|
|
|
|
* Returns a pointer to type <type> taken from the pool <pool_type> or
|
|
|
|
* dynamically allocated. In the first case, <pool_type> is updated to point to
|
|
|
|
* the next element in the list. Memory poisonning is performed if enabled.
|
|
|
|
*/
|
2017-11-24 16:34:44 +00:00
|
|
|
static inline void *pool_alloc(struct pool_head *pool)
|
2014-12-08 15:35:23 +00:00
|
|
|
{
|
|
|
|
void *p;
|
|
|
|
|
|
|
|
p = pool_alloc_dirty(pool);
|
2015-10-28 14:23:51 +00:00
|
|
|
#ifdef DEBUG_MEMORY_POOLS
|
|
|
|
if (p) {
|
2017-11-07 09:42:54 +00:00
|
|
|
HA_SPIN_LOCK(POOL_LOCK, &pool->lock);
|
2015-10-28 14:23:51 +00:00
|
|
|
/* keep track of where the element was allocated from */
|
|
|
|
*POOL_LINK(pool, p) = (void *)pool;
|
2017-11-07 09:42:54 +00:00
|
|
|
HA_SPIN_UNLOCK(POOL_LOCK, &pool->lock);
|
2015-10-28 14:23:51 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (p && mem_poison_byte >= 0) {
|
2014-12-08 15:35:23 +00:00
|
|
|
memset(p, mem_poison_byte, pool->size);
|
2015-10-28 14:23:51 +00:00
|
|
|
}
|
|
|
|
|
2014-12-08 15:35:23 +00:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2007-05-13 16:26:08 +00:00
|
|
|
/*
|
|
|
|
* Puts a memory area back to the corresponding pool.
|
|
|
|
* Items are chained directly through a pointer that
|
|
|
|
* is written in the beginning of the memory area, so
|
|
|
|
* there's no need for any carrier cell. This implies
|
|
|
|
* that each memory area is at least as big as one
|
2008-08-03 15:41:33 +00:00
|
|
|
* pointer. Just like with the libc's free(), nothing
|
|
|
|
* is done if <ptr> is NULL.
|
2007-05-13 16:26:08 +00:00
|
|
|
*/
|
2017-11-24 16:34:44 +00:00
|
|
|
static inline void pool_free(struct pool_head *pool, void *ptr)
|
2014-12-23 13:13:16 +00:00
|
|
|
{
|
|
|
|
if (likely(ptr != NULL)) {
|
2017-11-07 09:42:54 +00:00
|
|
|
HA_SPIN_LOCK(POOL_LOCK, &pool->lock);
|
2015-10-28 14:23:51 +00:00
|
|
|
#ifdef DEBUG_MEMORY_POOLS
|
|
|
|
/* we'll get late corruption if we refill to the wrong pool or double-free */
|
|
|
|
if (*POOL_LINK(pool, ptr) != (void *)pool)
|
|
|
|
*(int *)0 = 0;
|
|
|
|
#endif
|
2017-11-22 14:47:29 +00:00
|
|
|
|
|
|
|
#ifndef DEBUG_UAF /* normal pool behaviour */
|
2015-10-28 14:09:29 +00:00
|
|
|
*POOL_LINK(pool, ptr) = (void *)pool->free_list;
|
2014-12-23 13:13:16 +00:00
|
|
|
pool->free_list = (void *)ptr;
|
2017-11-22 14:47:29 +00:00
|
|
|
#else /* release the entry for real to detect use after free */
|
|
|
|
/* ensure we crash on double free or free of a const area*/
|
|
|
|
*(uint32_t *)ptr = 0xDEADADD4;
|
|
|
|
pool_free_area(ptr, pool->size + POOL_EXTRA);
|
|
|
|
pool->allocated--;
|
|
|
|
#endif /* DEBUG_UAF */
|
2014-12-23 13:13:16 +00:00
|
|
|
pool->used--;
|
2017-11-07 09:42:54 +00:00
|
|
|
HA_SPIN_UNLOCK(POOL_LOCK, &pool->lock);
|
2014-12-23 13:13:16 +00:00
|
|
|
}
|
|
|
|
}
|
2018-02-22 13:05:55 +00:00
|
|
|
#endif /* CONFIG_HAP_LOCKLESS_POOLS */
|
2006-06-29 15:53:05 +00:00
|
|
|
#endif /* _COMMON_MEMORY_H */
|
2006-06-26 00:48:02 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Local variables:
|
|
|
|
* c-indent-level: 8
|
|
|
|
* c-basic-offset: 8
|
|
|
|
* End:
|
|
|
|
*/
|