From 9a0e8d0d14e04e229132a63444c75dbc192d159a Mon Sep 17 00:00:00 2001 From: Nicholas Clark Date: Thu, 11 Feb 2010 15:33:29 +0000 Subject: [PATCH] Move output_tag_int(), output_nv() and output_str() to FileHandle.xs from NYTProf.xs, and make them static. Remove their prototypes from NYTProf.h. --- FileHandle.h | 1 + FileHandle.xs | 81 ++++++++++++++++++++++++++++++++++++++++++++++++++++ NYTProf.h | 10 ------ NYTProf.xs | 88 --------------------------------------------------------- 4 files changed, 82 insertions(+), 98 deletions(-) diff --git a/FileHandle.h b/FileHandle.h index be7b600..f2809dd 100644 --- a/FileHandle.h +++ b/FileHandle.h @@ -43,6 +43,7 @@ const char *NYTP_type_of_offset(NYTP_file file); # define NYTP_type_of_offset(file) "" #endif +#define NYTP_TAG_NO_TAG '\0' /* Used as a flag to mean "no tag" */ #define NYTP_TAG_ATTRIBUTE ':' /* :name=value\n */ #define NYTP_TAG_COMMENT '#' /* till newline */ #define NYTP_TAG_TIME_BLOCK '*' diff --git a/FileHandle.xs b/FileHandle.xs index a383972..284da48 100644 --- a/FileHandle.xs +++ b/FileHandle.xs @@ -622,6 +622,87 @@ NYTP_close(NYTP_file file, int discard) { return fclose(raw_file) == 0 ? 0 : errno; } +/** + * Output an integer in bytes, optionally preceded by a tag. Use the special tag + * NYTP_TAG_NO_TAG to suppress the tag output. A wrapper macro output_int(fh, i) + * does this for you. + * "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 + */ +static size_t +output_tag_int(NYTP_file file, unsigned char tag, unsigned int i) +{ + U8 buffer[6]; + U8 *p = buffer; + + if (tag != NYTP_TAG_NO_TAG) + *p++ = tag; + + /* general case. handles all integers */ + if (i < 0x80) { /* < 8 bits */ + *p++ = (U8)i; + } + else if (i < 0x4000) { /* < 15 bits */ + *p++ = (U8)((i >> 8) | 0x80); + *p++ = (U8)i; + } + else if (i < 0x200000) { /* < 22 bits */ + *p++ = (U8)((i >> 16) | 0xC0); + *p++ = (U8)(i >> 8); + *p++ = (U8)i; + } + else if (i < 0x10000000) { /* 32 bits */ + *p++ = (U8)((i >> 24) | 0xE0); + *p++ = (U8)(i >> 16); + *p++ = (U8)(i >> 8); + *p++ = (U8)i; + } + else { /* need all the bytes. */ + *p++ = 0xFF; + *p++ = (U8)(i >> 24); + *p++ = (U8)(i >> 16); + *p++ = (U8)(i >> 8); + *p++ = (U8)i; + } + return NYTP_write(file, buffer, p - buffer); +} + +#define output_int(fh, i) output_tag_int((fh), NYTP_TAG_NO_TAG, (unsigned int)(i)) + +static size_t +output_str(NYTP_file file, const 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; + } + + total = retval = output_tag_int(file, tag, len); + if (retval <= 0) + return retval; + + if (len) { + total += retval = NYTP_write(file, str, len); + if (retval <= 0) + return retval; + } + + return total; +} + +/** + * 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.) + */ +size_t +output_nv(NYTP_file file, NV nv) +{ + return NYTP_write(file, (unsigned char *)&nv, sizeof(NV)); +} + size_t NYTP_write_comment(NYTP_file ofile, const char *format, ...) { size_t retval; diff --git a/NYTProf.h b/NYTProf.h index d2d6d9b..ede5361 100644 --- a/NYTProf.h +++ b/NYTProf.h @@ -16,13 +16,3 @@ * ************************************************************************ */ -/* FIXME - The callers of these functions should be refactored into their own - 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. */ - -size_t output_tag_int(NYTP_file file, unsigned char tag, unsigned int); -size_t output_str(NYTP_file file, const 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 57ce444..6a02bdf 100644 --- a/NYTProf.xs +++ b/NYTProf.xs @@ -471,34 +471,6 @@ output_header(pTHX) NYTP_flush(out); } - -size_t -output_str(NYTP_file file, const 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); - - total = retval = output_tag_int(file, tag, len); - if (retval <= 0) - return retval; - - if (len) { - total += retval = NYTP_write(file, str, len); - if (retval <= 0) - return retval; - } - - return total; -} - - static SV * read_str(pTHX_ SV *sv) { STRLEN len; @@ -956,53 +928,6 @@ get_file_id(pTHX_ char* file_name, STRLEN file_name_len, int created_via) return found->id; } - -/** - * Output an integer in bytes, optionally preceded by a tag. Use the special tag - * NYTP_TAG_NO_TAG to suppress the tag output. A wrapper macro output_int(fh, i) - * does this for you. - * "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 - */ -size_t -output_tag_int(NYTP_file file, unsigned char tag, unsigned int i) -{ - U8 buffer[6]; - U8 *p = buffer; - - if (tag != NYTP_TAG_NO_TAG) - *p++ = tag; - - /* general case. handles all integers */ - if (i < 0x80) { /* < 8 bits */ - *p++ = (U8)i; - } - else if (i < 0x4000) { /* < 15 bits */ - *p++ = (U8)((i >> 8) | 0x80); - *p++ = (U8)i; - } - else if (i < 0x200000) { /* < 22 bits */ - *p++ = (U8)((i >> 16) | 0xC0); - *p++ = (U8)(i >> 8); - *p++ = (U8)i; - } - else if (i < 0x10000000) { /* 32 bits */ - *p++ = (U8)((i >> 24) | 0xE0); - *p++ = (U8)(i >> 16); - *p++ = (U8)(i >> 8); - *p++ = (U8)i; - } - else { /* need all the bytes. */ - *p++ = 0xFF; - *p++ = (U8)(i >> 24); - *p++ = (U8)(i >> 16); - *p++ = (U8)(i >> 8); - *p++ = (U8)i; - } - return NYTP_write(file, buffer, p - buffer); -} - - static UV uv_from_av(pTHX_ AV *av, int idx, UV default_uv) { @@ -1010,19 +935,6 @@ uv_from_av(pTHX_ AV *av, int idx, UV default_uv) UV uv = (!svp || !SvOK(*svp)) ? default_uv : SvUV(*svp); return 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.) - */ -size_t -output_nv(NYTP_file file, NV nv) -{ - return NYTP_write(file, (unsigned char *)&nv, sizeof(NV)); -} - static NV nv_from_av(pTHX_ AV *av, int idx, NV default_nv) -- 1.6.0