lists.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  1. #include <common.h>
  2. #include <malloc.h>
  3. #include <lists.h>
  4. #define MAX(a,b) (((a)>(b)) ? (a) : (b))
  5. #define MIN(a,b) (((a)<(b)) ? (a) : (b))
  6. #define CAT4CHARS(a,b,c,d) ((a<<24) | (b<<16) | (c<<8) | d)
  7. /* increase list size by 10% every time it is full */
  8. #define kDefaultAllocationPercentIncrease 10
  9. /* always increase list size by 4 items when it is full */
  10. #define kDefaultAllocationminNumItemsIncrease 4
  11. /*
  12. * how many items to expand the list by when it becomes full
  13. * = current listSize (in items) + (hiword percent of list size) + loword
  14. */
  15. #define NUMITEMSPERALLOC(list) MAX(((*list)->listSize * \
  16. ((*list)->percentIncrease + 100)) / 100, \
  17. (*list)->minNumItemsIncrease )
  18. #define ITEMPTR(list,item) &(((char *)&(*list)->itemList)[(*(list))->itemSize * (item)])
  19. #define LIST_SIGNATURE CAT4CHARS('L', 'I', 'S', 'T');
  20. #define calloc(size,num) malloc(size*num)
  21. /********************************************************************/
  22. Handle NewHandle (unsigned int numBytes)
  23. {
  24. void *memPtr;
  25. HandleRecord *hanPtr;
  26. memPtr = calloc (numBytes, 1);
  27. hanPtr = (HandleRecord *) calloc (sizeof (HandleRecord), 1);
  28. if (hanPtr && (memPtr || numBytes == 0)) {
  29. hanPtr->ptr = memPtr;
  30. hanPtr->size = numBytes;
  31. return (Handle) hanPtr;
  32. } else {
  33. free (memPtr);
  34. free (hanPtr);
  35. return NULL;
  36. }
  37. }
  38. /********************************************************************/
  39. void DisposeHandle (Handle handle)
  40. {
  41. if (handle) {
  42. free (*handle);
  43. free ((void *) handle);
  44. }
  45. }
  46. /********************************************************************/
  47. unsigned int GetHandleSize (Handle handle)
  48. {
  49. return ((HandleRecord *) handle)->size;
  50. }
  51. /********************************************************************/
  52. int SetHandleSize (Handle handle, unsigned int newSize)
  53. {
  54. HandleRecord *hanRecPtr = (HandleRecord *) handle;
  55. void *newPtr, *oldPtr;
  56. unsigned int oldSize;
  57. oldPtr = hanRecPtr->ptr;
  58. oldSize = hanRecPtr->size;
  59. if (oldSize == newSize)
  60. return 1;
  61. if (oldPtr == NULL) {
  62. newPtr = malloc (newSize);
  63. } else {
  64. newPtr = realloc (oldPtr, newSize);
  65. }
  66. if (newPtr || (newSize == 0)) {
  67. hanRecPtr->ptr = newPtr;
  68. hanRecPtr->size = newSize;
  69. if (newSize > oldSize)
  70. memset ((char *) newPtr + oldSize, 0, newSize - oldSize);
  71. return 1;
  72. } else
  73. return 0;
  74. }
  75. #ifdef CFG_ALL_LIST_FUNCTIONS
  76. /* Used to compare list elements by their raw data contents */
  77. static int ListMemBlockCmp (void *a, void *b, int size)
  78. {
  79. return memcmp (a, b, size);
  80. }
  81. /***************************************************************************/
  82. /*
  83. * Binary search numElements of size elementSize in array for a match
  84. * to the. item. Return the index of the element that matches
  85. * (0 - numElements - 1). If no match is found return the -i-1 where
  86. * i is the index (0 - numElements) where the item should be placed.
  87. * (*theCmp)(a,b) should return <0 if a<b, 0 if a==b, >0 if a>b.
  88. *
  89. * This function is like the C-Library function bsearch() except that
  90. * this function returns the index where the item should be placed if
  91. * it is not found.
  92. */
  93. int BinSearch ( void *array, int numElements, int elementSize,
  94. void *itemPtr, CompareFunction compareFunction)
  95. {
  96. int low, high, mid, cmp;
  97. void *arrayItemPtr;
  98. for (low = 0, high = numElements - 1, mid = 0, cmp = -1; low <= high;) {
  99. mid = (low + high) >> 1;
  100. arrayItemPtr = (void *) (((char *) array) + (mid * elementSize));
  101. cmp = compareFunction
  102. ? compareFunction (itemPtr, arrayItemPtr)
  103. : ListMemBlockCmp (itemPtr, arrayItemPtr, elementSize);
  104. if (cmp == 0) {
  105. return mid;
  106. } else if (cmp < 0) {
  107. high = mid - 1;
  108. } else {
  109. low = mid + 1;
  110. }
  111. }
  112. if (cmp > 0)
  113. mid++;
  114. return -mid - 1;
  115. }
  116. #endif /* CFG_ALL_LIST_FUNCTIONS */
  117. /*******************************************************************************/
  118. /*
  119. * If numNewItems == 0 then expand the list by the number of items
  120. * indicated by its allocation policy.
  121. * If numNewItems > 0 then expand the list by exactly the number of
  122. * items indicated.
  123. * If numNewItems < 0 then expand the list by the absolute value of
  124. * numNewItems plus the number of items indicated by its allocation
  125. * policy.
  126. * Returns 1 for success, 0 if out of memory
  127. */
  128. static int ExpandListSpace (list_t list, int numNewItems)
  129. {
  130. if (numNewItems == 0) {
  131. numNewItems = NUMITEMSPERALLOC (list);
  132. } else if (numNewItems < 0) {
  133. numNewItems = (-numNewItems) + NUMITEMSPERALLOC (list);
  134. }
  135. if (SetHandleSize ((Handle) list,
  136. sizeof (ListStruct) +
  137. ((*list)->listSize +
  138. numNewItems) * (*list)->itemSize)) {
  139. (*list)->listSize += numNewItems;
  140. return 1;
  141. } else {
  142. return 0;
  143. }
  144. }
  145. /*******************************/
  146. #ifdef CFG_ALL_LIST_FUNCTIONS
  147. /*
  148. * This function reallocate the list, minus any currently unused
  149. * portion of its allotted memory.
  150. */
  151. void ListCompact (list_t list)
  152. {
  153. if (!SetHandleSize ((Handle) list,
  154. sizeof (ListStruct) +
  155. (*list)->numItems * (*list)->itemSize)) {
  156. return;
  157. }
  158. (*list)->listSize = (*list)->numItems;
  159. }
  160. #endif /* CFG_ALL_LIST_FUNCTIONS */
  161. /*******************************/
  162. list_t ListCreate (int elementSize)
  163. {
  164. list_t list;
  165. list = (list_t) (NewHandle (sizeof (ListStruct))); /* create empty list */
  166. if (list) {
  167. (*list)->signature = LIST_SIGNATURE;
  168. (*list)->numItems = 0;
  169. (*list)->listSize = 0;
  170. (*list)->itemSize = elementSize;
  171. (*list)->percentIncrease = kDefaultAllocationPercentIncrease;
  172. (*list)->minNumItemsIncrease =
  173. kDefaultAllocationminNumItemsIncrease;
  174. }
  175. return list;
  176. }
  177. /*******************************/
  178. void ListSetAllocationPolicy (list_t list, int minItemsPerAlloc,
  179. int percentIncreasePerAlloc)
  180. {
  181. (*list)->percentIncrease = percentIncreasePerAlloc;
  182. (*list)->minNumItemsIncrease = minItemsPerAlloc;
  183. }
  184. /*******************************/
  185. void ListDispose (list_t list)
  186. {
  187. DisposeHandle ((Handle) list);
  188. }
  189. /*******************************/
  190. #ifdef CFG_ALL_LIST_FUNCTIONS
  191. void ListDisposePtrList (list_t list)
  192. {
  193. int index;
  194. int numItems;
  195. if (list) {
  196. numItems = ListNumItems (list);
  197. for (index = 1; index <= numItems; index++)
  198. free (*(void **) ListGetPtrToItem (list, index));
  199. ListDispose (list);
  200. }
  201. }
  202. /*******************************/
  203. /*
  204. * keeps memory, resets the number of items to 0
  205. */
  206. void ListClear (list_t list)
  207. {
  208. if (!list)
  209. return;
  210. (*list)->numItems = 0;
  211. }
  212. /*******************************/
  213. /*
  214. * copy is only as large as necessary
  215. */
  216. list_t ListCopy (list_t originalList)
  217. {
  218. list_t tempList = NULL;
  219. int numItems;
  220. if (!originalList)
  221. return NULL;
  222. tempList = ListCreate ((*originalList)->itemSize);
  223. if (tempList) {
  224. numItems = ListNumItems (originalList);
  225. if (!SetHandleSize ((Handle) tempList,
  226. sizeof (ListStruct) +
  227. numItems * (*tempList)->itemSize)) {
  228. ListDispose (tempList);
  229. return NULL;
  230. }
  231. (*tempList)->numItems = (*originalList)->numItems;
  232. (*tempList)->listSize = (*originalList)->numItems;
  233. (*tempList)->itemSize = (*originalList)->itemSize;
  234. (*tempList)->percentIncrease = (*originalList)->percentIncrease;
  235. (*tempList)->minNumItemsIncrease =
  236. (*originalList)->minNumItemsIncrease;
  237. memcpy (ITEMPTR (tempList, 0), ITEMPTR (originalList, 0),
  238. numItems * (*tempList)->itemSize);
  239. }
  240. return tempList;
  241. }
  242. /********************************/
  243. /*
  244. * list1 = list1 + list2
  245. */
  246. int ListAppend (list_t list1, list_t list2)
  247. {
  248. int numItemsL1, numItemsL2;
  249. if (!list2)
  250. return 1;
  251. if (!list1)
  252. return 0;
  253. if ((*list1)->itemSize != (*list2)->itemSize)
  254. return 0;
  255. numItemsL1 = ListNumItems (list1);
  256. numItemsL2 = ListNumItems (list2);
  257. if (numItemsL2 == 0)
  258. return 1;
  259. if (!SetHandleSize ((Handle) list1,
  260. sizeof (ListStruct) + (numItemsL1 + numItemsL2) *
  261. (*list1)->itemSize)) {
  262. return 0;
  263. }
  264. (*list1)->numItems = numItemsL1 + numItemsL2;
  265. (*list1)->listSize = numItemsL1 + numItemsL2;
  266. memmove (ITEMPTR (list1, numItemsL1),
  267. ITEMPTR (list2, 0),
  268. numItemsL2 * (*list2)->itemSize);
  269. return 1;
  270. }
  271. #endif /* CFG_ALL_LIST_FUNCTIONS */
  272. /*******************************/
  273. /*
  274. * returns 1 if the item is inserted, returns 0 if out of memory or
  275. * bad arguments were passed.
  276. */
  277. int ListInsertItem (list_t list, void *ptrToItem, int itemPosition)
  278. {
  279. return ListInsertItems (list, ptrToItem, itemPosition, 1);
  280. }
  281. /*******************************/
  282. int ListInsertItems (list_t list, void *ptrToItems, int firstItemPosition,
  283. int numItemsToInsert)
  284. {
  285. int numItems = (*list)->numItems;
  286. if (firstItemPosition == numItems + 1)
  287. firstItemPosition = LIST_END;
  288. else if (firstItemPosition > numItems)
  289. return 0;
  290. if ((*list)->numItems >= (*list)->listSize) {
  291. if (!ExpandListSpace (list, -numItemsToInsert))
  292. return 0;
  293. }
  294. if (firstItemPosition == LIST_START) {
  295. if (numItems == 0) {
  296. /* special case for empty list */
  297. firstItemPosition = LIST_END;
  298. } else {
  299. firstItemPosition = 1;
  300. }
  301. }
  302. if (firstItemPosition == LIST_END) { /* add at the end of the list */
  303. if (ptrToItems)
  304. memcpy (ITEMPTR (list, numItems), ptrToItems,
  305. (*list)->itemSize * numItemsToInsert);
  306. else
  307. memset (ITEMPTR (list, numItems), 0,
  308. (*list)->itemSize * numItemsToInsert);
  309. (*list)->numItems += numItemsToInsert;
  310. } else { /* move part of list up to make room for new item */
  311. memmove (ITEMPTR (list, firstItemPosition - 1 + numItemsToInsert),
  312. ITEMPTR (list, firstItemPosition - 1),
  313. (numItems + 1 - firstItemPosition) * (*list)->itemSize);
  314. if (ptrToItems)
  315. memmove (ITEMPTR (list, firstItemPosition - 1), ptrToItems,
  316. (*list)->itemSize * numItemsToInsert);
  317. else
  318. memset (ITEMPTR (list, firstItemPosition - 1), 0,
  319. (*list)->itemSize * numItemsToInsert);
  320. (*list)->numItems += numItemsToInsert;
  321. }
  322. return 1;
  323. }
  324. #ifdef CFG_ALL_LIST_FUNCTIONS
  325. /*******************************/
  326. int ListEqual (list_t list1, list_t list2)
  327. {
  328. if (list1 == list2)
  329. return 1;
  330. if (list1 == NULL || list2 == NULL)
  331. return 0;
  332. if ((*list1)->itemSize == (*list1)->itemSize) {
  333. if ((*list1)->numItems == (*list2)->numItems) {
  334. return (memcmp (ITEMPTR (list1, 0), ITEMPTR (list2, 0),
  335. (*list1)->itemSize * (*list1)->numItems) == 0);
  336. }
  337. }
  338. return 0;
  339. }
  340. /*******************************/
  341. /*
  342. * The item pointed to by ptrToItem is copied over the current item
  343. * at itemPosition
  344. */
  345. void ListReplaceItem (list_t list, void *ptrToItem, int itemPosition)
  346. {
  347. ListReplaceItems (list, ptrToItem, itemPosition, 1);
  348. }
  349. /*******************************/
  350. /*
  351. * The item pointed to by ptrToItems is copied over the current item
  352. * at itemPosition
  353. */
  354. void ListReplaceItems ( list_t list, void *ptrToItems,
  355. int firstItemPosition, int numItemsToReplace)
  356. {
  357. if (firstItemPosition == LIST_END)
  358. firstItemPosition = (*list)->numItems;
  359. else if (firstItemPosition == LIST_START)
  360. firstItemPosition = 1;
  361. memmove (ITEMPTR (list, firstItemPosition - 1), ptrToItems,
  362. (*list)->itemSize * numItemsToReplace);
  363. }
  364. /*******************************/
  365. void ListGetItem (list_t list, void *itemDestination, int itemPosition)
  366. {
  367. ListGetItems (list, itemDestination, itemPosition, 1);
  368. }
  369. #endif /* CFG_ALL_LIST_FUNCTIONS */
  370. /*******************************/
  371. #if defined(CFG_ALL_LIST_FUNCTIONS) || defined(CFG_DEVICE_DEREGISTER)
  372. void ListRemoveItem (list_t list, void *itemDestination, int itemPosition)
  373. {
  374. ListRemoveItems (list, itemDestination, itemPosition, 1);
  375. }
  376. /*******************************/
  377. void ListRemoveItems (list_t list, void *itemsDestination,
  378. int firstItemPosition, int numItemsToRemove)
  379. {
  380. int firstItemAfterChunk, numToMove;
  381. if (firstItemPosition == LIST_START)
  382. firstItemPosition = 1;
  383. else if (firstItemPosition == LIST_END)
  384. firstItemPosition = (*list)->numItems;
  385. if (itemsDestination != NULL)
  386. memcpy (itemsDestination, ITEMPTR (list, firstItemPosition - 1),
  387. (*list)->itemSize * numItemsToRemove);
  388. firstItemAfterChunk = firstItemPosition + numItemsToRemove;
  389. numToMove = (*list)->numItems - (firstItemAfterChunk - 1);
  390. if (numToMove > 0) {
  391. /*
  392. * move part of list down to cover hole left by removed item
  393. */
  394. memmove (ITEMPTR (list, firstItemPosition - 1),
  395. ITEMPTR (list, firstItemAfterChunk - 1),
  396. (*list)->itemSize * numToMove);
  397. }
  398. (*list)->numItems -= numItemsToRemove;
  399. }
  400. #endif /* CFG_ALL_LIST_FUNCTIONS || CFG_DEVICE_DEREGISTER */
  401. /*******************************/
  402. void ListGetItems (list_t list, void *itemsDestination,
  403. int firstItemPosition, int numItemsToGet)
  404. {
  405. if (firstItemPosition == LIST_START)
  406. firstItemPosition = 1;
  407. else if (firstItemPosition == LIST_END)
  408. firstItemPosition = (*list)->numItems;
  409. memcpy (itemsDestination,
  410. ITEMPTR (list, firstItemPosition - 1),
  411. (*list)->itemSize * numItemsToGet);
  412. }
  413. /*******************************/
  414. /*
  415. * Returns a pointer to the item at itemPosition. returns null if an
  416. * errors occurred.
  417. */
  418. void *ListGetPtrToItem (list_t list, int itemPosition)
  419. {
  420. if (itemPosition == LIST_START)
  421. itemPosition = 1;
  422. else if (itemPosition == LIST_END)
  423. itemPosition = (*list)->numItems;
  424. return ITEMPTR (list, itemPosition - 1);
  425. }
  426. /*******************************/
  427. /*
  428. * returns a pointer the lists data (abstraction violation for
  429. * optimization)
  430. */
  431. void *ListGetDataPtr (list_t list)
  432. {
  433. return &((*list)->itemList[0]);
  434. }
  435. /********************************/
  436. #ifdef CFG_ALL_LIST_FUNCTIONS
  437. int ListApplyToEach (list_t list, int ascending,
  438. ListApplicationFunc funcToApply,
  439. void *callbackData)
  440. {
  441. int result = 0, index;
  442. if (!list || !funcToApply)
  443. goto Error;
  444. if (ascending) {
  445. for (index = 1; index <= ListNumItems (list); index++) {
  446. result = funcToApply (index,
  447. ListGetPtrToItem (list, index),
  448. callbackData);
  449. if (result < 0)
  450. goto Error;
  451. }
  452. } else {
  453. for (index = ListNumItems (list);
  454. index > 0 && index <= ListNumItems (list);
  455. index--) {
  456. result = funcToApply (index,
  457. ListGetPtrToItem (list, index),
  458. callbackData);
  459. if (result < 0)
  460. goto Error;
  461. }
  462. }
  463. Error:
  464. return result;
  465. }
  466. #endif /* CFG_ALL_LIST_FUNCTIONS */
  467. /********************************/
  468. int ListGetItemSize (list_t list)
  469. {
  470. return (*list)->itemSize;
  471. }
  472. /********************************/
  473. int ListNumItems (list_t list)
  474. {
  475. return (*list)->numItems;
  476. }
  477. /*******************************/
  478. #ifdef CFG_ALL_LIST_FUNCTIONS
  479. void ListRemoveDuplicates (list_t list, CompareFunction compareFunction)
  480. {
  481. int numItems, index, startIndexForFind, duplicatesIndex;
  482. numItems = ListNumItems (list);
  483. for (index = 1; index < numItems; index++) {
  484. startIndexForFind = index + 1;
  485. while (startIndexForFind <= numItems) {
  486. duplicatesIndex =
  487. ListFindItem (list,
  488. ListGetPtrToItem (list, index),
  489. startIndexForFind,
  490. compareFunction);
  491. if (duplicatesIndex > 0) {
  492. ListRemoveItem (list, NULL, duplicatesIndex);
  493. numItems--;
  494. startIndexForFind = duplicatesIndex;
  495. } else {
  496. break;
  497. }
  498. }
  499. }
  500. }
  501. /*******************************/
  502. /*******************************/
  503. int ListFindItem (list_t list, void *ptrToItem, int startingPosition,
  504. CompareFunction compareFunction)
  505. {
  506. int numItems, size, index, cmp;
  507. void *listItemPtr;
  508. if ((numItems = (*list)->numItems) == 0)
  509. return 0;
  510. size = (*list)->itemSize;
  511. if (startingPosition == LIST_START)
  512. startingPosition = 1;
  513. else if (startingPosition == LIST_END)
  514. startingPosition = numItems;
  515. for (index = startingPosition; index <= numItems; index++) {
  516. listItemPtr = ITEMPTR (list, index - 1);
  517. cmp = compareFunction
  518. ? compareFunction (ptrToItem, listItemPtr)
  519. : ListMemBlockCmp (ptrToItem, listItemPtr, size);
  520. if (cmp == 0)
  521. return index;
  522. }
  523. return 0;
  524. }
  525. /*******************************/
  526. int ShortCompare (void *a, void *b)
  527. {
  528. if (*(short *) a < *(short *) b)
  529. return -1;
  530. if (*(short *) a > *(short *) b)
  531. return 1;
  532. return 0;
  533. }
  534. /*******************************/
  535. int IntCompare (void *a, void *b)
  536. {
  537. if (*(int *) a < *(int *) b)
  538. return -1;
  539. if (*(int *) a > *(int *) b)
  540. return 1;
  541. return 0;
  542. }
  543. /*******************************/
  544. int CStringCompare (void *a, void *b)
  545. {
  546. return strcmp (*(char **) a, *(char **) b);
  547. }
  548. /*******************************/
  549. int ListBinSearch (list_t list, void *ptrToItem,
  550. CompareFunction compareFunction)
  551. {
  552. int index;
  553. index = BinSearch (ITEMPTR (list, 0),
  554. (int) (*list)->numItems,
  555. (int) (*list)->itemSize, ptrToItem,
  556. compareFunction);
  557. if (index >= 0)
  558. index++; /* lists start from 1 */
  559. else
  560. index = 0; /* item not found */
  561. return index;
  562. }
  563. /**************************************************************************/
  564. /*
  565. * Reserves memory for numItems in the list. If it succeeds then
  566. * numItems items can be inserted without possibility of an out of
  567. * memory error (useful to simplify error recovery in complex
  568. * functions). Returns 1 if success, 0 if out of memory.
  569. */
  570. int ListPreAllocate (list_t list, int numItems)
  571. {
  572. if ((*list)->listSize - (*list)->numItems < numItems) {
  573. return ExpandListSpace (list,
  574. numItems - ((*list)->listSize -
  575. (*list)->numItems));
  576. } else {
  577. return 1; /* enough items are already pre-allocated */
  578. }
  579. }
  580. #endif /* CFG_ALL_LIST_FUNCTIONS */