1 static char RCSId
[] = "$Id: heap.c,v 1.3 1993/07/04 04:04:21 root Exp root $";
2 static char Copyright
[] = "Copyright Robert J. Amstadt, 1993";
6 #include "prototypes.h"
10 MDESC
*LOCAL_FreeList
;
12 /**********************************************************************
16 HEAP_Init(MDESC
**free_list
, void *start
, int length
)
18 *free_list
= (MDESC
*) start
;
19 (*free_list
)->prev
= NULL
;
20 (*free_list
)->next
= NULL
;
21 (*free_list
)->length
= length
- sizeof(MDESC
);
24 /**********************************************************************
28 HEAP_Alloc(MDESC
**free_list
, int flags
, int bytes
)
33 printf("HeapAlloc: free_list %08x, flags %x, bytes %d\n",
34 free_list
, flags
, bytes
);
38 * Find free block big enough.
40 for (m
= *free_list
; m
!= NULL
; m
= m
->next
)
42 if (m
->length
>= bytes
&& m
->length
< bytes
+ 4 * sizeof(MDESC
))
46 else if (m
->length
> bytes
)
48 m_new
= m
+ (bytes
/ sizeof(MDESC
)) + 2;
52 m
->prev
->next
= m_new
;
55 m
->next
->prev
= m_new
;
57 m_new
->next
= m
->next
;
58 m_new
->prev
= m
->prev
;
59 m_new
->length
= m
->length
- ((int) m_new
- (int) m
);
60 m
->length
-= (m_new
->length
+ sizeof(MDESC
));
64 return (void *) (m
+ 1);
73 m
->prev
->next
= m
->next
;
76 m
->next
->prev
= m
->prev
;
80 return (void *) (m
+ 1);
86 /**********************************************************************
90 HEAP_Free(MDESC
**free_list
, void *block
)
99 m_free
= (MDESC
*) block
- 1;
100 if (m_free
->prev
!= m_free
|| m_free
->next
!= m_free
||
101 ((int) m_free
& 0xffff0000) != ((int) *free_list
& 0xffff0000))
104 printf("Attempt to free bad pointer,"
105 "m_free = %08x, *free_list = %08x\n",
112 * Find location in free list.
115 for (m
= *free_list
; m
!= NULL
&& m
< m_free
; m
= m
->next
)
118 if (m_prev
!= NULL
&& (int) m_prev
+ m_prev
->length
> (int) m_free
)
121 printf("Attempt to free bad pointer,"
122 "m_free = %08x, m_prev = %08x (length %x)\n",
123 m_free
, m_prev
, m_prev
->length
);
128 if ((m
!= NULL
&& (int) m_free
+ m_free
->length
> (int) m
) ||
129 (int) m_free
+ m_free
->length
> ((int) m_free
| 0xffff))
132 printf("Attempt to free bad pointer,"
133 "m_free = %08x (length %x), m = %08x\n",
134 m_free
, m_free
->length
, m
);
140 * Put block back in free list.
141 * Does it merge with the previos block?
145 if ((int) m_prev
+ m_prev
->length
== (int) m_free
)
147 m_prev
->length
+= sizeof(MDESC
) + m_free
->length
;
152 m_prev
->next
= m_free
;
153 m_free
->prev
= m_prev
;
163 * Does it merge with the next block?
167 if ((int) m_free
+ m_free
->length
== (int) m
)
169 m_free
->length
+= sizeof(MDESC
) + m
->length
;
170 m_free
->next
= m
->next
;
184 /**********************************************************************
188 HEAP_LocalInit(void *start
, int length
)
190 HEAP_Init(&LOCAL_FreeList
, start
, length
);
193 /**********************************************************************
197 HEAP_LocalAlloc(int flags
, int bytes
)
202 printf("LocalAlloc: flags %x, bytes %d\n", flags
, bytes
);
205 m
= HEAP_Alloc(&LOCAL_FreeList
, flags
, bytes
);
208 printf("LocalAlloc: returning %x\n", (int) m
);