Line data Source code
1 : /**
2 : * Copyright (c) 2025 Huawei Technologies Co., Ltd.
3 : * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
4 : * CANN Open Software License Agreement Version 2.0 (the "License").
5 : * Please refer to the License for details. You may not use this file except in compliance with the License.
6 : * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
7 : * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
8 : * See LICENSE in the root of the software repository for the full text of the License.
9 : */
10 : #include "trace_queue.h"
11 : #include "adiag_print.h"
12 : #include "adiag_utils.h"
13 : #include "trace_types.h"
14 :
15 30 : void XFreeTraceNode(TraceNode **node)
16 : {
17 30 : ADIAG_CHK_NULL_PTR(node, return);
18 30 : ADIAG_CHK_NULL_PTR(*node, return);
19 :
20 30 : TraceNode *tmp = (TraceNode *)*node;
21 30 : ADIAG_SAFE_FREE(tmp->data);
22 30 : ADIAG_SAFE_FREE(tmp);
23 30 : *node = NULL;
24 : }
25 :
26 33 : TraStatus TraceQueueInit(TraceQueue *queue)
27 : {
28 33 : ADIAG_CHK_NULL_PTR(queue, return TRACE_INVALID_PTR);
29 :
30 33 : queue->count = 0;
31 33 : queue->size = 0;
32 33 : queue->head = NULL;
33 33 : queue->rear = NULL;
34 33 : return TRACE_SUCCESS;
35 : }
36 :
37 : // not free trace_queue
38 22 : TraStatus TraceQueueFree(TraceQueue *queue)
39 : {
40 22 : ADIAG_CHK_NULL_PTR(queue, return TRACE_INVALID_PTR);
41 :
42 22 : TraceNode *tmp = NULL;
43 22 : int32_t num = (int32_t)queue->count;
44 25 : for (int32_t i = 0; i < num; i++) {
45 3 : TraStatus ret = TraceQueueDequeue(queue, &tmp);
46 3 : if (ret == TRACE_QUEUE_NULL) {
47 0 : break;
48 : }
49 3 : XFreeTraceNode(&tmp);
50 : }
51 :
52 22 : return TRACE_SUCCESS;
53 : }
54 :
55 32 : STATIC TraStatus TraceQueueFull(const TraceQueue *queue)
56 : {
57 32 : if ((queue->count >= MAX_QUEUE_COUNT) ||
58 30 : (queue->size >= MAX_QUEUE_SIZE)) {
59 2 : return TRACE_QUEUE_FULL;
60 : }
61 :
62 30 : return TRACE_SUCCESS;
63 : }
64 :
65 81 : STATIC TraStatus TraceQueueNULL(const TraceQueue *queue)
66 : {
67 81 : if ((queue->count == 0) || (queue->size == 0) ||
68 50 : ((queue->head == NULL) && (queue->rear == NULL))) {
69 31 : return TRACE_QUEUE_NULL;
70 : }
71 :
72 50 : return TRACE_SUCCESS;
73 : }
74 :
75 33 : TraStatus TraceQueueEnqueue(TraceQueue *queue, TraceNode *node)
76 : {
77 33 : ADIAG_CHK_NULL_PTR(queue, return TRACE_INVALID_PTR);
78 32 : ADIAG_CHK_NULL_PTR(node, return TRACE_INVALID_PTR);
79 32 : ADIAG_CHK_EXPR_ACTION(node->dataLen == 0, return TRACE_INVALID_PARAM, "trace node is empty.");
80 :
81 : // queue would be more than max_size, but node max_count
82 32 : if (TraceQueueFull(queue) != TRACE_SUCCESS) {
83 2 : ADIAG_RUN_INF("queue is full.");
84 2 : return TRACE_QUEUE_FULL;
85 : }
86 :
87 30 : if (TraceQueueNULL(queue) != TRACE_SUCCESS) {
88 10 : queue->head = node;
89 10 : queue->rear = node;
90 10 : queue->count = 1;
91 10 : queue->size = node->dataLen;
92 : } else {
93 20 : queue->rear->next = node; // firstly, join the node to list
94 20 : queue->rear = node; // secondly, mv rear to listRear
95 20 : queue->count++;
96 20 : queue->size += node->dataLen;
97 : }
98 :
99 30 : return TRACE_SUCCESS;
100 : }
101 :
102 53 : TraStatus TraceQueueDequeue(TraceQueue *queue, TraceNode **node)
103 : {
104 53 : ADIAG_CHK_NULL_PTR(queue, return TRACE_INVALID_PTR);
105 51 : ADIAG_CHK_NULL_PTR(node, return TRACE_INVALID_PTR);
106 :
107 51 : if (TraceQueueNULL(queue) != TRACE_SUCCESS) {
108 21 : return TRACE_QUEUE_NULL;
109 : }
110 :
111 30 : *node = queue->head; // get head
112 30 : if (queue->count == 1) {
113 10 : (void)TraceQueueInit(queue);
114 : } else {
115 20 : TraceNode *tmp = queue->head;
116 20 : queue->count--;
117 20 : queue->size -= tmp->dataLen;
118 20 : queue->head = tmp->next;
119 20 : tmp->next = NULL;
120 : }
121 :
122 30 : return TRACE_SUCCESS;
123 : }
|