1 /*	SCCS Id: @(#)ovlinit.c	3.4	1994/03/20	          */
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 
10 #ifdef _MSC_VER
11 
12 #define RESERVED_PARAGRAPHS	5120	/* leave 80K for malloc and inits */
13  					/* subject to change before release */
14 
15 /*
16  * memavail() Returns the amount of RAM available (in paragraphs which are 16
17  *  bytes) - the amount to be reserved for heap allocations.
18  *
19  */
memavail(minovl)20 unsigned 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 
_resizeOvrBuffer()71 void _resizeOvrBuffer()
72 {
73    mem_top = (unsigned far *) MK_FP( _psp, 0x02 );
74    total = *mem_top - _psp;
75 
76    ProgramSize = * (unsigned far *) MK_FP( _psp - 1, 0x03 );
77    tmpbuffer = total - ProgramSize - RSRVD_TOTAL / 16;
78    memAlloc = min (MAX_OVRBUF / 16, tmpbuffer);
79    if (tmpbuffer >= MIN_OVRBUF / 16)
80 	_ovrbuffer = memAlloc;
81    else {
82 	_ovrbuffer = 1;
83 	appFail = 1;
84    };
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 
97 #pragma startup _resizeOvrBuffer 0      /* Put function in table */
98 
99 void
startup()100 startup ()
101 {
102 	if (appFail) {
103 	    printf ("NetHack fits in memory, but it cannot allocate memory");
104 	    printf (" for the overlay buffer\nand the runtime functions.  ");
105 	    printf ("Please free up just %ld more bytes.",
106 			(long)(MIN_OVRBUF - tmpbuffer * 16L));
107 	    exit (-1);
108 	} else {
109 
110 	/* Now try to use expanded memory for the overlay manager */
111 	/* If that doesn't work, we revert to extended memory */
112 
113 	emsstatus = _OvrInitEms (0, 0, 0);
114 #ifdef RECOGNIZE_XMS
115 	xmsstatus = (emsstatus) ? _OvrInitExt (0, 0) : -1;
116 #endif
117 
118      }
119 }
120 
121 void
show_borlandc_stats(win)122 show_borlandc_stats(win)
123 winid win;
124 {
125 	char buf[BUFSZ];
126 
127 	putstr(win, 0, "");
128 	putstr(win, 0, "");
129 	putstr(win, 0, "Memory usage stats"); putstr(win, 0, "");
130 	putstr(win, 0, "");
131 	Sprintf (buf, "Overlay buffer memory allocation: %ld bytes.",
132 	    memAlloc * 16L); putstr(win, 0, buf);
133 	Sprintf (buf, "_ovrbuffer = %u.", _ovrbuffer); 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) Sprintf (buf, "EMS search failed (%d).", emsstatus);
140 	else Sprintf (buf, "EMS search successful.");
141 	putstr(win, 0, buf);
142 #ifdef RECOGNIZE_XMS
143 	if (xmsstatus) Sprintf (buf, "XMS search failed (%d).", xmsstatus);
144 	else Sprintf (buf, "XMS search successful.");
145 	putstr(win, 0, buf);
146 #endif
147 
148 
149 }
150 
151 #endif  /* #ifdef RESIZE_OVL */
152 #endif /* #ifdef __BORLANDC__ */
153 
154 /*ovlinit.c*/
155