* system.h (malloc, realloc, calloc, strdup, bzero, bcmp, rindex):
[official-gcc.git] / boehm-gc / new_hblk.c
blob1e1273f854ef54f673ee805e8667c900d5f8d449
1 /*
2 * Copyright 1988, 1989 Hans-J. Boehm, Alan J. Demers
3 * Copyright (c) 1991-1994 by Xerox Corporation. All rights reserved.
5 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
6 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
8 * Permission is hereby granted to use or copy this program
9 * for any purpose, provided the above notices are retained on all copies.
10 * Permission to modify the code and to distribute modified code is granted,
11 * provided the above notices are retained, and a notice that the code was
12 * modified is included with the above copyright notice.
14 * This file contains the functions:
15 * ptr_t GC_build_flXXX(h, old_fl)
16 * void GC_new_hblk(n)
18 /* Boehm, May 19, 1994 2:09 pm PDT */
21 # include <stdio.h>
22 # include "gc_priv.h"
24 #ifndef SMALL_CONFIG
26 * Build a free list for size 1 objects inside hblk h. Set the last link to
27 * be ofl. Return a pointer tpo the first free list entry.
29 ptr_t GC_build_fl1(h, ofl)
30 struct hblk *h;
31 ptr_t ofl;
33 register word * p = (word *)h;
34 register word * lim = (word *)(h + 1);
36 p[0] = (word)ofl;
37 p[1] = (word)(p);
38 p[2] = (word)(p+1);
39 p[3] = (word)(p+2);
40 p += 4;
41 for (; p < lim; p += 4) {
42 p[0] = (word)(p-1);
43 p[1] = (word)(p);
44 p[2] = (word)(p+1);
45 p[3] = (word)(p+2);
47 return((ptr_t)(p-1));
50 /* The same for size 2 cleared objects */
51 ptr_t GC_build_fl_clear2(h, ofl)
52 struct hblk *h;
53 ptr_t ofl;
55 register word * p = (word *)h;
56 register word * lim = (word *)(h + 1);
58 p[0] = (word)ofl;
59 p[1] = 0;
60 p[2] = (word)p;
61 p[3] = 0;
62 p += 4;
63 for (; p < lim; p += 4) {
64 p[0] = (word)(p-2);
65 p[1] = 0;
66 p[2] = (word)p;
67 p[3] = 0;
69 return((ptr_t)(p-2));
72 /* The same for size 3 cleared objects */
73 ptr_t GC_build_fl_clear3(h, ofl)
74 struct hblk *h;
75 ptr_t ofl;
77 register word * p = (word *)h;
78 register word * lim = (word *)(h + 1) - 2;
80 p[0] = (word)ofl;
81 p[1] = 0;
82 p[2] = 0;
83 p += 3;
84 for (; p < lim; p += 3) {
85 p[0] = (word)(p-3);
86 p[1] = 0;
87 p[2] = 0;
89 return((ptr_t)(p-3));
92 /* The same for size 4 cleared objects */
93 ptr_t GC_build_fl_clear4(h, ofl)
94 struct hblk *h;
95 ptr_t ofl;
97 register word * p = (word *)h;
98 register word * lim = (word *)(h + 1);
100 p[0] = (word)ofl;
101 p[1] = 0;
102 p[2] = 0;
103 p[3] = 0;
104 p += 4;
105 for (; p < lim; p += 4) {
106 PREFETCH_FOR_WRITE(p+64);
107 p[0] = (word)(p-4);
108 p[1] = 0;
109 CLEAR_DOUBLE(p+2);
111 return((ptr_t)(p-4));
114 /* The same for size 2 uncleared objects */
115 ptr_t GC_build_fl2(h, ofl)
116 struct hblk *h;
117 ptr_t ofl;
119 register word * p = (word *)h;
120 register word * lim = (word *)(h + 1);
122 p[0] = (word)ofl;
123 p[2] = (word)p;
124 p += 4;
125 for (; p < lim; p += 4) {
126 p[0] = (word)(p-2);
127 p[2] = (word)p;
129 return((ptr_t)(p-2));
132 /* The same for size 4 uncleared objects */
133 ptr_t GC_build_fl4(h, ofl)
134 struct hblk *h;
135 ptr_t ofl;
137 register word * p = (word *)h;
138 register word * lim = (word *)(h + 1);
140 p[0] = (word)ofl;
141 p[4] = (word)p;
142 p += 8;
143 for (; p < lim; p += 8) {
144 PREFETCH_FOR_WRITE(p+64);
145 p[0] = (word)(p-4);
146 p[4] = (word)p;
148 return((ptr_t)(p-4));
151 #endif /* !SMALL_CONFIG */
154 * Allocate a new heapblock for small objects of size n.
155 * Add all of the heapblock's objects to the free list for objects
156 * of that size.
157 * Set all mark bits if objects are uncollectable.
158 * Will fail to do anything if we are out of memory.
160 void GC_new_hblk(sz, kind)
161 register word sz;
162 int kind;
164 register word *p,
165 *prev;
166 word *last_object; /* points to last object in new hblk */
167 register struct hblk *h; /* the new heap block */
168 register GC_bool clear = GC_obj_kinds[kind].ok_init;
170 # ifdef PRINTSTATS
171 if ((sizeof (struct hblk)) > HBLKSIZE) {
172 ABORT("HBLK SZ inconsistency");
174 # endif
176 /* Allocate a new heap block */
177 h = GC_allochblk(sz, kind, 0);
178 if (h == 0) return;
180 /* Mark all objects if appropriate. */
181 if (IS_UNCOLLECTABLE(kind)) GC_set_hdr_marks(HDR(h));
183 PREFETCH_FOR_WRITE((char *)h);
184 PREFETCH_FOR_WRITE((char *)h + 128);
185 PREFETCH_FOR_WRITE((char *)h + 256);
186 PREFETCH_FOR_WRITE((char *)h + 378);
187 /* Handle small objects sizes more efficiently. For larger objects */
188 /* the difference is less significant. */
189 # ifndef SMALL_CONFIG
190 switch (sz) {
191 case 1: GC_obj_kinds[kind].ok_freelist[1] =
192 GC_build_fl1(h, GC_obj_kinds[kind].ok_freelist[1]);
193 return;
194 case 2: if (clear) {
195 GC_obj_kinds[kind].ok_freelist[2] =
196 GC_build_fl_clear2(h, GC_obj_kinds[kind].ok_freelist[2]);
197 } else {
198 GC_obj_kinds[kind].ok_freelist[2] =
199 GC_build_fl2(h, GC_obj_kinds[kind].ok_freelist[2]);
201 return;
202 case 3: if (clear) {
203 GC_obj_kinds[kind].ok_freelist[3] =
204 GC_build_fl_clear3(h, GC_obj_kinds[kind].ok_freelist[3]);
205 return;
206 } else {
207 /* It's messy to do better than the default here. */
208 break;
210 case 4: if (clear) {
211 GC_obj_kinds[kind].ok_freelist[4] =
212 GC_build_fl_clear4(h, GC_obj_kinds[kind].ok_freelist[4]);
213 } else {
214 GC_obj_kinds[kind].ok_freelist[4] =
215 GC_build_fl4(h, GC_obj_kinds[kind].ok_freelist[4]);
217 return;
218 default:
219 break;
221 # endif /* !SMALL_CONFIG */
223 /* Clear the page if necessary. */
224 if (clear) BZERO(h, HBLKSIZE);
226 /* Add objects to free list */
227 p = &(h -> hb_body[sz]); /* second object in *h */
228 prev = &(h -> hb_body[0]); /* One object behind p */
229 last_object = (word *)((char *)h + HBLKSIZE);
230 last_object -= sz;
231 /* Last place for last object to start */
233 /* make a list of all objects in *h with head as last object */
234 while (p <= last_object) {
235 /* current object's link points to last object */
236 obj_link(p) = (ptr_t)prev;
237 prev = p;
238 p += sz;
240 p -= sz; /* p now points to last object */
243 * put p (which is now head of list of objects in *h) as first
244 * pointer in the appropriate free list for this size.
246 obj_link(h -> hb_body) = GC_obj_kinds[kind].ok_freelist[sz];
247 GC_obj_kinds[kind].ok_freelist[sz] = ((ptr_t)p);