xlist.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852
  1. #include "xlist.h"
  2. #include "elog.h"
  3. #include "pthread.h"
  4. #define MALLOC malloc
  5. #define FREE free
  6. #define LOGD log_d
  7. #define LOGE log_e
  8. #define LOGW log_w
  9. #define MIN(a,b) (((a)>(b))?(b):(a))
  10. #define MAX(a,b) (((a)>(b))?(a):(b))
  11. typedef struct {
  12. list_node_t *head;
  13. list_node_t *tail;
  14. int size;
  15. }dlist_t;
  16. typedef struct {
  17. dlist_t used;
  18. handle_t lock;
  19. list_cfg_t cfg;
  20. dlist_t free;
  21. }list_t;
  22. static list_node_t *node_get(list_t *l, int index);
  23. static void* lock_init(void)
  24. {
  25. int r;
  26. pthread_mutex_t *m=MALLOC(sizeof(pthread_mutex_t));
  27. if(!m) {
  28. return NULL;
  29. }
  30. r = pthread_mutex_init(m, NULL);
  31. if(r) {
  32. FREE(m);
  33. return NULL;
  34. }
  35. return m;
  36. }
  37. static int lock_free(void *h)
  38. {
  39. if(!h) {
  40. return -1;
  41. }
  42. pthread_mutex_destroy((pthread_mutex_t*)h);
  43. FREE(h);
  44. return 0;
  45. }
  46. static int lock_on(void *h)
  47. {
  48. if(!h) {
  49. return -1;
  50. }
  51. return pthread_mutex_lock((pthread_mutex_t*)h);
  52. }
  53. static int lock_off(void *h)
  54. {
  55. if(!h) {
  56. return -1;
  57. }
  58. return pthread_mutex_unlock((pthread_mutex_t*)h);
  59. }
  60. static list_node_t* node_from_free(list_t *l, int dlen)
  61. {
  62. int i;
  63. list_node_t *tmp=NULL;
  64. dlist_t *dl=&l->free;
  65. tmp = dl->head; //��ͷ����ʼ�������ʽڵ�
  66. while(tmp) {
  67. if(tmp->data.buf && tmp->data.blen>=dlen) {
  68. if(tmp==dl->head) { //head
  69. if(dl->size==1) {
  70. dl->head = NULL;
  71. dl->tail = NULL;
  72. }
  73. else {
  74. tmp->next->prev = NULL;
  75. dl->head = tmp->next;
  76. }
  77. }
  78. else if(tmp==dl->tail) {
  79. if(dl->size==1) {
  80. dl->head = NULL;
  81. dl->tail = NULL;
  82. }
  83. else {
  84. tmp->next->next = NULL;
  85. dl->tail = tmp->next;
  86. }
  87. }
  88. else {
  89. tmp->prev->next = tmp->next;
  90. tmp->next->prev = tmp->prev;
  91. }
  92. dl->size--;
  93. return tmp;
  94. }
  95. tmp = tmp->next;
  96. }
  97. return NULL;
  98. }
  99. static int node_to_free(list_t *l, list_node_t *node)
  100. {
  101. int r=-1;
  102. list_node_t *tail=NULL;
  103. dlist_t *dl=&l->free;
  104. tail = dl->tail; //�½ڵ����free�б�β��
  105. if(tail==NULL) { //�޽ڵ�
  106. node->prev = NULL;
  107. node->next = NULL;
  108. dl->head = node;
  109. dl->tail = node;
  110. }
  111. else {
  112. node->prev = tail;
  113. node->next = NULL;
  114. tail->next = node;
  115. dl->tail = node;
  116. if(dl->head==NULL) {
  117. dl->head = node;
  118. }
  119. }
  120. dl->size++;
  121. return 0;
  122. }
  123. static list_node_t *node_new(list_t *l, node_t *n)
  124. {
  125. list_node_t *node=NULL;
  126. node = node_from_free(l, n->dlen);
  127. if(!node) {
  128. node = MALLOC(sizeof(list_node_t));
  129. if (!node) return NULL;
  130. node->prev = NULL;
  131. node->next = NULL;
  132. node->data.buf = MALLOC(n->dlen);
  133. if(!node->data.buf) {
  134. FREE(node);
  135. return NULL;
  136. }
  137. node->data.blen = n->dlen;
  138. }
  139. memcpy(node->data.buf, n->buf, n->dlen);
  140. node->data.dlen = n->dlen;
  141. node->data.tp = n->tp;
  142. return node;
  143. }
  144. static int node_free(list_t *l, list_node_t *node)
  145. {
  146. FREE(node->data.buf);
  147. FREE(node);
  148. return 0;
  149. }
  150. static int node_remove(list_t *l, list_node_t *node, int ds)
  151. {
  152. int index;
  153. list_node_t *tmp=node;
  154. dlist_t *dl=&l->used;
  155. if(tmp==dl->head) {
  156. if(dl->size==1) {
  157. dl->head = NULL;
  158. dl->tail = NULL;
  159. }
  160. else {
  161. dl->head = tmp->next;
  162. dl->head->prev = NULL;
  163. }
  164. }
  165. else if(tmp==dl->tail) {
  166. if(dl->size==1) {
  167. dl->head = NULL;
  168. dl->tail = NULL;
  169. }
  170. else {
  171. dl->tail = tmp->prev;
  172. if(dl->tail) {
  173. dl->tail->next = NULL;
  174. }
  175. }
  176. }
  177. else {
  178. tmp->prev->next = tmp->next;
  179. tmp->next->prev = tmp->prev;
  180. }
  181. dl->size--;
  182. if(ds==1) {
  183. node_to_free(l, tmp);
  184. }
  185. else if(ds==2) {
  186. node_free(l, tmp);
  187. }
  188. return 0;
  189. }
  190. static int node_insert(list_t *l, list_node_t *node, int index)
  191. {
  192. list_node_t *xd=NULL,*tmp=node;
  193. dlist_t *dl=&l->used;
  194. if(dl->size==0) {
  195. tmp->prev = NULL;
  196. tmp->next = NULL;
  197. dl->head = tmp;
  198. dl->tail = tmp;
  199. }
  200. else {
  201. if(index==0) { //head
  202. tmp->prev = NULL;
  203. tmp->next = dl->head;
  204. xd->prev = tmp;
  205. dl->head = tmp;
  206. }
  207. else if((index==(dl->size)) || (index==-1)) { //tail
  208. tmp->prev = dl->tail;
  209. tmp->next = NULL;
  210. dl->tail->next = tmp;
  211. dl->tail = tmp;
  212. }
  213. else {
  214. xd = node_get(l, index);
  215. tmp->prev = xd->prev;
  216. tmp->next = xd;
  217. xd->prev->next = tmp;
  218. xd->prev = tmp;
  219. }
  220. }
  221. dl->size++;
  222. return 0;
  223. }
  224. static int node_set(list_t *l, list_node_t *node, node_t *nd)
  225. {
  226. FREE(node->data.buf);
  227. node->data.buf = MALLOC(nd->dlen);
  228. if(!node->data.buf) {
  229. return -1;
  230. }
  231. memcpy(node->data.buf, nd->buf, nd->dlen);
  232. node->data.blen = nd->dlen;
  233. node->data.dlen = nd->dlen;
  234. return 0;
  235. }
  236. static list_node_t *node_get(list_t *l, int index)
  237. {
  238. int idx=0;
  239. dlist_t *dl=&l->used;
  240. list_node_t *tmp=dl->head;
  241. if(index==0) {
  242. return dl->head;
  243. }
  244. else if(index==dl->size-1) {
  245. return dl->tail;
  246. }
  247. else {
  248. while(tmp) {
  249. if(idx==index) {
  250. return tmp;
  251. }
  252. tmp = tmp->next;
  253. idx++;
  254. }
  255. }
  256. return NULL;
  257. }
  258. static int node_swap(list_node_t *n1, list_node_t *n2)
  259. {
  260. if(!n1 || !n2) {
  261. return -1;;
  262. }
  263. node_t x=n1->data;
  264. n1->data = n2->data;
  265. n2->data = x;
  266. return 0;
  267. }
  268. ///////////////////////////////////////////////////////////
  269. handle_t xlist_init(list_cfg_t *cfg)
  270. {
  271. list_t *l=NULL;
  272. if (!cfg)
  273. return NULL;
  274. l = MALLOC(sizeof(list_t));
  275. if (!l)
  276. return NULL;
  277. l->used.head = NULL;
  278. l->used.tail = NULL;
  279. l->used.size = 0;
  280. l->lock = lock_init();
  281. l->cfg = *cfg;
  282. l->free.head = NULL;
  283. l->free.tail = NULL;
  284. l->free.size = 0;
  285. return l;
  286. }
  287. int xlist_free(handle_t l)
  288. {
  289. int i;
  290. list_node_t *xd;
  291. list_t *hl=(list_t*)l;
  292. if(!hl) {
  293. return -1;
  294. }
  295. if(lock_on(hl->lock)) {
  296. if (hl->cfg.log) LOGE("___ list_free lock failed\n");
  297. return -1;
  298. }
  299. xd = hl->used.head;
  300. while(xd) {
  301. node_free(hl, xd);
  302. xd = xd->next;
  303. }
  304. xd = hl->free.head;
  305. while(xd) {
  306. node_free(hl, xd);
  307. xd = xd->next;
  308. }
  309. lock_off(hl->lock);
  310. lock_free(hl->lock);
  311. free(hl->free.head);
  312. free(hl);
  313. return 0;
  314. }
  315. int xlist_get_node(handle_t l, list_node_t **lnode, int index)
  316. {
  317. int r=-1;
  318. list_node_t *ln=NULL;
  319. list_t *hl=(list_t*)l;
  320. if(!hl || !lnode || index<0) {
  321. return -1;
  322. }
  323. if(lock_on(hl->lock)) {
  324. if (hl->cfg.log) LOGE("___ list_get lock failed\n");
  325. return -1;
  326. }
  327. if (hl->used.size<=0 || index>=hl->used.size) {
  328. goto quit;
  329. }
  330. ln = node_get(hl, index);
  331. if(!ln) {
  332. goto quit;
  333. }
  334. *lnode = ln;
  335. r = 0;
  336. quit:
  337. lock_off(hl->lock);
  338. return r;
  339. }
  340. int xlist_set_node(handle_t l, list_node_t *lnode, int index)
  341. {
  342. int r=-1;
  343. list_node_t *ln=NULL;
  344. list_t *hl=(list_t*)l;
  345. if(!hl || !lnode || index<0) {
  346. return -1;
  347. }
  348. if(lock_on(hl->lock)) {
  349. if (hl->cfg.log) LOGE("___ list_set lock failed\n");
  350. return -1;
  351. }
  352. if (hl->used.size<=0 || index>=hl->used.size) {
  353. goto quit;
  354. }
  355. ln = node_get(hl, index);
  356. if(ln) {
  357. r = node_set(hl, ln, &lnode->data);
  358. }
  359. quit:
  360. lock_off(hl->lock);
  361. return r;
  362. }
  363. int xlist_take_node(handle_t l, list_node_t **lnode, int index)
  364. {
  365. int r=-1;
  366. list_node_t *ln=NULL;
  367. list_t *hl=(list_t*)l;
  368. if(!hl || !lnode || index<0) {
  369. return -1;
  370. }
  371. if(lock_on(hl->lock)) {
  372. if (hl->cfg.log) LOGE("___ list_take_node lock failed\n");
  373. return -1;
  374. }
  375. if (hl->used.size<=0 || index>=hl->used.size) {
  376. goto quit;
  377. }
  378. ln = node_get(hl, index);
  379. if(!ln) {
  380. goto quit;
  381. }
  382. *lnode = ln;
  383. r = node_remove(hl, ln, 0);
  384. quit:
  385. lock_off(hl->lock);
  386. return r;
  387. }
  388. int xlist_back_node(handle_t l, list_node_t *lnode)
  389. {
  390. list_t *hl=(list_t*)l;
  391. if(!hl || !lnode) {
  392. return -1;
  393. }
  394. if(lock_on(hl->lock)) {
  395. if (hl->cfg.log) LOGE("___ list_back_node lock failed\n");
  396. return -1;
  397. }
  398. node_to_free(l, lnode);
  399. lock_off(hl->lock);
  400. return 0;
  401. }
  402. int xlist_discard_node(handle_t l, list_node_t *lnode)
  403. {
  404. list_t *hl=(list_t*)l;
  405. if(!hl || !lnode) {
  406. return -1;
  407. }
  408. if(lock_on(hl->lock)) {
  409. if (hl->cfg.log) LOGE("___ list_discard_node lock failed\n");
  410. return -1;
  411. }
  412. node_free(l, lnode);
  413. lock_off(hl->lock);
  414. return 0;
  415. }
  416. int xlist_insert(handle_t l, node_t *node, int index)
  417. {
  418. int r=-1;
  419. list_t *hl=(list_t*)l;
  420. list_node_t *xd,*tmp;
  421. if(!hl || !node) {
  422. return -1;
  423. }
  424. if(lock_on(hl->lock)) {
  425. if (hl->cfg.log) LOGE("___list_insert lock failed\n");
  426. return -1;
  427. }
  428. if (index>hl->used.size) {
  429. if (hl->cfg.log) LOGE("___list_insert, invalid index, size: %d, index: %d\n", hl->used.size, index);
  430. goto quit;
  431. }
  432. if (hl->cfg.max>0 && hl->used.size>=hl->cfg.max) {
  433. //����װ��ʱ, ���ݲ��Զ�����
  434. //���Ҫ�����λ��Ϊĩβ, ����
  435. if(index<0 || index>=hl->used.size-1) {
  436. if(hl->cfg.mode==LIST_FULL_FILO) { //����Ϊ��������ʱ
  437. if (hl->cfg.log) {
  438. //LOGW("___list_insert, list full, discard the new data\n");
  439. }
  440. r = -2; goto quit;
  441. }
  442. if (hl->cfg.log) {
  443. //LOGW("___list_insert, list full, discard the old data\n");
  444. }
  445. xd = node_get(hl, 0); //�����ϵ����ݶ�
  446. node_remove(hl, xd, 1);
  447. }
  448. else { //�������λ��λ���м�, ���趪���һ������
  449. if (hl->cfg.log) {
  450. LOGW("___list_insert, insert to the middle, discard the last data\n");
  451. }
  452. xd = node_get(hl, hl->used.size-1);
  453. node_remove(hl, xd, 1);
  454. }
  455. }
  456. tmp = node_new(hl, node);
  457. if(!tmp) {
  458. if (hl->cfg.log) LOGE("___list_insert, malloc %d error, l: 0x%08x, used.size: %d, free.size: %d\n", node->dlen, (uint32_t)hl, hl->used.size, hl->free.size);
  459. goto quit;
  460. }
  461. r = node_insert(l, tmp, index);
  462. quit:
  463. lock_off(hl->lock);
  464. return r;
  465. }
  466. int xlist_insert_node(handle_t l, list_node_t *lnode, int index)
  467. {
  468. int r=-1;
  469. list_t *hl=(list_t*)l;
  470. list_node_t *xd,*tmp=lnode;
  471. if(!hl || !lnode) {
  472. return -1;
  473. }
  474. if(lock_on(hl->lock)) {
  475. if (hl->cfg.log) LOGE("___ list_addto lock failed\n");
  476. return -1;
  477. }
  478. if (index>hl->used.size) {
  479. if (hl->cfg.log) LOGE("list_addto, invalid index, size: %d, index: %d\n", hl->used.size, index);
  480. goto quit;
  481. }
  482. if (hl->cfg.max>0 && hl->used.size>=hl->cfg.max) {
  483. //����װ��ʱ, ���ݲ��Զ�����
  484. //���Ҫ�����λ��Ϊĩβ, ����
  485. if(index<0 || index>=hl->used.size-1) {
  486. if(hl->cfg.mode==LIST_FULL_FILO) { //����Ϊ��������ʱ
  487. r = 0; goto quit;
  488. }
  489. xd = node_get(hl, 0); //�����ϵ����ݶ�
  490. node_remove(hl, xd, 1);
  491. }
  492. else { //�������λ��λ���м�, ���趪���һ������
  493. xd = node_get(hl, hl->used.size-1);
  494. node_remove(hl, xd, 1);
  495. }
  496. }
  497. r = node_insert(l, lnode, index);
  498. quit:
  499. lock_off(hl->lock);
  500. return r;
  501. }
  502. int xlist_append(handle_t l, int tp, void *data, int len)
  503. {
  504. node_t node={tp, data, len, len};
  505. return xlist_insert(l, &node, -1);
  506. }
  507. int xlist_append_node(handle_t l, list_node_t *lnode)
  508. {
  509. return xlist_insert_node(l, lnode, -1);
  510. }
  511. int xlist_infront(handle_t l, int tp, void *data, int len)
  512. {
  513. node_t node={tp, data, len, len};
  514. return xlist_insert(l, &node, 0);
  515. }
  516. int xlist_infront_node(handle_t l, list_node_t *lnode)
  517. {
  518. return xlist_insert_node(l, lnode, 0);
  519. }
  520. int xlist_remove(handle_t l, int index)
  521. {
  522. int r=-1;
  523. list_node_t *xd;
  524. list_t *hl=(list_t*)l;
  525. if (!hl) {
  526. return -1;
  527. }
  528. if(lock_on(hl->lock)) {
  529. if (hl->cfg.log) LOGE("___ list_remove lock failed\n");
  530. return -1;
  531. }
  532. xd = node_get(l, index);
  533. if(!xd) {
  534. goto quit;
  535. }
  536. r = node_remove(hl, xd, 1);
  537. quit:
  538. lock_off(hl->lock);
  539. return r;
  540. }
  541. int xlist_delete(handle_t l, int index)
  542. {
  543. int r=-1;
  544. list_node_t *xd;
  545. list_t *hl=(list_t*)l;
  546. if (!hl) {
  547. return -1;
  548. }
  549. if(lock_on(hl->lock)) {
  550. if (hl->cfg.log) LOGE("___ list_delete lock failed\n");
  551. return -1;
  552. }
  553. if(hl->used.size<=0 || index>=hl->used.size) {
  554. goto quit;
  555. }
  556. xd = node_get(l, index);
  557. r = node_free(hl, xd);
  558. quit:
  559. lock_off(hl->lock);
  560. return r;
  561. }
  562. int xlist_sort(handle_t l, int order)
  563. {
  564. int r=-1;
  565. list_t *hl=(list_t*)l;
  566. if (!hl || order>=LIST_SORT_MAX) {
  567. return -1;
  568. }
  569. if(lock_on(hl->lock)) {
  570. if (hl->cfg.log) LOGE("___ list_sort lock failed\n");
  571. return -1;
  572. }
  573. if(hl->used.size<2) {
  574. goto quit;
  575. }
  576. {
  577. int len,cmp;
  578. dlist_t *dl=&hl->used;
  579. list_node_t *p2,*p1=dl->head;
  580. while(p1) {
  581. p2 = p1->next;
  582. while(p2) {
  583. len = MIN(p1->data.dlen, p2->data.dlen);
  584. cmp = memcmp(p1->data.buf, p2->data.buf, len);
  585. if(((cmp>0) && (order==LIST_SORT_ASCEND)) || ((cmp<0) && (order==LIST_SORT_DESCEND))) {
  586. node_swap(p1, p2);
  587. }
  588. p2 = p2->next;
  589. }
  590. p1 = p1->next;
  591. }
  592. }
  593. r = 0;
  594. quit:
  595. lock_off(hl->lock);
  596. return r;
  597. }
  598. int xlist_size(handle_t l)
  599. {
  600. int size=0;
  601. list_t *hl=(list_t*)l;
  602. if (!hl) {
  603. return -1;
  604. }
  605. if(lock_on(hl->lock)) {
  606. if (hl->cfg.log) LOGE("___ list_size lock failed\n");
  607. return -1;
  608. }
  609. size = hl->used.size;
  610. lock_off(hl->lock);
  611. return size;
  612. }
  613. int xlist_clear(handle_t l)
  614. {
  615. int r=-1;
  616. list_node_t *xd;
  617. list_t *hl=(list_t*)l;
  618. if (!hl) {
  619. return -1;
  620. }
  621. if(lock_on(hl->lock)) {
  622. if (hl->cfg.log) LOGE("___ list_clear lock failed\n");
  623. return -1;
  624. }
  625. if(hl->used.size<=0) {
  626. goto quit;
  627. }
  628. xd = hl->used.head;
  629. while(xd) {
  630. node_remove(hl, xd, 1);
  631. xd = xd->next;
  632. }
  633. hl->used.size = 0;
  634. hl->used.head = NULL;
  635. hl->used.tail = NULL;
  636. r = 0;
  637. quit:
  638. lock_off(hl->lock);
  639. return r;
  640. }
  641. int xlist_iterator(handle_t l, node_t *node, list_callback_t callback, void *arg)
  642. {
  643. int r=-1,act=0;
  644. list_node_t *xd=NULL;
  645. list_t *hl=(list_t*)l;
  646. dlist_t *dl=NULL;
  647. if (!hl) {
  648. return -1;
  649. }
  650. if(lock_on(hl->lock)) {
  651. if (hl->cfg.log) LOGE("___ list_iterator lock failed\n");
  652. return -1;
  653. }
  654. dl = &hl->used;
  655. if (dl->size<=0) {
  656. goto quit;
  657. }
  658. xd = dl->head;
  659. while (xd) {
  660. if (callback) {
  661. r = callback(hl, node, &xd->data, arg, &act);
  662. if(act==LIST_ACT_STOP) {
  663. break;
  664. }
  665. else if(act==LIST_ACT_REMOVE) {
  666. node_remove(l, xd, 1);
  667. }
  668. else if(act==LIST_ACT_DELETE) {
  669. node_remove(l, xd, 2);
  670. }
  671. }
  672. xd = xd->next;
  673. }
  674. quit:
  675. lock_off(hl->lock);
  676. return r;
  677. }