Efficient alloc/free/realloc for Z80 CP/M

165 views
Skip to first unread message

ladislau szilagyi

unread,
Jun 6, 2024, 4:53:40 AMJun 6
to RC2014-Z80
I published here: https://github.com/Laci1953/RC2014-CPM/tree/main/Memory%20management%20library

a new memory management library for Z80 based computers.

It contains the following functions: - alloc - free - realloc These routines may be called from C source files (HiTech C) The routines must be executed ONLY on "real" Z80 computers (they contain undocumented Z80 instructions...) Results of a test (see test.c) comparing the "classic" HiTech C malloc/free with this new package: (NEW ALLOC) D>sdir test.com Directory For Drive D: User 0 Name Bytes Recs Attributes ------------ ------ ------ ------------ TEST COM 8k 47 Dir RW D>test Total 1249 alloc calls Total 560 free calls Total 44490 bytes allocated in 689 buffers D>test Total 1296 alloc calls Total 606 free calls Total 44150 bytes allocated in 690 buffers D>test Total 1424 alloc calls Total 741 free calls Total 43933 bytes allocated in 683 buffers D>test Total 1627 alloc calls Total 933 free calls Total 44256 bytes allocated in 694 buffers D>test Total 1175 alloc calls Total 494 free calls Total 44425 bytes allocated in 681 buffers D>test Total 1319 alloc calls Total 655 free calls Total 43936 bytes allocated in 664 buffers D>test Total 1245 alloc calls Total 575 free calls Total 44367 bytes allocated in 670 buffers D> (OLD ALLOC) D>sdir test.com Directory For Drive D: User 0 Name Bytes Recs Attributes ------------ ------ ------ ------------ TEST COM 8k 45 Dir RW D>test Total 1326 alloc calls Total 679 free calls Total 40323 bytes allocated in 647 buffers D>test Total 1069 alloc calls Total 410 free calls Total 41289 bytes allocated in 659 buffers D>test Total 1351 alloc calls Total 781 free calls Total 38274 bytes allocated in 570 buffers D>test Total 1069 alloc calls Total 410 free calls Total 41289 bytes allocated in 659 buffers D>test Total 1417 alloc calls Total 817 free calls Total 37912 bytes allocated in 600 buffers D>test Total 1103 alloc calls Total 473 free calls Total 40279 bytes allocated in 630 buffers D>test Total 1251 alloc calls Total 625 free calls Total 39636 bytes allocated in 626 buffers D> It can be noticed that this new alloc/free package allows the user to allocate more memory (~4Kb more), compared to the old one. This is probably because the new alloc/free algorithm works better, compared with the old one, in the case of a highly fragmented free memory.

ladislau szilagyi

unread,
Jun 13, 2024, 6:22:55 AMJun 13
to RC2014-Z80
I added a relevant test, to show that this new library can be used in all C programs.

I choose a simple program, designed to build a .as source file starting from a .sym file.

Instead using only the functions provided by the standard HiTech C library, we will use also specific functions provided by the new allocation package.
The particular C library function important in our test is 'sbrk'. It allocates a buffer in the heap.
It is used by all file related system functions to allocate a 512 buffer, to be used by disk I/O operations.
So, we will include the new 'myalloc.as' in the C command line, to "force" the linker to use the new 'sbrk', instead of the 'sbrk' from CLIB.LIB

D>c -v -o -ft.sym -mt.map t.c myalloc.as

D>

Let's verify that the new 'sbrk' is used, and NOT the 'old sbrk' from CLIB.LIB :

D>type t.map
Machine type is Z80

CRTCPM.OBJ      text            0      13D      data         24DE        1
T.OBJ           text          13D      1C4      data         24DF       9A
                bss          27B3       6B
MYALLOC.OBJ     text          301      418
...
                                  Symbol Table

__Hbss      bss   2A29  __Hdata     data  27B3  __Htext     text  24DE
__Lbss      bss   27B3  __Ldata     data  24DE  __Ltext     text  0000
...
_sbrk       text  0336  _setfcb     text  1C8E  _setuid     text  1FAC
...

D>

As you noticed, in the list of symbols, sbrk = 0336H, which is inside myalloc.obj address space.
Therefore, the 'new' sbrk will be used.

Let's give it a try...
 
D>t t.as t.sym

D>type t.as
GLOBAL _myrealloc
_myrealloc EQU 0051EH
GLOBAL __cpm_clean
__cpm_clean EQU 01FF2H
GLOBAL _total_free
_total_free EQU 00320H
GLOBAL _i
_i EQU 02818H
...

It works OK.

Therefore, the new alloc library can be safely used by C programs.

Ladislau

Reply all
Reply to author
Forward
0 new messages