simclist.h 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971
  1. /*
  2. * Copyright (c) 2007,2008 Mij <mij@bitchx.it>
  3. *
  4. * Permission to use, copy, modify, and distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. /*
  17. * SimCList library. See http://mij.oltrelinux.com/devel/simclist
  18. */
  19. #ifndef SIMCLIST_H
  20. #define SIMCLIST_H
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. #include <inttypes.h>
  25. #include <errno.h>
  26. #include <sys/types.h>
  27. /* Be friend of both C90 and C99 compilers */
  28. #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
  29. /* "inline" and "restrict" are keywords */
  30. #else
  31. # define inline /* inline */
  32. # define restrict /* restrict */
  33. #endif
  34. /**
  35. * Type representing list hashes.
  36. *
  37. * This is a signed integer value.
  38. */
  39. typedef int32_t list_hash_t;
  40. #ifndef SIMCLIST_NO_DUMPRESTORE
  41. typedef struct {
  42. uint16_t version; /* dump version */
  43. int64_t timestamp; /* when the list has been dumped, microseconds from UNIX epoch */
  44. uint32_t list_size;
  45. uint32_t list_numels;
  46. list_hash_t list_hash; /* hash of the list when dumped, or 0 if invalid */
  47. uint32_t dumpsize;
  48. int consistent; /* 1 if the dump is verified complete/consistent; 0 otherwise */
  49. } list_dump_info_t;
  50. #endif
  51. /**
  52. * a comparator of elements.
  53. *
  54. * A comparator of elements is a function that:
  55. * -# receives two references to elements a and b
  56. * -# returns {<0, 0, >0} if (a > b), (a == b), (a < b) respectively
  57. *
  58. * It is responsability of the function to handle possible NULL values.
  59. */
  60. typedef int (*element_comparator)(const void *a, const void *b);
  61. /**
  62. * a seeker of elements.
  63. *
  64. * An element seeker is a function that:
  65. * -# receives a reference to an element el
  66. * -# receives a reference to some indicator data
  67. * -# returns non-0 if the element matches the indicator, 0 otherwise
  68. *
  69. * It is responsability of the function to handle possible NULL values in any
  70. * argument.
  71. */
  72. typedef int (*element_seeker)(const void *el, const void *indicator);
  73. /**
  74. * an element lenght meter.
  75. *
  76. * An element meter is a function that:
  77. * -# receives the reference to an element el
  78. * -# returns its size in bytes
  79. *
  80. * It is responsability of the function to handle possible NULL values.
  81. */
  82. typedef size_t (*element_meter)(const void *el);
  83. /**
  84. * a function computing the hash of elements.
  85. *
  86. * An hash computing function is a function that:
  87. * -# receives the reference to an element el
  88. * -# returns a hash value for el
  89. *
  90. * It is responsability of the function to handle possible NULL values.
  91. */
  92. typedef list_hash_t (*element_hash_computer)(const void *el);
  93. /**
  94. * a function for serializing an element.
  95. *
  96. * A serializer function is one that gets a reference to an element,
  97. * and returns a reference to a buffer that contains its serialization
  98. * along with the length of this buffer.
  99. * It is responsability of the function to handle possible NULL values,
  100. * returning a NULL buffer and a 0 buffer length.
  101. *
  102. * These functions have 3 goals:
  103. * -# "freeze" and "flatten" the memory representation of the element
  104. * -# provide a portable (wrt byte order, or type size) representation of the element, if the dump can be used on different sw/hw combinations
  105. * -# possibly extract a compressed representation of the element
  106. *
  107. * @param el reference to the element data
  108. * @param serialize_buffer reference to fill with the length of the buffer
  109. * @return reference to the buffer with the serialized data
  110. */
  111. typedef void *(*element_serializer)(const void *restrict el, uint32_t *restrict serializ_len);
  112. /**
  113. * a function for un-serializing an element.
  114. *
  115. * An unserializer function accomplishes the inverse operation of the
  116. * serializer function. An unserializer function is one that gets a
  117. * serialized representation of an element and turns it backe to the original
  118. * element. The serialized representation is passed as a reference to a buffer
  119. * with its data, and the function allocates and returns the buffer containing
  120. * the original element, and it sets the length of this buffer into the
  121. * integer passed by reference.
  122. *
  123. * @param data reference to the buffer with the serialized representation of the element
  124. * @param data_len reference to the location where to store the length of the data in the buffer returned
  125. * @return reference to a buffer with the original, unserialized representation of the element
  126. */
  127. typedef void *(*element_unserializer)(const void *restrict data, uint32_t *restrict data_len);
  128. /* [private-use] list entry -- olds actual user datum */
  129. struct list_entry_s {
  130. void *data;
  131. /* doubly-linked list service references */
  132. struct list_entry_s *next;
  133. struct list_entry_s *prev;
  134. };
  135. /* [private-use] list attributes */
  136. struct list_attributes_s {
  137. /* user-set routine for comparing list elements */
  138. element_comparator comparator;
  139. /* user-set routing for seeking elements */
  140. element_seeker seeker;
  141. /* user-set routine for determining the length of an element */
  142. element_meter meter;
  143. int copy_data;
  144. /* user-set routine for computing the hash of an element */
  145. element_hash_computer hasher;
  146. /* user-set routine for serializing an element */
  147. element_serializer serializer;
  148. /* user-set routine for unserializing an element */
  149. element_unserializer unserializer;
  150. };
  151. /** list object */
  152. typedef struct {
  153. struct list_entry_s *head_sentinel;
  154. struct list_entry_s *tail_sentinel;
  155. struct list_entry_s *mid;
  156. unsigned int numels;
  157. /* array of spare elements */
  158. struct list_entry_s **spareels;
  159. unsigned int spareelsnum;
  160. #ifdef SIMCLIST_WITH_THREADS
  161. /* how many threads are currently running */
  162. unsigned int threadcount;
  163. #endif
  164. /* service variables for list iteration */
  165. int iter_active;
  166. unsigned int iter_pos;
  167. struct list_entry_s *iter_curentry;
  168. /* list attributes */
  169. struct list_attributes_s attrs;
  170. } list_t;
  171. /**
  172. * initialize a list object for use.
  173. *
  174. * @param l must point to a user-provided memory location
  175. * @return 0 for success. -1 for failure
  176. */
  177. int list_init(list_t *restrict l);
  178. /**
  179. * completely remove the list from memory.
  180. *
  181. * This function is the inverse of list_init(). It is meant to be called when
  182. * the list is no longer going to be used. Elements and possible memory taken
  183. * for internal use are freed.
  184. *
  185. * @param l list to destroy
  186. */
  187. void list_destroy(list_t *restrict l);
  188. /**
  189. * set the comparator function for list elements.
  190. *
  191. * Comparator functions are used for searching and sorting. If NULL is passed
  192. * as reference to the function, the comparator is disabled.
  193. *
  194. * @param l list to operate
  195. * @param comparator_fun pointer to the actual comparator function
  196. * @return 0 if the attribute was successfully set; -1 otherwise
  197. *
  198. * @see element_comparator()
  199. */
  200. int list_attributes_comparator(list_t *restrict l, element_comparator comparator_fun);
  201. /**
  202. * set a seeker function for list elements.
  203. *
  204. * Seeker functions are used for finding elements. If NULL is passed as reference
  205. * to the function, the seeker is disabled.
  206. *
  207. * @param l list to operate
  208. * @param seeker_fun pointer to the actual seeker function
  209. * @return 0 if the attribute was successfully set; -1 otherwise
  210. *
  211. * @see element_seeker()
  212. */
  213. int list_attributes_seeker(list_t *restrict l, element_seeker seeker_fun);
  214. /**
  215. * require to free element data when list entry is removed (default: don't free).
  216. *
  217. * [ advanced preference ]
  218. *
  219. * By default, when an element is removed from the list, it disappears from
  220. * the list by its actual data is not free()d. With this option, every
  221. * deletion causes element data to be freed.
  222. *
  223. * It is responsability of this function to correctly handle NULL values, if
  224. * NULL elements are inserted into the list.
  225. *
  226. * @param l list to operate
  227. * @param metric_fun pointer to the actual metric function
  228. * @param copy_data 0: do not free element data (default); non-0: do free
  229. * @return 0 if the attribute was successfully set; -1 otherwise
  230. *
  231. * @see element_meter()
  232. * @see list_meter_int8_t()
  233. * @see list_meter_int16_t()
  234. * @see list_meter_int32_t()
  235. * @see list_meter_int64_t()
  236. * @see list_meter_uint8_t()
  237. * @see list_meter_uint16_t()
  238. * @see list_meter_uint32_t()
  239. * @see list_meter_uint64_t()
  240. * @see list_meter_float()
  241. * @see list_meter_double()
  242. * @see list_meter_string()
  243. */
  244. int list_attributes_copy(list_t *restrict l, element_meter metric_fun, int copy_data);
  245. /**
  246. * set the element hash computing function for the list elements.
  247. *
  248. * [ advanced preference ]
  249. *
  250. * An hash can be requested depicting the list status at a given time. An hash
  251. * only depends on the elements and their order. By default, the hash of an
  252. * element is only computed on its reference. With this function, the user can
  253. * set a custom function computing the hash of an element. If such function is
  254. * provided, the list_hash() function automatically computes the list hash using
  255. * the custom function instead of simply referring to element references.
  256. *
  257. * @param l list to operate
  258. * @param hash_computer_fun pointer to the actual hash computing function
  259. * @return 0 if the attribute was successfully set; -1 otherwise
  260. *
  261. * @see element_hash_computer()
  262. */
  263. int list_attributes_hash_computer(list_t *restrict l, element_hash_computer hash_computer_fun);
  264. /**
  265. * set the element serializer function for the list elements.
  266. *
  267. * [ advanced preference ]
  268. *
  269. * Serialize functions are used for dumping the list to some persistent
  270. * storage. The serializer function is called for each element; it is passed
  271. * a reference to the element and a reference to a size_t object. It will
  272. * provide (and return) the buffer with the serialization of the element and
  273. * fill the size_t object with the length of this serialization data.
  274. *
  275. * @param l list to operate
  276. * @param serializer_fun pointer to the actual serializer function
  277. * @return 0 if the attribute was successfully set; -1 otherwise
  278. *
  279. * @see element_serializer()
  280. * @see list_dump_filedescriptor()
  281. * @see list_restore_filedescriptor()
  282. */
  283. int list_attributes_serializer(list_t *restrict l, element_serializer serializer_fun);
  284. /**
  285. * set the element unserializer function for the list elements.
  286. *
  287. * [ advanced preference ]
  288. *
  289. * Unserialize functions are used for restoring the list from some persistent
  290. * storage. The unserializer function is called for each element segment read
  291. * from the storage; it is passed the segment and a reference to an integer.
  292. * It shall allocate and return a buffer compiled with the resumed memory
  293. * representation of the element, and set the integer value to the length of
  294. * this buffer.
  295. *
  296. * @param l list to operate
  297. * @param unserializer_fun pointer to the actual unserializer function
  298. * @return 0 if the attribute was successfully set; -1 otherwise
  299. *
  300. * @see element_unserializer()
  301. * @see list_dump_filedescriptor()
  302. * @see list_restore_filedescriptor()
  303. */
  304. int list_attributes_unserializer(list_t *restrict l, element_unserializer unserializer_fun);
  305. /**
  306. * append data at the end of the list.
  307. *
  308. * This function is useful for adding elements with a FIFO/queue policy.
  309. *
  310. * @param l list to operate
  311. * @param data pointer to user data to append
  312. *
  313. * @return 1 for success. < 0 for failure
  314. */
  315. int list_append(list_t *restrict l, const void *data);
  316. /**
  317. * insert data in the head of the list.
  318. *
  319. * This function is useful for adding elements with a LIFO/Stack policy.
  320. *
  321. * @param l list to operate
  322. * @param data pointer to user data to append
  323. *
  324. * @return 1 for success. < 0 for failure
  325. */
  326. int list_prepend(list_t *restrict l, const void *restrict data);
  327. /**
  328. * extract the element in the top of the list.
  329. *
  330. * This function is for using a list with a FIFO/queue policy.
  331. *
  332. * @param l list to operate
  333. * @return reference to user datum, or NULL on errors
  334. */
  335. void *list_fetch(list_t *restrict l);
  336. /**
  337. * retrieve an element at a given position.
  338. *
  339. * @param l list to operate
  340. * @param pos [0,size-1] position index of the element wanted
  341. * @return reference to user datum, or NULL on errors
  342. */
  343. void *list_get_at(const list_t *restrict l, unsigned int pos);
  344. /**
  345. * return the maximum element of the list.
  346. *
  347. * @warning Requires a comparator function to be set for the list.
  348. *
  349. * Returns the maximum element with respect to the comparator function output.
  350. *
  351. * @see list_attributes_comparator()
  352. *
  353. * @param l list to operate
  354. * @return the reference to the element, or NULL
  355. */
  356. void *list_get_max(const list_t *restrict l);
  357. /**
  358. * return the minimum element of the list.
  359. *
  360. * @warning Requires a comparator function to be set for the list.
  361. *
  362. * Returns the minimum element with respect to the comparator function output.
  363. *
  364. * @see list_attributes_comparator()
  365. *
  366. * @param l list to operate
  367. * @return the reference to the element, or NULL
  368. */
  369. void *list_get_min(const list_t *restrict l);
  370. /**
  371. * retrieve and remove from list an element at a given position.
  372. *
  373. * @param l list to operate
  374. * @param pos [0,size-1] position index of the element wanted
  375. * @return reference to user datum, or NULL on errors
  376. */
  377. void *list_extract_at(list_t *restrict l, unsigned int pos);
  378. /**
  379. * insert an element at a given position.
  380. *
  381. * @param l list to operate
  382. * @param data reference to data to be inserted
  383. * @param pos [0,size-1] position index to insert the element at
  384. * @return positive value on success. Negative on failure
  385. */
  386. int list_insert_at(list_t *restrict l, const void *data, unsigned int pos);
  387. /**
  388. * expunge the first found given element from the list.
  389. *
  390. * Inspects the given list looking for the given element; if the element
  391. * is found, it is removed. Only the first occurence is removed.
  392. * If a comparator function was not set, elements are compared by reference.
  393. * Otherwise, the comparator is used to match the element.
  394. *
  395. * @param l list to operate
  396. * @param data reference of the element to search for
  397. * @return 0 on success. Negative value on failure
  398. *
  399. * @see list_attributes_comparator()
  400. * @see list_delete_at()
  401. */
  402. int list_delete(list_t *restrict l, const void *data);
  403. /**
  404. * expunge an element at a given position from the list.
  405. *
  406. * @param l list to operate
  407. * @param pos [0,size-1] position index of the element to be deleted
  408. * @return 0 on success. Negative value on failure
  409. */
  410. int list_delete_at(list_t *restrict l, unsigned int pos);
  411. /**
  412. * expunge an array of elements from the list, given their position range.
  413. *
  414. * @param l list to operate
  415. * @param posstart [0,size-1] position index of the first element to be deleted
  416. * @param posend [posstart,size-1] position of the last element to be deleted
  417. * @return the number of elements successfully removed
  418. */
  419. int list_delete_range(list_t *restrict l, unsigned int posstart, unsigned int posend);
  420. /**
  421. * clear all the elements off of the list.
  422. *
  423. * The element datums will not be freed.
  424. *
  425. * @see list_delete_range()
  426. * @see list_size()
  427. *
  428. * @param l list to operate
  429. * @return the number of elements in the list before cleaning
  430. */
  431. int list_clear(list_t *restrict l);
  432. /**
  433. * inspect the number of elements in the list.
  434. *
  435. * @param l list to operate
  436. * @return number of elements currently held by the list
  437. */
  438. unsigned int list_size(const list_t *restrict l);
  439. /**
  440. * inspect whether the list is empty.
  441. *
  442. * @param l list to operate
  443. * @return 0 iff the list is not empty
  444. *
  445. * @see list_size()
  446. */
  447. int list_empty(const list_t *restrict l);
  448. /**
  449. * find the position of an element in a list.
  450. *
  451. * @warning Requires a comparator function to be set for the list.
  452. *
  453. * Inspects the given list looking for the given element; if the element
  454. * is found, its position into the list is returned.
  455. * Elements are inspected comparing references if a comparator has not been
  456. * set. Otherwise, the comparator is used to find the element.
  457. *
  458. * @param l list to operate
  459. * @param data reference of the element to search for
  460. * @return position of element in the list, or <0 if not found
  461. *
  462. * @see list_attributes_comparator()
  463. * @see list_get_at()
  464. */
  465. int list_locate(const list_t *restrict l, const void *data);
  466. /**
  467. * returns an element given an indicator.
  468. *
  469. * @warning Requires a seeker function to be set for the list.
  470. *
  471. * Inspect the given list looking with the seeker if an element matches
  472. * an indicator. If such element is found, the reference to the element
  473. * is returned.
  474. *
  475. * @param l list to operate
  476. * @param indicator indicator data to pass to the seeker along with elements
  477. * @return reference to the element accepted by the seeker, or NULL if none found
  478. */
  479. void *list_seek(list_t *restrict l, const void *indicator);
  480. /**
  481. * inspect whether some data is member of the list.
  482. *
  483. * @warning Requires a comparator function to be set for the list.
  484. *
  485. * By default, a per-reference comparison is accomplished. That is,
  486. * the data is in list if any element of the list points to the same
  487. * location of data.
  488. * A "semantic" comparison is accomplished, otherwise, if a comparator
  489. * function has been set previously, with list_attributes_comparator();
  490. * in which case, the given data reference is believed to be in list iff
  491. * comparator_fun(elementdata, userdata) == 0 for any element in the list.
  492. *
  493. * @param l list to operate
  494. * @param data reference to the data to search
  495. * @return 0 iff the list does not contain data as an element
  496. *
  497. * @see list_attributes_comparator()
  498. */
  499. int list_contains(const list_t *restrict l, const void *data);
  500. /**
  501. * concatenate two lists
  502. *
  503. * Concatenates one list with another, and stores the result into a
  504. * user-provided list object, which must be different from both the
  505. * lists to concatenate. Attributes from the original lists are not
  506. * cloned.
  507. * The destination list referred is threated as virgin room: if it
  508. * is an existing list containing elements, memory leaks will happen.
  509. * It is OK to specify the same list twice as source, for "doubling"
  510. * it in the destination.
  511. *
  512. * @param l1 base list
  513. * @param l2 list to append to the base
  514. * @param dest reference to the destination list
  515. * @return 0 for success, -1 for errors
  516. */
  517. int list_concat(const list_t *l1, const list_t *l2, list_t *restrict dest);
  518. /**
  519. * sort list elements.
  520. *
  521. * @warning Requires a comparator function to be set for the list.
  522. *
  523. * Sorts the list in ascending or descending order as specified by the versus
  524. * flag. The algorithm chooses autonomously what algorithm is best suited for
  525. * sorting the list wrt its current status.
  526. *
  527. * @param l list to operate
  528. * @param versus positive: order small to big; negative: order big to small
  529. * @return 0: sorting went OK non-0: errors happened
  530. *
  531. * @see list_attributes_comparator()
  532. */
  533. int list_sort(list_t *restrict l, int versus);
  534. /**
  535. * start an iteration session.
  536. *
  537. * This function prepares the list to be iterated.
  538. *
  539. * @param l list to operate
  540. * @return 0 if the list cannot be currently iterated. >0 otherwise
  541. *
  542. * @see list_iterator_stop()
  543. */
  544. int list_iterator_start(list_t *restrict l);
  545. /**
  546. * return the next element in the iteration session.
  547. *
  548. * @param l list to operate
  549. * @return element datum, or NULL on errors
  550. */
  551. void *list_iterator_next(list_t *restrict l);
  552. /**
  553. * inspect whether more elements are available in the iteration session.
  554. *
  555. * @param l list to operate
  556. * @return 0 iff no more elements are available.
  557. */
  558. int list_iterator_hasnext(const list_t *restrict l);
  559. /**
  560. * end an iteration session.
  561. *
  562. * @param l list to operate
  563. * @return 0 iff the iteration session cannot be stopped
  564. */
  565. int list_iterator_stop(list_t *restrict l);
  566. /**
  567. * return the hash of the current status of the list.
  568. *
  569. * @param l list to operate
  570. * @param hash where the resulting hash is put
  571. *
  572. * @return 0 for success; <0 for failure
  573. */
  574. int list_hash(const list_t *restrict l, list_hash_t *restrict hash);
  575. #ifndef SIMCLIST_NO_DUMPRESTORE
  576. /**
  577. * get meta informations on a list dump on filedescriptor.
  578. *
  579. * [ advanced function ]
  580. *
  581. * Extracts the meta information from a SimCList dump located in a file
  582. * descriptor. The file descriptor must be open and positioned at the
  583. * beginning of the SimCList dump block.
  584. *
  585. * @param fd file descriptor to get metadata from
  586. * @param info reference to a dump metainformation structure to fill
  587. * @return 0 for success; <0 for failure
  588. *
  589. * @see list_dump_filedescriptor()
  590. */
  591. int list_dump_getinfo_filedescriptor(int fd, list_dump_info_t *restrict info);
  592. /**
  593. * get meta informations on a list dump on file.
  594. *
  595. * [ advanced function ]
  596. *
  597. * Extracts the meta information from a SimCList dump located in a file.
  598. *
  599. * @param filename filename of the file to fetch from
  600. * @param info reference to a dump metainformation structure to fill
  601. * @return 0 for success; <0 for failure
  602. *
  603. * @see list_dump_filedescriptor()
  604. */
  605. int list_dump_getinfo_file(const char *restrict filename, list_dump_info_t *restrict info);
  606. /**
  607. * dump the list into an open, writable file descriptor.
  608. *
  609. * This function "dumps" the list to a persistent storage so it can be
  610. * preserved across process terminations.
  611. * When called, the file descriptor must be open for writing and positioned
  612. * where the serialized data must begin. It writes its serialization of the
  613. * list in a form which is portable across different architectures. Dump can
  614. * be safely performed on stream-only (non seekable) descriptors. The file
  615. * descriptor is not closed at the end of the operations.
  616. *
  617. * To use dump functions, either of these conditions must be satisfied:
  618. * -# a metric function has been specified with list_attributes_copy()
  619. * -# a serializer function has been specified with list_attributes_serializer()
  620. *
  621. * If a metric function has been specified, each element of the list is dumped
  622. * as-is from memory, copying it from its pointer for its length down to the
  623. * file descriptor. This might have impacts on portability of the dump to
  624. * different architectures.
  625. *
  626. * If a serializer function has been specified, its result for each element is
  627. * dumped to the file descriptor.
  628. *
  629. *
  630. * @param l list to operate
  631. * @param fd file descriptor to write to
  632. * @param len location to store the resulting length of the dump (bytes), or NULL
  633. *
  634. * @return 0 if successful; -1 otherwise
  635. *
  636. * @see element_serializer()
  637. * @see list_attributes_copy()
  638. * @see list_attributes_serializer()
  639. */
  640. int list_dump_filedescriptor(const list_t *restrict l, int fd, size_t *restrict len);
  641. /**
  642. * dump the list to a file name.
  643. *
  644. * This function creates a filename and dumps the current content of the list
  645. * to it. If the file exists it is overwritten. The number of bytes written to
  646. * the file can be returned in a specified argument.
  647. *
  648. * @param l list to operate
  649. * @param filename filename to write to
  650. * @param len location to store the resulting length of the dump (bytes), or NULL
  651. *
  652. * @return 0 if successful; -1 otherwise
  653. *
  654. * @see list_attributes_copy()
  655. * @see element_serializer()
  656. * @see list_attributes_serializer()
  657. * @see list_dump_filedescriptor()
  658. * @see list_restore_file()
  659. *
  660. * This function stores a representation of the list
  661. */
  662. int list_dump_file(const list_t *restrict l, const char *restrict filename, size_t *restrict len);
  663. /**
  664. * restore the list from an open, readable file descriptor to memory.
  665. *
  666. * This function is the "inverse" of list_dump_filedescriptor(). It restores
  667. * the list content from a (open, read-ready) file descriptor to memory. An
  668. * unserializer might be needed to restore elements from the persistent
  669. * representation back into memory-consistent format. List attributes can not
  670. * be restored and must be set manually.
  671. *
  672. * @see list_dump_filedescriptor()
  673. * @see list_attributes_serializer()
  674. * @see list_attributes_unserializer()
  675. *
  676. * @param l list to restore to
  677. * @param fd file descriptor to read from.
  678. * @param len location to store the length of the dump read (bytes), or NULL
  679. * @return 0 if successful; -1 otherwise
  680. */
  681. int list_restore_filedescriptor(list_t *restrict l, int fd, size_t *restrict len);
  682. /**
  683. * restore the list from a file name.
  684. *
  685. * This function restores the content of a list from a file into memory. It is
  686. * the inverse of list_dump_file().
  687. *
  688. * @see element_unserializer()
  689. * @see list_attributes_unserializer()
  690. * @see list_dump_file()
  691. * @see list_restore_filedescriptor()
  692. *
  693. * @param l list to restore to
  694. * @param filename filename to read data from
  695. * @param len location to store the length of the dump read (bytes), or NULL
  696. * @return 0 if successful; -1 otherwise
  697. */
  698. int list_restore_file(list_t *restrict l, const char *restrict filename, size_t *len);
  699. #endif
  700. /* ready-made comparators, meters and hash computers */
  701. /* comparator functions */
  702. /**
  703. * ready-made comparator for int8_t elements.
  704. * @see list_attributes_comparator()
  705. */
  706. int list_comparator_int8_t(const void *a, const void *b);
  707. /**
  708. * ready-made comparator for int16_t elements.
  709. * @see list_attributes_comparator()
  710. */
  711. int list_comparator_int16_t(const void *a, const void *b);
  712. /**
  713. * ready-made comparator for int32_t elements.
  714. * @see list_attributes_comparator()
  715. */
  716. int list_comparator_int32_t(const void *a, const void *b);
  717. /**
  718. * ready-made comparator for int64_t elements.
  719. * @see list_attributes_comparator()
  720. */
  721. int list_comparator_int64_t(const void *a, const void *b);
  722. /**
  723. * ready-made comparator for uint8_t elements.
  724. * @see list_attributes_comparator()
  725. */
  726. int list_comparator_uint8_t(const void *a, const void *b);
  727. /**
  728. * ready-made comparator for uint16_t elements.
  729. * @see list_attributes_comparator()
  730. */
  731. int list_comparator_uint16_t(const void *a, const void *b);
  732. /**
  733. * ready-made comparator for uint32_t elements.
  734. * @see list_attributes_comparator()
  735. */
  736. int list_comparator_uint32_t(const void *a, const void *b);
  737. /**
  738. * ready-made comparator for uint64_t elements.
  739. * @see list_attributes_comparator()
  740. */
  741. int list_comparator_uint64_t(const void *a, const void *b);
  742. /**
  743. * ready-made comparator for float elements.
  744. * @see list_attributes_comparator()
  745. */
  746. int list_comparator_float(const void *a, const void *b);
  747. /**
  748. * ready-made comparator for double elements.
  749. * @see list_attributes_comparator()
  750. */
  751. int list_comparator_double(const void *a, const void *b);
  752. /**
  753. * ready-made comparator for string elements.
  754. * @see list_attributes_comparator()
  755. */
  756. int list_comparator_string(const void *a, const void *b);
  757. /* metric functions */
  758. /**
  759. * ready-made metric function for int8_t elements.
  760. * @see list_attributes_copy()
  761. */
  762. size_t list_meter_int8_t(const void *el);
  763. /**
  764. * ready-made metric function for int16_t elements.
  765. * @see list_attributes_copy()
  766. */
  767. size_t list_meter_int16_t(const void *el);
  768. /**
  769. * ready-made metric function for int32_t elements.
  770. * @see list_attributes_copy()
  771. */
  772. size_t list_meter_int32_t(const void *el);
  773. /**
  774. * ready-made metric function for int64_t elements.
  775. * @see list_attributes_copy()
  776. */
  777. size_t list_meter_int64_t(const void *el);
  778. /**
  779. * ready-made metric function for uint8_t elements.
  780. * @see list_attributes_copy()
  781. */
  782. size_t list_meter_uint8_t(const void *el);
  783. /**
  784. * ready-made metric function for uint16_t elements.
  785. * @see list_attributes_copy()
  786. */
  787. size_t list_meter_uint16_t(const void *el);
  788. /**
  789. * ready-made metric function for uint32_t elements.
  790. * @see list_attributes_copy()
  791. */
  792. size_t list_meter_uint32_t(const void *el);
  793. /**
  794. * ready-made metric function for uint64_t elements.
  795. * @see list_attributes_copy()
  796. */
  797. size_t list_meter_uint64_t(const void *el);
  798. /**
  799. * ready-made metric function for float elements.
  800. * @see list_attributes_copy()
  801. */
  802. size_t list_meter_float(const void *el);
  803. /**
  804. * ready-made metric function for double elements.
  805. * @see list_attributes_copy()
  806. */
  807. size_t list_meter_double(const void *el);
  808. /**
  809. * ready-made metric function for string elements.
  810. * @see list_attributes_copy()
  811. */
  812. size_t list_meter_string(const void *el);
  813. /* hash functions */
  814. /**
  815. * ready-made hash function for int8_t elements.
  816. * @see list_attributes_hash_computer()
  817. */
  818. list_hash_t list_hashcomputer_int8_t(const void *el);
  819. /**
  820. * ready-made hash function for int16_t elements.
  821. * @see list_attributes_hash_computer()
  822. */
  823. list_hash_t list_hashcomputer_int16_t(const void *el);
  824. /**
  825. * ready-made hash function for int32_t elements.
  826. * @see list_attributes_hash_computer()
  827. */
  828. list_hash_t list_hashcomputer_int32_t(const void *el);
  829. /**
  830. * ready-made hash function for int64_t elements.
  831. * @see list_attributes_hash_computer()
  832. */
  833. list_hash_t list_hashcomputer_int64_t(const void *el);
  834. /**
  835. * ready-made hash function for uint8_t elements.
  836. * @see list_attributes_hash_computer()
  837. */
  838. list_hash_t list_hashcomputer_uint8_t(const void *el);
  839. /**
  840. * ready-made hash function for uint16_t elements.
  841. * @see list_attributes_hash_computer()
  842. */
  843. list_hash_t list_hashcomputer_uint16_t(const void *el);
  844. /**
  845. * ready-made hash function for uint32_t elements.
  846. * @see list_attributes_hash_computer()
  847. */
  848. list_hash_t list_hashcomputer_uint32_t(const void *el);
  849. /**
  850. * ready-made hash function for uint64_t elements.
  851. * @see list_attributes_hash_computer()
  852. */
  853. list_hash_t list_hashcomputer_uint64_t(const void *el);
  854. /**
  855. * ready-made hash function for float elements.
  856. * @see list_attributes_hash_computer()
  857. */
  858. list_hash_t list_hashcomputer_float(const void *el);
  859. /**
  860. * ready-made hash function for double elements.
  861. * @see list_attributes_hash_computer()
  862. */
  863. list_hash_t list_hashcomputer_double(const void *el);
  864. /**
  865. * ready-made hash function for string elements.
  866. * @see list_attributes_hash_computer()
  867. */
  868. list_hash_t list_hashcomputer_string(const void *el);
  869. #ifdef __cplusplus
  870. }
  871. #endif
  872. #endif