~ubuntu-branches/ubuntu/quantal/libgc/quantal

« back to all changes in this revision

Viewing changes to mallocx.c

  • Committer: Bazaar Package Importer
  • Author(s): Christoph Egger
  • Date: 2011-02-19 12:19:56 UTC
  • mfrom: (1.3.2 upstream) (0.1.5 experimental)
  • mto: This revision was merged to the branch mainline in revision 14.
  • Revision ID: james.westby@ubuntu.com-20110219121956-67rb69xlt5nud3v2
Tags: 1:7.1-5
Upload to unstable

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
/* Some externally visible but unadvertised variables to allow access to */
32
32
/* free lists from inlined allocators without including gc_priv.h        */
33
33
/* or introducing dependencies on internal data structure layouts.       */
34
 
ptr_t * GC_CONST GC_objfreelist_ptr = GC_objfreelist;
35
 
ptr_t * GC_CONST GC_aobjfreelist_ptr = GC_aobjfreelist;
36
 
ptr_t * GC_CONST GC_uobjfreelist_ptr = GC_uobjfreelist;
 
34
void ** const GC_objfreelist_ptr = GC_objfreelist;
 
35
void ** const GC_aobjfreelist_ptr = GC_aobjfreelist;
 
36
void ** const GC_uobjfreelist_ptr = GC_uobjfreelist;
37
37
# ifdef ATOMIC_UNCOLLECTABLE
38
 
    ptr_t * GC_CONST GC_auobjfreelist_ptr = GC_auobjfreelist;
 
38
    void ** const GC_auobjfreelist_ptr = GC_auobjfreelist;
39
39
# endif
40
40
 
41
41
 
42
 
GC_PTR GC_generic_or_special_malloc(lb,knd)
43
 
word lb;
44
 
int knd;
 
42
void * GC_generic_or_special_malloc(size_t lb, int knd)
45
43
{
46
44
    switch(knd) {
47
45
#     ifdef STUBBORN_ALLOC
68
66
/* lb bytes.  The object may be (and quite likely will be) moved.     */
69
67
/* The kind (e.g. atomic) is the same as that of the old.             */
70
68
/* Shrinking of large blocks is not implemented well.                 */
71
 
# ifdef __STDC__
72
 
    GC_PTR GC_realloc(GC_PTR p, size_t lb)
73
 
# else
74
 
    GC_PTR GC_realloc(p,lb)
75
 
    GC_PTR p;
76
 
    size_t lb;
77
 
# endif
 
69
void * GC_realloc(void * p, size_t lb)
78
70
{
79
 
register struct hblk * h;
80
 
register hdr * hhdr;
81
 
register word sz;        /* Current size in bytes       */
82
 
register word orig_sz;   /* Original sz in bytes        */
83
 
int obj_kind;
 
71
    struct hblk * h;
 
72
    hdr * hhdr;
 
73
    size_t sz;   /* Current size in bytes       */
 
74
    size_t orig_sz;      /* Original sz in bytes        */
 
75
    int obj_kind;
84
76
 
85
77
    if (p == 0) return(GC_malloc(lb));  /* Required by ANSI */
86
78
    h = HBLKPTR(p);
87
79
    hhdr = HDR(h);
88
80
    sz = hhdr -> hb_sz;
89
81
    obj_kind = hhdr -> hb_obj_kind;
90
 
    sz = WORDS_TO_BYTES(sz);
91
82
    orig_sz = sz;
92
83
 
93
84
    if (sz > MAXOBJBYTES) {
95
86
          register word descr;
96
87
          
97
88
          sz = (sz+HBLKSIZE-1) & (~HBLKMASK);
98
 
          hhdr -> hb_sz = BYTES_TO_WORDS(sz);
 
89
          hhdr -> hb_sz = sz;
99
90
          descr = GC_obj_kinds[obj_kind].ok_descriptor;
100
91
          if (GC_obj_kinds[obj_kind].ok_relocate_descr) descr += sz;
101
92
          hhdr -> hb_descr = descr;
 
93
#         ifdef MARK_BIT_PER_OBJ
 
94
            GC_ASSERT(hhdr -> hb_inv_sz == LARGE_INV_SZ);
 
95
#         else
 
96
            GC_ASSERT(hhdr -> hb_large_block &&
 
97
                      hhdr -> hb_map[ANY_INDEX] == 1);
 
98
#         endif
102
99
          if (IS_UNCOLLECTABLE(obj_kind)) GC_non_gc_bytes += (sz - orig_sz);
103
100
          /* Extra area is already cleared by GC_alloc_large_and_clear. */
104
101
    }
116
113
            return(p);
117
114
        } else {
118
115
            /* shrink */
119
 
              GC_PTR result =
 
116
              void * result =
120
117
                        GC_generic_or_special_malloc((word)lb, obj_kind);
121
118
 
122
119
              if (result == 0) return(0);
130
127
        }
131
128
    } else {
132
129
        /* grow */
133
 
          GC_PTR result =
 
130
          void * result =
134
131
                GC_generic_or_special_malloc((word)lb, obj_kind);
135
132
 
136
133
          if (result == 0) return(0);
157
154
# define GC_debug_realloc_replacement(p, lb) \
158
155
        GC_debug_realloc(p, lb, RA "unknown", 0)
159
156
 
160
 
# ifdef __STDC__
161
 
    GC_PTR realloc(GC_PTR p, size_t lb)
162
 
# else
163
 
    GC_PTR realloc(p,lb)
164
 
    GC_PTR p;
165
 
    size_t lb;
166
 
# endif
 
157
void * realloc(void * p, size_t lb)
167
158
  {
168
159
    return(REDIRECT_REALLOC(p, lb));
169
160
  }
172
163
# endif /* REDIRECT_REALLOC */
173
164
 
174
165
 
175
 
/* Allocate memory such that only pointers to near the          */
176
 
/* beginning of the object are considered.                      */
 
166
/* Allocate memory such that only pointers to near the          */
 
167
/* beginning of the object are considered.                      */
177
168
/* We avoid holding allocation lock while we clear memory.      */
178
 
ptr_t GC_generic_malloc_ignore_off_page(lb, k)
179
 
register size_t lb;
180
 
register int k;
 
169
void * GC_generic_malloc_ignore_off_page(size_t lb, int k)
181
170
{
182
 
    register ptr_t result;
183
 
    word lw;
 
171
    void *result;
 
172
    size_t lw;
 
173
    size_t lb_rounded;
184
174
    word n_blocks;
185
175
    GC_bool init;
186
176
    DCL_LOCK_STATE;
188
178
    if (SMALL_OBJ(lb))
189
179
        return(GC_generic_malloc((word)lb, k));
190
180
    lw = ROUNDED_UP_WORDS(lb);
191
 
    n_blocks = OBJ_SZ_TO_BLOCKS(lw);
 
181
    lb_rounded = WORDS_TO_BYTES(lw);
 
182
    n_blocks = OBJ_SZ_TO_BLOCKS(lb_rounded);
192
183
    init = GC_obj_kinds[k].ok_init;
193
184
    if (GC_have_errors) GC_print_all_errors();
194
185
    GC_INVOKE_FINALIZERS();
195
 
    DISABLE_SIGNALS();
196
186
    LOCK();
197
 
    result = (ptr_t)GC_alloc_large(lw, k, IGNORE_OFF_PAGE);
 
187
    result = (ptr_t)GC_alloc_large(ADD_SLOP(lb), k, IGNORE_OFF_PAGE);
198
188
    if (0 != result) {
199
189
        if (GC_debugging_started) {
200
190
            BZERO(result, n_blocks * HBLKSIZE);
209
199
#           endif
210
200
        }
211
201
    }
212
 
    GC_words_allocd += lw;
 
202
    GC_bytes_allocd += lb_rounded;
213
203
    UNLOCK();
214
 
    ENABLE_SIGNALS();
215
204
    if (0 == result) {
216
205
        return((*GC_oom_fn)(lb));
217
206
    } else {
222
211
    }
223
212
}
224
213
 
225
 
# if defined(__STDC__) || defined(__cplusplus)
226
 
  void * GC_malloc_ignore_off_page(size_t lb)
227
 
# else
228
 
  char * GC_malloc_ignore_off_page(lb)
229
 
  register size_t lb;
230
 
# endif
231
 
{
232
 
    return((GC_PTR)GC_generic_malloc_ignore_off_page(lb, NORMAL));
233
 
}
234
 
 
235
 
# if defined(__STDC__) || defined(__cplusplus)
236
 
  void * GC_malloc_atomic_ignore_off_page(size_t lb)
237
 
# else
238
 
  char * GC_malloc_atomic_ignore_off_page(lb)
239
 
  register size_t lb;
240
 
# endif
241
 
{
242
 
    return((GC_PTR)GC_generic_malloc_ignore_off_page(lb, PTRFREE));
243
 
}
244
 
 
245
 
/* Increment GC_words_allocd from code that doesn't have direct access  */
 
214
void * GC_malloc_ignore_off_page(size_t lb)
 
215
{
 
216
    return((void *)GC_generic_malloc_ignore_off_page(lb, NORMAL));
 
217
}
 
218
 
 
219
void * GC_malloc_atomic_ignore_off_page(size_t lb)
 
220
{
 
221
    return((void *)GC_generic_malloc_ignore_off_page(lb, PTRFREE));
 
222
}
 
223
 
 
224
/* Increment GC_bytes_allocd from code that doesn't have direct access  */
246
225
/* to GC_arrays.                                                        */
247
 
# ifdef __STDC__
248
 
void GC_incr_words_allocd(size_t n)
249
 
{
250
 
    GC_words_allocd += n;
251
 
}
252
 
 
253
 
/* The same for GC_mem_freed.                           */
254
 
void GC_incr_mem_freed(size_t n)
255
 
{
256
 
    GC_mem_freed += n;
257
 
}
258
 
# endif /* __STDC__ */
259
 
 
260
 
/* Analogous to the above, but assumes a small object size, and         */
261
 
/* bypasses MERGE_SIZES mechanism.  Used by gc_inline.h.                */
262
 
ptr_t GC_generic_malloc_words_small_inner(lw, k)
263
 
register word lw;
264
 
register int k;
265
 
{
266
 
register ptr_t op;
267
 
register ptr_t *opp;
268
 
register struct obj_kind * kind = GC_obj_kinds + k;
269
 
 
270
 
    opp = &(kind -> ok_freelist[lw]);
271
 
    if( (op = *opp) == 0 ) {
272
 
        if (!GC_is_initialized) {
273
 
            GC_init_inner();
274
 
        }
275
 
        if (kind -> ok_reclaim_list != 0 || GC_alloc_reclaim_list(kind)) {
276
 
            op = GC_clear_stack(GC_allocobj((word)lw, k));
277
 
        }
278
 
        if (op == 0) {
279
 
            UNLOCK();
280
 
            ENABLE_SIGNALS();
281
 
            return ((*GC_oom_fn)(WORDS_TO_BYTES(lw)));
282
 
        }
283
 
    }
284
 
    *opp = obj_link(op);
285
 
    obj_link(op) = 0;
286
 
    GC_words_allocd += lw;
287
 
    return((ptr_t)op);
288
 
}
289
 
 
290
 
/* Analogous to the above, but assumes a small object size, and         */
291
 
/* bypasses MERGE_SIZES mechanism.  Used by gc_inline.h.                */
292
 
#ifdef __STDC__
293
 
     ptr_t GC_generic_malloc_words_small(size_t lw, int k)
294
 
#else 
295
 
     ptr_t GC_generic_malloc_words_small(lw, k)
296
 
     register word lw;
297
 
     register int k;
298
 
#endif
299
 
{
300
 
register ptr_t op;
301
 
DCL_LOCK_STATE;
302
 
 
303
 
    if (GC_have_errors) GC_print_all_errors();
304
 
    GC_INVOKE_FINALIZERS();
305
 
    DISABLE_SIGNALS();
306
 
    LOCK();
307
 
    op = GC_generic_malloc_words_small_inner(lw, k);
308
 
    UNLOCK();
309
 
    ENABLE_SIGNALS();
310
 
    return((ptr_t)op);
311
 
}
312
 
 
313
 
#if defined(THREADS) && !defined(SRC_M3)
314
 
 
315
 
extern signed_word GC_mem_found;   /* Protected by GC lock.  */
 
226
void GC_incr_bytes_allocd(size_t n)
 
227
{
 
228
    GC_bytes_allocd += n;
 
229
}
 
230
 
 
231
/* The same for GC_bytes_freed.                         */
 
232
void GC_incr_bytes_freed(size_t n)
 
233
{
 
234
    GC_bytes_freed += n;
 
235
}
 
236
 
 
237
#if defined(THREADS)
 
238
 
 
239
extern signed_word GC_bytes_found;   /* Protected by GC lock.  */
316
240
 
317
241
#ifdef PARALLEL_MARK
318
 
volatile signed_word GC_words_allocd_tmp = 0;
319
 
                        /* Number of words of memory allocated since    */
 
242
volatile signed_word GC_bytes_allocd_tmp = 0;
 
243
                        /* Number of bytes of memory allocated since    */
320
244
                        /* we released the GC lock.  Instead of         */
321
245
                        /* reacquiring the GC lock just to add this in, */
322
246
                        /* we add it in the next time we reacquire      */
323
247
                        /* the lock.  (Atomically adding it doesn't     */
324
248
                        /* work, since we would have to atomically      */
325
249
                        /* update it in GC_malloc, which is too         */
326
 
                        /* expensive.                                   */
 
250
                        /* expensive.)                                   */
327
251
#endif /* PARALLEL_MARK */
328
252
 
329
 
/* See reclaim.c: */
330
 
extern ptr_t GC_reclaim_generic();
331
 
 
332
253
/* Return a list of 1 or more objects of the indicated size, linked     */
333
254
/* through the first word in the object.  This has the advantage that   */
334
255
/* it acquires the allocation lock only once, and may greatly reduce    */
338
259
/* GC_malloc_many or friends to replenish it.  (We do not round up      */
339
260
/* object sizes, since a call indicates the intention to consume many   */
340
261
/* objects of exactly this size.)                                       */
 
262
/* We assume that the size is a multiple of GRANULE_BYTES.              */
341
263
/* We return the free-list by assigning it to *result, since it is      */
342
264
/* not safe to return, e.g. a linked list of pointer-free objects,      */
343
265
/* since the collector would not retain the entire list if it were      */
344
266
/* invoked just as we were returning.                                   */
345
267
/* Note that the client should usually clear the link field.            */
346
 
void GC_generic_malloc_many(lb, k, result)
347
 
register word lb;
348
 
register int k;
349
 
ptr_t *result;
 
268
void GC_generic_malloc_many(size_t lb, int k, void **result)
350
269
{
351
 
ptr_t op;
352
 
ptr_t p;
353
 
ptr_t *opp;
354
 
word lw;
355
 
word my_words_allocd = 0;
 
270
void *op;
 
271
void *p;
 
272
void **opp;
 
273
size_t lw;      /* Length in words.     */
 
274
size_t lg;      /* Length in granules.  */
 
275
signed_word my_bytes_allocd = 0;
356
276
struct obj_kind * ok = &(GC_obj_kinds[k]);
357
277
DCL_LOCK_STATE;
358
278
 
359
 
#   if defined(GATHERSTATS) || defined(PARALLEL_MARK)
360
 
#     define COUNT_ARG , &my_words_allocd
361
 
#   else
362
 
#     define COUNT_ARG
363
 
#     define NEED_TO_COUNT
364
 
#   endif
 
279
    GC_ASSERT((lb & (GRANULE_BYTES-1)) == 0);
365
280
    if (!SMALL_OBJ(lb)) {
366
281
        op = GC_generic_malloc(lb, k);
367
282
        if(0 != op) obj_link(op) = 0;
368
283
        *result = op;
369
284
        return;
370
285
    }
371
 
    lw = ALIGNED_WORDS(lb);
 
286
    lw = BYTES_TO_WORDS(lb);
 
287
    lg = BYTES_TO_GRANULES(lb);
372
288
    if (GC_have_errors) GC_print_all_errors();
373
289
    GC_INVOKE_FINALIZERS();
374
 
    DISABLE_SIGNALS();
375
290
    LOCK();
376
291
    if (!GC_is_initialized) GC_init_inner();
377
292
    /* Do our share of marking work */
387
302
        struct hblk * hbp;
388
303
        hdr * hhdr;
389
304
 
390
 
        rlh += lw;
 
305
        rlh += lg;
391
306
        while ((hbp = *rlh) != 0) {
392
307
            hhdr = HDR(hbp);
393
308
            *rlh = hhdr -> hb_next;
 
309
            GC_ASSERT(hhdr -> hb_sz == lb);
394
310
            hhdr -> hb_last_reclaimed = (unsigned short) GC_gc_no;
395
311
#           ifdef PARALLEL_MARK
396
312
                {
397
 
                  signed_word my_words_allocd_tmp = GC_words_allocd_tmp;
 
313
                  signed_word my_bytes_allocd_tmp = GC_bytes_allocd_tmp;
398
314
 
399
 
                  GC_ASSERT(my_words_allocd_tmp >= 0);
 
315
                  GC_ASSERT(my_bytes_allocd_tmp >= 0);
400
316
                  /* We only decrement it while holding the GC lock.    */
401
317
                  /* Thus we can't accidentally adjust it down in more  */
402
318
                  /* than one thread simultaneously.                    */
403
 
                  if (my_words_allocd_tmp != 0) {
404
 
                    (void)GC_atomic_add(
405
 
                                (volatile GC_word *)(&GC_words_allocd_tmp),
406
 
                                (GC_word)(-my_words_allocd_tmp));
407
 
                    GC_words_allocd += my_words_allocd_tmp;
 
319
                  if (my_bytes_allocd_tmp != 0) {
 
320
                    (void)AO_fetch_and_add(
 
321
                                (volatile AO_t *)(&GC_bytes_allocd_tmp),
 
322
                                (AO_t)(-my_bytes_allocd_tmp));
 
323
                    GC_bytes_allocd += my_bytes_allocd_tmp;
408
324
                  }
409
325
                }
410
326
                GC_acquire_mark_lock();
411
327
                ++ GC_fl_builder_count;
412
328
                UNLOCK();
413
 
                ENABLE_SIGNALS();
414
329
                GC_release_mark_lock();
415
330
#           endif
416
 
            op = GC_reclaim_generic(hbp, hhdr, lw,
417
 
                                    ok -> ok_init, 0 COUNT_ARG);
 
331
            op = GC_reclaim_generic(hbp, hhdr, lb,
 
332
                                    ok -> ok_init, 0, &my_bytes_allocd);
418
333
            if (op != 0) {
419
 
#             ifdef NEED_TO_COUNT
420
 
                /* We are neither gathering statistics, nor marking in  */
421
 
                /* parallel.  Thus GC_reclaim_generic doesn't count     */
422
 
                /* for us.                                              */
423
 
                for (p = op; p != 0; p = obj_link(p)) {
424
 
                  my_words_allocd += lw;
425
 
                }
426
 
#             endif
427
 
#             if defined(GATHERSTATS)
428
 
                /* We also reclaimed memory, so we need to adjust       */
429
 
                /* that count.                                          */
430
 
                /* This should be atomic, so the results may be         */
431
 
                /* inaccurate.                                          */
432
 
                GC_mem_found += my_words_allocd;
433
 
#             endif
 
334
              /* We also reclaimed memory, so we need to adjust         */
 
335
              /* that count.                                            */
 
336
              /* This should be atomic, so the results may be           */
 
337
              /* inaccurate.                                            */
 
338
              GC_bytes_found += my_bytes_allocd;
434
339
#             ifdef PARALLEL_MARK
435
340
                *result = op;
436
 
                (void)GC_atomic_add(
437
 
                                (volatile GC_word *)(&GC_words_allocd_tmp),
438
 
                                (GC_word)(my_words_allocd));
 
341
                (void)AO_fetch_and_add(
 
342
                                (volatile AO_t *)(&GC_bytes_allocd_tmp),
 
343
                                (AO_t)(my_bytes_allocd));
439
344
                GC_acquire_mark_lock();
440
345
                -- GC_fl_builder_count;
441
346
                if (GC_fl_builder_count == 0) GC_notify_all_builder();
443
348
                (void) GC_clear_stack(0);
444
349
                return;
445
350
#             else
446
 
                GC_words_allocd += my_words_allocd;
 
351
                GC_bytes_allocd += my_bytes_allocd;
447
352
                goto out;
448
353
#             endif
449
354
            }
452
357
              -- GC_fl_builder_count;
453
358
              if (GC_fl_builder_count == 0) GC_notify_all_builder();
454
359
              GC_release_mark_lock();
455
 
              DISABLE_SIGNALS();
456
360
              LOCK();
457
361
              /* GC lock is needed for reclaim list access.     We      */
458
362
              /* must decrement fl_builder_count before reaquiring GC   */
463
367
    /* Next try to use prefix of global free list if there is one.      */
464
368
    /* We don't refill it, but we need to use it up before allocating   */
465
369
    /* a new block ourselves.                                           */
466
 
      opp = &(GC_obj_kinds[k].ok_freelist[lw]);
 
370
      opp = &(GC_obj_kinds[k].ok_freelist[lg]);
467
371
      if ( (op = *opp) != 0 ) {
468
372
        *opp = 0;
469
 
        my_words_allocd = 0;
 
373
        my_bytes_allocd = 0;
470
374
        for (p = op; p != 0; p = obj_link(p)) {
471
 
          my_words_allocd += lw;
472
 
          if (my_words_allocd >= BODY_SZ) {
 
375
          my_bytes_allocd += lb;
 
376
          if (my_bytes_allocd >= HBLKSIZE) {
473
377
            *opp = obj_link(p);
474
378
            obj_link(p) = 0;
475
379
            break;
476
380
          }
477
381
        }
478
 
        GC_words_allocd += my_words_allocd;
 
382
        GC_bytes_allocd += my_bytes_allocd;
479
383
        goto out;
480
384
      }
481
385
    /* Next try to allocate a new block worth of objects of this size.  */
482
386
    {
483
 
        struct hblk *h = GC_allochblk(lw, k, 0);
 
387
        struct hblk *h = GC_allochblk(lb, k, 0);
484
388
        if (h != 0) {
485
389
          if (IS_UNCOLLECTABLE(k)) GC_set_hdr_marks(HDR(h));
486
 
          GC_words_allocd += BYTES_TO_WORDS(HBLKSIZE)
487
 
                               - BYTES_TO_WORDS(HBLKSIZE) % lw;
 
390
          GC_bytes_allocd += HBLKSIZE - HBLKSIZE % lb;
488
391
#         ifdef PARALLEL_MARK
489
392
            GC_acquire_mark_lock();
490
393
            ++ GC_fl_builder_count;
491
394
            UNLOCK();
492
 
            ENABLE_SIGNALS();
493
395
            GC_release_mark_lock();
494
396
#         endif
495
397
 
516
418
  out:
517
419
    *result = op;
518
420
    UNLOCK();
519
 
    ENABLE_SIGNALS();
520
421
    (void) GC_clear_stack(0);
521
422
}
522
423
 
523
 
GC_PTR GC_malloc_many(size_t lb)
 
424
void * GC_malloc_many(size_t lb)
524
425
{
525
 
    ptr_t result;
526
 
    GC_generic_malloc_many(lb, NORMAL, &result);
 
426
    void *result;
 
427
    GC_generic_malloc_many(((lb + EXTRA_BYTES + GRANULE_BYTES-1)
 
428
                           & ~(GRANULE_BYTES-1)),
 
429
                           NORMAL, &result);
527
430
    return result;
528
431
}
529
432
 
532
435
# endif
533
436
 
534
437
/* Allocate lb bytes of pointerful, traced, but not collectable data */
535
 
# ifdef __STDC__
536
 
    GC_PTR GC_malloc_uncollectable(size_t lb)
537
 
# else
538
 
    GC_PTR GC_malloc_uncollectable(lb)
539
 
    size_t lb;
540
 
# endif
 
438
void * GC_malloc_uncollectable(size_t lb)
541
439
{
542
 
register ptr_t op;
543
 
register ptr_t *opp;
544
 
register word lw;
545
 
DCL_LOCK_STATE;
 
440
    void *op;
 
441
    void **opp;
 
442
    size_t lg;
 
443
    DCL_LOCK_STATE;
546
444
 
547
445
    if( SMALL_OBJ(lb) ) {
548
 
#       ifdef MERGE_SIZES
549
 
          if (EXTRA_BYTES != 0 && lb != 0) lb--;
 
446
        if (EXTRA_BYTES != 0 && lb != 0) lb--;
550
447
                  /* We don't need the extra byte, since this won't be  */
551
448
                  /* collected anyway.                                  */
552
 
          lw = GC_size_map[lb];
553
 
#       else
554
 
          lw = ALIGNED_WORDS(lb);
555
 
#       endif
556
 
        opp = &(GC_uobjfreelist[lw]);
557
 
        FASTLOCK();
558
 
        if( FASTLOCK_SUCCEEDED() && (op = *opp) != 0 ) {
 
449
        lg = GC_size_map[lb];
 
450
        opp = &(GC_uobjfreelist[lg]);
 
451
        LOCK();
 
452
        if( (op = *opp) != 0 ) {
559
453
            /* See above comment on signals.    */
560
454
            *opp = obj_link(op);
561
455
            obj_link(op) = 0;
562
 
            GC_words_allocd += lw;
 
456
            GC_bytes_allocd += GRANULES_TO_BYTES(lg);
563
457
            /* Mark bit ws already set on free list.  It will be        */
564
458
            /* cleared only temporarily during a collection, as a       */
565
459
            /* result of the normal free list mark bit clearing.        */
566
 
            GC_non_gc_bytes += WORDS_TO_BYTES(lw);
567
 
            FASTUNLOCK();
568
 
            return((GC_PTR) op);
569
 
        }
570
 
        FASTUNLOCK();
571
 
        op = (ptr_t)GC_generic_malloc((word)lb, UNCOLLECTABLE);
 
460
            GC_non_gc_bytes += GRANULES_TO_BYTES(lg);
 
461
            UNLOCK();
 
462
        } else {
 
463
            UNLOCK();
 
464
            op = (ptr_t)GC_generic_malloc((word)lb, UNCOLLECTABLE);
 
465
            /* For small objects, the free lists are completely marked. */
 
466
        }
 
467
        GC_ASSERT(0 == op || GC_is_marked(op));
 
468
        return((void *) op);
572
469
    } else {
 
470
        hdr * hhdr;
 
471
        
573
472
        op = (ptr_t)GC_generic_malloc((word)lb, UNCOLLECTABLE);
574
 
    }
575
 
    if (0 == op) return(0);
576
 
    /* We don't need the lock here, since we have an undisguised        */
577
 
    /* pointer.  We do need to hold the lock while we adjust            */
578
 
    /* mark bits.                                                       */
579
 
    {
580
 
        register struct hblk * h;
581
 
        
582
 
        h = HBLKPTR(op);
583
 
        lw = HDR(h) -> hb_sz;
584
 
        
585
 
        DISABLE_SIGNALS();
 
473
        if (0 == op) return(0);
 
474
        
 
475
        GC_ASSERT(((word)op & (HBLKSIZE - 1)) == 0); /* large block */
 
476
        hhdr = HDR((struct hbklk *)op);
 
477
        /* We don't need the lock here, since we have an undisguised    */
 
478
        /* pointer.  We do need to hold the lock while we adjust        */
 
479
        /* mark bits.                                                   */
 
480
        lb = hhdr -> hb_sz;
586
481
        LOCK();
587
 
        GC_set_mark_bit(op);
588
 
        GC_non_gc_bytes += WORDS_TO_BYTES(lw);
 
482
        set_mark_bit_from_hdr(hhdr, 0); /* Only object. */
 
483
        GC_ASSERT(hhdr -> hb_n_marks == 0);
 
484
        hhdr -> hb_n_marks = 1;
589
485
        UNLOCK();
590
 
        ENABLE_SIGNALS();
591
 
        return((GC_PTR) op);
 
486
        return((void *) op);
592
487
    }
593
488
}
594
489
 
595
 
#ifdef __STDC__
596
490
/* Not well tested nor integrated.      */
597
491
/* Debug version is tricky and currently missing.       */
598
492
#include <limits.h>
599
493
 
600
 
GC_PTR GC_memalign(size_t align, size_t lb) 
 
494
void * GC_memalign(size_t align, size_t lb) 
601
495
602
496
    size_t new_lb;
603
497
    size_t offset;
604
498
    ptr_t result;
605
499
 
606
 
#   ifdef ALIGN_DOUBLE
607
 
        if (align <= WORDS_TO_BYTES(2) && lb > align) return GC_malloc(lb);
608
 
#   endif
609
 
    if (align <= WORDS_TO_BYTES(1)) return GC_malloc(lb);
 
500
    if (align <= GRANULE_BYTES) return GC_malloc(lb);
610
501
    if (align >= HBLKSIZE/2 || lb >= HBLKSIZE/2) {
611
502
        if (align > HBLKSIZE) return GC_oom_fn(LONG_MAX-1024) /* Fail */;
612
503
        return GC_malloc(lb <= HBLKSIZE? HBLKSIZE : lb);
624
515
            GC_register_displacement(offset);
625
516
        }
626
517
    }
627
 
    result = (GC_PTR) ((ptr_t)result + offset);
 
518
    result = (void *) ((ptr_t)result + offset);
628
519
    GC_ASSERT((word)result % align == 0);
629
520
    return result;
630
521
}
631
 
#endif 
632
522
 
633
523
# ifdef ATOMIC_UNCOLLECTABLE
634
524
/* Allocate lb bytes of pointerfree, untraced, uncollectable data       */
635
525
/* This is normally roughly equivalent to the system malloc.            */
636
526
/* But it may be useful if malloc is redefined.                         */
637
 
# ifdef __STDC__
638
 
    GC_PTR GC_malloc_atomic_uncollectable(size_t lb)
639
 
# else
640
 
    GC_PTR GC_malloc_atomic_uncollectable(lb)
641
 
    size_t lb;
642
 
# endif
 
527
void * GC_malloc_atomic_uncollectable(size_t lb)
643
528
{
644
 
register ptr_t op;
645
 
register ptr_t *opp;
646
 
register word lw;
647
 
DCL_LOCK_STATE;
 
529
    void *op;
 
530
    void **opp;
 
531
    size_t lg;
 
532
    DCL_LOCK_STATE;
648
533
 
649
534
    if( SMALL_OBJ(lb) ) {
650
 
#       ifdef MERGE_SIZES
651
 
          if (EXTRA_BYTES != 0 && lb != 0) lb--;
 
535
        if (EXTRA_BYTES != 0 && lb != 0) lb--;
652
536
                  /* We don't need the extra byte, since this won't be  */
653
537
                  /* collected anyway.                                  */
654
 
          lw = GC_size_map[lb];
655
 
#       else
656
 
          lw = ALIGNED_WORDS(lb);
657
 
#       endif
658
 
        opp = &(GC_auobjfreelist[lw]);
659
 
        FASTLOCK();
660
 
        if( FASTLOCK_SUCCEEDED() && (op = *opp) != 0 ) {
 
538
        lg = GC_size_map[lb];
 
539
        opp = &(GC_auobjfreelist[lg]);
 
540
        LOCK();
 
541
        if( (op = *opp) != 0 ) {
661
542
            /* See above comment on signals.    */
662
543
            *opp = obj_link(op);
663
544
            obj_link(op) = 0;
664
 
            GC_words_allocd += lw;
 
545
            GC_bytes_allocd += GRANULES_TO_BYTES(lg);
665
546
            /* Mark bit was already set while object was on free list. */
666
 
            GC_non_gc_bytes += WORDS_TO_BYTES(lw);
667
 
            FASTUNLOCK();
668
 
            return((GC_PTR) op);
669
 
        }
670
 
        FASTUNLOCK();
671
 
        op = (ptr_t)GC_generic_malloc((word)lb, AUNCOLLECTABLE);
 
547
            GC_non_gc_bytes += GRANULES_TO_BYTES(lg);
 
548
            UNLOCK();
 
549
        } else {
 
550
            UNLOCK();
 
551
            op = (ptr_t)GC_generic_malloc(lb, AUNCOLLECTABLE);
 
552
        }
 
553
        GC_ASSERT(0 == op || GC_is_marked(op));
 
554
        return((void *) op);
672
555
    } else {
673
 
        op = (ptr_t)GC_generic_malloc((word)lb, AUNCOLLECTABLE);
674
 
    }
675
 
    if (0 == op) return(0);
676
 
    /* We don't need the lock here, since we have an undisguised        */
677
 
    /* pointer.  We do need to hold the lock while we adjust            */
678
 
    /* mark bits.                                                       */
679
 
    {
680
 
        register struct hblk * h;
681
 
        
682
 
        h = HBLKPTR(op);
683
 
        lw = HDR(h) -> hb_sz;
684
 
        
685
 
        DISABLE_SIGNALS();
 
556
        hdr * hhdr;
 
557
        
 
558
        op = (ptr_t)GC_generic_malloc(lb, AUNCOLLECTABLE);
 
559
        if (0 == op) return(0);
 
560
 
 
561
        GC_ASSERT(((word)op & (HBLKSIZE - 1)) == 0);
 
562
        hhdr = HDR((struct hbklk *)op);
 
563
        lb = hhdr -> hb_sz;
 
564
        
686
565
        LOCK();
687
 
        GC_set_mark_bit(op);
688
 
        GC_non_gc_bytes += WORDS_TO_BYTES(lw);
 
566
        set_mark_bit_from_hdr(hhdr, 0); /* Only object. */
 
567
        GC_ASSERT(hhdr -> hb_n_marks == 0);
 
568
        hhdr -> hb_n_marks = 1;
689
569
        UNLOCK();
690
 
        ENABLE_SIGNALS();
691
 
        return((GC_PTR) op);
 
570
        return((void *) op);
692
571
    }
693
572
}
694
573