shift pistol HUD sprite
[d2d-psx.git] / src / memory.c
blobb5144a60a5f8a30f5b07b72fabb0d427774bb776
1 /*
2 * Copyright (C) Prikol Software 1996-1997
3 * Copyright (C) Aleksey Volynskov 1996-1997
4 * Copyright (C) fgsfds 2019
6 * This file is part of the Doom2D PSX project.
8 * Doom2D PSX 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 PSX 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
23 #include "glob.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <malloc.h>
27 #include <string.h>
28 #include <libapi.h>
29 #include "error.h"
30 #include "files.h"
31 #include "memory.h"
33 dword dpmi_memavl(void);
35 extern int d_start, d_end;
37 extern mwad_t wad[];
39 static byte m_active = FALSE;
41 static void * resp[MAX_WAD];
42 static short resl[MAX_WAD];
44 void M_startup(void *heapbase, unsigned long heapsize) {
45 if (m_active)
46 return;
48 logo("M_startup(): memory init: %u kb heap at %p\n", heapsize >> 10, heapbase);
50 InitHeap3((void*)heapbase, heapsize);
52 memset(resp, 0, sizeof(resp));
53 memset(resl, 0, sizeof(resl));
54 // logo(" свободно DPMI-памяти: %uK\n",dpmi_memavl()>>10);
55 m_active = TRUE;
58 void M_shutdown(void) {
59 if (!m_active)
60 return;
62 m_active = FALSE;
65 static void allocres(int h) {
66 int *p;
67 if (!(p = malloc3(wad[h].l + 4)))
68 ERR_fatal("M_lock: malloc3 failed");
69 *p = h;
70 ++p;
71 resp[h] = p;
72 F_loadres(h, p, 0, wad[h].l);
75 void * M_lock(int h) {
76 if ((h == -1) || (h == 0xFFFF))
77 return NULL;
79 h &= -1 - 0x8000;
80 if (h >= MAX_WAD)
81 ERR_fatal("M_lock: funny resource number");
82 if (!resl[h]) {
83 if (!resp[h])
84 allocres(h);
86 ++resl[h];
87 return resp[h];
90 void M_unlock(void * p) {
91 int h;
93 if (!p)
94 return;
96 h = ((int *) p)[-1];
97 if (h >= MAX_WAD)
98 ERR_fatal("M_unlock: funny resource number");
99 if (!resl[h])
100 return;
102 --resl[h];
104 if (!resl[h]) {
105 // printf("M_unlock(): freeing resource %x\n", h);
106 free3((int*)p - 1);
107 resp[h] = NULL;