Skip to content

Commit

Permalink
Define gc counted allocator functions with libc API and use them in
Browse files Browse the repository at this point in the history
CHOLMOD wrappers
  • Loading branch information
andreasnoack committed Jul 6, 2015
1 parent a9fcb72 commit dc13041
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
11 changes: 11 additions & 0 deletions base/sparse/cholmod.jl
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ const version = VersionNumber(version_array...)
function __init__()
### Check if the linked library is compatible with the Julia code
if Libdl.dlsym(Libdl.dlopen("libcholmod"), :cholmod_version) == C_NULL
hasversion = false
warn("""
CHOLMOD version incompatibility
Expand All @@ -81,6 +82,7 @@ function __init__()
versions of all dependencies.
""")
else
hasversion = true
tmp = Array(Cint, 3)
ccall((:cholmod_version, :libcholmod), Cint, (Ptr{Cint},), version_array)
ccall((:jl_cholmod_version, :libsuitesparse_wrapper), Cint, (Ptr{Cint},), tmp)
Expand Down Expand Up @@ -140,6 +142,15 @@ function __init__()
start(commonStruct) # initializes CHOLMOD
set_print_level(commonStruct, 0) # no printing from CHOLMOD by default

# Register gc tracked allocator if CHOLMOD is new enough
if hasversion && version >= v"3.0.0"
cnfg = cglobal((:SuiteSparse_config, :libcholmod), Ptr{Void})
unsafe_store!(cnfg, cglobal(:jl_malloc, Ptr{Void}), 1)
unsafe_store!(cnfg, cglobal(:jl_calloc, Ptr{Void}), 2)
unsafe_store!(cnfg, cglobal(:jl_realloc, Ptr{Void}), 3)
unsafe_store!(cnfg, cglobal(:jl_free, Ptr{Void}), 4)
end

end

function set_print_level(cm::Array{UInt8}, lev::Integer)
Expand Down
50 changes: 50 additions & 0 deletions src/gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -2690,6 +2690,17 @@ DLLEXPORT void *jl_gc_counted_malloc(size_t sz)
return b;
}

DLLEXPORT void *jl_gc_counted_calloc(size_t nm, size_t sz)
{
maybe_collect();
allocd_bytes += nm*sz;
gc_num.malloc++;
void *b = calloc(nm, sz);
if (b == NULL)
jl_throw(jl_memory_exception);
return b;
}

DLLEXPORT void jl_gc_counted_free(void *p, size_t sz)
{
free(p);
Expand All @@ -2712,6 +2723,45 @@ DLLEXPORT void *jl_gc_counted_realloc_with_old_size(void *p, size_t old, size_t
return b;
}

DLLEXPORT void *jl_malloc(size_t sz)
{
size_t *p = (size_t *)jl_gc_counted_malloc(sz + sizeof(size_t));
p[0] = sz;
return (void *)(p + 1);
}

DLLEXPORT void *jl_calloc(size_t nm, size_t sz)
{
size_t *p;
if (sizeof(size_t) <= sz)
++nm;
else
{
nm += sizeof(size_t) / sz;
if ((sizeof(size_t) % sz) != 0)
++nm;
}
p = (size_t *)jl_gc_counted_calloc(nm, sz);
p[0] = nm*sz;
return (void *)(p + 1);
}

DLLEXPORT void jl_free(void *p)
{
size_t *pp = (size_t *)p - 1;
size_t sz = pp[0];
jl_gc_counted_free(pp, sz + sizeof(size_t));
}

DLLEXPORT void *jl_realloc(void *p, size_t sz)
{
size_t *pp = (size_t *)p - 1;
size_t szold = pp[0];
size_t *pnew = (size_t *)jl_gc_counted_realloc_with_old_size(pp, szold + sizeof(size_t), sz + sizeof(size_t));
pnew[0] = sz;
return (void *)(pnew + 1);
}

DLLEXPORT void *jl_gc_managed_malloc(size_t sz)
{
maybe_collect();
Expand Down

0 comments on commit dc13041

Please sign in to comment.