Skip to content

Commit

Permalink
Refinement per @vtjnash comments.
Browse files Browse the repository at this point in the history
Make uv_write() a function (was a macro) and do c_free in "finally" clause.

Explicit null-termination in jl_safe_printf() to allow for win32's
non-posix vsnprintf behaviour. See also JuliaLang#9624.

Use jl_safe_printf() in profile thread.

Send help message to jl_printf(JL_STDOUT,)  was stderr.
  • Loading branch information
samoconnor committed Feb 2, 2015
1 parent fabd42c commit 15c93f6
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 16 deletions.
24 changes: 11 additions & 13 deletions base/stream.jl
Original file line number Diff line number Diff line change
Expand Up @@ -725,26 +725,27 @@ end
# finish_read(state...)
#end

macro uv_write(stream, data, len)
esc(quote
check_open(s)
uvw = c_malloc(_sizeof_uv_write)
function uv_write(s::AsyncStream, p, n::Integer)
check_open(s)
uvw = c_malloc(_sizeof_uv_write)
try
uv_req_set_data(uvw,C_NULL)
err = ccall(:jl_uv_write,
Int32,
(Ptr{Void}, Ptr{Void}, UInt, Ptr{Void}, Ptr{Void}),
handle($(stream)), $(data), $(len), uvw,
handle(s), p, n, uvw,
uv_jl_writecb_task::Ptr{Void})
if err < 0
c_free(uvw)
uv_error("write", err)
end
ct = current_task()
uv_req_set_data(uvw,ct)
ct.state = :waiting
stream_wait(ct)
finally
c_free(uvw)
end)
end
return n
end

## low-level calls ##
Expand All @@ -754,17 +755,14 @@ write(s::AsyncStream, c::Char) = write(s, string(c))
function write{T}(s::AsyncStream, a::Array{T})
if isbits(T)
n = uint(length(a)*sizeof(T))
@uv_write s a n
return int(n)
return uv_write(s, a, n);
else
check_open(s)
invoke(write,(IO,Array),s,a)
end
end
function write(s::AsyncStream, p::Ptr, n::Integer)
@uv_write s p n
return int(n)
end

write(s::AsyncStream, p::Ptr, n::Integer) = uv_write(s, p, n)

function _uv_hook_writecb_task(s::AsyncStream,req::Ptr{Void},status::Int32)
d = uv_req_data(req)
Expand Down
2 changes: 2 additions & 0 deletions src/jl_uv.c
Original file line number Diff line number Diff line change
Expand Up @@ -605,13 +605,15 @@ int jl_printf(uv_stream_t *s, const char *format, ...)
DLLEXPORT void jl_safe_printf(const char *fmt, ...)
{
static char buf[1000];
buf[0] = '\0';

va_list args;
va_start(args, fmt);
// Not async signal safe on some platforms?
vsnprintf(buf, sizeof(buf), fmt, args);
va_end(args);

buf[999] = '\0';
write(STDERR_FILENO, buf, strlen(buf));
}

Expand Down
2 changes: 1 addition & 1 deletion src/profile.c
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ void *mach_profile_listener(void *arg)
bt_size_cur += rec_backtrace_ctx_dwarf((ptrint_t*)bt_data_prof+bt_size_cur, bt_size_max-bt_size_cur-1, &uc);
}
else if (forceDwarf == -1) {
jl_printf(JL_STDERR, "Warning: Profiler attempt to access an invalid memory location\n");
jl_safe_printf("Warning: Profiler attempt to access an invalid memory location\n");
}

forceDwarf = -2;
Expand Down
2 changes: 1 addition & 1 deletion src/task.c
Original file line number Diff line number Diff line change
Expand Up @@ -702,7 +702,7 @@ DLLEXPORT void gdblookup(ptrint_t ip)
if (line_num == ip)
jl_safe_printf("unknown function (ip: %d)\n", line_num);
else if (line_num == -1)
jl_safe_printf("%s at %s (unknown line)\n", func_name, file_name, line_num);
jl_safe_printf("%s at %s (unknown line)\n", func_name, file_name);
else
jl_safe_printf("%s at %s:%d\n", func_name, file_name, line_num);
}
Expand Down
2 changes: 1 addition & 1 deletion ui/repl.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ void parse_opts(int *argcp, char ***argvp)
jl_compileropts.cpu_target = strdup(optarg);
break;
case 'h':
jl_printf(JL_STDERR, "%s%s", usage, opts);
jl_printf(JL_STDOUT, "%s%s", usage, opts);
jl_exit(0);
case 'O':
jl_compileropts.opt_level = 1;
Expand Down

0 comments on commit 15c93f6

Please sign in to comment.