list_slst.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. /*
  2. ******************************************************************************
  3. * # Hist:
  4. * Date; Author; Description
  5. * 30 Oct. 2017; Chen, George; file creation, transported from prtos
  6. ******************************************************************************
  7. */
  8. /*
  9. ******************************************************************************
  10. * Includes
  11. ******************************************************************************
  12. */
  13. // #include <string.h>
  14. #include "OSAL.h"
  15. #include "list_slst.h"
  16. // #include "logs_logs.h"
  17. /*
  18. ******************************************************************************
  19. * defines
  20. ******************************************************************************
  21. */
  22. // #define LIST_SLST_DEFS_LOGS_TAG "SLST"
  23. #ifdef LIST_SLST_DEFS_LOGS_TAG
  24. /* ERROR */
  25. #define log_err(fmt, ...) \
  26. logs_logs_err(LIST_SLST_DEFS_LOGS_TAG, fmt, ##__VA_ARGS__)
  27. /* WARNING */
  28. #define log_war(fmt, ...) \
  29. logs_logs_war(LIST_SLST_DEFS_LOGS_TAG, fmt, ##__VA_ARGS__)
  30. /* INFORMATION */
  31. #define log_inf(fmt, ...) \
  32. logs_logs_inf(LIST_SLST_DEFS_LOGS_TAG, fmt, ##__VA_ARGS__)
  33. /* VERB */
  34. #define log_ver(fmt, ...) \
  35. logs_logs_ver(LIST_SLST_DEFS_LOGS_TAG, fmt, ##__VA_ARGS__)
  36. /* Function entry */
  37. #define log_ent(fmt, ...) \
  38. logs_logs_ent(fmt, ##__VA_ARGS__)
  39. /* function exit */
  40. #define log_exi(fmt, ...) \
  41. logs_logs_exi(fmt, ##__VA_ARGS__)
  42. #else
  43. /* ERROR */
  44. #define log_err(fmt, ...)
  45. /* WARNING */
  46. #define log_war(fmt, ...)
  47. /* INFORMATION */
  48. #define log_inf(fmt, ...)
  49. /* VERB */
  50. #define log_ver(fmt, ...)
  51. /* Function entry */
  52. #define log_ent(fmt, ...)
  53. /* function exit */
  54. #define log_exi(fmt, ...)
  55. #endif /* LIST_SLST_DEFS_LOGS_TAG */
  56. /*
  57. ******************************************************************************
  58. * Private Functions
  59. ******************************************************************************
  60. */
  61. /*
  62. * # name:
  63. * # desc:
  64. * # para:
  65. * # rslt:
  66. */
  67. static signed int
  68. list_snod_ini_lin(list_snod_t* snod)
  69. {
  70. signed int rslt = 0;
  71. if ( 0 == snod ) { /* validation check */
  72. //
  73. rslt = 0;
  74. } else { /* initialize to 0 */
  75. snod->next = 0;
  76. rslt = 1;
  77. }
  78. return ( rslt );
  79. }
  80. /*
  81. * # name:
  82. * # desc:
  83. * # para:
  84. * # rslt:
  85. */
  86. static signed int
  87. list_snod_ini_cir(list_snod_t* snod)
  88. {
  89. signed int rslt = 0;
  90. if ( 0 == snod ) { /* validation check */
  91. //
  92. rslt = 0;
  93. } else { /* initial to self */
  94. snod->next = snod;
  95. rslt = 1;
  96. }
  97. return ( rslt );
  98. }
  99. /*
  100. * # name:
  101. * # desc:
  102. * # para:
  103. * # rslt:
  104. */
  105. static signed int
  106. list_snod_ins_aft(list_snod_t* nod0, list_snod_t* nod1)
  107. {
  108. signed int rslt = 0;
  109. /* validation checking */
  110. if ( 0 == nod0 || 0 == nod1 ) {
  111. rslt = 0;
  112. goto ERR_INV;
  113. }
  114. /* insert list node */
  115. nod1->next = nod0->next;
  116. nod0->next = nod1;
  117. /* result success */
  118. rslt = 1;
  119. goto ERR_SUC;
  120. ERR_INV:
  121. ERR_SUC:
  122. return ( rslt );
  123. }
  124. /*
  125. * # name:
  126. * # desc:
  127. * # para:
  128. * # rslt:
  129. */
  130. static signed int
  131. list_snod_rmv_aft(list_snod_t* snod)
  132. {
  133. signed int rslt = 0;
  134. /* validation checking */
  135. if ( 0 == snod ) {
  136. rslt = 0;
  137. goto ERR_INV;
  138. }
  139. if ( 0 != snod->next ) {
  140. snod->next = snod->next->next;
  141. }
  142. /* result success */
  143. rslt = 1;
  144. goto ERR_SUC;
  145. ERR_INV:
  146. ERR_SUC:
  147. return ( rslt );
  148. }
  149. /*
  150. ******************************************************************************
  151. * Public Functions
  152. ******************************************************************************
  153. */
  154. signed int
  155. list_snod_ini(list_snod_t* snod, list_snod_e type)
  156. {
  157. signed int rslt = 0;
  158. if ( list_slst_enum_snod_lin == type ) {
  159. rslt = list_snod_ini_lin(snod);
  160. } else
  161. if ( list_slst_enum_snod_cir == type ) {
  162. rslt = list_snod_ini_cir(snod);
  163. }
  164. return ( rslt );
  165. }
  166. /*
  167. ******************************************************************************
  168. * Public Functions : slst
  169. ******************************************************************************
  170. */
  171. /*
  172. * # name:
  173. * # desc:
  174. * # para:
  175. * # rslt:
  176. */
  177. signed int
  178. list_slst_ini(list_slst_t* slst)
  179. {
  180. signed int rslt = 0;
  181. /* validation checking */
  182. if ( 0 == slst ) {
  183. rslt = 0;
  184. goto ERR_INV;
  185. }
  186. slst->head = 0; // [set by sys] head node
  187. slst->tail = 0; // [set by sys] tail node
  188. slst->iter = 0; // [set by sys] iter node
  189. slst->coun = 0; // [set by sys] list size of entity
  190. /* result success */
  191. rslt = 1;
  192. goto ERR_SUC;
  193. ERR_INV:
  194. ERR_SUC:
  195. return ( rslt );
  196. }
  197. /*
  198. * # name:
  199. * # desc: insert at tail
  200. * # para:
  201. * # rslt:
  202. */
  203. signed int
  204. list_slst_add(list_slst_t* slst, list_snod_t* snod)
  205. {
  206. signed int rslt = 0;
  207. list_snod_t* curr = 0;
  208. if ( 0 == snod ) {
  209. rslt = 0;
  210. goto ERR_INV;
  211. }
  212. list_slst_emu(slst);
  213. while ( 1 ) {
  214. curr = list_slst_nxt(slst);
  215. if ( 0 == curr ) { // not found
  216. if ( 0 >= slst->coun ) { // empty list?,
  217. slst->head = snod; // then, become the first node
  218. slst->tail = snod;
  219. } else { // otherwise, insert @ tail
  220. // ignore result check, always true at this case
  221. list_snod_ins_aft(slst->tail, snod);
  222. slst->tail = snod; // update tail
  223. }
  224. slst->coun ++; // update list capability
  225. rslt = 1;
  226. goto ERR_SUC; // SUCCESS
  227. } else
  228. if ( snod == curr ) { // found
  229. rslt = 0;
  230. goto ERR_DOE; // DOEXIST
  231. }
  232. }
  233. ERR_DOE:
  234. ERR_INV:
  235. ERR_SUC:
  236. return ( rslt );
  237. }
  238. /* TODO: MARK HERE */
  239. /*
  240. * # name:
  241. * # desc: remove at head
  242. * # para:
  243. * # rslt:
  244. */
  245. signed int
  246. list_slst_rmv(list_slst_t* slst, list_snod_t* snod)
  247. {
  248. signed int rslt = 0;
  249. list_snod_t* prev = 0;
  250. list_snod_t* curr = 0;
  251. /* validation checking */
  252. if ( 0 == snod ) {
  253. rslt = 0;
  254. goto ERR_INV;
  255. }
  256. list_slst_emu(slst);
  257. while ( 1 ) {
  258. curr = list_slst_nxt(slst);
  259. if ( 0 == curr ) { // not found
  260. rslt = 0;
  261. goto ERR_NOF;
  262. } else
  263. if ( snod == curr ) { // found
  264. slst->coun --; // [set by sys] list size of entity
  265. if ( 0 >= slst->coun ) {
  266. slst->tail = slst->head = 0;
  267. } else
  268. if ( snod == slst->head ) {
  269. slst->head = snod->next;
  270. } else
  271. if ( snod == slst->tail ) {
  272. slst->tail = prev;
  273. }
  274. // remove given snod
  275. if ( 0 != prev ) {
  276. list_snod_rmv_aft(prev);
  277. }
  278. rslt = 1;
  279. goto ERR_SUC;
  280. }
  281. prev = curr;
  282. }
  283. ERR_NOF:
  284. ERR_INV:
  285. ERR_SUC:
  286. return ( rslt );
  287. }
  288. /*
  289. * # name:
  290. * # desc:
  291. * # para:
  292. * # rslt:
  293. */
  294. signed int
  295. list_slst_has(list_slst_t* slst, const list_snod_t* snod)
  296. {
  297. signed int rslt = 0;
  298. list_snod_t* curr = 0;
  299. /* validation checking */
  300. if ( 0 == snod ) {
  301. rslt = 0;
  302. goto ERR_INV;
  303. }
  304. list_slst_emu(slst);
  305. while ( 1 ) {
  306. curr = list_slst_nxt(slst);
  307. if ( 0 == curr ) { // not found
  308. rslt = 0;
  309. break;
  310. } else
  311. if ( snod == curr ) { // found
  312. rslt = 1;
  313. break;
  314. }
  315. }
  316. ERR_INV:
  317. return ( rslt );
  318. }
  319. /*
  320. * # name:
  321. * # desc:
  322. * # para:
  323. * # rslt:
  324. */
  325. signed int
  326. list_slst_emu(list_slst_t* slst)
  327. {
  328. return ( 0 != (slst->iter = slst->head) ? 1 : 0 );
  329. }
  330. /*
  331. * # name:
  332. * # desc:
  333. * # para:
  334. * # rslt:
  335. */
  336. list_snod_t*
  337. list_slst_nxt(list_slst_t* slst)
  338. {
  339. list_snod_t* rslt = 0;
  340. if ( 0 != slst ) {
  341. rslt = slst->iter;
  342. }
  343. if ( 0 != rslt ) {
  344. slst->iter = rslt->next;
  345. }
  346. if ( slst->iter == slst->head ) {
  347. rslt = 0;
  348. }
  349. return ( rslt );
  350. }
  351. /*
  352. ******************************************************************************
  353. * Public Functions : sque
  354. ******************************************************************************
  355. */
  356. /*
  357. * # name:
  358. * # desc:
  359. * # para:
  360. * # rslt:
  361. */
  362. signed int
  363. list_sque_ini(list_sque_t* sque)
  364. {
  365. signed int rslt = 0;
  366. if ( 0 == sque ) {
  367. rslt = 0;
  368. goto ERR_INV;
  369. }
  370. // ignore validation check, always result true in this case
  371. list_slst_ini(&sque->list);
  372. /*
  373. * initialize post & pend ptrs to 0.
  374. * sque->list now is a empty list.
  375. * util_sque_fsh should called after the sque->list is filled
  376. * before using the sque.
  377. */
  378. sque->post = 0; // [set by sys] head node to pop out/out box
  379. sque->pend = 0; // [set by sys] tail node to push in/in box
  380. sque->coun = 0; // [set by sys] counts of records
  381. /* result success */
  382. rslt = 1;
  383. goto ERR_SUC;
  384. ERR_INV:
  385. ERR_SUC:
  386. return ( rslt );
  387. }
  388. /*
  389. * # name:
  390. * # desc:
  391. * # para:
  392. * # rslt:
  393. */
  394. signed int
  395. list_sque_fsh(list_sque_t* sque)
  396. {
  397. signed int rslt = 0;
  398. /* validation checking */
  399. if ( 0 == sque ) {
  400. rslt = 0;
  401. goto ERR_INV;
  402. }
  403. sque->post = sque->list.head;
  404. sque->pend = sque->list.head;
  405. sque->coun = 0;
  406. /* result success */
  407. rslt = 1;
  408. goto ERR_SUC;
  409. ERR_INV:
  410. ERR_SUC:
  411. return ( rslt );
  412. }
  413. /*
  414. * # name:
  415. * # desc:
  416. * # para:
  417. * # rslt:
  418. */
  419. signed int
  420. list_sque_pop(list_sque_t* sque, void* data, unsigned int size)
  421. {
  422. signed int rslt = 0;
  423. /* validation checking */
  424. /* NOTE: dst and scpy validation should be checked by user or in scpy */
  425. if ( 0 == sque ) {
  426. rslt = 0;
  427. goto ERR_INV;
  428. }
  429. /* NOTE: do nothing on empty queue */
  430. if ( 0 >= sque->coun ) {
  431. rslt = 0;
  432. goto ERR_INV;
  433. }
  434. /* NOTE: push & pops only works on circular queue */
  435. if ( sque->list.head != sque->list.tail->next ) {
  436. rslt = 0;
  437. goto ERR_INV;
  438. }
  439. osal_memcpy(data, sque->post+1, size);
  440. /* maintainence */
  441. sque->post = sque->post->next;
  442. sque->coun --;
  443. /* result success */
  444. rslt = 1;
  445. goto ERR_SUC;
  446. ERR_INV:
  447. ERR_SUC:
  448. return ( rslt );
  449. }
  450. /*
  451. * # name:
  452. * # desc:
  453. * # para:
  454. * # rslt:
  455. */
  456. signed int
  457. list_sque_psh(list_sque_t* sque, void* data, unsigned int size)
  458. {
  459. signed int rslt = 0;
  460. /* validation checking */
  461. /* NOTE: src and scpy validation should be checked by user or in scpy */
  462. if ( 0 == sque ) {
  463. rslt = 0;
  464. goto ERR_INV;
  465. }
  466. /* NOTE: do nothing on full list */
  467. if ( sque->coun >= sque->list.coun ) {
  468. rslt = 0;
  469. goto ERR_INV;
  470. }
  471. /* NOTE: push & pops only works on circular queue */
  472. if ( sque->list.head != sque->list.tail->next ) {
  473. rslt = 0;
  474. goto ERR_INV;
  475. }
  476. osal_memcpy(sque->pend+1, data, size);
  477. sque->pend = sque->pend->next;
  478. sque->coun ++;
  479. /* result success */
  480. rslt = 1;
  481. goto ERR_SUC;
  482. ERR_INV:
  483. ERR_SUC:
  484. return ( rslt );
  485. }