Back to home page

DOS ain't dead

Forum index page

Log in | Register

Back to index page
Thread view  Board view
mbbrutman

Homepage

Washington, USA,
06.08.2026, 07:29
(edited by mbbrutman, 07.08.2026, 01:08)
 

Using overlays to hide initialization code (Developers)

Please forgive me in advance if I don't have the right terminology or vocabulary ...

Assume you have a large program like a web server. Quite a bit of code is only used during initialization. Things like parsing the command line arguments, reading config files, and validating the configuration options require code that is used once at startup and never again. But yet it remains resident in memory.

TSRs and device drivers have a simple approach to this - just put the initialization code at the end, and then use that memory for other storage or return it to the system.

The web server though is a giant mass of C code. So I figured I'd try a close approximation, and use overlays instead. I isolated a lot of the initialization code into a separate obj, and then laid out my sections so that other code would force it out after it was done. That failed miserably ... in theory it worked but the overlay manager ate all of the savings.

It seems like this should be a pretty common thing to do; run some init code, and then save some space by replacing it with the longer lived code that you care about. Was there a compiler toolchain that supported that simpler use case, or would somebody who wanted to do that have to roll their own using the DOS function calls to load an overlay?

(I'm using Open Watcom 1.9.)


Thanks,
Mike

mceric

Germany,
06.08.2026, 10:27

@ mbbrutman
 

Using overlays to hide initialization code

Hi Mike! If your code is too large for 640k RAM, you could use overlays and some older compilers have built-in support for those, but 32-bit compilers and DOS extenders would probably be a lot easier. You can use gigabytes of RAM with those, problem solved :-)

Unless of course your code is even larger than your RAM, but then you would have to swap in and out either the overlays or other contents. Typical DOS extenders and/or for example DPMI hosts like CWSDPMI also implement swapping. Other names that come to mind: DPMIONE, HDPMI, DOS4GW, etc.

---
FreeDOS / DOSEMU2 / ...

Laaca

Homepage

Czech republic,
06.08.2026, 11:14

@ mbbrutman
 

Using overlays to hide initialization code

I believe that exactly the same thing I do in my tool AcpiTool. Large mass of pascal source which do a lot of stuff like examining the ACPI tables. Then it accordingly modifies one of its procedures which remain in memory as TSR and rest of the program is removed from memory. I thing it should be possibke to do in OpenWatcom too.

---
DOS-u-akbar!

Japheth

Homepage

Germany (South),
06.08.2026, 21:36

@ mbbrutman
 

Using overlays to hide initialization code

> ... or would somebody who wanted to do that have to roll
> their own using the DOS function calls to load an overlay?
>
> (I'm using Open Watcom 1.9.)

I don't think there is a much better approach than using overlays.

The most extreme memory saving DOS program that I put my hands on is DEBUG. Here the initialization code is at the end of the binary, behind data and stack. After initialization, the place is used for uninitialized variables. But DEBUG is an assembly program, where full control for segments and their ordering is possible. With Open Watcom, you're more or less stuck to the DOSSEG segment ordering ( 1. code, 2. segments not belonging to dgroup, 3. dgroup ). In protected mode, you possibly could mark sections (16-bit) or pages (32-bit) as discardable, but for real-mode I cannot see anything similar.

---
MS-DOS forever!

Oso2k

06.08.2026, 22:07

@ mbbrutman
 

Using overlays to hide initialization code

> TSRs and device drivers have a simple approach to this - just put the
> initialization code at the end, and then use that memory for other storage
> or return it to the system.

Don't some TSRs use some tricks to load code from disk?

bretjohn

Homepage E-mail

Rio Rancho, NM,
06.08.2026, 23:14

@ mbbrutman
 

Using overlays to hide initialization code

> TSRs and device drivers have a simple approach to this - just put the
> initialization code at the end, and then use that memory for other storage
> or return it to the system.

For TSR's, I use a method I learned from ECM. I move the running program (including initialization code) to the top of conventional memory, and then "install" the TSR in free memory space wherever appropriate ("holes" in conventional or upper memory, EMS, DPMS, allocate XMS memory if needed, etc.). If the TSR is complicated, I divide it up into smaller chunks to give it a greater chance of finding a memory "hole" somewhere. I can even install a TSR, uninstall it, and then install it again in the same memory space even if other programs were installed afterwards. It's a very flexible method.

marcov

06.08.2026, 23:17

@ mbbrutman
 

Using overlays to hide initialization code

> The web server though is a giant mass of C code. So I figured I'd try a
> close approximation, and use overlays instead. I isolated a lot of the
> initialization code into a separate obj, and then laid out my sections so
> that other code would force it out after it was done. That failed
> miserably ... in theory it worked but the overlay manager ate all of the
> savings.

I don't know watcom but played with Borland and Topspeed overlays.

In both cases a block of memory/address space is allocated as a buffer for overlays. If you use it only once, the buffer is still allocated, i.e. you must have multiple overlays to make it worthwhile?

mbbrutman

Homepage

Washington, USA,
07.08.2026, 01:15

@ mbbrutman
 

Using overlays to hide initialization code

Eeek, I posted in "Announcements!" I fixed that.

Some quick follow-ups:

* The code needs to be 16 bit real-mode. I want it to run on an 8088.
* The code fits in 512KB easily. I'm just offended that I have to keep initialization code around that I'll never use again. I'd rather use the RAM space for more features.
* It the mTCP HTTP server; it's too big to turn it into a TSR, so it's going to be C code forever.

Overlay support seems to be the correct way to handle this situation. I defined my "root" code (always stays resident) and then two sections that can overlay each other. One section was for the init code and the other section was for code that gets used often. I was hoping the initialization code would be forced out of memory but it's possible that the overlay manager is allocating too much space and not evicting it. It's also possible that the overlay manager itself is just so large that it is hiding the savings.

When you think about it, this model of "throw away the init code when you are done with it" is a simplification of the overlay scheme and almost any non-trivial program can benefit from it. I'm just kind of surprised that compilers like Open Watcom addressed the more complicated general overlay mechanism without having a simple path for this more limited use of it.

bretjohn

Homepage E-mail

Rio Rancho, NM,
07.08.2026, 02:02

@ mbbrutman
 

Using overlays to hide initialization code

The other thing I've done is use the initialization code (and PSP which isn't needed after a TSR is installed) memory for stack(s) and/or heap(s), even in TSRs. As Japheth noted, when you're using an HLL you don't have a lot of control over where things go in memory, but with ASM you have almost total control over everything (which can also get you into trouble).

Your issue (leaving unused/unwanted code/data in memory) is a problem with almost all modern programming techniques including even basic libraries where you only use a subset of the available features.

marcov

07.08.2026, 10:06

@ bretjohn
 

Using overlays to hide initialization code

> Your issue (leaving unused/unwanted code/data in memory) is a problem with
> almost all modern programming techniques including even basic libraries
> where you only use a subset of the available features.

In most systems, code segments can be paged out. So it would be more a matter of grouping that code to be near each other at linking time.

Back to index page
Thread view  Board view
23742 Postings in 2250 Threads, 410 registered users (0 online)
DOS ain't dead | Admin contact
RSS Feed
powered by my little forum