Changeset d439067 for lib/trace.c


Ignore:
Timestamp:
12/10/18 15:59:45 (2 years ago)
Author:
Shane Alcock <salcock@…>
Branches:
develop
Children:
10fd24b
Parents:
418c78d
Message:

Move packet cached fields into a distinct structure.

This will help tidy up the packet structure a little, as well as
simplify the cache clearing process (and maybe even speed it up
a little).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • lib/trace.c

    r509ee47 rd439067  
    103103/* Set once pstart is called used for backwards compatibility reasons */
    104104int libtrace_parallel = 0;
     105
     106static const libtrace_packet_cache_t clearcache = {
     107        -1, -1, -1, -1, NULL, 0, 0, NULL, 0, 0, NULL, 0, 0};
    105108
    106109/* strncpy is not assured to copy the final \0, so we
     
    14021405                return ~0U;
    14031406        }
    1404         if (packet->capture_length == -1) {
     1407        if (packet->cached.capture_length == -1) {
    14051408                if (!packet->trace->format->get_capture_length)
    14061409                        return ~0U;
    14071410                /* Cast away constness because this is "just" a cache */
    1408                 ((libtrace_packet_t*)packet)->capture_length =
     1411                ((libtrace_packet_t*)packet)->cached.capture_length =
    14091412                        packet->trace->format->get_capture_length(packet);
    14101413        }
    14111414
    1412         if (!(packet->capture_length < LIBTRACE_PACKET_BUFSIZE)) {
     1415        if (!(packet->cached.capture_length < LIBTRACE_PACKET_BUFSIZE)) {
    14131416                fprintf(stderr, "Capture length is greater than the buffer size in trace_get_capture_length()\n");
    14141417                return 0;
     
    14161419        }
    14171420
    1418         return packet->capture_length;
     1421        return packet->cached.capture_length;
    14191422}
    14201423
     
    14321435        }
    14331436
    1434         if (packet->wire_length == -1) {
     1437        if (packet->cached.wire_length == -1) {
    14351438                if (!packet->trace->format->get_wire_length)
    14361439                        return ~0U;
    1437                 ((libtrace_packet_t *)packet)->wire_length =
     1440                ((libtrace_packet_t *)packet)->cached.wire_length =
    14381441                        packet->trace->format->get_wire_length(packet);
    14391442        }
    14401443
    1441         if (!(packet->wire_length < LIBTRACE_PACKET_BUFSIZE)) {
     1444        if (!(packet->cached.wire_length < LIBTRACE_PACKET_BUFSIZE)) {
    14421445                fprintf(stderr, "Wire length is greater than the buffer size in trace_get_wire_length()\n");
    14431446                return 0;
    14441447                /* should we be returning ~OU here? */
    14451448        }
    1446         return packet->wire_length;
     1449        return packet->cached.wire_length;
    14471450
    14481451}
     
    14601463        }
    14611464
    1462         if (packet->framing_length >= 0) {
    1463                 return packet->framing_length;
     1465        if (packet->cached.framing_length >= 0) {
     1466                return packet->cached.framing_length;
    14641467        }
    14651468
    14661469        if (packet->trace->format->get_framing_length) {
    1467                 ((libtrace_packet_t *)packet)->framing_length =
     1470                ((libtrace_packet_t *)packet)->cached.framing_length =
    14681471                       packet->trace->format->get_framing_length(packet);
    1469                 return packet->framing_length;
     1472                return packet->cached.framing_length;
    14701473        }
    14711474        return ~0U;
     
    14831486        }
    14841487
    1485         if (packet->link_type == 0) {
     1488        if (packet->cached.link_type == 0) {
    14861489                if (!packet->trace->format->get_link_type)
    14871490                        return TRACE_TYPE_UNKNOWN;
    1488                 ((libtrace_packet_t *)packet)->link_type =
     1491                ((libtrace_packet_t *)packet)->cached.link_type =
    14891492                        packet->trace->format->get_link_type(packet);
    14901493        }
    14911494
    1492         return packet->link_type;
     1495        return packet->cached.link_type;
    14931496}
    14941497
     
    19761979
    19771980        if (packet->trace->format->set_capture_length) {
    1978                 packet->capture_length = packet->trace->format->set_capture_length(packet,size);
    1979                 return packet->capture_length;
     1981                packet->cached.capture_length = packet->trace->format->set_capture_length(packet,size);
     1982                return packet->cached.capture_length;
    19801983        }
    19811984
     
    25702573
    25712574
    2572 void trace_clear_cache(libtrace_packet_t *packet) {
    2573 
    2574         packet->l2_header = NULL;
    2575         packet->l3_header = NULL;
    2576         packet->l4_header = NULL;
    2577         packet->link_type = 0;
    2578         packet->l3_ethertype = 0;
    2579         packet->transport_proto = 0;
    2580         packet->capture_length = -1;
    2581         packet->wire_length = -1;
    2582         packet->framing_length = -1;
    2583         packet->payload_length = -1;
    2584         packet->l2_remaining = 0;
    2585         packet->l3_remaining = 0;
    2586         packet->l4_remaining = 0;
    2587         packet->refcount = 0;
     2575inline void trace_clear_cache(libtrace_packet_t *packet) {
     2576
     2577        packet->cached = clearcache;
    25882578}
    25892579
Note: See TracChangeset for help on using the changeset viewer.