wmem: allow wmem_destroy_list to ignore a NULL list.
[wireshark-sm.git] / epan / tvbuff_real.c
blobba8bd82df52b7b858acbaa3c28bd3cec3d0cf756
1 /* tvbuff_real.c
3 * Copyright (c) 2000 by Gilbert Ramirez <gram@alumni.rice.edu>
5 * Wireshark - Network traffic analyzer
6 * By Gerald Combs <gerald@wireshark.org>
7 * Copyright 1998 Gerald Combs
9 * SPDX-License-Identifier: GPL-2.0-or-later
12 #include "config.h"
14 #include "tvbuff.h"
15 #include "tvbuff-int.h"
16 #include "proto.h" /* XXX - only used for DISSECTOR_ASSERT, probably a new header file? */
17 #include "exceptions.h"
19 struct tvb_real {
20 struct tvbuff tvb;
22 /** Func to call when actually freed */
23 tvbuff_free_cb_t free_cb;
26 static void
27 real_free(tvbuff_t *tvb)
29 struct tvb_real *real_tvb = (struct tvb_real *) tvb;
31 if (real_tvb->free_cb) {
33 * XXX - do this with a union?
35 real_tvb->free_cb((void *)tvb->real_data);
39 static unsigned
40 real_offset(const tvbuff_t *tvb _U_, const unsigned counter)
42 return counter;
45 static const struct tvb_ops tvb_real_ops = {
46 sizeof(struct tvb_real), /* size */
48 real_free, /* free */
49 real_offset, /* offset */
50 NULL, /* get_ptr */
51 NULL, /* memcpy */
52 NULL, /* find_uint8 */
53 NULL, /* pbrk_uint8 */
54 NULL, /* clone */
57 tvbuff_t *
58 tvb_new_real_data(const uint8_t* data, const unsigned length, const int reported_length)
60 tvbuff_t *tvb;
61 struct tvb_real *real_tvb;
63 THROW_ON(reported_length < -1, ReportedBoundsError);
65 tvb = tvb_new(&tvb_real_ops);
67 tvb->real_data = data;
68 tvb->length = length;
69 tvb->reported_length = reported_length;
70 tvb->contained_length = reported_length;
71 tvb->initialized = true;
74 * This is the top-level real tvbuff for this data source,
75 * so its data source tvbuff is itself.
77 tvb->ds_tvb = tvb;
79 real_tvb = (struct tvb_real *) tvb;
80 real_tvb->free_cb = NULL;
82 return tvb;
85 void
86 tvb_set_free_cb(tvbuff_t *tvb, const tvbuff_free_cb_t func)
88 struct tvb_real *real_tvb = (struct tvb_real *) tvb;
90 DISSECTOR_ASSERT(tvb);
91 DISSECTOR_ASSERT(tvb->ops == &tvb_real_ops);
92 real_tvb->free_cb = func;
95 void
96 tvb_set_child_real_data_tvbuff(tvbuff_t *parent, tvbuff_t *child)
98 DISSECTOR_ASSERT(parent && child);
99 DISSECTOR_ASSERT(parent->initialized);
100 DISSECTOR_ASSERT(child->initialized);
101 DISSECTOR_ASSERT(child->ops == &tvb_real_ops);
102 tvb_add_to_chain(parent, child);
105 tvbuff_t *
106 tvb_new_child_real_data(tvbuff_t *parent, const uint8_t* data, const unsigned length, const int reported_length)
108 tvbuff_t *tvb = tvb_new_real_data(data, length, reported_length);
110 tvb_set_child_real_data_tvbuff(parent, tvb);
112 return tvb;
116 * Editor modelines - https://www.wireshark.org/tools/modelines.html
118 * Local variables:
119 * c-basic-offset: 8
120 * tab-width: 8
121 * indent-tabs-mode: t
122 * End:
124 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
125 * :indentSize=8:tabSize=8:noTabs=false: