1 #ifndef __MALLOC_H__
2 #define __MALLOC_H__
3 
4 #include <sys/compiler.h>
5 
6 /*
7  * Now some trickery to link in the correct routines for far
8  *
9  * $Id: malloc.h,v 1.17 2016-06-11 19:53:08 dom Exp $
10  */
11 
12 
13 #ifndef FARDATA
14 
15 // The Near Malloc Library is still a simple first
16 // fit linear search of a list of free blocks.  The
17 // list of free blocks is kept sorted by address so
18 // that merging of adjacent blocks can occur.
19 //
20 // The block memory allocator (balloc.lib) is an
21 // alternative for allocating blocks of fixed size.
22 // Its main advantage is that it is very quick O(1)
23 // in comparison to the O(N) of this library.
24 //
25 // Space must be declared to hold the process's
26 // standard heap:
27 //
28 // long heap;
29 //
30 // An alternative is to reserve four bytes
31 // in RAM at address xxxx using:
32 //
33 // extern long heap(xxxx);
34 //
35 // The heap must be initialized to empty with a
36 // call to mallinit() or by setting heap=0L.
37 // Then available memory must be added by one or
38 // more calls to sbrk() as in:
39 //
40 // mallinit();        /* heap = 0L; is an alternative              */
41 // sbrk(50000,4000);  /* add 4000 bytes from 50000-53999 inclusive */
42 // sbrk(25000,126);   /* add 126 bytes from 25000-25125 inclusive  */
43 // a = malloc(100);
44 
45 /* Trick to force a default malloc initialization    */
46 /* Activate it by invoking zcc with i.e. '-DAMALLOC' */
47 
48 // Automatic Preset for malloc:  3/4 of the free memory
49 #ifdef AMALLOC
50 #pragma output USING_amalloc
51 #endif
52 #ifdef AMALLOC3
53 #pragma output USING_amalloc
54 #endif
55 
56 // Automatic Preset for malloc:  2/4 of the free memory
57 #ifdef AMALLOC2
58 #pragma output USING_amalloc
59 #pragma output USING_amalloc_2
60 #endif
61 
62 // Automatic Preset for malloc:  1/4 of the free memory
63 #ifdef AMALLOC1
64 #pragma output USING_amalloc
65 #pragma output USING_amalloc_2
66 #pragma output USING_amalloc_1
67 #endif
68 
69 extern void __LIB__              mallinit(void);
70 extern void __LIB__              sbrk(void *addr, unsigned int size) __smallc;
71 extern void __LIB__    sbrk_callee(void *addr, unsigned int size) __smallc __z88dk_callee;
72 extern void __LIB__              *calloc(unsigned int nobj, unsigned int size) __smallc;
73 extern void __LIB__    *calloc_callee(unsigned int nobj, unsigned int size) __smallc __z88dk_callee;
74 extern void __LIB__  free(void *addr) __z88dk_fastcall;
75 extern void __LIB__  *malloc(unsigned int size) __z88dk_fastcall;
76 extern void __LIB__              *realloc(void *p, unsigned int size) __smallc;
77 extern void __LIB__    *realloc_callee(void *p, unsigned int size) __smallc __z88dk_callee;
78 extern void __LIB__              mallinfo(unsigned int *total, unsigned int *largest) __smallc;
79 extern void __LIB__    mallinfo_callee(unsigned int *total, unsigned int *largest) __smallc __z88dk_callee;
80 
81 #define sbrk(a,b)      sbrk_callee(a,b)
82 #define calloc(a,b)    calloc_callee(a,b)
83 #define realloc(a,b)   realloc_callee(a,b)
84 #define mallinfo(a,b)  mallinfo_callee(a,b)
85 
86 // The following is to allow programs using the
87 // older version of the near malloc library to
88 // continue to work
89 
90 #define HEAPSIZE(bp)       unsigned char heap[bp+4];
91 #define heapinit(a)        mallinit(); sbrk_callee(heap+4,a);
92 #define getfree()          asm("EXTERN\tMAHeapInfo\nEXTERN\t_heap\nld\thl,_heap\ncall\tMAHeapInfo\nex\tde,hl\n")
93 #define getlarge()         asm("EXTERN\tMAHeapInfo\nEXTERN\t_heap\nld\thl,_heap\ncall\tMAHeapInfo\nld\tl,c\nld\th,b\n")
94 #define realloc_down(a,b)  realloc_callee(a,b)
95 
96 
97 
98 // Named Heap Functions
99 //
100 // The near malloc library supports multiple independent
101 // heaps; by referring to one by name, allocation
102 // and deallocation can be performed from a specific heap.
103 //
104 // To create a new heap, simply declare a long to hold
105 // the heap's pointer as in:
106 //
107 // long myheap;
108 //
109 // or, to place in RAM at specific address xxxx:
110 //
111 // extern long myheap(xxxx);
112 //
113 // Heaps must be initialized to empty with a call to
114 // HeapCreate() or by setting them =0L (myheap=0L; eg).
115 // Then available memory must be added to the heap
116 // with one or more calls to HeapSbrk():
117 //
118 // HeapCreate(&myheap);             /* myheap = 0L;       */
119 // HeapSbrk(&myheap, 50000, 5000);  /* add memory to heap */
120 // a = HeapAlloc(&myheap, 14);
121 //
122 // The main intent of multiple heaps is to allow various
123 // heaps to be valid in different memory configurations, allowing
124 // program segments to get valid near memory while different
125 // memory configurations are active.
126 //
127 // The stdlib functions implicitly use the heap named "heap".
128 // So, for example, a call to HeapAlloc(heap,size) is equivalent
129 // to a call to malloc(size).
130 
131 extern void __LIB__  HeapCreate(void *heap) __z88dk_fastcall;
132 extern void __LIB__              HeapSbrk(void *heap, void *addr, unsigned int size) __smallc;
133 extern void __LIB__    HeapSbrk_callee(void *heap, void *addr, unsigned int size) __smallc __z88dk_callee;
134 extern void __LIB__              *HeapCalloc(void *heap, unsigned int nobj, unsigned int size) __smallc;
135 extern void __LIB__    *HeapCalloc_callee(void *heap, unsigned int nobj, unsigned int size) __smallc __z88dk_callee;
136 extern void __LIB__              HeapFree(void *heap, void *addr) __smallc;
137 extern void __LIB__    HeapFree_callee(void *heap, void *addr) __smallc __z88dk_callee;
138 extern void __LIB__              *HeapAlloc(void *heap, unsigned int size) __smallc;
139 extern void __LIB__    *HeapAlloc_callee(void *heap, unsigned int size) __smallc __z88dk_callee;
140 extern void __LIB__              *HeapRealloc(void *heap, void *p, unsigned int size) __smallc;
141 extern void __LIB__    *HeapRealloc_callee(void *heap, void *p, unsigned int size) __smallc __z88dk_callee;
142 extern void __LIB__              HeapInfo(unsigned int *total, unsigned int *largest, void *heap) __smallc;
143 extern void __LIB__    HeapInfo_callee(unsigned int *total, unsigned int *largest, void *heap) __smallc __z88dk_callee;
144 
145 #define HeapSbrk(a,b,c)     HeapSbrk_callee(a,b,c)
146 #define HeapCalloc(a,b,c)   HeapCalloc_callee(a,b,c)
147 #define HeapFree(a,b)       HeapFree_callee(a,b)
148 #define HeapAlloc(a,b)      HeapAlloc_callee(a,b)
149 #define HeapRealloc(a,b,c)  HeapRealloc_callee(a,b,c)
150 #define HeapInfo(a,b,c)     HeapInfo_callee(a,b,c)
151 
152 #else
153 
154 /*
155  * Now some definitions for far functions
156  */
157 
158 #define calloc(a,b) calloc_far(a,b)
159 #define malloc(a)   malloc_far(a)
160 #define free(a)     free_far(a)
161 
162 // realloc, sbrk, mallinit, mallinfo not implemented in far lib
163 
164 #define realloc(a,b)
165 #define sbrk(a,b)      heapinit_far(b)
166 #define mallinit()
167 #define mallinfo(a,b)
168 
169 // these are for compatibility with the older version of the near malloc lib
170 
171 #define HEAPSIZE(bp)
172 #define getfree()
173 #define getlarge()
174 #define heapinit(a)    heapinit_far(a)
175 
176 extern far void __LIB__ *calloc_far(int, int);
177 extern far void __LIB__ *malloc_far(long);
178 extern void __LIB__ free_far(far void *);
179 extern void __LIB__ freeall_far();
180 
181 /* Create the correct memory spec */
182 #ifdef MAKE_PACKAGE
183 #pragma output far_mmset
184 #endif
185 
186 
187 
188 #endif /* FARDATA */
189 
190 
191 
192 #endif /* _MALLOC_H */
193