basic resource management system for the VM

This commit is contained in:
Bill Currie 2002-01-30 06:21:20 +00:00
parent 9c6def5c7d
commit 8561dfa93a
5 changed files with 134 additions and 5 deletions

View file

@ -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;

View file

@ -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;

View file

@ -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

View file

@ -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)

View file

@ -0,0 +1,95 @@
/*
pr_resource.c
progs resource management
Copyright (C) 2001 Bill Currie <bill@taniwha.org>
Author: Bill Currie <bill@taniwha.org>
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 <stdlib.h>
#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;
}