From e4b0308e2986e4e91ae2d05b42f4319b4e1fb28e Mon Sep 17 00:00:00 2001 From: Nicholas Clark Date: Wed, 10 Feb 2010 16:44:12 +0000 Subject: [PATCH] Convert output_tag_int, output_nv and output_str to return the written count. --- NYTProf.h | 6 +++--- NYTProf.xs | 25 ++++++++++++++++++------- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/NYTProf.h b/NYTProf.h index 9cec01f..46b4df4 100644 --- a/NYTProf.h +++ b/NYTProf.h @@ -20,9 +20,9 @@ library file, with a public API, the XS interface adapted to use that API, and these 3 return to being static functions, within that library. */ -void output_tag_int(NYTP_file file, unsigned char tag, unsigned int); -void output_str(NYTP_file file, char *str, I32 len); -void output_nv(NYTP_file file, NV nv); +size_t output_tag_int(NYTP_file file, unsigned char tag, unsigned int); +size_t output_str(NYTP_file file, char *str, I32 len); +size_t output_nv(NYTP_file file, NV nv); #define NYTP_TAG_NO_TAG '\0' /* Used as a flag to mean "no tag" */ #define output_int(fh, i) output_tag_int((fh), NYTP_TAG_NO_TAG, (unsigned int)(i)) diff --git a/NYTProf.xs b/NYTProf.xs index 139d909..40cba44 100644 --- a/NYTProf.xs +++ b/NYTProf.xs @@ -489,17 +489,28 @@ output_header(pTHX) } -void +size_t output_str(NYTP_file file, char *str, I32 len) { /* negative len signifies utf8 */ unsigned char tag = NYTP_TAG_STRING; + size_t retval; + size_t total; + if (len < 0) { tag = NYTP_TAG_STRING_UTF8; len = -len; } if (trace_level >= 10) logwarn("output_str('%.*s', %d)\n", (int)len, str, (int)len); - output_tag_int(file, tag, len); - NYTP_write(file, str, len); + + total = retval = output_tag_int(file, tag, len); + if (retval <= 0) + return retval; + + total += retval = NYTP_write(file, str, len); + if (retval <= 0) + return retval; + + return total; } @@ -968,7 +979,7 @@ get_file_id(pTHX_ char* file_name, STRLEN file_name_len, int created_via) * "In bytes" means output the number in binary, using the least number of bytes * possible. All numbers are positive. Use sign slot as a marker */ -void +size_t output_tag_int(NYTP_file file, unsigned char tag, unsigned int i) { U8 buffer[6]; @@ -1003,7 +1014,7 @@ output_tag_int(NYTP_file file, unsigned char tag, unsigned int i) *p++ = (U8)(i >> 8); *p++ = (U8)i; } - NYTP_write(file, buffer, p - buffer); + return NYTP_write(file, buffer, p - buffer); } @@ -1022,10 +1033,10 @@ output_uv_from_av(pTHX_ AV *av, int idx, UV default_uv) * Output a double precision float via a simple binary write of the memory. * (Minor portbility issues are seen as less important than speed and space.) */ -void +size_t output_nv(NYTP_file file, NV nv) { - NYTP_write(file, (unsigned char *)&nv, sizeof(NV)); + return NYTP_write(file, (unsigned char *)&nv, sizeof(NV)); } -- 1.6.0