From 8561dfa93ad4ecd3f38c2294add2b466e884f1f1 Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Wed, 30 Jan 2002 06:21:20 +0000 Subject: [PATCH] basic resource management system for the VM --- include/QF/progs.h | 20 +++++++ libs/console/menu.c | 11 ++-- libs/gamecode/engine/Makefile.am | 2 +- libs/gamecode/engine/pr_load.c | 11 ++++ libs/gamecode/engine/pr_resource.c | 95 ++++++++++++++++++++++++++++++ 5 files changed, 134 insertions(+), 5 deletions(-) create mode 100644 libs/gamecode/engine/pr_resource.c diff --git a/include/QF/progs.h b/include/QF/progs.h index 929bd54cd..db99ce3b2 100644 --- a/include/QF/progs.h +++ b/include/QF/progs.h @@ -64,6 +64,11 @@ typedef struct progs_s progs_t; #define PROGS_T #endif +#ifndef PR_RESOURCE_T +typedef struct pr_resource_s pr_resource_t; +#define PR_RESOURCE_T +#endif + //============================================================================ void PR_Init (void); @@ -184,6 +189,17 @@ char *PR_GetString(progs_t *pr, int num); int PR_SetString(progs_t *pr, const char *s); void PR_GarbageCollect (progs_t *pr); +// +// PR Resources stuff +// + +void +PR_Resources_Init (progs_t *pr); +void PR_Resources_Clear (progs_t *pr); +void PR_Resources_Register (progs_t *pr, const char *name, void *data, + void (*clear)(progs_t *, void *)); +void *PR_Resources_Find (progs_t *pr, const char *name); + // // PR Zone stuff // @@ -299,10 +315,14 @@ struct progs_s { void (*free_edict)(progs_t *pr, edict_t *ent); void *(*allocate_progs_mem)(progs_t *pr, int size); + void (*free_progs_mem)(progs_t *pr, void *mem); builtin_t **builtins; int numbuiltins; + pr_resource_t *resources; + struct hashtab_s *resource_hash; + // debug info char *debugfile; struct pr_debug_header_s *debug; diff --git a/libs/console/menu.c b/libs/console/menu.c index 578208380..ec8164a7f 100644 --- a/libs/console/menu.c +++ b/libs/console/menu.c @@ -322,11 +322,18 @@ menu_allocate_progs_mem (progs_t *pr, int size) return malloc (size); } +static void +menu_free_progs_mem (progs_t *pr, void *mem) +{ + free (mem); +} + void Menu_Init (void) { menu_pr_state.progs_name = "menu.dat"; menu_pr_state.allocate_progs_mem = menu_allocate_progs_mem; + menu_pr_state.free_progs_mem = menu_free_progs_mem; menu_hash = Hash_NewTable (61, menu_get_key, menu_free, 0); @@ -365,10 +372,6 @@ Menu_Load (void) menu_pr_state.time = con_data.realtime; - if (menu_pr_state.progs) { - free (menu_pr_state.progs); - menu_pr_state.progs = 0; - } Hash_FlushTable (menu_hash); menu = 0; top_menu = 0; diff --git a/libs/gamecode/engine/Makefile.am b/libs/gamecode/engine/Makefile.am index ce7ceeeac..aea79ea9b 100644 --- a/libs/gamecode/engine/Makefile.am +++ b/libs/gamecode/engine/Makefile.am @@ -7,4 +7,4 @@ lib_LTLIBRARIES= libQFgamecode.la libQFgamecode_la_LDFLAGS= -version-info 1:0:0 libQFgamecode_la_SOURCES= \ pr_builtins.c pr_edict.c pr_debug.c pr_exec.c pr_load.c pr_opcode.c \ - pr_resolve.c pr_strings.c pr_zone.c + pr_resolve.c pr_resource.c pr_strings.c pr_zone.c diff --git a/libs/gamecode/engine/pr_load.c b/libs/gamecode/engine/pr_load.c index 0c6cc2eb8..c7c5f6451 100644 --- a/libs/gamecode/engine/pr_load.c +++ b/libs/gamecode/engine/pr_load.c @@ -75,6 +75,11 @@ allocate_progs_mem (progs_t *pr, int size) return Hunk_AllocName (size, pr->progs_name); } +static void +free_progs_mem (progs_t *pr, void *mem) +{ +} + void PR_LoadProgsFile (progs_t * pr, VFile *file, int size, int edicts, int zone) { @@ -143,6 +148,12 @@ PR_LoadProgsFile (progs_t * pr, VFile *file, int size, int edicts, int zone) if (!pr->allocate_progs_mem) pr->allocate_progs_mem = allocate_progs_mem; + if (!pr->free_progs_mem) + pr->free_progs_mem = free_progs_mem; + + PR_Resources_Clear (pr); + if (pr->progs) + pr->free_progs_mem (pr, pr->progs); pr->progs = pr->allocate_progs_mem (pr, pr->progs_size + pr->zone_size + pr->pr_edictareasize); if (!pr->progs) diff --git a/libs/gamecode/engine/pr_resource.c b/libs/gamecode/engine/pr_resource.c new file mode 100644 index 000000000..09d194e80 --- /dev/null +++ b/libs/gamecode/engine/pr_resource.c @@ -0,0 +1,95 @@ +/* + pr_resource.c + + progs resource management + + Copyright (C) 2001 Bill Currie + + Author: Bill Currie + Date: 2002/1/29 + + 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 + +*/ +static const char rcsid[] = + "$Id$"; + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#include + +#include "QF/hash.h" +#include "QF/progs.h" +#include "QF/sys.h" + +struct pr_resource_s { + const char *name; + pr_resource_t *next; + void *data; + void (*clear)(progs_t *pr, void *data); +}; + +static const char * +resource_get_key (void *r, void *unused) +{ + return ((pr_resource_t *)r)->name; +} + +void +PR_Resources_Init (progs_t *pr) +{ + pr->resource_hash = Hash_NewTable (1021, resource_get_key, 0, 0); + pr->resources = 0; +} + +void +PR_Resources_Clear (progs_t *pr) +{ + pr_resource_t *res = pr->resources; + while (res) { + res->clear (pr, res->data); + res = res->next; + } +} + +void +PR_Resources_Register (progs_t *pr, const char *name, void *data, + void (*clear)(progs_t *, void *)) +{ + pr_resource_t *res = malloc (sizeof (pr_resource_t)); + if (!res) + Sys_Error ("PR_Resources_Register: out of memory"); + res->name = name; + res->data = data; + res->clear = clear; + res->next = pr->resources; + pr->resources = res->next; + Hash_Add (pr->resource_hash, res); +} + +void * +PR_Resources_Find (progs_t *pr, const char *name) +{ + pr_resource_t *res = Hash_Find (pr->resource_hash, name); + if (!res) + return 0; + return res->data; +}