LIBRARIES PUBLIC DOCUMENT MEMUTIL
About Functions of Memory Management Utility for HI-TECH C By Tatsuhiko Syoji
- SBRK .AS
- Source files of improved sbrk, setheaptop and getheaptop functions.
- SBRK .AS
- Object files of improved sbrk, setheaptop and getheaptop functions.
- MEMUTIL .H
- Header files of setheaptop and getheaptop functions.
- MEMUTIL .H
- Manuals of setheaptop and getheaptop functions and explanation about memory management.
What is this?
I changed standard memory managing function of HI-TECH C, sbrk to get memory blocks of certain address.
This function crushes if you don't know about memory address management.
Understand memory management of HI-TECH C and use it at your own risk.
Memory management of HI-TECH C
HI-TECH C manages memory roughly by 5 ways.
1st way is by data psect. Global valuables with default values and static valuables inside functions with default values belong this group and are made as block.
Memory address of valuable in this group are assigned a top address of the block which specified with -P option of linker(-Pdata=1000H). If address is not specified, it assigned soon after the address of text psect.
2nd way is by bss psect. Global valuables without default values and static valuables inside functions without default values belong this group and are made as block. Memory address of valuable in this group are assigned a top address of the block which specified with -P option of linker(-Pdata=1000H). If address is not specified, it assigned soon after the address of text psect.
3rd way is using stacks. Auto valyable belongs this group. Internal function ncsv assign memory for valuable from stacks when this function is called. And internal function cret frees memory.
4th way is using memory area assigned address with org operand in assembler. Memory address is decided by org instruction, length of program code after that and length of data.
Last way is using memory obtained from heap. Memory blocks which is gotten by sbrk,malloc and calloc from heap belongs to this group.
When malloc or calloc function is executed, it try to get higher address memory from top of heap to stack.
Top of heap locates after bss psect which represents by system definition symbol __Hbss.
Scheme of this is below.
low -Pdata=xxxx -> ------------ |data psect| | | -Pbss=xxxx -> ------------ |bss psect | | | __Hbss -> ------------ |Heap | | | | | |/ | | | | | | /| | | | | -Pbss=xxxx -> ------------ |Stacks | |(auto val.)| -Pbss=xxxx -> ------------ |System | |Work etc.| ------------ high
How setheaptop function works
Function setheaptop changes top address of heap and forces to get memory from certain address. (Strictly speaking, address cannot be specified because there is area managed by malloc and calloc functions.)
Scheme of this is below.
low -Pdata=xxxx -> ------------ |data psect| | | -Pbss=xxxx -> ------------ |bss psect | | | __Hbss -> ------------ | | | | setheaptop ------------ Specified addr.| | |heap | | | | | |/ | | | | | | /| | | | | -Pbss=xxxx -> ------------ |Stack | |(auto val.)| -Pbss=xxxx -> ------------ |System | |Work etc.| ------------ high
We can get and use memory block of certain size and certain address by them.
Sometimes while we make programs for MSX, we should locate data at certain address. That is why this function is required.
For example, data which referred by interruption should be located at page 1(4000H) and higher.
Data psect or bss psect can locate data on certain area. It contains a big unnecessary area because execution file contains that as well though.
Auto valuable consume stacks. It is inconvenient that in case stack should exist in page 3.
This inconvenient situation can be avoided by this function which can get memory block of certain address.
You might think we should directly assign address without such a function assuming memory is free.
In this case, we deal multiple memory blocks, it is very annoying and risky that users should avoid overlap of memory address or stacks...
This function calculate addresses automatically to check overlap stacks and memory or top address of next assigned memory block. It is the advantage of this package that makes users control memory block easily and gives flexibility of address assignment.
How to use
Copy MEMUTIL.H to where header files exist after HI-TECH C installed. Prepare libr.com(HI-TECH C package i ncluded),libc.lib(HI-TECH C package included or compatible one) and sbrk.obj( of this package). Execute this command below to complete installation of this package.
libr r libc.lib sbrk.obj
Functions of this package can be used by including memutil.h and linking libc.lib after installation.
- void setheaptop(unsigned int addr)
- Changes top address of heap to address specified with addr.
- It is the best to use before malloc and calloc functions.
- unsigned int getheaptop(void)
- Gets top address of the unused heap.
- Returns 0 if it is called without calling any of malloc, calloc or setheaptop functions before.
Copyrights and others
This library is modified variant of the sources of HI-TECH C for CP/M Version 3.09 and license is same as HI-TECH C for CP/M Version 3.09.
This is citation from original Z80READ.ME.
The HI-TECH Z80 CP/M C compiler V3.09 is provided free of charge for any use, private or commercial, strictly as-is. No warranty or product support is offered or implied.
You may use this software for whatever you like, providing you acknowledge that the copyright to this software remains with HI-TECH Software.
追加した関数については作者であるTatsuが著作権を所有しています。 The author Tatsu has a copyrights about extended functions.