Home My Page Projects Code Snippets Project Openings SML/NJ
Summary Activity Forums Tracker Lists Tasks Docs Surveys News SCM Files

SCM Repository

[smlnj] Annotation of /sml/trunk/src/runtime/gc/big-objects.c
ViewVC logotype

Annotation of /sml/trunk/src/runtime/gc/big-objects.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 2 - (view) (download) (as text)

1 : monnier 2 /* big-objects.c
2 :     *
3 :     * COPYRIGHT (c) 1993 by AT&T Bell Laboratories.
4 :     *
5 :     * Code for managing big-object regions.
6 :     */
7 :    
8 :     #include "ml-base.h"
9 :     #include "memory.h"
10 :     #include "heap.h"
11 :     #include "heap-monitor.h"
12 :    
13 :    
14 :     #ifdef BO_DEBUG
15 :     /* PrintRegionMap:
16 :     */
17 :     void PrintRegionMap (bigobj_region_t *r)
18 :     {
19 :     bigobj_desc_t *dp, *dq;
20 :     int i;
21 :    
22 :     SayDebug ("[%d] %d/%d, @%#x: ", r->minGen, r->nFree, r->nPages, r->firstPage);
23 :     for (i = 0, dq = NIL(bigobj_desc_t *); i < r->nPages; i++) {
24 :     dp = r->objMap[i];
25 :     if (dp != dq) {
26 :     SayDebug ("|");
27 :     dq = dp;
28 :     }
29 :     if (BO_IS_FREE(dp))
30 :     SayDebug ("_");
31 :     else
32 :     SayDebug ("X");
33 :     }
34 :     SayDebug ("|\n");
35 :    
36 :     } /* end of PrintRegionMap */
37 :     #endif
38 :    
39 :    
40 :     /* BO_AllocRegion:
41 :     *
42 :     * Allocate a big object region that is large enough to hold an object of at
43 :     * least szB bytes. It returns the descriptor for the free big-object that
44 :     * is the region.
45 :     * NOTE: it does not mark the BIBOP entries for the region; this should be
46 :     * done by the caller.
47 :     */
48 :     bigobj_desc_t *BO_AllocRegion (heap_t *heap, Addr_t szB)
49 :     {
50 :     int npages, oldNpages, i;
51 :     Addr_t hdrSzB, memObjSzB;
52 :     bigobj_region_t *region;
53 :     mem_obj_t *memObj;
54 :     bigobj_desc_t *desc;
55 :    
56 :     /* compute the memory object size.
57 :     * NOTE: there probably is a closed form for this, but I'm too lazy
58 :     * to try to figure it out.
59 :     */
60 :     npages = ROUNDUP(szB, BIGOBJ_PAGE_SZB) >> BIGOBJ_PAGE_SHIFT;
61 :     do {
62 :     oldNpages = npages;
63 :     hdrSzB = ROUNDUP(BOREGION_HDR_SZB(npages), BIGOBJ_PAGE_SZB);
64 :     szB = (npages << BIGOBJ_PAGE_SHIFT);
65 :     memObjSzB = RND_MEMOBJ_SZB(hdrSzB+szB);
66 :     memObjSzB = (memObjSzB < MIN_BOREGION_SZB) ? MIN_BOREGION_SZB : memObjSzB;
67 :     npages = (memObjSzB - hdrSzB) >> BIGOBJ_PAGE_SHIFT;
68 :     } while (npages != oldNpages);
69 :    
70 :     if ((memObj = MEM_AllocMemObj (memObjSzB)) == NIL(mem_obj_t *))
71 :     Die ("unable to allocate memory object for bigobject region");
72 :     region = (bigobj_region_t *)MEMOBJ_BASE(memObj);
73 :    
74 :     if ((desc = NEW_OBJ(bigobj_desc_t)) == NIL(bigobj_desc_t *))
75 :     Die ("unable to allocate big-object descriptor");
76 :    
77 :     /* initialize the region header */
78 :     region->firstPage = ((Addr_t)region + hdrSzB);
79 :     region->nPages = npages;
80 :     region->nFree = npages;
81 :     region->minGen = MAX_NUM_GENS;
82 :     region->memObj = memObj;
83 :     region->next = heap->bigRegions;
84 :     heap->bigRegions = region;
85 :     heap->numBORegions++;
86 :     for (i = 0; i < npages; i++)
87 :     region->objMap[i] = desc;
88 :    
89 :     /* initialize the descriptor for the region's memory */
90 :     desc->obj = region->firstPage;
91 :     desc->sizeB = szB;
92 :     desc->state = BO_FREE;
93 :     desc->region = region;
94 :    
95 :     #ifdef BO_DEBUG
96 :     SayDebug ("BO_AllocRegion: %d pages @ %#x\n", npages, region->firstPage);
97 :     #endif
98 :     return desc;
99 :    
100 :     } /* end of BO_AllocRegion */
101 :    
102 :    
103 :     /* BO_Alloc:
104 :     *
105 :     * Allocate a big object of the given size.
106 :     */
107 :     bigobj_desc_t *BO_Alloc (heap_t *heap, int gen, Addr_t objSzB)
108 :     {
109 :     bigobj_desc_t *hdr, *dp, *newObj;
110 :     bigobj_region_t *region;
111 :     Addr_t totSzB;
112 :     int i, npages, firstPage;
113 :    
114 :     totSzB = ROUNDUP(objSzB, BIGOBJ_PAGE_SZB);
115 :     npages = (totSzB >> BIGOBJ_PAGE_SHIFT);
116 :    
117 :     /* search for a free object that is big enough (first-fit) */
118 :     hdr = heap->freeBigObjs;
119 :     for (dp = hdr->next; (dp != hdr) && (dp->sizeB < totSzB); dp = dp->next)
120 :     continue;
121 :    
122 :     if (dp == hdr) {
123 :     /* no free object fits, so allocate a new region */
124 :     dp = BO_AllocRegion (heap, totSzB);
125 :     region = dp->region;
126 :     if (dp->sizeB == totSzB)
127 :     /* allocate the whole region to the object */
128 :     newObj = dp;
129 :     else {
130 :     /* split the free object */
131 :     newObj = NEW_OBJ(bigobj_desc_t);
132 :     newObj->obj = dp->obj;
133 :     newObj->region = region;
134 :     dp->obj = (Addr_t)(dp->obj) + totSzB;
135 :     dp->sizeB -= totSzB;
136 :     ADD_BODESC(heap->freeBigObjs, dp);
137 :     firstPage = ADDR_TO_BOPAGE(region, newObj->obj);
138 :     for (i = 0; i < npages; i++)
139 :     region->objMap[firstPage+i] = newObj;
140 :     }
141 :     }
142 :     else if (dp->sizeB == totSzB) {
143 :     REMOVE_BODESC(dp);
144 :     newObj = dp;
145 :     region = dp->region;
146 :     }
147 :     else {
148 :     /* split the free object, leaving dp in the free list. */
149 :     region = dp->region;
150 :     newObj = NEW_OBJ(bigobj_desc_t);
151 :     newObj->obj = dp->obj;
152 :     newObj->region = region;
153 :     dp->obj = (Addr_t)(dp->obj) + totSzB;
154 :     dp->sizeB -= totSzB;
155 :     firstPage = ADDR_TO_BOPAGE(region, newObj->obj);
156 :     for (i = 0; i < npages; i++)
157 :     dp->region->objMap[firstPage+i] = newObj;
158 :     }
159 :    
160 :     newObj->sizeB = objSzB;
161 :     newObj->state = BO_YOUNG;
162 :     newObj->gen = gen;
163 :     region->nFree -= npages;
164 :    
165 :     if (region->minGen > gen) {
166 :     /* update the generation part of the descriptor */
167 :     region->minGen = gen;
168 :     MarkRegion (BIBOP, (ml_val_t *)region, MEMOBJ_SZB(region->memObj),
169 :     AID_BIGOBJ(gen));
170 :     BIBOP[BIBOP_ADDR_TO_INDEX(region)] = AID_BIGOBJ_HDR(gen);
171 :     }
172 :    
173 :     #ifdef BO_DEBUG
174 :     SayDebug ("BO_Alloc: %d bytes @ %#x\n", objSzB, newObj->obj);
175 :     PrintRegionMap(region);
176 :     #endif
177 :     return newObj;
178 :    
179 :     } /* end of BO_Alloc */
180 :    
181 :    
182 :     /* BO_Free:
183 :     *
184 :     * Mark a big object as free and add it to the free list.
185 :     */
186 :     void BO_Free (heap_t *heap, bigobj_desc_t *desc)
187 :     {
188 :     bigobj_region_t *region = desc->region;
189 :     bigobj_desc_t *dp;
190 :     int firstPage, lastPage, i, j;
191 :     Addr_t totSzB = ROUNDUP(desc->sizeB, BIGOBJ_PAGE_SZB);
192 :    
193 :     firstPage = ADDR_TO_BOPAGE(region, desc->obj);
194 :     lastPage = firstPage + (totSzB >> BIGOBJ_PAGE_SHIFT);
195 :    
196 :     #ifdef BO_DEBUG
197 :     SayDebug ("BO_Free: @ %#x, bibop gen = %x, gen = %d, state = %d, pages=[%d..%d)\n",
198 :     desc->obj, (unsigned)EXTRACT_GEN(ADDR_TO_PAGEID(BIBOP, desc->obj)), desc->gen, desc->state, firstPage, lastPage);
199 :     PrintRegionMap(region);
200 :     #endif
201 :     if ((firstPage > 0) && BO_IS_FREE(region->objMap[firstPage-1])) {
202 :     /* coalesce with adjacent free object */
203 :     dp = region->objMap[firstPage-1];
204 :     REMOVE_BODESC(dp);
205 :     for (i = ADDR_TO_BOPAGE(region, dp->obj); i < firstPage; i++)
206 :     region->objMap[i] = desc;
207 :     desc->obj = dp->obj;
208 :     totSzB += dp->sizeB;
209 :     FREE (dp);
210 :     }
211 :    
212 :     if ((lastPage < region->nPages) && BO_IS_FREE(region->objMap[lastPage])) {
213 :     /* coalesce with adjacent free object */
214 :     dp = region->objMap[lastPage];
215 :     REMOVE_BODESC(dp);
216 :     for (i = lastPage, j = i+(dp->sizeB >> BIGOBJ_PAGE_SHIFT); i < j; i++)
217 :     region->objMap[i] = desc;
218 :     totSzB += dp->sizeB;
219 :     FREE (dp);
220 :     }
221 :    
222 :     desc->sizeB = totSzB;
223 :     desc->state = BO_FREE;
224 :    
225 :     region->nFree += (lastPage - firstPage);
226 :     /** what if (region->nFree == region->nPages) ??? **/
227 :    
228 :     /* add desc to the free list */
229 :     ADD_BODESC(heap->freeBigObjs, desc);
230 :    
231 :     } /* end of BO_Free */
232 :    
233 :    
234 :     /* BO_GetDesc:
235 :     *
236 :     * Given an address into a big object, return the object's descriptor.
237 :     */
238 :     bigobj_desc_t *BO_GetDesc (ml_val_t addr)
239 :     {
240 :     bibop_t bibop = BIBOP;
241 :     int i;
242 :     aid_t aid;
243 :     bigobj_region_t *rp;
244 :    
245 :     for (i = BIBOP_ADDR_TO_INDEX(addr); !BO_IS_HDR(aid = bibop[i]); i--)
246 :     continue;
247 :    
248 :     rp = (bigobj_region_t *)BIBOP_INDEX_TO_ADDR(i);
249 :    
250 :     return ADDR_TO_BODESC(rp, addr);
251 :    
252 :     } /* end of BO_GetDesc */

root@smlnj-gforge.cs.uchicago.edu
ViewVC Help
Powered by ViewVC 1.0.0