-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuf_list.c
243 lines (199 loc) · 5.46 KB
/
buf_list.c
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
/*
* Copyright 2022-2024 The Onps Project Author All Rights Reserved.
*
* Author:Neo-T
*
* Licensed under the Apache License 2.0 (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
* in the file LICENSE in the source distribution or at
* http://www.onps.org.cn/apache2.0.txt
*
*/
#include "port/datatype.h"
#include "port/os_datatype.h"
#include "port/os_adapter.h"
#include "port/sys_config.h"
#include "onps_errors.h"
#define SYMBOL_GLOBALS
#include "mmu/buf_list.h"
#undef SYMBOL_GLOBALS
//* 不使用结构体设计方式,目的是为了节省内存,同时避免因使用强制对齐方式降低系统执行性能的问题
static void *l_pvaBufNode[BUF_LIST_NUM];
static USHORT l_usaBufSize[BUF_LIST_NUM];
static SHORT l_saFreeBufNode[BUF_LIST_NUM];
static SHORT l_sFreeBufList = -1;
//static HMUTEX l_hMtxMMUBufList = INVALID_HMUTEX;
//* 协议栈初始加载时别忘了要先调用这个初始设置函数
BOOL buf_list_init(EN_ONPSERR *penErr)
{
INT i;
//* 清0
memset(l_pvaBufNode, 0, sizeof(l_pvaBufNode));
//* 链接
for (i = 0; i < BUF_LIST_NUM - 1; i++)
l_saFreeBufNode[i] = i + 1;
l_saFreeBufNode[BUF_LIST_NUM - 1] = -1;
l_sFreeBufList = 0;
#if 0
l_hMtxMMUBufList = os_thread_mutex_init();
if (INVALID_HMUTEX != l_hMtxMMUBufList)
return TRUE;
if(penErr)
*penErr = ERRMUTEXINITFAILED;
return FALSE;
#else
return TRUE;
#endif
}
void buf_list_uninit(void)
{
//if (INVALID_HMUTEX != l_hMtxMMUBufList)
// os_thread_mutex_uninit(l_hMtxMMUBufList);
}
SHORT buf_list_get(EN_ONPSERR *penErr)
{
SHORT sRtnNode;
os_critical_init();
//os_thread_mutex_lock(l_hMtxMMUBufList);
os_enter_critical();
{
if (l_sFreeBufList < 0)
{
os_exit_critical();
//os_thread_mutex_unlock(l_hMtxMMUBufList);
if (penErr)
*penErr = ERRNOBUFLISTNODE;
return -1;
}
sRtnNode = l_sFreeBufList;
l_sFreeBufList = l_saFreeBufNode[l_sFreeBufList];
}
//os_thread_mutex_unlock(l_hMtxMMUBufList);
os_exit_critical();
return sRtnNode;
}
SHORT buf_list_get_ext(void *pvData, UINT unDataSize, EN_ONPSERR *penErr)
{
SHORT sRtnNode;
os_critical_init();
//os_thread_mutex_lock(l_hMtxMMUBufList);
os_enter_critical();
{
if (l_sFreeBufList < 0)
{
//os_thread_mutex_unlock(l_hMtxMMUBufList);
os_exit_critical();
if (penErr)
*penErr = ERRNOBUFLISTNODE;
return -1;
}
sRtnNode = l_sFreeBufList;
l_sFreeBufList = l_saFreeBufNode[l_sFreeBufList];
}
os_exit_critical();
//os_thread_mutex_unlock(l_hMtxMMUBufList);
l_pvaBufNode[sRtnNode] = pvData;
l_usaBufSize[sRtnNode] = (USHORT)unDataSize;
return sRtnNode;
}
void buf_list_attach_data(SHORT sNode, void *pvData, UINT unDataSize)
{
l_pvaBufNode[sNode] = pvData;
l_usaBufSize[sNode] = (USHORT)unDataSize;
}
void buf_list_free(SHORT sNode)
{
if (sNode < 0)
return;
os_critical_init();
//os_thread_mutex_lock(l_hMtxMMUBufList);
os_enter_critical();
{
l_pvaBufNode[sNode] = NULL;
l_usaBufSize[sNode] = 0;
l_saFreeBufNode[sNode] = l_sFreeBufList;
l_sFreeBufList = sNode;
}
//os_thread_mutex_unlock(l_hMtxMMUBufList);
os_exit_critical();
}
void buf_list_free_head(SHORT *psHead, SHORT sNode)
{
if (sNode < 0)
return;
*psHead = l_saFreeBufNode[sNode];
buf_list_free(sNode);
}
void buf_list_put_head(SHORT *psHead, SHORT sNode)
{
l_saFreeBufNode[sNode] = *psHead;
*psHead = sNode;
}
void buf_list_put_tail(SHORT sHead, SHORT sNode)
{
SHORT sNextNode = sHead;
while (sNextNode >= 0)
{
if (l_saFreeBufNode[sNextNode] < 0)
{
l_saFreeBufNode[sNextNode] = sNode;
l_saFreeBufNode[sNode] = -1;
break;
}
sNextNode = l_saFreeBufNode[sNextNode];
}
}
void *buf_list_get_next_node(SHORT *psNextNode, USHORT *pusDataLen)
{
if (*psNextNode < 0)
return NULL;
void *pvData = NULL;
*pusDataLen = l_usaBufSize[*psNextNode];
pvData = l_pvaBufNode[*psNextNode];
*psNextNode = l_saFreeBufNode[*psNextNode];
return pvData;
}
UINT buf_list_get_len(SHORT sBufListHead)
{
SHORT sNextNode = sBufListHead;
USHORT usDataLen;
UINT unTotalLen = 0;
while (NULL != buf_list_get_next_node(&sNextNode, &usDataLen))
unTotalLen += (UINT)usDataLen;
return unTotalLen;
}
//* 将list保存地报文取出、合并后保存到参数pubPacket指向的缓冲区
void buf_list_merge_packet(SHORT sBufListHead, UCHAR *pubPacket)
{
SHORT sNextNode = sBufListHead;
UCHAR *pubData;
USHORT usDataLen;
UINT unMergeBytes = 0;
__lblGetNextNode:
pubData = (UCHAR *)buf_list_get_next_node(&sNextNode, &usDataLen);
if (NULL == pubData)
return;
//* 搬运数据
memcpy(pubPacket + unMergeBytes, pubData, usDataLen);
unMergeBytes += (UINT)usDataLen;
goto __lblGetNextNode;
}
SHORT buf_list_free_nodes_num(void)
{
SHORT sFreeNodesNum = 0;
os_critical_init();
os_enter_critical();
{
if (!(l_sFreeBufList < 0))
{
SHORT sNextNode = l_sFreeBufList;
while (!(sNextNode < 0))
{
sFreeNodesNum++;
sNextNode = l_saFreeBufNode[sNextNode];
}
}
}
os_exit_critical();
return sFreeNodesNum;
}