2012-12-06 11:52:53 +00:00
|
|
|
/*
|
|
|
|
alloc.h
|
|
|
|
|
|
|
|
High-tide allocator.
|
|
|
|
|
|
|
|
Copyright (C) 2012 Bill Currie <bill@taniwha.org>
|
|
|
|
|
|
|
|
Author: Bill Currie <bill@taniwha.org>
|
|
|
|
Date: 2012/12/06
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program 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 General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to:
|
|
|
|
|
|
|
|
Free Software Foundation, Inc.
|
|
|
|
59 Temple Place - Suite 330
|
|
|
|
Boston, MA 02111-1307, USA
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef __QF_alloc_h
|
|
|
|
#define __QF_alloc_h
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
/** \defgroup alloc High-tide allocator.
|
|
|
|
\ingroup utils
|
|
|
|
*/
|
2020-02-11 06:20:49 +00:00
|
|
|
///@{
|
2012-12-06 11:52:53 +00:00
|
|
|
|
|
|
|
#ifndef DEBUG_QF_MEMORY
|
|
|
|
/** High-tide structure allocator for use in linked lists.
|
|
|
|
|
2013-03-08 13:16:31 +00:00
|
|
|
Using a free-list with the name of \c NAME_freelist, return a single
|
|
|
|
element.
|
2012-12-06 11:52:53 +00:00
|
|
|
The type of the element must be a structure with a field named \c next.
|
|
|
|
When the free-list is empty, memory is claimed from the system in blocks.
|
2013-03-08 13:16:31 +00:00
|
|
|
Elements may be returned to the pool by linking them into the free-list.
|
2012-12-06 11:52:53 +00:00
|
|
|
|
|
|
|
\param s The number of structures in the block.
|
|
|
|
\param t The structure type.
|
2013-03-08 13:16:31 +00:00
|
|
|
\param n The \c NAME portion of the \c NAME_freelist free-list.
|
2012-12-06 11:52:53 +00:00
|
|
|
\param v The destination of the pointer to the allocated
|
|
|
|
element. The contents of the allocated element will be
|
|
|
|
memset to 0.
|
|
|
|
|
|
|
|
\hideinitializer
|
|
|
|
*/
|
2013-03-08 13:16:31 +00:00
|
|
|
#define ALLOC(s, t, n, v) \
|
|
|
|
do { \
|
|
|
|
if (!n##_freelist) { \
|
|
|
|
int i; \
|
|
|
|
n##_freelist = malloc ((s) * sizeof (t)); \
|
|
|
|
for (i = 0; i < (s) - 1; i++) \
|
|
|
|
n##_freelist[i].next = &n##_freelist[i + 1];\
|
|
|
|
n##_freelist[i].next = 0; \
|
|
|
|
} \
|
|
|
|
v = n##_freelist; \
|
|
|
|
n##_freelist = n##_freelist->next; \
|
|
|
|
memset (v, 0, sizeof (*v)); \
|
2012-12-06 11:52:53 +00:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
/** Free a block allocated by #ALLOC
|
|
|
|
|
2013-03-08 13:16:31 +00:00
|
|
|
\param n The \c NAME portion of the \c NAME_freelist free-list.
|
2012-12-06 11:52:53 +00:00
|
|
|
\param p The pointer to the block to be freed.
|
|
|
|
|
|
|
|
\hideinitializer
|
|
|
|
*/
|
2013-03-08 13:16:31 +00:00
|
|
|
#define FREE(n, p) \
|
|
|
|
do { \
|
|
|
|
p->next = n##_freelist; \
|
|
|
|
n##_freelist = p; \
|
2012-12-06 11:52:53 +00:00
|
|
|
} while (0)
|
|
|
|
#else
|
2013-03-08 13:16:31 +00:00
|
|
|
#define ALLOC(s, t, n, v) \
|
|
|
|
do { \
|
|
|
|
__attribute__((unused)) t **dummy = &n##_freelist; \
|
|
|
|
v = (t *) calloc (1, sizeof (t)); \
|
2012-12-06 11:52:53 +00:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
#define FREE(n, p) do { free (p); } while (0)
|
|
|
|
#endif
|
|
|
|
|
2020-02-11 06:20:49 +00:00
|
|
|
///@}
|
2012-12-06 11:52:53 +00:00
|
|
|
|
|
|
|
#endif//__QF_alloc_h
|