Mall is a single module with several singleton methods, most of which are glibc-specific. All constants may be used as the first argument to Mall.opt.
amount of extra space to allocate when allocating from the heap
maximum number of arenas to allocate (glibc 2.10+)
max request size for “fastbins”
the request size threshold for using mmap() (instead of sbrk())
unused in glibc
the maximum number of active mmap() requests in use at once
unused in glibc
bitmask value used for debug message output (glibc)
unused in glibc
perturb memory allocations with a given byte (for debugging) (glibc)
maximum amount of unused memory at the top of the heapto keep before releasing it back to the kernel
initial number of arenas to allocate (glibc 2.10+)
Dump malloc stats to STDERR
This calls malloc_stats() internally, a function that is glibc-specific
static VALUE dump_stats(VALUE klass) { fflush(stderr); malloc_stats(); fflush(stderr); return Qnil; }
Returns a hash with the following keys:
:arena - bytes allocated via sbrk() (and not mmap()) :ordblks - number of free (unused) chunks :smblks - number of fastbin blocks[1] :hblks - number of allocated mmap()-ed regions :hblkhd - bytes allocated in mmap()-ed regions :usmblks - maximum total allocated space[1] :fsmblks - space available in freed fastbin blocks[1] :uordblks - total allocated space in use :fordblks - total free space :keepcost - top-most, releasable (via malloc_trim) space
All values are limited to 32-bit integers. This uses the limited mallinfo(3) function, consider using Mall.xml and parsing its output if you are using glibc (2.10+) with malloc_info(3)
See also: http:// gnu.org/software/libc/manual/html_node/Statistics-of-Malloc.html
this key is unused by glibc (ptmalloc2)
static VALUE info(VALUE klass) { VALUE rv = rb_hash_new(); struct mallinfo stats = mallinfo(); /* whee aggregate returns :( */ #define MALLINFO_SET(KEY) \ rb_hash_aset(rv, sym_##KEY, INT2FIX(stats.KEY)) MALLINFO_SET(arena); MALLINFO_SET(ordblks); MALLINFO_SET(smblks); MALLINFO_SET(hblks); MALLINFO_SET(hblkhd); MALLINFO_SET(usmblks); MALLINFO_SET(fsmblks); MALLINFO_SET(uordblks); MALLINFO_SET(fordblks); MALLINFO_SET(keepcost); #undef MALLINFO_SET return rv; }
some malloc implementations may not like mallopt() being called after malloc has been initialized (first call to malloc()). This is not the case with glibc malloc.
See also: gnu.org/software/libc/manual/html_node/Malloc-Tunable-Parameters.html
static VALUE opt(VALUE klass, VALUE param, VALUE value) { int rv = mallopt(FIX2INT(param), FIX2INT(value)); return rv == 0 ? Qfalse : Qtrue; }
Attempt to trim off the top of the heap and release it back to the OS. pad represents the amount of free space (in bytes) to leave unreleased for future allocations.
Returns true if memory was released and false if not.
This method is glibc-specific.
static VALUE trim(VALUE klass, VALUE pad) { unsigned long tmp = NUM2ULONG(pad); int rv = malloc_trim((size_t)tmp); return rv == 1 ? Qtrue : Qfalse; return Qfalse; }
Called with no arguments, this returns an XML string suitable for parsing with your favorite XML parser.
If specified, options must currently be 0, but is reserved for future expansion.
The second optional argument may be any object that responds to “<<” so it may be an IO, Array, StringIO, or String object among other things.
This relies on malloc_info(3) which is only in glibc 2.10+
static VALUE xml(int argc, VALUE *argv, VALUE self) { int err; long len; VALUE options, out, buf; int xoptions; FILE *fp; rb_scan_args(argc, argv, "02", &options, &out); xoptions = NIL_P(options) ? 0 : NUM2INT(options); fp = tmpfile(); if (fp == NULL) rb_sys_fail("tmpfile"); err = malloc_info(xoptions, fp); if (err != 0) xmlerr(fp, err, "malloc_info"); len = ftell(fp); if (len < 0) xmlerr(fp, errno, "ftell"); rewind(fp); buf = rb_str_new(0, len); if (fread(RSTRING_PTR(buf), 1, len, fp) != len) xmlerr(fp, ferror(fp), "fread"); fclose(fp); if (NIL_P(out)) return buf; return rb_funcall(out, id_ltlt, 1, buf); }
Originally generated with the Darkfish Rdoc Generator 2, modified by wrongdoc.