Mirror of :pserver:cvs@cvs.fefe.de:/cvs libowfat https://www.fefe.de/libowfat/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.7 KiB

13 years ago
12 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
13 years ago
  1. /* this header file comes from libowfat, http://www.fefe.de/libowfat/ */
  2. #ifndef IARRAY_H
  3. #define IARRAY_H
  4. /* This header defines an indirect array for use with the io_* routines.
  5. * Assumptions:
  6. * - the elements are small (many fit on one page),
  7. * - the platform has an atomic compare-and-swap instruction
  8. * - the compiler supports it via __sync_val_compare_and_swap
  9. */
  10. #include <libowfat/uint64.h>
  11. #include <stddef.h>
  12. #ifdef _WIN32
  13. #include <windows.h>
  14. #else
  15. #include <pthread.h>
  16. #endif
  17. /* The basic data structure is a static array of pointers to pages.
  18. * Each page also contains a next pointer to form a linked list.
  19. * To get to element n, you take n % the number of elements in the
  20. * static array (iarray->pages) to get to the list of pages that
  21. * contains it. Then keep going to the next page until you are on the
  22. * right page.
  23. * Note: The elements on each page are not contiguous. If the fanout is
  24. * 16, the indices on page 0 are 0, 16, 32, ...
  25. * To get to element 0, you'd go to iarray->pages[0].data,
  26. * to get to element 1, you'd go to iarray->pages[1].data,
  27. * to get to element 16, you'd go to iarray->pages[0].data+iarray->elemsize.
  28. */
  29. typedef struct _iarray_page {
  30. struct _iarray_page* next;
  31. char data[];
  32. } iarray_page;
  33. typedef struct {
  34. iarray_page* pages[16];
  35. size_t elemsize,elemperpage,bytesperpage,len;
  36. } iarray;
  37. void iarray_init(iarray* ia,size_t elemsize);
  38. void* iarray_get(iarray* ia,size_t pos);
  39. void* iarray_allocate(iarray* ia,size_t pos);
  40. size_t iarray_length(iarray* ia);
  41. /* WARNING: do not use the array during or after iarray_free, make sure
  42. * no threads are potentially doing anything with the iarray while it is
  43. * being freed! */
  44. void iarray_free(iarray* ia);
  45. #endif