DEADSOFTWARE

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