OSDN Git Service

import nethack-3.6.0
[jnethack/source.git] / sys / msdos / ovlinit.c
1 /* NetHack 3.6  ovlinit.c       $NHDT-Date: 1432512792 2015/05/25 00:13:12 $  $NHDT-Branch: master $:$NHDT-Revision: 1.8 $ */
2 /* Copyright (c) NetHack PC Development Team 1995                 */
3 /* NetHack may be freely redistributed.  See license for details. */
4
5 #include "hack.h"
6 #include <dos.h>
7 #include <stdio.h>
8
9 #ifdef _MSC_VER
10
11 #define RESERVED_PARAGRAPHS 5120 /* leave 80K for malloc and inits */
12                                  /* subject to change before release */
13
14 /*
15  * memavail() Returns the amount of RAM available (in paragraphs which are 16
16  *  bytes) - the amount to be reserved for heap allocations.
17  *
18  */
19 unsigned
20 memavail(minovl)
21 unsigned minovl; /* minimum size of overlay heap */
22 {
23     unsigned available;
24
25     unsigned farparaavail;
26     unsigned tmp;
27
28     /*
29      * _dos_allocmem will return the maximum block size available.
30      * It uses DOS (int 21h) service 0x48.
31      */
32
33     _dos_allocmem(0xFFFF, &farparaavail);
34     available = farparaavail - RESERVED_PARAGRAPHS;
35     tmp = RESERVED_PARAGRAPHS + minovl;
36     if (farparaavail < tmp) {
37         panic("Not enough free RAM to begin a game of NetHack (%ld bytes)",
38               (long) ((long) tmp * 16L));
39     }
40     return available;
41 }
42 #endif /*_MSC_VER*/
43
44 #ifdef __BORLANDC__
45
46 #define RSRVD_MALLOC 65 * 1024L /* malloc() calls use about 65K    */
47 #define RSRVD_CRTL 50 * 1024L   /* C runtime library uses 50K      */
48 #define RSRVD_TOTAL 115 * 1024L /* reserved for use in malloc()    */
49                                 /* as well as by C runtime library */
50                                 /* routines which allocate memory  */
51                                 /* after this routine runs.        */
52 #define MIN_OVRBUF 30 * 1024L   /* Overlay buffer gets minimum of  */
53 #define MAX_OVRBUF 200 * 1024L  /* 30K and maximum of 200K.        */
54
55 #define RESIZE_OVL
56 #ifdef RESIZE_OVL
57
58 extern unsigned _ovrbuffer = 0; /* Use default size initially */
59 unsigned appFail = 0;           /* Fail flag if not enough RAM */
60 unsigned memAlloc = 0;
61 unsigned long ProgramSize;
62 unsigned long runAlloc;
63 unsigned far *mem_top;
64 unsigned total;
65 signed long tmpbuffer;
66 int emsstatus;
67 int xmsstatus;
68
69 void NDECL(_resizeOvrBuffer);
70
71 void
72 _resizeOvrBuffer()
73 {
74     mem_top = (unsigned far *) MK_FP(_psp, 0x02);
75     total = *mem_top - _psp;
76
77     ProgramSize = *(unsigned far *) MK_FP(_psp - 1, 0x03);
78     tmpbuffer = total - ProgramSize - RSRVD_TOTAL / 16;
79     memAlloc = min(MAX_OVRBUF / 16, tmpbuffer);
80     if (tmpbuffer >= MIN_OVRBUF / 16)
81         _ovrbuffer = memAlloc;
82     else {
83         _ovrbuffer = 1;
84         appFail = 1;
85     };
86
87     /*
88      * Remember, when inside this code, nothing has been setup on
89      * the system, so do NOT call any RTL functions for I/O or
90      * anything else that might rely on a startup function.  This
91      * includes accessing any global objects as their constructors
92      * have not been called yet.
93      */
94 }
95
96 #pragma startup _resizeOvrBuffer 0 /* Put function in table */
97
98 void
99 startup()
100 {
101     if (appFail) {
102         printf("NetHack fits in memory, but it cannot allocate memory");
103         printf(" for the overlay buffer\nand the runtime functions.  ");
104         printf("Please free up just %ld more bytes.",
105                (long) (MIN_OVRBUF - tmpbuffer * 16L));
106         exit(-1);
107     } else {
108         /* Now try to use expanded memory for the overlay manager */
109         /* If that doesn't work, we revert to extended memory */
110
111         emsstatus = _OvrInitEms(0, 0, 0);
112 #ifdef RECOGNIZE_XMS
113         xmsstatus = (emsstatus) ? _OvrInitExt(0, 0) : -1;
114 #endif
115     }
116 }
117
118 void
119 show_borlandc_stats(win)
120 winid win;
121 {
122     char buf[BUFSZ];
123
124     putstr(win, 0, "");
125     putstr(win, 0, "");
126     putstr(win, 0, "Memory usage stats");
127     putstr(win, 0, "");
128     putstr(win, 0, "");
129     Sprintf(buf, "Overlay buffer memory allocation: %ld bytes.",
130             memAlloc * 16L);
131     putstr(win, 0, buf);
132     Sprintf(buf, "_ovrbuffer = %u.", _ovrbuffer);
133     putstr(win, 0, buf);
134     Sprintf(buf, "Startup memory usage: 0x%X", ProgramSize);
135     putstr(win, 0, buf);
136     runAlloc = *(unsigned far *) MK_FP(_psp - 1, 0x03);
137     Sprintf(buf, "Current memory usage: 0x%X", runAlloc);
138     putstr(win, 0, buf);
139     if (emsstatus)
140         Sprintf(buf, "EMS search failed (%d).", emsstatus);
141     else
142         Sprintf(buf, "EMS search successful.");
143     putstr(win, 0, buf);
144 #ifdef RECOGNIZE_XMS
145     if (xmsstatus)
146         Sprintf(buf, "XMS search failed (%d).", xmsstatus);
147     else
148         Sprintf(buf, "XMS search successful.");
149     putstr(win, 0, buf);
150 #endif
151 }
152
153 #endif /* #ifdef RESIZE_OVL */
154 #endif /* #ifdef __BORLANDC__ */
155
156 /*ovlinit.c*/