X-Git-Url: https://deadsoftware.ru/gitweb?a=blobdiff_plain;f=src%2Fmemory.c;h=404bbc481b36a967e5fa04c94c406842afd14a0c;hb=9df860ab2839447c7143bbe705a573df152d7734;hp=f1187462d2a047ffe760ae283ee3d33bc634ad36;hpb=2cd3d04c04fb347e6eea354dccf14099f56bd352;p=flatwaifu.git diff --git a/src/memory.c b/src/memory.c index f118746..404bbc4 100644 --- a/src/memory.c +++ b/src/memory.c @@ -19,69 +19,73 @@ #include #include #include +#include #include "error.h" #include "files.h" #include "memory.h" -static byte m_active; -static void *resp[MAX_WAD]; -static short resl[MAX_WAD]; +#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) { - if(m_active) return; - logo("M_startup: setup memory\n"); - memset(resp,0,sizeof(resp)); - memset(resl,0,sizeof(resl)); - // logo(" free DPMI-memory: %uK\n",dpmi_memavl()>>10); - m_active=TRUE; + memset(blocks, 0, sizeof(blocks)); } void M_shutdown (void) { - if(!m_active) return; - m_active=FALSE; -} - -static void allocres (int h) { - int *p,s; - - if(h>d_start && h=MAX_WAD) ERR_fatal("M_lock: invalid resource id"); - if(!resl[h]) if(!resp[h]) allocres(h); - ++resl[h]; - return resp[h]; +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; } void M_unlock (void *p) { - int h; - - if(!p) return; - h=((int*)p)[-1]; - if(h>=MAX_WAD) ERR_fatal("M_unlock: invalid resource id"); - if(!resl[h]) return; - --resl[h]; + 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 + } } -int M_locked (int h) { - return (h != -1) && (h != 0xFFFF) && (resl[h] != 0); +int M_locked (int id) { + assert(id >= -1 && id < MAX_RESOURCES); + return (id >= 0) && (blocks[id] != NULL) && (blocks[id]->ref >= 1); } -int M_was_locked (int h) { - return (h != -1) && (h != 0xFFFF) && (resp[h] != NULL); +int M_was_locked (int id) { + assert(id >= -1 && id < MAX_RESOURCES); + return (id >= 0) && (blocks[id] != NULL) && (blocks[id]->ref >= 0); }