DEADSOFTWARE

bec045671e5a8f3202cefb02bd7f615cb5913c4d
[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 <malloc.h>
26 #include <string.h>
27 #include "error.h"
28 #include "files.h"
29 #include "memory.h"
31 dword dpmi_memavl(void);
33 extern int d_start,d_end;
35 extern mwad_t wad[];
37 static byte m_active=FALSE;
39 static void *resp[MAX_WAD];
40 static short resl[MAX_WAD];
42 void M_startup(void) {
43 if(m_active) return;
44 logo("M_startup: настройка памяти\n");
45 memset(resp,0,sizeof(resp));
46 memset(resl,0,sizeof(resl));
47 // logo(" свободно DPMI-памяти: %uK\n",dpmi_memavl()>>10);
48 m_active=TRUE;
49 }
51 void M_shutdown(void) {
53 if(!m_active) return;
54 m_active=FALSE;
55 }
57 static void allocres(int h) {
58 int *p,s;
60 if(h>d_start && h<d_end) s=1; else s=0;
61 if(!(p=malloc(wad[h].l+4+s*8)))
62 ERR_fatal("M_lock: не хватает памяти");
63 *p=h;
64 ++p;
65 resp[h]=p;
66 if(s) {
67 p[0]=p[1]=p[2]=p[3]=0;
68 F_loadres(h,p,0,2);
69 F_loadres(h,p+1,2,2);
70 F_loadres(h,p+2,4,2);
71 F_loadres(h,p+3,6,2);
72 F_loadres(h,p+4,8,wad[h].l-8);
73 }else F_loadres(h,p,0,wad[h].l);
74 }
76 void *M_lock(int h) {
77 if(h==-1 || h==0xFFFF) return NULL;
78 h&=-1-0x8000;
79 if(h>=MAX_WAD) ERR_fatal("M_lock: странный номер ресурса");
80 if(!resl[h]) if(!resp[h]) allocres(h);
81 ++resl[h];
82 return resp[h];
83 }
85 void M_unlock(void *p) {
86 int h;
88 if(!p) return;
89 h=((int*)p)[-1];
90 if(h>=MAX_WAD) ERR_fatal("M_unlock: странный номер ресурса");
91 if(!resl[h]) return;
92 --resl[h];
93 }