summaryrefslogtreecommitdiff
path: root/common/src/nx_packet_data_append.c
blob: 6fbb63af94ee606444e6b05d630ad7758a2863b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/***************************************************************************
 * Copyright (c) 2024 Microsoft Corporation
 * Copyright (c) 2025-present Eclipse ThreadX Contributors
 *
 * This program and the accompanying materials are made available under the
 * terms of the MIT License which is available at
 * https://opensource.org/licenses/MIT.
 *
 * SPDX-License-Identifier: MIT
 **************************************************************************/


/**************************************************************************/
/**************************************************************************/
/**                                                                       */
/** NetX Component                                                        */
/**                                                                       */
/**   Packet Pool Management (Packet)                                     */
/**                                                                       */
/**************************************************************************/
/**************************************************************************/

#define NX_SOURCE_CODE


/* Include necessary system files.  */

#include "nx_api.h"
#include "nx_packet.h"


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _nx_packet_data_append                              PORTABLE C      */
/*                                                           6.4.3        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yuxin Zhou, Microsoft Corporation                                   */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function copies the specified data to the end of the specified */
/*    packet.  Additional packets are allocated from the specified pool   */
/*    if needed.                                                          */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    packet_ptr                            Pointer to packet to append to*/
/*    data_start                            Pointer to start of the data  */
/*    data_size                             Number of bytes to append     */
/*    pool_ptr                              Pool to allocate packet from  */
/*    wait_option                           Suspension option             */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    status                                Completion status             */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _nx_packet_allocate                   Allocate data packet          */
/*    _nx_packet_release                    Release data packet           */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application Code                                                    */
/*                                                                        */
/**************************************************************************/
UINT  _nx_packet_data_append(NX_PACKET *packet_ptr, VOID *data_start, ULONG data_size,
                             NX_PACKET_POOL *pool_ptr, ULONG wait_option)
{

#ifndef NX_DISABLE_PACKET_CHAIN
UINT       status;                 /* Return status              */
NX_PACKET *new_list_ptr;           /* Head of new list pointer   */
NX_PACKET *last_packet =  NX_NULL; /* Last supplied packet       */
#endif /* NX_DISABLE_PACKET_CHAIN */
ULONG      available_bytes;        /* Number of available bytes  */
ULONG      copy_size;              /* Size for each memory copy  */
UCHAR     *source_ptr;             /* Buffer source pointer      */
NX_PACKET *work_ptr;               /* Working packet pointer     */


    /* If trace is enabled, insert this event into the trace buffer.  */
    NX_TRACE_IN_LINE_INSERT(NX_TRACE_PACKET_DATA_APPEND, packet_ptr, data_start, data_size, pool_ptr, NX_TRACE_PACKET_EVENTS, 0, 0);

#ifndef NX_DISABLE_PACKET_CHAIN
    /* Calculate the number of bytes available at the end of the supplied packet.  */
    if (packet_ptr -> nx_packet_last)
    {

        /* More than one packet.  Walk the packet chain starting at the last packet
           to calculate the remaining bytes.  */
        available_bytes =  0;
        work_ptr =  packet_ptr -> nx_packet_last;
        do
        {

            /* Calculate the available bytes in this packet.  */
            /*lint -e{946} -e{947} suppress pointer subtraction, since it is necessary. */
            /*lint -e{737} suppress loss of sign, since nx_packet_data_end is assumed to be larger than nx_packet_append_ptr. */
            available_bytes =  available_bytes +
                (ULONG)(work_ptr -> nx_packet_data_end - work_ptr -> nx_packet_append_ptr);

            /* Remember the last packet.  */
            last_packet =  work_ptr;

            /* Move to the next packet.   There typically won't be another packet, but just in
               case the logic is here for it!  */
            work_ptr =  work_ptr -> nx_packet_next;
        } while (work_ptr);
    }
    else
#endif /* NX_DISABLE_PACKET_CHAIN */
    {

        /* Just calculate the number of bytes available in the first packet.  */
        /*lint -e{946} -e{947} suppress pointer subtraction, since it is necessary. */
        available_bytes =  (ULONG)(packet_ptr -> nx_packet_data_end - packet_ptr -> nx_packet_append_ptr);
    }

    /* Determine if any new packets are required to satisfy this request. */
    if (available_bytes < data_size)
    {

#ifndef NX_DISABLE_PACKET_CHAIN
        /* Setup a temporary head pointer.  */
        new_list_ptr =  NX_NULL;

        /* Loop to pickup enough packets to complete the append request.  */
        while (available_bytes < data_size)
        {

            /* Allocate a new packet.  */
            status =  _nx_packet_allocate(pool_ptr, &work_ptr, 0, wait_option);

            /* Determine if an error is present.  */
            if (status)
            {

                /* Yes, an error is present.   */

                /* First release any packets that have been allocated so far.  */
                if (new_list_ptr)
                {
                    _nx_packet_release(new_list_ptr);
                }

                /* Return the error status to the caller of this service.  */
                return(status);
            }

            /* Add debug information. */
            NX_PACKET_DEBUG(__FILE__, __LINE__, work_ptr);

            /* No error is present.  Link the new packet to the temporary list being built.  */
            if (new_list_ptr)
            {

                /* Determine if there is already more than one packet on the list.  */
                if (new_list_ptr -> nx_packet_last)
                {

                    /* Yes, link up the last packet to the new packet and update the
                       last pointer.  */
                    /*lint -e{644} suppress variable might not be initialized, since "work_ptr" was initialized in _nx_packet_allocate. */
                    (new_list_ptr -> nx_packet_last) -> nx_packet_next =  work_ptr;
                    new_list_ptr -> nx_packet_last =  work_ptr;
                }
                else
                {

                    /* Second packet allocated.  Just setup the last and next in the
                       head pointer.  */
                    new_list_ptr -> nx_packet_last =  work_ptr;
                    new_list_ptr -> nx_packet_next =  work_ptr;
                }
            }
            else
            {

                /* Just setup the temporary list head.  */
                new_list_ptr =  work_ptr;
            }

            /* Adjust the number of available bytes according to how much space
               is in the new packet.  */
            /*lint -e{946} -e{947} suppress pointer subtraction, since it is necessary. */
            /*lint -e{737} suppress loss of sign, since nx_packet_data_end is assumed to be larger than nx_packet_append_ptr. */
            /*lint -e{613} suppress possible use of null pointer, since "work_ptr" was set in _nx_packet_allocate. */
            available_bytes =  available_bytes +
                (ULONG)(work_ptr -> nx_packet_data_end - work_ptr -> nx_packet_append_ptr);
        }

        /* At this point, all the necessary packets have been allocated and are present
           on the temporary list.  We need to link this new list to the end of the supplied
           packet.  */
        if (last_packet)
        {

            /* Already more than one packet.  Add the new packet list to the end.  */
            last_packet -> nx_packet_next =  new_list_ptr;
        }
        else
        {

            /* Link the new packet list to the head packet.  */
            packet_ptr -> nx_packet_next =  new_list_ptr;
        }

        /* Clear the last packet that was used to maintain the new list.  */
        /*lint -e{613} suppress possible use of null pointer, since "new_list_ptr" was set in previous loop. */
        new_list_ptr -> nx_packet_last =  NX_NULL;
#else
        NX_PARAMETER_NOT_USED(pool_ptr);
        NX_PARAMETER_NOT_USED(wait_option);

        return(NX_SIZE_ERROR);
#endif /* NX_DISABLE_PACKET_CHAIN */
    }

    /* Setup the new data length in the packet.  */
    packet_ptr -> nx_packet_length =   packet_ptr -> nx_packet_length + data_size;

    /* Now copy the supplied data buffer at the end of the packet.  */
    source_ptr =  (UCHAR *)data_start;
#ifndef NX_DISABLE_PACKET_CHAIN
    if (packet_ptr -> nx_packet_last)
    {
        work_ptr =    packet_ptr -> nx_packet_last;
    }
    else
    {
#endif /* NX_DISABLE_PACKET_CHAIN */
        work_ptr =    packet_ptr;
#ifndef NX_DISABLE_PACKET_CHAIN
    }
    while (data_size)
    {

        /* Determine the amount of memory to copy.  */
        /*lint -e{946} -e{947} suppress pointer subtraction, since it is necessary. */
        if (data_size < (ULONG)(work_ptr -> nx_packet_data_end - work_ptr -> nx_packet_append_ptr))
        {
            copy_size =  data_size;
        }
        else
        {

            /*lint -e{946} -e{947} suppress pointer subtraction, since it is necessary. */
            copy_size =  (ULONG)(work_ptr -> nx_packet_data_end - work_ptr -> nx_packet_append_ptr);
        }
#else
        copy_size = data_size;
#endif /* NX_DISABLE_PACKET_CHAIN */

        /* Copy the data into the current packet buffer.  */
        memcpy(work_ptr -> nx_packet_append_ptr, source_ptr, copy_size); /* Use case of memcpy is verified.  lgtm[cpp/banned-api-usage-required-any] */

        /* Adjust the remaining data size.  */
        data_size =  data_size - copy_size;

        /* Update this packets append pointer.  */
        work_ptr -> nx_packet_append_ptr =  work_ptr -> nx_packet_append_ptr + copy_size;

#ifndef NX_DISABLE_PACKET_CHAIN
        /* Any more data left to append?  */
        if (data_size)
        {

            /* Yes, there is more to move.  Update the source pointer, move the work pointer
               to the next packet in the chain and update the last packet pointer.  */
            source_ptr =  source_ptr + copy_size;
            work_ptr =  work_ptr -> nx_packet_next;
            packet_ptr -> nx_packet_last =  work_ptr;
        }
    }
#endif /* NX_DISABLE_PACKET_CHAIN */

    /* Add debug information. */
    NX_PACKET_DEBUG(__FILE__, __LINE__, packet_ptr);

    /* Return successful status.  */
    return(NX_SUCCESS);
}