install: add install sources and resources
[doom2d-restoration.git] / GAME / MEMORY.C
blob505ebefaa35e0b52b86f986e59595189cb87ffe4
1 #include "glob.h"
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <malloc.h>
5 #include <string.h>
6 #include "error.h"
7 #include "files.h"
8 #include "memory.h"
10 dword dpmi_memavl(void);
12 extern int d_start,d_end;
14 extern mwad_t wad[];
16 static byte m_active=FALSE;
18 static void *resp[MAX_WAD];
19 static short resl[MAX_WAD];
21 void M_startup(void) {
22   if(m_active) return;
23   logo("M_startup: настройка памяти\n");
24   memset(resp,0,sizeof(resp));
25   memset(resl,0,sizeof(resl));
26   logo("  свободно DPMI-памяти: %uK\n",dpmi_memavl()>>10);
27   m_active=TRUE;
30 void M_shutdown(void) {
31 //  FILE *h;
32 //  int i;
34   if(!m_active) return;
35   m_active=FALSE;
37   if(!(h=fopen("res_use.txt","wt"))) return;
38   for(i=0;i<MAX_WAD;++i) if(resp[i]) {
39     fprintf(h,"%.8s\n",wad[i].n);
40   }
41   fclose(h);
45 static void allocres(int h) {
46   int *p,s;
48   if(h>d_start && h<d_end) s=1; else s=0;
49   if(!(p=malloc(wad[h].l+4+s*8)))
50     ERR_fatal("M_lock: не хватает памяти");
51   *p=h;
52   ++p;
53   resp[h]=p;
54   if(s) {
55     p[0]=p[1]=p[2]=p[3]=0;
56     F_loadres(h,p,0,2);
57     F_loadres(h,p+1,2,2);
58     F_loadres(h,p+2,4,2);
59     F_loadres(h,p+3,6,2);
60     F_loadres(h,p+4,8,wad[h].l-8);
61   }else F_loadres(h,p,0,wad[h].l);
64 void *M_lock(int h) {
65   if(h==-1 || h==0xFFFF) return NULL;
66   h&=-1-0x8000;
67   if(h>=MAX_WAD) ERR_fatal("M_lock: странный номер ресурса");
68   if(!resl[h]) if(!resp[h]) allocres(h);
69   ++resl[h];
70   return resp[h];
73 void M_unlock(void *p) {
74   int h;
76   if(!p) return;
77   h=((int*)p)[-1];
78   if(h>=MAX_WAD) ERR_fatal("M_unlock: странный номер ресурса");
79   if(!resl[h]) return;
80   --resl[h];