/[projet1]/public/pc/tools/osdk/main/Osdk/_final_/include2/alloc.h
Defence Force logotype

Contents of /public/pc/tools/osdk/main/Osdk/_final_/include2/alloc.h

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1159 - (show annotations)
Sun Aug 10 18:31:35 2014 UTC (5 years, 5 months ago) by dbug
File MIME type: text/plain
File size: 6797 byte(s)


1 /* alloc.h */
2
3 #ifndef _ALLOC_
4
5 #define _ALLOC_
6
7 /* This header file has a horrible amount of comments. Sorry about this --
8 memory allocation is a tricky job, and I want to make sure everything
9 is clear (to me, mostly). Oh, it will also help new users get accustomed to
10 some of the most heavily used C library functions (at least on larger
11 systems -- not much call for malloc(1<<20) on an Oric!). :-) - Alexios */
12
13
14 /* Heap manager variables */
15
16
17 /* stacksize is the size of the C (*NOT* the CPU) stack. This is an upward
18 stack which starts at the point where program code ends (endcode label).
19 You can change this variable _BEFORE_ the heap is initialised (i.e.
20 before the first call to heapinit() or malloc(), or immediately after a call
21 to freall(). Its default value is 512 bytes. If you do not use heap
22 functions, you need not worry about this. */
23
24 extern unsigned int stacksize;
25
26
27 /* heapovh is the size in bytes of the memory block descriptor (MBD), which
28 precedes (in RAM) all malloc()ed memory blocks. The ABSOLUTE MINIMUM
29 (and default) is 4 bytes (a pointer to the next block, and the length of the
30 block). It may be changed by the user or for later expansion (swapping to
31 floppy, etc.). Be sure to change it _BEFORE_ any calls to heapinit() or
32 malloc(), or immediately after a call to freall(). */
33
34 extern unsigned char heapovh;
35
36
37 /* heapstart points to the beginning of the heap in memory. This is initialised
38 automatically by heapinit() (which is executed by malloc() the first time
39 malloc() is called). There is usually no need to change this. Please note,
40 this is the beginning of the HEAP. The first MBD may or may not be there.
41 See heapdesc. */
42
43 extern unsigned int heapstart;
44
45
46 /* heapend points to the end of the heap + 1. That is, it points to the first
47 byte in memory which should NOT be allocated to the heap. The default value
48 is 0xb400 (the character table for the TEXT mode). If you plan to use HIRES
49 with memory allocation, you should assign 0x9800 to heapend. You can change
50 it any time you like, but, if you do so AFTER the first call to malloc() or
51 heapinit(), be sure to call heapupdate if you want heapsize and coreleft to
52 return the right results. */
53
54 extern unsigned int heapend;
55
56
57 /* heapdesc simply points to the first MBD in the heap. It contains NULL
58 (0x0000) if the heap contains no memory block descriptors. */
59
60 extern unsigned int heapdesc;
61
62
63 /* nheapdesc is the number of memory blocks allocated in the heap. DO NOT
64 CHANGE THIS VARIABLE, unless you particularly like broken statistics (it
65 will not interfere with the correct operation of the memory (de)allocation
66 functions). */
67
68 extern unsigned int nheapdesc;
69
70
71 /* nheapbytes is the number of bytes allocated in the heap. This includes the
72 MBDs. For example, if heapovh=4 and you've allocated three blocks with sizes
73 256, 300 and 1024 respectively, nheapbytes will be equal to 4+256 + 4+300 +
74 4+1024 = 1592 bytes. DO NOT CHANGE THIS VARIABLE, unless you particularly
75 like broken statistics (it will not interfere with the correct operation of
76 the memory (de)allocation functions). */
77
78 extern unsigned int nheapbytes;
79
80
81 /* heapsize is calculated by heapinit() or heapupdate(). It contains the result
82 of the expression heapend-heapstart. */
83
84 extern unsigned int heapsize;
85
86
87
88
89 /* Prototypes for memory allocation functions follow. */
90
91
92 /* Initialise the heap. Call heapupdate() as well. You DO NOT need to call this
93 function explicitly: malloc() invokes it when first called. */
94
95 extern void heapinit();
96
97
98 /* Recalculates heapsize to account for run-time changes in heapstart or
99 heapend. */
100
101 extern void heapupdate();
102
103
104 /* Allocates a block of size bytes and initialises all bytes to zero. Returns a
105 pointer to the memory block. If the block cannot be allocated, NULL is
106 returned. */
107
108 #define zalloc(size) (memset(malloc(size),0x00,size)
109
110
111 /* Allocates a block able to accommodate an array of num_elems elements, each
112 of which is elem_size bytes long. All bytes in the memory block are
113 initialised to zero. Returns a pointer to the newly allocated memory block.
114 If the block cannot be allocated, NULL is returned. */
115
116 #define calloc(num_elems,elem_size) (zalloc((num_elems)*(elem_size)))
117
118
119 /* This is the main allocation function. It allocates a memory block of
120 num_bytes bytes, and returns a pointer to the block. If the block cannot be
121 allocated (too fragmented heap or too little memory), NULL is returned. */
122
123 extern void *malloc(unsigned int num_bytes);
124
125
126 /* free() deallocates the memory block pointed to by p, which is the value
127 returned by malloc() when the memory block was allocated. Nothing will
128 happen if the pointer does not point to a valid and existing memory block.
129 */
130
131 extern void free(void *p);
132
133
134 /* freeall is a macro which marks all the heap as deallocated. The next
135 call to malloc() will re-initialise the heap and start over. */
136
137 #define freeall (heapstart=heapdesc=nheapdesc=nheapbytes=0)
138
139
140 /* coreleft returns the free heap memory in bytes. */
141
142 #define coreleft (heapsize-nheapbytes)
143
144
145 /* this is the basic structure of an MBD. */
146
147 typedef struct memdesc {
148 struct memdesc *next; /* pointer to the next MBD in memory. */
149 unsigned int len; /* length of this memory block + heapovh */
150 } memdesc;
151
152
153 /* Traverses through all MBDs. The first call to heapwalk should be
154 heapwalk(NULL). This returns the first MBD in memory (the actual memory
155 block starts heapovh bytes after the MBD). Feeding back this result to
156 heapwalk() will return the next MBD and so on. NULL will be returned if
157 there are no more MBDs. Example:
158
159 {
160 memdesc *m=NULL;
161 int i=0;
162
163 while((m=heapwalk(m))!=NULL){
164 printf("Block %d starts at 0x%x. Length: %d\n",
165 i++,
166 (unsigned int)m+heapovh,
167 m->len-heapovh);
168 }
169 }
170
171 */
172
173 #define heapwalk(current) ((current)==NULL?(memdesc*)heapdesc:(current)->next)
174
175
176 /* Dynamically changes the size of a memory block mem_address, from its current
177 size to newsize bytes. Returns a pointer to the new memory block. The
178 pointer returned may or may not differ from mem_address, but realloc
179 guarrantees the first min(current_size,newsize) bytes have the same values
180 as the corresponding bytes in the old block (it will invoke memcpy() to copy
181 bytes to the new block, if necessary).
182
183 Calling realloc() with a mem_address of NULL is the same as calling
184 malloc(newsize). Calling realloc() with newsize==0 is the same as calling
185 free(mem_address).
186 */
187
188 extern void *realloc(void *mem_address, unsigned int newsize);
189
190 #endif /* _ALLOC_ */
191
192 /* end of file alloc.h */
193

  ViewVC Help
Powered by ViewVC 1.1.26