Move set.c into libQFutil.

Also move the ALLOC/FREE macros from qfcc.h to QF/alloc.h (needed to for
set.c).

Both modules are more generally useful than just for qfcc (eg, set
builtins for ruamoko).
This commit is contained in:
Bill Currie 2012-12-06 20:52:53 +09:00
parent b28ac6672b
commit eb8fd55677
24 changed files with 149 additions and 91 deletions

View file

@ -1,15 +1,15 @@
AUTOMAKE_OPTIONS = foreign
pkgincludedir = $(includedir)/QF
nobase_pkginclude_HEADERS = \
bspfile.h cbuf.h cdaudio.h checksum.h clip_hull.h cmd.h \
alloc.h bspfile.h cbuf.h cdaudio.h checksum.h clip_hull.h cmd.h \
console.h crc.h csqc.h cvar.h dstring.h draw.h gib.h hash.h hl.h \
idparse.h image.h in_event.h info.h input.h iqm.h joystick.h keys.h \
link.h llist.h locs.h mathlib.h mdfour.h model.h modelgen.h msg.h \
object.h pak.h pakfile.h pcx.h png.h plugin.h pr_comp.h pr_debug.h \
pr_obj.h progs.h qargs.h qdefs.h qendian.h qfplist.h qtypes.h quakefs.h \
quakeio.h render.h riff.h ruamoko.h screen.h script.h sizebuf.h skin.h \
sound.h spritegn.h sys.h teamplay.h tga.h uint32.h va.h ver_check.h vid.h \
vrect.h view.h wad.h wadfile.h winding.h zone.h \
quakeio.h render.h riff.h ruamoko.h set.h screen.h script.h sizebuf.h \
skin.h sound.h spritegn.h sys.h teamplay.h tga.h uint32.h va.h \
ver_check.h vid.h vrect.h view.h wad.h wadfile.h winding.h zone.h \
\
GL/ati.h GL/defines.h GL/extensions.h GL/funcs.h GL/qf_draw.h \
GL/qf_explosions.h GL/qf_funcs_list.h GL/qf_iqm.h GL/qf_lightmap.h \

97
include/QF/alloc.h Normal file
View file

@ -0,0 +1,97 @@
/*
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
*/
//@{
#ifndef DEBUG_QF_MEMORY
/** High-tide structure allocator for use in linked lists.
Using a free-list with the name of \c free_NAME, return a single element.
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.
elements may be returned to the pool by linking them into the free-list.
\param s The number of structures in the block.
\param t The structure type.
\param n The \c NAME portion of the \c free_NAME free-list.
\param v The destination of the pointer to the allocated
element. The contents of the allocated element will be
memset to 0.
\hideinitializer
*/
#define ALLOC(s, t, n, v) \
do { \
if (!free_##n) { \
int i; \
free_##n = malloc ((s) * sizeof (t)); \
for (i = 0; i < (s) - 1; i++) \
free_##n[i].next = &free_##n[i + 1];\
free_##n[i].next = 0; \
} \
v = free_##n; \
free_##n = free_##n->next; \
memset (v, 0, sizeof (*v)); \
} while (0)
/** Free a block allocated by #ALLOC
\param n The \c NAME portion of the \c free_NAME free-list.
\param p The pointer to the block to be freed.
\hideinitializer
*/
#define FREE(n, p) \
do { \
p->next = free_##n; \
free_##n = p->next; \
} while (0)
#else
#define ALLOC(s, t, n, v) \
do { \
__attribute__((unused)) t **dummy = &free_##n; \
v = (t *) calloc (1, sizeof (t)); \
} while (0)
#define FREE(n, p) do { free (p); } while (0)
#endif
//@}
#endif//__QF_alloc_h

View file

@ -28,11 +28,11 @@
*/
#ifndef set_h
#define set_h
#ifndef __QF_set_h
#define __QF_set_h
/** \defgroup qfcc_set Set handling
\ingroup qfcc
/** \defgroup set Set handling
\ingroup utils
*/
//@{
@ -319,4 +319,4 @@ set_iter_t *set_next (set_iter_t *set_iter);
const char *set_as_string (const set_t *set);
//@}
#endif//set_h
#endif//__QF_set_h

View file

@ -52,8 +52,8 @@ libQFutil_la_SOURCES= \
bspfile.c buildnum.c cbuf.c checksum.c cmd.c crc.c cvar.c dstring.c \
fendian.c hash.c idparse.c info.c link.c llist.c \
mathlib.c mdfour.c msg.c pakfile.c plugin.c qargs.c qendian.c \
qfplist.c quakefs.c quakeio.c riff.c script.c sizebuf.c string.c sys.c \
va.c ver_check.c vrect.c wad.c wadfile.c zone.c $(dirent) $(fnmatch) \
$(getopt)
qfplist.c quakefs.c quakeio.c riff.c script.c set.c sizebuf.c string.c \
sys.c va.c ver_check.c vrect.c wad.c wadfile.c zone.c \
$(dirent) $(fnmatch) $(getopt)
EXTRA_DIST= $(fnmatch_src) $(getopt_src)

View file

@ -40,11 +40,10 @@
#include <stdlib.h>
#include "QF/alloc.h"
#include "QF/dstring.h"
#include "QF/mathlib.h"
#include "qfcc.h"
#include "set.h"
#include "QF/set.h"
#define BITS (sizeof (((set_t *) 0)->map[0]) * 8)

View file

@ -3,5 +3,5 @@ AUTOMAKE_OPTIONS= foreign
EXTRA_DIST= class.h codespace.h cpp.h dags.h debug.h def.h defspace.h \
diagnostic.h dot.h emit.h expr.h flow.h function.h grab.h idstuff.h \
linker.h method.h obj_file.h obj_type.h opcodes.h options.h pragma.h \
qfcc.h qfprogs.h reloc.h set.h shared.h statements.h strpool.h \
struct.h switch.h symtab.h type.h value.h
qfcc.h qfprogs.h reloc.h shared.h statements.h strpool.h struct.h \
switch.h symtab.h type.h value.h

View file

@ -123,59 +123,6 @@ char *fix_backslash (char *path);
#define NORMALIZE(x) x
#endif
#ifndef DEBUG_QFCC_MEMORY
/** High-tide structure allocator for use in linked lists.
Using a free-list with the name of \c free_NAME, return a single element.
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.
elements may be returned to the pool by linking them into the free-list.
\param s The number of structures in the block.
\param t The structure type.
\param n The \c NAME portion of the \c free_NAME free-list.
\param v The destination of the pointer to the allocated
element. The contents of the allocated element will be
memset to 0.
\hideinitializer
*/
#define ALLOC(s, t, n, v) \
do { \
if (!free_##n) { \
int i; \
free_##n = malloc ((s) * sizeof (t)); \
for (i = 0; i < (s) - 1; i++) \
free_##n[i].next = &free_##n[i + 1];\
free_##n[i].next = 0; \
} \
v = free_##n; \
free_##n = free_##n->next; \
memset (v, 0, sizeof (*v)); \
} while (0)
/** Free a block allocated by #ALLOC
\param n The \c NAME portion of the \c free_NAME free-list.
\param p The pointer to the block to be freed.
\hideinitializer
*/
#define FREE(n, p) \
do { \
p->next = free_##n; \
free_##n = p->next; \
} while (0)
#else
#define ALLOC(s, t, n, v) \
do { \
__attribute__((unused)) t **dummy = &free_##n; \
v = (t *) calloc (1, sizeof (t)); \
} while (0)
#define FREE(n, p) do { free (p); } while (0)
#endif
/** Round \a x up to the next multiple of \a a.
\note \a a must be a power of two or this will break.
\note There are no side effects on \a x.

View file

@ -42,7 +42,7 @@ common_src=\
class.c codespace.c constfold.c cpp.c dags.c debug.c def.c defspace.c \
diagnostic.c dot.c dot_dag.c dot_expr.c dot_flow.c dot_sblock.c emit.c \
expr.c flow.c function.c grab.c idstuff.c linker.c method.c obj_file.c \
obj_type.c opcodes.c options.c pragma.c qfcc.c reloc.c set.c shared.c \
obj_type.c opcodes.c options.c pragma.c qfcc.c reloc.c shared.c \
statements.c strpool.c struct.c switch.c symtab.c type.c value.c
qfcc_SOURCES= qc-lex.l qc-parse.y qp-lex.l qp-parse.y $(common_src)

View file

@ -39,14 +39,15 @@
#endif
#include <stdlib.h>
#include "QF/alloc.h"
#include "QF/dstring.h"
#include "QF/mathlib.h"
#include "QF/set.h"
#include "dags.h"
#include "diagnostic.h"
#include "flow.h"
#include "qfcc.h"
#include "set.h"
#include "statements.h"
#include "strpool.h"
#include "symtab.h"

View file

@ -40,6 +40,7 @@
#include <stdlib.h>
#include <ctype.h>
#include "QF/alloc.h"
#include "QF/pr_comp.h"
#include "debug.h"

View file

@ -40,9 +40,10 @@
#endif
#include <stdlib.h>
#include <QF/hash.h>
#include <QF/sys.h>
#include <QF/va.h>
#include "QF/alloc.h"
#include "QF/hash.h"
#include "QF/sys.h"
#include "QF/va.h"
#include "qfcc.h"
#include "def.h"

View file

@ -39,9 +39,10 @@
#endif
#include <stdlib.h>
#include <QF/hash.h>
#include <QF/sys.h>
#include <QF/va.h>
#include "QF/alloc.h"
#include "QF/hash.h"
#include "QF/sys.h"
#include "QF/va.h"
#include "qfcc.h"
#include "defspace.h"

View file

@ -39,12 +39,12 @@
#endif
#include <stdlib.h>
#include <QF/dstring.h>
#include <QF/quakeio.h>
#include <QF/va.h>
#include "QF/dstring.h"
#include "QF/quakeio.h"
#include "QF/set.h"
#include "QF/va.h"
#include "dags.h"
#include "set.h"
#include "statements.h"
#include "strpool.h"
#include "symtab.h"

View file

@ -39,15 +39,15 @@
#endif
#include <stdlib.h>
#include <QF/dstring.h>
#include <QF/quakeio.h>
#include <QF/va.h>
#include "QF/dstring.h"
#include "QF/quakeio.h"
#include "QF/set.h"
#include "QF/va.h"
#include "dags.h"
#include "flow.h"
#include "function.h"
#include "expr.h"
#include "set.h"
#include "statements.h"
#include "strpool.h"

View file

@ -39,10 +39,11 @@
#endif
#include <stdlib.h>
#include <QF/dstring.h>
#include <QF/mathlib.h>
#include <QF/sys.h>
#include <QF/va.h>
#include "QF/alloc.h"
#include "QF/dstring.h"
#include "QF/mathlib.h"
#include "QF/sys.h"
#include "QF/va.h"
#include "qfcc.h"
#include "class.h"

View file

@ -39,7 +39,9 @@
#endif
#include <stdlib.h>
#include "QF/alloc.h"
#include "QF/dstring.h"
#include "QF/set.h"
#include "QF/va.h"
#include "dags.h"
@ -50,7 +52,6 @@
#include "function.h"
#include "options.h"
#include "qfcc.h"
#include "set.h"
#include "statements.h"
#include "symtab.h"
#include "type.h"

View file

@ -39,6 +39,7 @@
#endif
#include <stdlib.h>
#include "QF/alloc.h"
#include "QF/dstring.h"
#include "QF/hash.h"
#include "QF/va.h"

View file

@ -40,6 +40,7 @@
#include <ctype.h>
#include "QF/alloc.h"
#include "QF/hash.h"
#include "diagnostic.h"

View file

@ -50,6 +50,7 @@
#include <fcntl.h>
#include <errno.h>
#include "QF/alloc.h"
#include "QF/dstring.h"
#include "QF/hash.h"
#include "QF/pakfile.h"

View file

@ -39,6 +39,8 @@
#endif
#include <stdlib.h>
#include "QF/alloc.h"
#include "codespace.h"
#include "def.h"
#include "defspace.h"

View file

@ -39,6 +39,7 @@
#include <stdlib.h>
#include "QF/alloc.h"
#include "QF/va.h"
#include "dags.h"

View file

@ -34,6 +34,7 @@
#include <stdlib.h>
#include <string.h>
#include "QF/alloc.h"
#include "QF/hash.h"
#include "class.h"

View file

@ -41,6 +41,7 @@
#include <ctype.h>
#include <ctype.h>
#include "QF/alloc.h"
#include "QF/dstring.h"
#include "QF/hash.h"
#include "QF/sys.h"

View file

@ -39,6 +39,7 @@
#endif
#include <stdlib.h>
#include "QF/alloc.h"
#include "QF/dstring.h"
#include "QF/hash.h"
#include "QF/mathlib.h"