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. */
11 #define RESERVED_PARAGRAPHS 5120 /* leave 80K for malloc and inits */
12 /* subject to change before release */
15 * memavail() Returns the amount of RAM available (in paragraphs which are 16
16 * bytes) - the amount to be reserved for heap allocations.
21 unsigned minovl; /* minimum size of overlay heap */
25 unsigned farparaavail;
29 * _dos_allocmem will return the maximum block size available.
30 * It uses DOS (int 21h) service 0x48.
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));
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. */
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;
65 signed long tmpbuffer;
69 void NDECL(_resizeOvrBuffer);
74 mem_top = (unsigned far *) MK_FP(_psp, 0x02);
75 total = *mem_top - _psp;
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;
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.
96 #pragma startup _resizeOvrBuffer 0 /* Put function in table */
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));
108 /* Now try to use expanded memory for the overlay manager */
109 /* If that doesn't work, we revert to extended memory */
111 emsstatus = _OvrInitEms(0, 0, 0);
113 xmsstatus = (emsstatus) ? _OvrInitExt(0, 0) : -1;
119 show_borlandc_stats(win)
126 putstr(win, 0, "Memory usage stats");
129 Sprintf(buf, "Overlay buffer memory allocation: %ld bytes.",
132 Sprintf(buf, "_ovrbuffer = %u.", _ovrbuffer);
134 Sprintf(buf, "Startup memory usage: 0x%X", ProgramSize);
136 runAlloc = *(unsigned far *) MK_FP(_psp - 1, 0x03);
137 Sprintf(buf, "Current memory usage: 0x%X", runAlloc);
140 Sprintf(buf, "EMS search failed (%d).", emsstatus);
142 Sprintf(buf, "EMS search successful.");
146 Sprintf(buf, "XMS search failed (%d).", xmsstatus);
148 Sprintf(buf, "XMS search successful.");
153 #endif /* #ifdef RESIZE_OVL */
154 #endif /* #ifdef __BORLANDC__ */