DEADSOFTWARE

404bbc481b36a967e5fa04c94c406842afd14a0c
[flatwaifu.git] / src / memory.c
1 /* Copyright (C) 1996-1997 Aleksey Volynskov
2 * Copyright (C) 2011 Rambo
3 * Copyright (C) 2020 SovietPony
4 *
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, version 3 of the License ONLY.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
18 #include "glob.h"
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <assert.h>
23 #include "error.h"
24 #include "files.h"
25 #include "memory.h"
27 #include "common/wadres.h"
28 #include "common/streams.h"
30 typedef struct Block {
31 int id;
32 int ref;
33 char data[];
34 } Block;
36 static Block *blocks[MAX_RESOURCES];
38 void M_startup (void) {
39 memset(blocks, 0, sizeof(blocks));
40 }
42 void M_shutdown (void) {
43 // stub
44 }
46 void *M_lock (int id) {
47 assert(id >= -1 && id < MAX_RESOURCES);
48 if (id >= 0) {
49 Block *x = blocks[id];
50 if (x) {
51 x->ref += 1;
52 return x->data;
53 } else {
54 x = malloc(sizeof(Block) + WADRES_getsize(id));
55 if (x) {
56 x->id = id;
57 x->ref = 1;
58 WADRES_getdata(id, x->data);
59 blocks[id] = x;
60 return x->data;
61 }
62 }
63 }
64 return NULL;
65 }
67 void M_unlock (void *p) {
68 if (p) {
69 Block *x = p - sizeof(Block);
70 int id = x->id;
71 assert(id >= 0 && id < MAX_RESOURCES);
72 x->ref -= 1;
73 assert(x->ref >= 0);
74 #if 0
75 if (x->ref == 0) {
76 blocks[id] = NULL;
77 free(x);
78 }
79 #endif
80 }
81 }
83 int M_locked (int id) {
84 assert(id >= -1 && id < MAX_RESOURCES);
85 return (id >= 0) && (blocks[id] != NULL) && (blocks[id]->ref >= 1);
86 }
88 int M_was_locked (int id) {
89 assert(id >= -1 && id < MAX_RESOURCES);
90 return (id >= 0) && (blocks[id] != NULL) && (blocks[id]->ref >= 0);
91 }