From eb8fd55677560b865d251273aa09ba65a99a2899 Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Thu, 6 Dec 2012 20:52:53 +0900 Subject: [PATCH] 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). --- include/QF/Makefile.am | 8 +- include/QF/alloc.h | 97 ++++++++++++++++++++++++ {tools/qfcc/include => include/QF}/set.h | 10 +-- libs/util/Makefile.am | 6 +- {tools/qfcc/source => libs/util}/set.c | 5 +- tools/qfcc/include/Makefile.am | 4 +- tools/qfcc/include/qfcc.h | 53 ------------- tools/qfcc/source/Makefile.am | 2 +- tools/qfcc/source/dags.c | 3 +- tools/qfcc/source/debug.c | 1 + tools/qfcc/source/def.c | 7 +- tools/qfcc/source/defspace.c | 7 +- tools/qfcc/source/dot_dag.c | 8 +- tools/qfcc/source/dot_flow.c | 8 +- tools/qfcc/source/expr.c | 9 ++- tools/qfcc/source/flow.c | 3 +- tools/qfcc/source/function.c | 1 + tools/qfcc/source/grab.c | 1 + tools/qfcc/source/linker.c | 1 + tools/qfcc/source/reloc.c | 2 + tools/qfcc/source/statements.c | 1 + tools/qfcc/source/symtab.c | 1 + tools/qfcc/source/type.c | 1 + tools/qfcc/source/value.c | 1 + 24 files changed, 149 insertions(+), 91 deletions(-) create mode 100644 include/QF/alloc.h rename {tools/qfcc/include => include/QF}/set.h (98%) rename {tools/qfcc/source => libs/util}/set.c (99%) diff --git a/include/QF/Makefile.am b/include/QF/Makefile.am index bc10d9ddf..40edb3f5c 100644 --- a/include/QF/Makefile.am +++ b/include/QF/Makefile.am @@ -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 \ diff --git a/include/QF/alloc.h b/include/QF/alloc.h new file mode 100644 index 000000000..08ea9f097 --- /dev/null +++ b/include/QF/alloc.h @@ -0,0 +1,97 @@ +/* + alloc.h + + High-tide allocator. + + Copyright (C) 2012 Bill Currie + + Author: Bill Currie + 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 +#include + +/** \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 diff --git a/tools/qfcc/include/set.h b/include/QF/set.h similarity index 98% rename from tools/qfcc/include/set.h rename to include/QF/set.h index d62c55fb5..31a4de5d2 100644 --- a/tools/qfcc/include/set.h +++ b/include/QF/set.h @@ -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 diff --git a/libs/util/Makefile.am b/libs/util/Makefile.am index f59914e7e..2c8e206bd 100644 --- a/libs/util/Makefile.am +++ b/libs/util/Makefile.am @@ -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) diff --git a/tools/qfcc/source/set.c b/libs/util/set.c similarity index 99% rename from tools/qfcc/source/set.c rename to libs/util/set.c index a3ac20ca7..f80e476c0 100644 --- a/tools/qfcc/source/set.c +++ b/libs/util/set.c @@ -40,11 +40,10 @@ #include +#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) diff --git a/tools/qfcc/include/Makefile.am b/tools/qfcc/include/Makefile.am index 3c34ca1a1..140cae90a 100644 --- a/tools/qfcc/include/Makefile.am +++ b/tools/qfcc/include/Makefile.am @@ -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 diff --git a/tools/qfcc/include/qfcc.h b/tools/qfcc/include/qfcc.h index e10cd29d6..3aa464185 100644 --- a/tools/qfcc/include/qfcc.h +++ b/tools/qfcc/include/qfcc.h @@ -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. diff --git a/tools/qfcc/source/Makefile.am b/tools/qfcc/source/Makefile.am index 0a0dd89c0..b2c5f0711 100644 --- a/tools/qfcc/source/Makefile.am +++ b/tools/qfcc/source/Makefile.am @@ -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) diff --git a/tools/qfcc/source/dags.c b/tools/qfcc/source/dags.c index b3493b34b..402cfc2b9 100644 --- a/tools/qfcc/source/dags.c +++ b/tools/qfcc/source/dags.c @@ -39,14 +39,15 @@ #endif #include +#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" diff --git a/tools/qfcc/source/debug.c b/tools/qfcc/source/debug.c index 6082ef598..8b3827337 100644 --- a/tools/qfcc/source/debug.c +++ b/tools/qfcc/source/debug.c @@ -40,6 +40,7 @@ #include #include +#include "QF/alloc.h" #include "QF/pr_comp.h" #include "debug.h" diff --git a/tools/qfcc/source/def.c b/tools/qfcc/source/def.c index c56641534..e1f164e80 100644 --- a/tools/qfcc/source/def.c +++ b/tools/qfcc/source/def.c @@ -40,9 +40,10 @@ #endif #include -#include -#include -#include +#include "QF/alloc.h" +#include "QF/hash.h" +#include "QF/sys.h" +#include "QF/va.h" #include "qfcc.h" #include "def.h" diff --git a/tools/qfcc/source/defspace.c b/tools/qfcc/source/defspace.c index bfa5a47aa..64407690e 100644 --- a/tools/qfcc/source/defspace.c +++ b/tools/qfcc/source/defspace.c @@ -39,9 +39,10 @@ #endif #include -#include -#include -#include +#include "QF/alloc.h" +#include "QF/hash.h" +#include "QF/sys.h" +#include "QF/va.h" #include "qfcc.h" #include "defspace.h" diff --git a/tools/qfcc/source/dot_dag.c b/tools/qfcc/source/dot_dag.c index 6fd28d022..85a687079 100644 --- a/tools/qfcc/source/dot_dag.c +++ b/tools/qfcc/source/dot_dag.c @@ -39,12 +39,12 @@ #endif #include -#include -#include -#include +#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" diff --git a/tools/qfcc/source/dot_flow.c b/tools/qfcc/source/dot_flow.c index 141db49ac..0ad4dacb4 100644 --- a/tools/qfcc/source/dot_flow.c +++ b/tools/qfcc/source/dot_flow.c @@ -39,15 +39,15 @@ #endif #include -#include -#include -#include +#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" diff --git a/tools/qfcc/source/expr.c b/tools/qfcc/source/expr.c index 542756d20..c0c1d935d 100644 --- a/tools/qfcc/source/expr.c +++ b/tools/qfcc/source/expr.c @@ -39,10 +39,11 @@ #endif #include -#include -#include -#include -#include +#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" diff --git a/tools/qfcc/source/flow.c b/tools/qfcc/source/flow.c index 93756e17c..07671de22 100644 --- a/tools/qfcc/source/flow.c +++ b/tools/qfcc/source/flow.c @@ -39,7 +39,9 @@ #endif #include +#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" diff --git a/tools/qfcc/source/function.c b/tools/qfcc/source/function.c index feba863bd..f2f7964a0 100644 --- a/tools/qfcc/source/function.c +++ b/tools/qfcc/source/function.c @@ -39,6 +39,7 @@ #endif #include +#include "QF/alloc.h" #include "QF/dstring.h" #include "QF/hash.h" #include "QF/va.h" diff --git a/tools/qfcc/source/grab.c b/tools/qfcc/source/grab.c index 8aba16230..4c9f6e5fd 100644 --- a/tools/qfcc/source/grab.c +++ b/tools/qfcc/source/grab.c @@ -40,6 +40,7 @@ #include +#include "QF/alloc.h" #include "QF/hash.h" #include "diagnostic.h" diff --git a/tools/qfcc/source/linker.c b/tools/qfcc/source/linker.c index 7d843af6b..50c2ce015 100644 --- a/tools/qfcc/source/linker.c +++ b/tools/qfcc/source/linker.c @@ -50,6 +50,7 @@ #include #include +#include "QF/alloc.h" #include "QF/dstring.h" #include "QF/hash.h" #include "QF/pakfile.h" diff --git a/tools/qfcc/source/reloc.c b/tools/qfcc/source/reloc.c index eb9f731ad..6b0eac612 100644 --- a/tools/qfcc/source/reloc.c +++ b/tools/qfcc/source/reloc.c @@ -39,6 +39,8 @@ #endif #include +#include "QF/alloc.h" + #include "codespace.h" #include "def.h" #include "defspace.h" diff --git a/tools/qfcc/source/statements.c b/tools/qfcc/source/statements.c index 9e05d1193..37ead9ccf 100644 --- a/tools/qfcc/source/statements.c +++ b/tools/qfcc/source/statements.c @@ -39,6 +39,7 @@ #include +#include "QF/alloc.h" #include "QF/va.h" #include "dags.h" diff --git a/tools/qfcc/source/symtab.c b/tools/qfcc/source/symtab.c index c729a064f..ff2c0f4d6 100644 --- a/tools/qfcc/source/symtab.c +++ b/tools/qfcc/source/symtab.c @@ -34,6 +34,7 @@ #include #include +#include "QF/alloc.h" #include "QF/hash.h" #include "class.h" diff --git a/tools/qfcc/source/type.c b/tools/qfcc/source/type.c index b6122ef23..3a9428c92 100644 --- a/tools/qfcc/source/type.c +++ b/tools/qfcc/source/type.c @@ -41,6 +41,7 @@ #include #include +#include "QF/alloc.h" #include "QF/dstring.h" #include "QF/hash.h" #include "QF/sys.h" diff --git a/tools/qfcc/source/value.c b/tools/qfcc/source/value.c index 6234a1ce6..d481c4d3b 100644 --- a/tools/qfcc/source/value.c +++ b/tools/qfcc/source/value.c @@ -39,6 +39,7 @@ #endif #include +#include "QF/alloc.h" #include "QF/dstring.h" #include "QF/hash.h" #include "QF/mathlib.h"