Apply yet another set of the optimization patches:

- Use a fast path for the most common use of tvb_get_xxx functions:
offset is >= 0 and tvb->real_data is set (this one is always true).
- match_strval() is a linear search, put the most common protocols
TCP/UDP/RDP first.
- fix gtk1 g_strlcat declaration Use g_strlcat

svn path=/trunk/; revision=23285
This commit is contained in:
Anders Broman 2007-10-27 14:44:29 +00:00
parent 79e035e3f3
commit 1143ab41b1
5 changed files with 63 additions and 38 deletions

View File

@ -297,7 +297,7 @@ col_do_append_sep_va_fstr(column_info *cinfo, gint el, const gchar *separator,
*/
if (separator != NULL) {
if (len != 0) {
strncat(cinfo->col_buf[i], separator, max_len - len);
g_strlcat(cinfo->col_buf[i], separator, max_len);
len += sep_len;
}
}
@ -372,8 +372,7 @@ col_prepend_fstr(column_info *cinfo, gint el, const gchar *format, ...)
if (cinfo->col_fence[i] > 0)
cinfo->col_fence[i] += strlen(cinfo->col_buf[i]);
strncat(cinfo->col_buf[i], orig, max_len);
cinfo->col_buf[i][max_len - 1] = '\0';
g_strlcat(cinfo->col_buf[i], orig, max_len);
cinfo->col_data[i] = cinfo->col_buf[i];
}
}
@ -416,8 +415,7 @@ col_prepend_fence_fstr(column_info *cinfo, gint el, const gchar *format, ...)
} else {
cinfo->col_fence[i] = strlen(cinfo->col_buf[i]);
}
strncat(cinfo->col_buf[i], orig, max_len);
cinfo->col_buf[i][max_len - 1] = '\0';
g_strlcat(cinfo->col_buf[i], orig, max_len);
cinfo->col_data[i] = cinfo->col_buf[i];
}
}
@ -484,19 +482,17 @@ col_do_append_str(column_info *cinfo, gint el, const gchar* separator,
*/
COL_CHECK_APPEND(cinfo, i, max_len);
len = strlen(cinfo->col_buf[i]);
len = cinfo->col_buf[i][0];
/*
* If we have a separator, append it if the column isn't empty.
*/
if (separator != NULL) {
if (len != 0) {
strncat(cinfo->col_buf[i], separator, max_len - len);
len += sep_len;
g_strlcat(cinfo->col_buf[i], separator, max_len);
}
}
strncat(cinfo->col_buf[i], str, max_len - len);
cinfo->col_buf[i][max_len - 1] = 0;
g_strlcat(cinfo->col_buf[i], str, max_len);
}
}
}

View File

@ -36,11 +36,16 @@
#include <epan/packet.h>
#include <epan/addr_resolv.h>
#include <epan/dissectors/packet-ip.h>
#include <epan/strutil.h>
static const value_string ipproto_val[] = {
#if 0
{ IP_PROTO_IP, "IPv4" },
#endif
{ IP_PROTO_TCP, "TCP" },
{ IP_PROTO_UDP, "UDP" },
{ IP_PROTO_RDP, "Reliable Data" },
{ IP_PROTO_HOPOPTS, "IPv6 hop-by-hop option" },
{ IP_PROTO_ICMP, "ICMP" },
{ IP_PROTO_IGMP, "IGMP" },
@ -50,7 +55,6 @@ static const value_string ipproto_val[] = {
{ IP_PROTO_IPV4, "IPv4" },
#endif
{ IP_PROTO_STREAM, "Stream" },
{ IP_PROTO_TCP, "TCP" },
{ IP_PROTO_CBT, "CBT" },
{ IP_PROTO_EGP, "EGP" },
{ IP_PROTO_IGP, "IGRP" },
@ -61,7 +65,6 @@ static const value_string ipproto_val[] = {
{ IP_PROTO_EMCON, "EMCON" },
{ IP_PROTO_XNET, "XNET" },
{ IP_PROTO_CHAOS, "CHAOS" },
{ IP_PROTO_UDP, "UDP" },
{ IP_PROTO_MUX, "Multiplex" },
{ IP_PROTO_DCNMEAS, "DCN Measurement" },
{ IP_PROTO_HMP, "Host Monitoring" },
@ -71,7 +74,6 @@ static const value_string ipproto_val[] = {
{ IP_PROTO_TRUNK2, "Trunk-2" },
{ IP_PROTO_LEAF1, "Leaf-1" },
{ IP_PROTO_LEAF2, "Leaf-2" },
{ IP_PROTO_RDP, "Reliable Data" },
{ IP_PROTO_IRT, "IRT" },
{ IP_PROTO_TP, "ISO TP4" },
{ IP_PROTO_BULK, "Bulk Data" },
@ -186,14 +188,12 @@ static const value_string ipproto_val[] = {
};
const char *ipprotostr(int proto) {
static char buf[128];
const char *s;
#ifdef HAVE_GETPROTOBYNUMBER
struct protoent *pe;
#endif
if ((s = match_strval(proto, ipproto_val)) != NULL)
goto ok;
return s;
s = "Unknown";
#ifdef HAVE_GETPROTOBYNUMBER
/*
@ -201,17 +201,15 @@ const char *ipprotostr(int proto) {
* protocol names?
*/
if (g_resolv_flags != 0) {
static char buf[128];
struct protoent *pe;
pe = getprotobynumber(proto);
if (pe) {
s = pe->p_name;
goto ok;
g_strlcpy(buf, pe->p_name, sizeof(buf));
s = buf;
}
}
#endif
s = "Unknown";
ok:
g_snprintf(buf, sizeof(buf), "%s", s);
return buf;
return s;
}

View File

@ -955,7 +955,7 @@ convert_string_case(const char *string, gboolean case_insensitive)
/* g_strlcat(), g_strlcpy don't exist in GLib 1.2[.x] */
#if GLIB_MAJOR_VERSION < 2
gsize
g_strlcat(gchar *dest, gchar *src, gsize dest_size)
g_strlcat(gchar *dest, const gchar *src, gsize dest_size)
{
gchar *d = dest;
const gchar *s = src;

View File

@ -217,7 +217,7 @@ char * epan_strcasestr(const char *haystack, const char *needle);
/* g_strlcat() does not exist in GLib 1.2[.x] */
#if GLIB_MAJOR_VERSION < 2
gsize g_strlcat(gchar *dst, gchar *src, gsize size);
gsize g_strlcat(gchar *dst, const gchar *src, gsize size);
gsize g_strlcpy(gchar *dest, const gchar *src, gsize dest_size);
#endif

View File

@ -850,6 +850,36 @@ ensure_contiguous_no_exception(tvbuff_t *tvb, gint offset, gint length,
return NULL;
}
/* ----------------------------- */
static const guint8*
fast_ensure_contiguous(tvbuff_t *tvb, gint offset, guint length)
{
guint end_offset;
guint u_offset;
DISSECTOR_ASSERT(tvb && tvb->initialized);
if (offset < 0 || !tvb->real_data) {
return ensure_contiguous(tvb, offset, length);
}
u_offset = offset;
end_offset = u_offset + length;
/* don't need to check for overflow because length <= 8 */
if (end_offset <= tvb->length) {
return tvb->real_data + u_offset;
}
if (end_offset > tvb->reported_length) {
THROW(ReportedBoundsError);
}
THROW(BoundsError);
/* not reached */
return 0;
}
static const guint8*
ensure_contiguous(tvbuff_t *tvb, gint offset, gint length)
{
@ -1050,12 +1080,13 @@ tvb_get_ptr(tvbuff_t *tvb, gint offset, gint length)
return ensure_contiguous(tvb, offset, length);
}
/* ---------------- */
guint8
tvb_get_guint8(tvbuff_t *tvb, gint offset)
{
const guint8* ptr;
ptr = ensure_contiguous(tvb, offset, sizeof(guint8));
ptr = fast_ensure_contiguous(tvb, offset, sizeof(guint8));
return *ptr;
}
@ -1064,7 +1095,7 @@ tvb_get_ntohs(tvbuff_t *tvb, gint offset)
{
const guint8* ptr;
ptr = ensure_contiguous(tvb, offset, sizeof(guint16));
ptr = fast_ensure_contiguous(tvb, offset, sizeof(guint16));
return pntohs(ptr);
}
@ -1073,7 +1104,7 @@ tvb_get_ntoh24(tvbuff_t *tvb, gint offset)
{
const guint8* ptr;
ptr = ensure_contiguous(tvb, offset, 3);
ptr = fast_ensure_contiguous(tvb, offset, 3);
return pntoh24(ptr);
}
@ -1082,7 +1113,7 @@ tvb_get_ntohl(tvbuff_t *tvb, gint offset)
{
const guint8* ptr;
ptr = ensure_contiguous(tvb, offset, sizeof(guint32));
ptr = fast_ensure_contiguous(tvb, offset, sizeof(guint32));
return pntohl(ptr);
}
@ -1091,7 +1122,7 @@ tvb_get_ntoh64(tvbuff_t *tvb, gint offset)
{
const guint8* ptr;
ptr = ensure_contiguous(tvb, offset, sizeof(guint64));
ptr = fast_ensure_contiguous(tvb, offset, sizeof(guint64));
return pntoh64(ptr);
}
@ -1303,7 +1334,7 @@ tvb_get_letohs(tvbuff_t *tvb, gint offset)
{
const guint8* ptr;
ptr = ensure_contiguous(tvb, offset, sizeof(guint16));
ptr = fast_ensure_contiguous(tvb, offset, sizeof(guint16));
return pletohs(ptr);
}
@ -1312,7 +1343,7 @@ tvb_get_letoh24(tvbuff_t *tvb, gint offset)
{
const guint8* ptr;
ptr = ensure_contiguous(tvb, offset, 3);
ptr = fast_ensure_contiguous(tvb, offset, 3);
return pletoh24(ptr);
}
@ -1321,7 +1352,7 @@ tvb_get_letohl(tvbuff_t *tvb, gint offset)
{
const guint8* ptr;
ptr = ensure_contiguous(tvb, offset, sizeof(guint32));
ptr = fast_ensure_contiguous(tvb, offset, sizeof(guint32));
return pletohl(ptr);
}
@ -1330,7 +1361,7 @@ tvb_get_letoh64(tvbuff_t *tvb, gint offset)
{
const guint8* ptr;
ptr = ensure_contiguous(tvb, offset, sizeof(guint64));
ptr = fast_ensure_contiguous(tvb, offset, sizeof(guint64));
return pletoh64(ptr);
}
@ -1400,7 +1431,7 @@ tvb_get_ipv4(tvbuff_t *tvb, gint offset)
const guint8* ptr;
guint32 addr;
ptr = ensure_contiguous(tvb, offset, sizeof(guint32));
ptr = fast_ensure_contiguous(tvb, offset, sizeof(guint32));
memcpy(&addr, ptr, sizeof addr);
return addr;
}