2006-06-26 00:48:02 +00:00
|
|
|
/*
|
2006-06-29 15:53:05 +00:00
|
|
|
include/common/memory.h
|
2006-06-26 00:48:02 +00:00
|
|
|
Memory management definitions..
|
|
|
|
|
2007-05-13 16:26:08 +00:00
|
|
|
Copyright (C) 2000-2007 Willy Tarreau - w@1wt.eu
|
2006-06-26 00:48:02 +00:00
|
|
|
|
|
|
|
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-29 15:53:05 +00:00
|
|
|
#ifndef _COMMON_MEMORY_H
|
|
|
|
#define _COMMON_MEMORY_H
|
2006-06-26 00:48:02 +00:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
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>
|
2006-06-26 00:48:02 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns a pointer to an area of <__len> bytes taken from the pool <pool> or
|
|
|
|
* dynamically allocated. In the first case, <__pool> is updated to point to
|
|
|
|
* the next element in the list.
|
|
|
|
*/
|
|
|
|
#define pool_alloc_from(__pool, __len) \
|
|
|
|
({ \
|
|
|
|
void *__p; \
|
|
|
|
if ((__p = (__pool)) == NULL) \
|
|
|
|
__p = malloc(((__len) >= sizeof (void *)) ? \
|
|
|
|
(__len) : sizeof(void *)); \
|
|
|
|
else { \
|
|
|
|
__pool = *(void **)(__pool); \
|
|
|
|
} \
|
|
|
|
__p; \
|
|
|
|
})
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
#define pool_free_to(__pool, __ptr) \
|
|
|
|
({ \
|
|
|
|
*(void **)(__ptr) = (void *)(__pool); \
|
|
|
|
__pool = (void *)(__ptr); \
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef CONFIG_HAP_MEM_OPTIM
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
#define pool_alloc(type) \
|
|
|
|
({ \
|
|
|
|
void *__p; \
|
|
|
|
if ((__p = pool_##type) == NULL) \
|
|
|
|
__p = malloc(sizeof_##type); \
|
|
|
|
else { \
|
|
|
|
pool_##type = *(void **)pool_##type; \
|
|
|
|
} \
|
|
|
|
__p; \
|
|
|
|
})
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
#define pool_free(type, ptr) \
|
|
|
|
({ \
|
|
|
|
*(void **)ptr = (void *)pool_##type; \
|
|
|
|
pool_##type = (void *)ptr; \
|
|
|
|
})
|
|
|
|
|
|
|
|
#else
|
|
|
|
#define pool_alloc(type) (calloc(1,sizeof_##type))
|
|
|
|
#define pool_free(type, ptr) (free(ptr))
|
|
|
|
#endif /* CONFIG_HAP_MEM_OPTIM */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function destroys a pull by freeing it completely.
|
|
|
|
* This should be called only under extreme circumstances.
|
|
|
|
*/
|
|
|
|
static inline void pool_destroy(void **pool)
|
|
|
|
{
|
|
|
|
void *temp, *next;
|
|
|
|
next = pool;
|
|
|
|
while (next) {
|
|
|
|
temp = next;
|
|
|
|
next = *(void **)temp;
|
|
|
|
free(temp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-05-13 16:26:08 +00:00
|
|
|
|
|
|
|
/******* pools version 2 ********/
|
|
|
|
|
|
|
|
#define MEM_F_SHARED 0x1
|
|
|
|
|
|
|
|
struct pool_head {
|
|
|
|
void **free_list;
|
|
|
|
struct list list; /* list of all known pools */
|
|
|
|
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 */
|
|
|
|
char name[12]; /* name of the pool */
|
2007-05-13 16:26:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* Allocate a new entry for pool <pool>, and return it for immediate use.
|
|
|
|
* NULL is returned if no memory is available for a new creation.
|
|
|
|
*/
|
2007-05-13 17:38:49 +00:00
|
|
|
void *pool_refill_alloc(struct pool_head *pool);
|
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.
|
|
|
|
*/
|
|
|
|
void dump_pools(void);
|
|
|
|
|
2007-05-13 17:38:49 +00:00
|
|
|
/*
|
|
|
|
* This function frees whatever can be freed in pool <pool>.
|
|
|
|
*/
|
|
|
|
void pool_flush2(struct pool_head *pool);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function frees whatever can be freed in all pools, but respecting
|
|
|
|
* the minimum thresholds imposed by owners.
|
|
|
|
*/
|
|
|
|
void pool_gc2();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function destroys a pull by freeing it completely.
|
|
|
|
* This should be called only under extreme circumstances.
|
|
|
|
*/
|
2007-05-13 22:39:29 +00:00
|
|
|
void *pool_destroy2(struct pool_head *pool);
|
2007-05-13 17:38:49 +00:00
|
|
|
|
2007-05-13 16:26:08 +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.
|
|
|
|
*/
|
|
|
|
#define pool_alloc2(pool) \
|
|
|
|
({ \
|
|
|
|
void *__p; \
|
2007-05-13 17:38:49 +00:00
|
|
|
if ((__p = pool->free_list) == NULL) \
|
|
|
|
__p = pool_refill_alloc(pool); \
|
2007-05-13 16:26:08 +00:00
|
|
|
else { \
|
2007-05-13 17:38:49 +00:00
|
|
|
pool->free_list = *(void **)pool->free_list; \
|
|
|
|
pool->used++; \
|
2007-05-13 16:26:08 +00:00
|
|
|
} \
|
|
|
|
__p; \
|
|
|
|
})
|
|
|
|
|
|
|
|
/*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
#define pool_free2(pool, ptr) \
|
|
|
|
({ \
|
2007-05-13 17:38:49 +00:00
|
|
|
*(void **)ptr = (void *)pool->free_list; \
|
|
|
|
pool->free_list = (void *)ptr; \
|
|
|
|
pool->used--; \
|
2007-05-13 16:26:08 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
|
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:
|
|
|
|
*/
|