summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: e1ea0e5)
raw | patch | inline | side by side (parent: e1ea0e5)
author | DeaDDooMER <deaddoomer@deadsoftware.ru> | |
Thu, 8 Apr 2021 08:09:10 +0000 (11:09 +0300) | ||
committer | DeaDDooMER <deaddoomer@deadsoftware.ru> | |
Thu, 8 Apr 2021 08:09:10 +0000 (11:09 +0300) |
src/common/wadres.c | patch | blob | history | |
src/common/wadres.h | patch | blob | history | |
src/memory.c | patch | blob | history |
diff --git a/src/common/wadres.c b/src/common/wadres.c
index 5861fe5f41c093247088eb343fc9ef7f408cb024..fba4039451bf99749206b15e3e251bba3b3ac449 100644 (file)
--- a/src/common/wadres.c
+++ b/src/common/wadres.c
#include <stdint.h>
#include <string.h>
+#include <stdlib.h>
#include <assert.h>
#include "wadres.h"
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);
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 f478fd10d62e07991293c49e5bcf66ac6111648a..49f0f9dcbb469b09d1fb11e7f1dd51cec18e369d 100644 (file)
--- a/src/common/wadres.h
+++ b/src/common/wadres.h
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 404bbc481b36a967e5fa04c94c406842afd14a0c..bd3c609b860a429a81d4b01b97a1a2b0db33ba63 100644 (file)
--- a/src/memory.c
+++ b/src/memory.c
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
-#include "glob.h"
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <assert.h>
-#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) {
}
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);
}