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 29 : for (int32_t i = 0; i < num; i++) {
45 7 : TraStatus ret = TraceQueueDequeue(queue, &tmp);
46 7 : if (ret == TRACE_QUEUE_NULL) {
47 0 : break;
48 : }
49 7 : 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) || (queue->size >= MAX_QUEUE_SIZE)) {
58 2 : return TRACE_QUEUE_FULL;
59 : }
60 :
61 30 : return TRACE_SUCCESS;
62 : }
63 :
64 79 : STATIC TraStatus TraceQueueNULL(const TraceQueue* queue)
65 : {
66 79 : if ((queue->count == 0) || (queue->size == 0) || ((queue->head == NULL) && (queue->rear == NULL))) {
67 29 : return TRACE_QUEUE_NULL;
68 : }
69 :
70 50 : return TRACE_SUCCESS;
71 : }
72 :
73 33 : TraStatus TraceQueueEnqueue(TraceQueue* queue, TraceNode* node)
74 : {
75 33 : ADIAG_CHK_NULL_PTR(queue, return TRACE_INVALID_PTR);
76 32 : ADIAG_CHK_NULL_PTR(node, return TRACE_INVALID_PTR);
77 32 : ADIAG_CHK_EXPR_ACTION(node->dataLen == 0, return TRACE_INVALID_PARAM, "trace node is empty.");
78 :
79 : // queue would be more than max_size, but node max_count
80 32 : if (TraceQueueFull(queue) != TRACE_SUCCESS) {
81 2 : return TRACE_QUEUE_FULL;
82 : }
83 :
84 30 : if (TraceQueueNULL(queue) != TRACE_SUCCESS) {
85 10 : queue->head = node;
86 10 : queue->rear = node;
87 10 : queue->count = 1;
88 10 : queue->size = node->dataLen;
89 : } else {
90 20 : queue->rear->next = node; // firstly, join the node to list
91 20 : queue->rear = node; // secondly, mv rear to listRear
92 20 : queue->count++;
93 20 : queue->size += node->dataLen;
94 : }
95 :
96 30 : return TRACE_SUCCESS;
97 : }
98 :
99 51 : TraStatus TraceQueueDequeue(TraceQueue* queue, TraceNode** node)
100 : {
101 51 : ADIAG_CHK_NULL_PTR(queue, return TRACE_INVALID_PTR);
102 49 : ADIAG_CHK_NULL_PTR(node, return TRACE_INVALID_PTR);
103 :
104 49 : if (TraceQueueNULL(queue) != TRACE_SUCCESS) {
105 19 : return TRACE_QUEUE_NULL;
106 : }
107 :
108 30 : *node = queue->head; // get head
109 30 : if (queue->count == 1) {
110 10 : (void)TraceQueueInit(queue);
111 : } else {
112 20 : TraceNode* tmp = queue->head;
113 20 : queue->count--;
114 20 : queue->size -= tmp->dataLen;
115 20 : queue->head = tmp->next;
116 20 : tmp->next = NULL;
117 : }
118 :
119 30 : return TRACE_SUCCESS;
120 : }
|