1 | #include <config.h> |
---|---|
2 | #include "lst.h" |
3 | |
4 | /*************************** |
5 | * _lstFreeItem |
6 | * |
7 | * 1. FREES MEMORY USED BY THE LIST ITEM AND REMOVES IT FROM ITS LIST |
8 | * 2. WILL CLEAN UP root ITEM IF REQUIRED.. NEVER SETS bDelete... lstDelete DOES SET bDelete |
9 | * 3. CALLS _lstAdjustCurrent TO ENSURE THAT CURRENT DOES NOT END UP ON bDelete ITEM |
10 | ***************************/ |
11 | int _lstFreeItem( HLSTITEM hItem ) |
12 | { |
13 | HLST hLst; |
14 | HLSTITEM hItemRoot; |
15 | HLSTITEM hNewCurrent = NULL; |
16 | |
17 | if ( !hItem ) |
18 | return LST_ERROR; |
19 | |
20 | hLst = (HLST)hItem->hLst; |
21 | |
22 | /************* |
23 | * FREE root ITEM AS REQUIRED |
24 | *************/ |
25 | if ( hLst->hLstBase ) |
26 | { |
27 | hItemRoot = (HLSTITEM)hItem->pData; |
28 | |
29 | /************* |
30 | * dec ref count in root item |
31 | *************/ |
32 | hItemRoot->nRefs--; |
33 | |
34 | /************* |
35 | * DELETE root ITEM IF REF = 0 AND SOMEONE SAID DELETE IT |
36 | *************/ |
37 | if ( hItemRoot->nRefs < 1 && hItemRoot->bDelete ) |
38 | { |
39 | _lstFreeItem( hItemRoot ); |
40 | } |
41 | } |
42 | |
43 | /************* |
44 | * WE ALWAYS FREE hItem |
45 | *************/ |
46 | if ( hItem->pData && hLst->pFree ) |
47 | hLst->pFree( hItem->pData ); |
48 | |
49 | if ( !hItem->bDelete ) /* THIS IS REALLY ONLY A FACTOR FOR ROOT ITEMS */ |
50 | hLst->nItems--; |
51 | |
52 | if ( hItem == hLst->hFirst ) |
53 | hLst->hFirst = hItem->pNext; |
54 | |
55 | if ( hItem == hLst->hLast ) |
56 | hLst->hLast = hItem->pPrev; |
57 | |
58 | if ( hItem->pPrev ) |
59 | { |
60 | hItem->pPrev->pNext = hItem->pNext; |
61 | if ( hItem == hLst->hCurrent ) |
62 | hNewCurrent = hItem->pPrev; |
63 | } |
64 | |
65 | if ( hItem->pNext ) |
66 | { |
67 | hItem->pNext->pPrev = hItem->pPrev; |
68 | if ( !hNewCurrent && hItem == hLst->hCurrent ) |
69 | hNewCurrent = hItem->pNext; |
70 | } |
71 | |
72 | free( hItem ); |
73 | |
74 | hLst->hCurrent = hNewCurrent; |
75 | |
76 | _lstAdjustCurrent( hLst ); |
77 | |
78 | |
79 | return LST_SUCCESS; |
80 | } |
81 | |
82 | |
83 | |
84 |