[Z88dk-users] Using libraries: malloc on sdcc_iy

Bridge to the z88dk-users mailing list
Post Reply
obiwanjacobi
Member
Posts: 67
Joined: Tue Dec 22, 2015 7:39 am

[Z88dk-users] Using libraries: malloc on sdcc_iy

Post by obiwanjacobi »

This code

Code: Select all

#include <malloc.h>

void main() {
mallinit();
}
with this command line
z88dk zcc +zalt -SO2 -clib=sdcc_iy --max-allocs-per-node200000 "c:\Users\marc\Documents\HardwareProjects\Zalt\Zalt\Source\z88dk_target\_tests\Heap\Heap_test.c" -o "c:\Users\marc\Documents\HardwareProjects\Zalt\Zalt\Source\z88dk_target\_tests\Heap\Heap_test"
gives this error
c:\Users\marc\Documents\HardwareProjects\Zalt\Zalt\Source\z88dk_target\_tests\Heap\Heap_test.c:4: warning 112: function 'mallinit' implicit declaration
c:\Users\marc\Documents\HardwareProjects\Zalt\Zalt\Source\z88dk_target\_tests\Heap\Heap_test.c:4: warning 84: 'auto' variable 'mallinit' may be used before initialization
c:\Users\marc\Documents\HardwareProjects\Zalt\Zalt\Source\z88dk_target\_tests\Heap\Heap_test.c:4: warning 84: 'auto' variable 'mallinit' may be used before initialization
Error at file 'C:\Users\marc\AppData\Local\Temp\zccD4012.asm' line 286: symbol '_mallinit' not defined
1 errors occurred during assembly
Errors in source file c:\Users\marc\Documents\HardwareProjects\Zalt\Zalt\Source\z88dk_target\_tests\Heap\Heap_test.c:
Error at file 'C:\Users\marc\AppData\Local\Temp\zccD4012.asm' line 286: symbol '_mallinit' not defined
^ ---- jp _mallinit
and I can't figure out why.

The "implicit declaration" warning suggests it didn't see the prototype in malloc.h - but it's in there - I checked - assuming FARDATA is not defined. I could imaging a linker error when I may not have all the libraries build but this is a compiler error.

BTW: I have not made any changes to the complete set of libraries for the zalt target (yet).



------------------------------------------------------------------------------
Developer Access Program for Intel Xeon Phi Processors
Access to Intel Xeon Phi processor-based developer platforms.
With one year of Intel Parallel Studio XE.
Training and support from Colfax.
Order your platform today.http://sdm.link/intel
alvin
Well known member
Posts: 1872
Joined: Mon Jul 16, 2007 7:39 pm

Post by alvin »

It looks like you are mixing up the classic library and the new c library. mallinit() comes from the classic library's malloc implementation.

Some documentation: https://www.z88dk.org/wiki/doku.php?id= ... loc_malloc
Header file: http://z88dk.cvs.sourceforge.net/viewvc ... xt%2Fplain
Implementation: http://z88dk.cvs.sourceforge.net/viewvc ... alloc/z80/

In the new c library, heap initialization is done automatically by wedging a call to the init function in section CODE_CRT_INIT which is part of the startup code. What controls the amount of heap memory available is the compile time variable CLIB_MALLOC_HEAP_SIZE.

For most targets the default value of CLIB_MALLOC_HEAP_SIZE is -1. This instructs the implementation to use all available memory from the end of the BSS section to the bottom of the stack for the heap. The bottom of the stack is taken as the initial value of SP minus CRT_STACK_SIZE (another compile time variable by default is usually 256 or 512 bytes; this is the only use for this variable).

If that's not what you want you can set CLIB_MALLOC_HEAP_SIZE to a value > 14. This will cause the implementation to create a heap of that many bytes inside the BSS section. Again, this will be automatically initialized in the startup code.

If you don't want the implementation to do anything you can set CLIB_MALLOC_HEAP_SIZE to 0. Then your program will be responsible for setting up the heap. There is a little bit written on this at https://www.z88dk.org/wiki/doku.php?id= ... y_location The important point is you must set up a pointer to the block of memory in "__malloc_heap" and you can initialize the heap at that block of memory by calling "asm_heap_init" from asm or "void *heap_init_callee(void *heap, size_t size)" from C.

If you follow the documentation, you'll also see that the implementation supports a named heap API which just means you can create many heaps (maybe in different memory banks) and you can allocate from them if the name (rather pointer to) of the heap is supplied to the allocation functions. C's standard malloc api using malloc, calloc, realloc, etc is only different in that the name of the heap is implied.




That's fairly thick text.


Examples:


#include <malloc.h>

void main(void)
{
unsigned char *a;

a = malloc(100);
}


This should work without doing anything. The crt will have initialized the heap. Usually CLIB_MALLOC_HEAP_SIZE=-1 so the available heap memory will lie between the end of BSS and the bottom of the stack, which should be much more than 100 bytes.



#pragma output CLIB_MALLOC_HEAP_SIZE = 1024

#include <malloc.h>

void main(void)
{
unsigned char *a;

a = malloc(100);
}


In this one we've explicitly set the heap size to 1024 bytes (keep in mind there is some overhead so the availability is less than 1024 bytes). This heap will be created in the BSS section. Again you don't have to do any heap initialization.




#pragma output CLIB_MALLOC_HEAP_SIZE = 0

#include <malloc.h>

void main(void)
{
unsigned char *a;

a = malloc(100);
}

This one will complain about a missing heap variable. Setting CLIB_MALLOC_HEAP_SIZE to 0 indicates to the implementation that is shouldn't do any heap creation or initialization. In this case the program itself should be setting up the heap before calling malloc.



------------------------------------------------------------------------------
Developer Access Program for Intel Xeon Phi Processors
Access to Intel Xeon Phi processor-based developer platforms.
With one year of Intel Parallel Studio XE.
Training and support from Colfax.
Order your platform today.http://sdm.link/intel
obiwanjacobi
Member
Posts: 67
Joined: Tue Dec 22, 2015 7:39 am

Post by obiwanjacobi »

Thanx! :)

That explains it. I sort of figured as much but I could not navigate the wiki or the source code structure successfully to be sure.

Where are the include files located for the new library?



------------------------------------------------------------------------------
Developer Access Program for Intel Xeon Phi Processors
Access to Intel Xeon Phi processor-based developer platforms.
With one year of Intel Parallel Studio XE.
Training and support from Colfax.
Order your platform today.http://sdm.link/intel
alvin
Well known member
Posts: 1872
Joined: Mon Jul 16, 2007 7:39 pm

Post by alvin »

Where are the include files located for the new library?
All the include files for the new library are in:

http://z88dk.cvs.sourceforge.net/viewvc ... VELOPMENT/

The online view is showing a whole bunch of dead directories. The only valid ones are:

proto
clang
sccz80
sdcc

We create a separate set of header files for each compiler because of the differences in syntax for attributes. The clang directory is experimental and includes headers for clang. sccz80 contains headers for sccz80 and sdcc contains headers for sdcc. proto is the prototype set of headers from which all the others are generated.

For readability, the sdcc set of headers is probably easiest to look at. They all contain the same set of functions.


Source code is rooted in:

http://z88dk.cvs.sourceforge.net/viewvc ... VELOPMENT/



------------------------------------------------------------------------------
Developer Access Program for Intel Xeon Phi Processors
Access to Intel Xeon Phi processor-based developer platforms.
With one year of Intel Parallel Studio XE.
Training and support from Colfax.
Order your platform today.http://sdm.link/intel
obiwanjacobi
Member
Posts: 67
Joined: Tue Dec 22, 2015 7:39 am

Post by obiwanjacobi »

Ok, got it. Thanx again.



------------------------------------------------------------------------------
Developer Access Program for Intel Xeon Phi Processors
Access to Intel Xeon Phi processor-based developer platforms.
With one year of Intel Parallel Studio XE.
Training and support from Colfax.
Order your platform today.http://sdm.link/intel
Post Reply