From: DeaDDooMER Date: Thu, 8 Apr 2021 08:09:10 +0000 (+0300) Subject: memory: load and allocate resources in wadres X-Git-Url: http://deadsoftware.ru/gitweb?a=commitdiff_plain;h=bc1fa78a3bca3f391c4b8788d0cea937b9540c0a;p=flatwaifu.git memory: load and allocate resources in wadres --- diff --git a/src/common/wadres.c b/src/common/wadres.c index 5861fe5..fba4039 100644 --- a/src/common/wadres.c +++ b/src/common/wadres.c @@ -1,5 +1,6 @@ #include #include +#include #include #include "wadres.h" @@ -12,10 +13,17 @@ typedef struct Entry { int f; } Entry; +typedef struct Block { + int id; + int ref; + char data[]; +} Block; + static int n_wads; static int n_resources; static Stream *wads[MAX_WADS]; static Entry resources[MAX_RESOURCES]; +static Block *blocks[MAX_RESOURCES]; static int check_header (Stream *r) { assert(r != NULL); @@ -118,3 +126,50 @@ void WADRES_getdata (int id, void *data) { stream_read(data, resources[id].size, 1, r); stream_setpos(r, pos); } + +void *WADRES_lock (int id) { + assert(id >= -1 && id < MAX_RESOURCES); + if (id >= 0) { + Block *x = blocks[id]; + if (x) { + x->ref += 1; + return x->data; + } else { + x = malloc(sizeof(Block) + WADRES_getsize(id)); + if (x) { + x->id = id; + x->ref = 1; + WADRES_getdata(id, x->data); + blocks[id] = x; + return x->data; + } + } + } + return NULL; +} + +void WADRES_unlock (void *data) { + if (data) { + Block *x = data - sizeof(Block); + int id = x->id; + assert(id >= 0 && id < MAX_RESOURCES); + x->ref -= 1; + assert(x->ref >= 0); +#if 0 + if (x->ref == 0) { + blocks[id] = NULL; + free(x); + } +#endif + } +} + +int WADRES_locked (int id) { + assert(id >= -1 && id < MAX_RESOURCES); + return (id >= 0) && (blocks[id] != NULL) && (blocks[id]->ref >= 1); +} + +int WADRES_was_locked (int id) { + assert(id >= -1 && id < MAX_RESOURCES); + return (id >= 0) && (blocks[id] != NULL) && (blocks[id]->ref >= 0); +} diff --git a/src/common/wadres.h b/src/common/wadres.h index f478fd1..49f0f9d 100644 --- a/src/common/wadres.h +++ b/src/common/wadres.h @@ -18,4 +18,9 @@ long WADRES_getsize (int id); void WADRES_getname (int id, char *name); void WADRES_getdata (int id, void *data); +void *WADRES_lock (int id); +void WADRES_unlock (void *data); +int WADRES_locked (int id); +int WADRES_was_locked (int id); + #endif /* COMMON_WADRES_H_INCLUDED */ \ No newline at end of file diff --git a/src/memory.c b/src/memory.c index 404bbc4..bd3c609 100644 --- a/src/memory.c +++ b/src/memory.c @@ -15,28 +15,11 @@ * along with this program. If not, see . */ -#include "glob.h" -#include -#include -#include -#include -#include "error.h" -#include "files.h" #include "memory.h" - #include "common/wadres.h" -#include "common/streams.h" - -typedef struct Block { - int id; - int ref; - char data[]; -} Block; - -static Block *blocks[MAX_RESOURCES]; void M_startup (void) { - memset(blocks, 0, sizeof(blocks)); + // stub } void M_shutdown (void) { @@ -44,48 +27,17 @@ void M_shutdown (void) { } void *M_lock (int id) { - assert(id >= -1 && id < MAX_RESOURCES); - if (id >= 0) { - Block *x = blocks[id]; - if (x) { - x->ref += 1; - return x->data; - } else { - x = malloc(sizeof(Block) + WADRES_getsize(id)); - if (x) { - x->id = id; - x->ref = 1; - WADRES_getdata(id, x->data); - blocks[id] = x; - return x->data; - } - } - } - return NULL; + return WADRES_lock(id); } void M_unlock (void *p) { - if (p) { - Block *x = p - sizeof(Block); - int id = x->id; - assert(id >= 0 && id < MAX_RESOURCES); - x->ref -= 1; - assert(x->ref >= 0); -#if 0 - if (x->ref == 0) { - blocks[id] = NULL; - free(x); - } -#endif - } + WADRES_unlock(p); } int M_locked (int id) { - assert(id >= -1 && id < MAX_RESOURCES); - return (id >= 0) && (blocks[id] != NULL) && (blocks[id]->ref >= 1); + return WADRES_locked(id); } int M_was_locked (int id) { - assert(id >= -1 && id < MAX_RESOURCES); - return (id >= 0) && (blocks[id] != NULL) && (blocks[id]->ref >= 0); + return WADRES_was_locked(id); }