xwillfree.diff

Yura Sokolov, 10/04/2013 08:38 PM

Download (11.1 KB)

View differences:

array.c
210 210
            ARY_SET_HEAP_LEN(ary, len);
211 211
        }
212 212
        else {
213
            xwillfree(RARRAY(ary)->as.heap.aux.capa * sizeof(VALUE));
213 214
            REALLOC_N(RARRAY(ary)->as.heap.ptr, VALUE, (capacity));
214 215
        }
215 216
        ARY_SET_CAPA(ary, (capacity));
......
218 219
        if (!ARY_EMBED_P(ary)) {
219 220
            long len = RARRAY_LEN(ary);
220 221
	    const VALUE *ptr = RARRAY_CONST_PTR(ary);
222
            xwillfree(RARRAY(ary)->as.heap.aux.capa * sizeof(VALUE));
221 223
            if (len > capacity) len = capacity;
222 224
            MEMCPY((VALUE *)RARRAY(ary)->as.ary, ptr, VALUE, len);
223 225
            FL_SET_EMBED(ary);
......
234 236
    long old_capa = RARRAY(ary)->as.heap.aux.capa;
235 237
    assert(!ARY_SHARED_P(ary));
236 238
    assert(old_capa >= capacity);
237
    if (old_capa > capacity)
239
    if (old_capa > capacity) {
240
	xwillfree(old_capa * sizeof(VALUE));
238 241
	REALLOC_N(RARRAY(ary)->as.heap.ptr, VALUE, capacity);
242
    }
239 243
}
240 244

  
241 245
static void
......
533 537
rb_ary_free(VALUE ary)
534 538
{
535 539
    if (ARY_OWNS_HEAP_P(ary)) {
540
	xwillfree(RARRAY(ary)->as.heap.aux.capa * sizeof(VALUE));
536 541
	xfree((void *)ARY_HEAP_PTR(ary));
537 542
    }
538 543
}
......
712 717
    rb_ary_modify(ary);
713 718
    if (argc == 0) {
714 719
	if (ARY_OWNS_HEAP_P(ary) && RARRAY_CONST_PTR(ary) != 0) {
720
	    xwillfree(RARRAY(ary)->as.heap.aux.capa * sizeof(VALUE));
715 721
	    xfree((void *)RARRAY_CONST_PTR(ary));
716 722
	}
717 723
        rb_ary_unshare_safe(ary);
......
1633 1639
    }
1634 1640
    else {
1635 1641
	if (olen > len + ARY_DEFAULT_SIZE) {
1642
	    xwillfree(RARRAY(ary)->as.heap.aux.capa * sizeof(VALUE));
1636 1643
	    REALLOC_N(RARRAY(ary)->as.heap.ptr, VALUE, len);
1637 1644
	    ARY_SET_CAPA(ary, len);
1638 1645
	}
......
2420 2427
                    rb_ary_unshare(ary);
2421 2428
                }
2422 2429
                else {
2430
		    xwillfree(RARRAY(ary)->as.heap.aux.capa * sizeof(VALUE));
2423 2431
		    xfree((void *)ARY_HEAP_PTR(ary));
2424 2432
                }
2425 2433
                ARY_SET_PTR(ary, RARRAY_CONST_PTR(tmp));
......
3284 3292
        VALUE shared = 0;
3285 3293

  
3286 3294
        if (ARY_OWNS_HEAP_P(copy)) {
3295
	    xwillfree(RARRAY(copy)->as.heap.aux.capa * sizeof(VALUE));
3287 3296
	    RARRAY_PTR_USE(copy, ptr, xfree(ptr));
3288 3297
	}
3289 3298
        else if (ARY_SHARED_P(copy)) {
......
3300 3309
    else {
3301 3310
        VALUE shared = ary_make_shared(orig);
3302 3311
        if (ARY_OWNS_HEAP_P(copy)) {
3312
	    xwillfree(RARRAY(copy)->as.heap.aux.capa * sizeof(VALUE));
3303 3313
	    RARRAY_PTR_USE(copy, ptr, xfree(ptr));
3304 3314
        }
3305 3315
        else {
gc.c
90 90
#define GC_MALLOC_LIMIT (8 /* 8 MB */ * 1024 * 1024 /* 1MB */)
91 91
#endif
92 92
#ifndef GC_MALLOC_LIMIT_MAX
93
#define GC_MALLOC_LIMIT_MAX (512 /* 512 MB */ * 1024 * 1024 /* 1MB */)
93
#define GC_MALLOC_LIMIT_MAX (256 /* 256 MB */ * 1024 * 1024 /* 1MB */)
94 94
#endif
95 95
#ifndef GC_MALLOC_LIMIT_GROWTH_FACTOR
96 96
#define GC_MALLOC_LIMIT_GROWTH_FACTOR 1.8
......
317 317

  
318 318
typedef struct rb_objspace {
319 319
    struct {
320
	size_t limit;
321
	size_t increase;
320
	ssize_t limit;
321
	ssize_t increase;
322 322
#if CALC_EXACT_MALLOC_SIZE
323 323
	size_t allocated_size;
324 324
	size_t allocations;
......
1257 1257
      case T_OBJECT:
1258 1258
	if (!(RANY(obj)->as.basic.flags & ROBJECT_EMBED) &&
1259 1259
            RANY(obj)->as.object.as.heap.ivptr) {
1260
	    xwillfree(RANY(obj)->as.object.as.heap.numiv * sizeof(VALUE));
1260 1261
	    xfree(RANY(obj)->as.object.as.heap.ivptr);
1261 1262
	}
1262 1263
	break;
......
1354 1355

  
1355 1356
      case T_BIGNUM:
1356 1357
	if (!(RBASIC(obj)->flags & RBIGNUM_EMBED_FLAG) && RBIGNUM_DIGITS(obj)) {
1358
	    xwillfree(RANY(obj)->as.bignum.as.heap.len * sizeof(BDIGIT));
1357 1359
	    xfree(RBIGNUM_DIGITS(obj));
1358 1360
	}
1359 1361
	break;
......
1378 1380
      case T_STRUCT:
1379 1381
	if ((RBASIC(obj)->flags & RSTRUCT_EMBED_LEN_MASK) == 0 &&
1380 1382
	    RANY(obj)->as.rstruct.as.heap.ptr) {
1383
	    xwillfree(RANY(obj)->as.rstruct.as.heap.len * sizeof(VALUE));
1381 1384
	    xfree((void *)RANY(obj)->as.rstruct.as.heap.ptr);
1382 1385
	}
1383 1386
	break;
......
2415 2418

  
2416 2419
    /* reset malloc info */
2417 2420
    {
2418
	size_t inc = ATOMIC_SIZE_EXCHANGE(malloc_increase, 0);
2419
	size_t old_limit = malloc_limit;
2421
	ssize_t inc = ATOMIC_SIZE_EXCHANGE(malloc_increase, 0);
2422
	ssize_t old_limit = malloc_limit;
2420 2423

  
2421 2424
	if (inc > malloc_limit) {
2422 2425
	    malloc_limit += (size_t)(malloc_limit * (initial_malloc_limit_growth_factor - 1));
......
4826 4829
}
4827 4830

  
4828 4831
static void
4829
vm_malloc_increase(rb_objspace_t *objspace, size_t size, int do_gc)
4832
vm_malloc_increase(rb_objspace_t *objspace, size_t size)
4830 4833
{
4831 4834
    ATOMIC_SIZE_ADD(malloc_increase, size);
4832 4835

  
4833 4836
    if ((ruby_gc_stress && !ruby_disable_gc_stress) ||
4834
	(do_gc && (malloc_increase > malloc_limit))) {
4837
	(malloc_increase > malloc_limit)) {
4835 4838
	garbage_collect_with_gvl(objspace, 0, 0, GPR_FLAG_MALLOC);
4836 4839
    }
4837 4840
}
4838 4841

  
4842
static void
4843
vm_malloc_decrease(rb_objspace_t *objspace, size_t size)
4844
{
4845
	ATOMIC_SIZE_SUB(malloc_increase, size);
4846
}
4847

  
4839 4848
static inline size_t
4840 4849
vm_malloc_prepare(rb_objspace_t *objspace, size_t size)
4841 4850
{
......
4848 4857
    size += sizeof(size_t);
4849 4858
#endif
4850 4859

  
4851
    vm_malloc_increase(objspace, size, TRUE);
4860
    vm_malloc_increase(objspace, size);
4852 4861

  
4853 4862
    return size;
4854 4863
}
......
4908 4917
	return 0;
4909 4918
    }
4910 4919

  
4911
    vm_malloc_increase(objspace, size, FALSE);
4920
    vm_malloc_increase(objspace, size);
4912 4921

  
4913 4922
#if CALC_EXACT_MALLOC_SIZE
4914 4923
    size += sizeof(size_t);
......
5007 5016
	vm_xfree(&rb_objspace, x);
5008 5017
}
5009 5018

  
5019
void
5020
ruby_xwill_free(ssize_t size)
5021
{
5022
    vm_malloc_decrease(&rb_objspace, size);
5023
}
5010 5024

  
5011 5025
/* Mimic ruby_xmalloc, but need not rb_objspace.
5012 5026
 * should return pointer suitable for ruby_xfree
include/ruby/defines.h
111 111
#define xrealloc ruby_xrealloc
112 112
#define xrealloc2 ruby_xrealloc2
113 113
#define xfree ruby_xfree
114
#define xwillfree ruby_xwill_free
114 115

  
115 116
#if defined(__GNUC__) && __GNUC__ >= 4 && __GNUC_MINOR__ >= 3
116 117
# define RUBY_ATTR_ALLOC_SIZE(params) __attribute__ ((__alloc_size__ params))
......
124 125
void *xrealloc(void*,size_t) RUBY_ATTR_ALLOC_SIZE((2));
125 126
void *xrealloc2(void*,size_t,size_t) RUBY_ATTR_ALLOC_SIZE((2,3));
126 127
void xfree(void*);
128
void xwillfree(ssize_t);
127 129

  
128 130
#define STRINGIZE(expr) STRINGIZE0(expr)
129 131
#ifndef STRINGIZE0
st.c
78 78
#define calloc xcalloc
79 79
#define realloc xrealloc
80 80
#define free(x) xfree(x)
81
#define willfree(x) xwillfree(x)
82
#else
83
#define willfree(x)
81 84
#endif
82 85

  
83 86
#define numberof(array) (int)(sizeof(array) / sizeof((array)[0]))
......
89 92

  
90 93
/* preparation for possible allocation improvements */
91 94
#define st_alloc_entry() (st_table_entry *)malloc(sizeof(st_table_entry))
92
#define st_free_entry(entry) free(entry)
95
#define st_free_entry(entry) do { xwillfree(sizeof(*entry)); free(entry); } while(0)
93 96
#define st_alloc_table() (st_table *)malloc(sizeof(st_table))
94
#define st_dealloc_table(table) free(table)
97
#define st_dealloc_table(table) do { xwillfree(sizeof(*table)); free(table); } while(0)
95 98
#define st_alloc_bins(size) (st_table_entry **)calloc(size, sizeof(st_table_entry *))
96
#define st_free_bins(bins, size) free(bins)
99
#define st_free_bins(bins, size) do { xwillfree(size*sizeof(*bins)); free(bins); } while(0)
97 100
static inline st_table_entry**
98 101
st_realloc_bins(st_table_entry **bins, st_index_t newsize, st_index_t oldsize)
99 102
{
string.c
115 115
	}\
116 116
    }\
117 117
    else {\
118
	xwillfree(RSTRING(str)->as.heap.aux.capa);\
118 119
	REALLOC_N(RSTRING(str)->as.heap.ptr, char, (capacity)+termlen);\
119 120
	if (!STR_NOCAPA_P(str))\
120 121
	    RSTRING(str)->as.heap.aux.capa = (capacity);\
......
877 878
	st_delete(frozen_strings, &fstr, NULL);
878 879
    }
879 880
    if (!STR_EMBED_P(str) && !STR_SHARED_P(str)) {
881
	xwillfree(RSTRING(str)->as.heap.aux.capa);
880 882
	xfree(RSTRING(str)->as.heap.ptr);
881 883
    }
882 884
}
......
1434 1436
	long capa = len + expand;
1435 1437
	int termlen = TERM_LEN(str);
1436 1438
	if (!STR_EMBED_P(str)) {
1439
	    xwillfree(RSTRING(str)->as.heap.aux.capa);
1437 1440
	    REALLOC_N(RSTRING(str)->as.heap.ptr, char, capa + termlen);
1438 1441
	    RSTRING(str)->as.heap.aux.capa = capa;
1439 1442
	}
......
1460 1463
{
1461 1464
    str_modifiable(str);
1462 1465
    if (!STR_SHARED_P(str) && !STR_EMBED_P(str)) {
1466
	xwillfree(RSTRING(str)->as.heap.aux.capa);
1463 1467
	xfree(RSTRING_PTR(str));
1464 1468
	RSTRING(str)->as.heap.ptr = 0;
1465 1469
	RSTRING(str)->as.heap.len = 0;
......
1978 1982
	}
1979 1983
	else if (len + termlen <= RSTRING_EMBED_LEN_MAX + 1) {
1980 1984
	    char *ptr = RSTRING(str)->as.heap.ptr;
1985
            ssize_t oldlen = RSTRING(str)->as.heap.aux.capa;
1981 1986
	    STR_SET_EMBED(str);
1982 1987
	    if (slen > len) slen = len;
1983 1988
	    if (slen > 0) MEMCPY(RSTRING(str)->as.ary, ptr, char, slen);
1984 1989
	    TERM_FILL(RSTRING(str)->as.ary + len, termlen);
1985 1990
	    STR_SET_EMBED_LEN(str, len);
1986
	    if (independent) xfree(ptr);
1991
	    if (independent) {
1992
                xwillfree(oldlen);
1993
                xfree(ptr);
1994
            }
1987 1995
	    return str;
1988 1996
	}
1989 1997
	else if (!independent) {
1990 1998
	    str_make_independent_expand(str, len - slen);
1991 1999
	}
1992 2000
	else if (slen < len || slen - len > 1024) {
2001
	    xwillfree(RSTRING(str)->as.heap.aux.capa);
1993 2002
	    REALLOC_N(RSTRING(str)->as.heap.ptr, char, len + termlen);
1994 2003
	}
1995 2004
	if (!STR_NOCAPA_P(str)) {
......
3571 3580
    if (nlen <= RSTRING_EMBED_LEN_MAX) {
3572 3581
	char *oldptr = ptr;
3573 3582
	int fl = (int)(RBASIC(str)->flags & (STR_NOEMBED|ELTS_SHARED));
3583
        ssize_t oldlen = RSTRING(str)->as.heap.aux.capa;
3574 3584
	STR_SET_EMBED(str);
3575 3585
	STR_SET_EMBED_LEN(str, nlen);
3576 3586
	ptr = RSTRING(str)->as.ary;
3577 3587
	memmove(ptr, oldptr + len, nlen);
3578
	if (fl == STR_NOEMBED) xfree(oldptr);
3588
	if (fl == STR_NOEMBED) {
3589
            xwillfree(oldlen);
3590
            xfree(oldptr);
3591
        }
3579 3592
    }
3580 3593
    else {
3581 3594
	if (!STR_SHARED_P(str)) rb_str_new4(str);
......
5494 5507
	    t += tlen;
5495 5508
	}
5496 5509
	if (!STR_EMBED_P(str)) {
5510
	    xwillfree(RSTRING(str)->as.heap.aux.capa);
5497 5511
	    xfree(RSTRING(str)->as.heap.ptr);
5498 5512
	}
5499 5513
	*t = '\0';
......
5570 5584
	    t += tlen;
5571 5585
	}
5572 5586
	if (!STR_EMBED_P(str)) {
5587
	    xwillfree(RSTRING(str)->as.heap.aux.capa);
5573 5588
	    xfree(RSTRING(str)->as.heap.ptr);
5574 5589
	}
5575 5590
	*t = '\0';
time.c
1784 1784
static void
1785 1785
time_free(void *tobj)
1786 1786
{
1787
    if (tobj) xfree(tobj);
1787
    if (tobj) {
1788
        xwillfree(sizeof(struct time_object));
1789
        xfree(tobj);
1790
    }
1788 1791
}
1789 1792

  
1790 1793
static size_t
variable.c
1171 1171
                    ROBJECT(obj)->as.heap.ivptr = newptr;
1172 1172
                }
1173 1173
                else {
1174
                    xwillfree(ROBJECT(obj)->as.heap.numiv * sizeof(VALUE));
1174 1175
                    REALLOC_N(ROBJECT(obj)->as.heap.ivptr, VALUE, newsize);
1175 1176
                    newptr = ROBJECT(obj)->as.heap.ivptr;
1176 1177
                }