DEADSOFTWARE

gl: fix trap on emscripten
[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 "error.h"
23 #include "files.h"
24 #include "memory.h"
26 static byte m_active;
27 static void *resp[MAX_WAD];
28 static short resl[MAX_WAD];
30 void M_startup (void) {
31 if(m_active) return;
32 logo("M_startup: настройка памяти\n");
33 memset(resp,0,sizeof(resp));
34 memset(resl,0,sizeof(resl));
35 // logo(" свободно DPMI-памяти: %uK\n",dpmi_memavl()>>10);
36 m_active=TRUE;
37 }
39 void M_shutdown (void) {
40 if(!m_active) return;
41 m_active=FALSE;
42 }
44 static void allocres (int h) {
45 int *p,s;
47 if(h>d_start && h<d_end) s=1; else s=0;
48 if(!(p=malloc(wad[h].l+4+s*8)))
49 ERR_fatal("M_lock: не хватает памяти");
50 *p=h;
51 ++p;
52 resp[h]=p;
53 if(s) {
54 p[0]=p[1]=p[2]=p[3]=0;
55 F_loadres(h,p,0,2);
56 F_loadres(h,p+1,2,2);
57 F_loadres(h,p+2,4,2);
58 F_loadres(h,p+3,6,2);
59 F_loadres(h,p+4,8,wad[h].l-8);
60 }else F_loadres(h,p,0,wad[h].l);
61 }
63 void *M_lock (int h) {
64 if(h==-1 || h==0xFFFF) return NULL;
65 if(h>=MAX_WAD) ERR_fatal("M_lock: странный номер ресурса");
66 if(!resl[h]) if(!resp[h]) allocres(h);
67 ++resl[h];
68 return resp[h];
69 }
71 void M_unlock (void *p) {
72 int h;
74 if(!p) return;
75 h=((int*)p)[-1];
76 if(h>=MAX_WAD) ERR_fatal("M_unlock: странный номер ресурса");
77 if(!resl[h]) return;
78 --resl[h];
79 }
81 int M_locked (int h) {
82 return (h != -1) && (h != 0xFFFF) && (resl[h] != 0);
83 }
85 int M_was_locked (int h) {
86 return (h != -1) && (h != 0xFFFF) && (resp[h] != NULL);
87 }