DEADSOFTWARE

7089cc5cadb64d31aec69a2ecd8dccc6b518821d
[flatwaifu.git] / src / memory.c
1 /*
2 Copyright (C) Prikol Software 1996-1997
3 Copyright (C) Aleksey Volynskov 1996-1997
5 This file is part of the Doom2D:Rembo project.
7 Doom2D:Rembo is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License version 2 as
9 published by the Free Software Foundation.
11 Doom2D:Rembo is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, see <http://www.gnu.org/licenses/> or
18 write to the Free Software Foundation, Inc.,
19 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
22 #include "glob.h"
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include "error.h"
27 #include "files.h"
28 #include "memory.h"
30 static byte m_active;
31 static void *resp[MAX_WAD];
32 static short resl[MAX_WAD];
34 void M_startup (void) {
35 if(m_active) return;
36 logo("M_startup: настройка памяти\n");
37 memset(resp,0,sizeof(resp));
38 memset(resl,0,sizeof(resl));
39 // logo(" свободно DPMI-памяти: %uK\n",dpmi_memavl()>>10);
40 m_active=TRUE;
41 }
43 void M_shutdown (void) {
44 if(!m_active) return;
45 m_active=FALSE;
46 }
48 static void allocres (int h) {
49 int *p,s;
51 if(h>d_start && h<d_end) s=1; else s=0;
52 if(!(p=malloc(wad[h].l+4+s*8)))
53 ERR_fatal("M_lock: не хватает памяти");
54 *p=h;
55 ++p;
56 resp[h]=p;
57 if(s) {
58 p[0]=p[1]=p[2]=p[3]=0;
59 F_loadres(h,p,0,2);
60 F_loadres(h,p+1,2,2);
61 F_loadres(h,p+2,4,2);
62 F_loadres(h,p+3,6,2);
63 F_loadres(h,p+4,8,wad[h].l-8);
64 }else F_loadres(h,p,0,wad[h].l);
65 }
67 void *M_lock (int h) {
68 if(h==-1 || h==0xFFFF) return NULL;
69 if(h>=MAX_WAD) ERR_fatal("M_lock: странный номер ресурса");
70 if(!resl[h]) if(!resp[h]) allocres(h);
71 ++resl[h];
72 return resp[h];
73 }
75 void M_unlock (void *p) {
76 int h;
78 if(!p) return;
79 h=((int*)p)[-1];
80 if(h>=MAX_WAD) ERR_fatal("M_unlock: странный номер ресурса");
81 if(!resl[h]) return;
82 --resl[h];
83 }
85 int M_locked (int h) {
86 return (h != -1) && (h != 0xFFFF) && (resl[h] != 0);
87 }
89 int M_was_locked (int h) {
90 return (h != -1) && (h != 0xFFFF) && (resp[h] != NULL);
91 }