DEADSOFTWARE

d3c5e8ccf8364e55b875f59fa472ab986d460fb4
[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 dword dpmi_memavl(void);
32 extern int d_start,d_end;
34 extern mwad_t wad[];
36 static byte m_active=FALSE;
38 static void *resp[MAX_WAD];
39 static short resl[MAX_WAD];
41 void M_startup(void) {
42 if(m_active) return;
43 logo("M_startup: настройка памяти\n");
44 memset(resp,0,sizeof(resp));
45 memset(resl,0,sizeof(resl));
46 // logo(" свободно DPMI-памяти: %uK\n",dpmi_memavl()>>10);
47 m_active=TRUE;
48 }
50 void M_shutdown(void) {
52 if(!m_active) return;
53 m_active=FALSE;
54 }
56 static void allocres(int h) {
57 int *p,s;
59 if(h>d_start && h<d_end) s=1; else s=0;
60 if(!(p=malloc(wad[h].l+4+s*8)))
61 ERR_fatal("M_lock: не хватает памяти");
62 *p=h;
63 ++p;
64 resp[h]=p;
65 if(s) {
66 p[0]=p[1]=p[2]=p[3]=0;
67 F_loadres(h,p,0,2);
68 F_loadres(h,p+1,2,2);
69 F_loadres(h,p+2,4,2);
70 F_loadres(h,p+3,6,2);
71 F_loadres(h,p+4,8,wad[h].l-8);
72 }else F_loadres(h,p,0,wad[h].l);
73 }
75 void *M_lock(int h) {
76 if(h==-1 || h==0xFFFF) return NULL;
77 h&=-1-0x8000;
78 if(h>=MAX_WAD) ERR_fatal("M_lock: странный номер ресурса");
79 if(!resl[h]) if(!resp[h]) allocres(h);
80 ++resl[h];
81 return resp[h];
82 }
84 void M_unlock(void *p) {
85 int h;
87 if(!p) return;
88 h=((int*)p)[-1];
89 if(h>=MAX_WAD) ERR_fatal("M_unlock: странный номер ресурса");
90 if(!resl[h]) return;
91 --resl[h];
92 }
94 int M_locked (int h) {
95 return (h != -1) && (h != 0xFFFF) && (resl[h & (-1 - 0x8000)] != 0);
96 }
98 int M_was_locked (int h) {
99 return (h != -1) && (h != 0xFFFF) && (resp[h & (-1 - 0x8000)] != NULL);