erlang各种有用的函数包括一些有用nif封装,还有一些性能测试case。
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.

71 lines
1.9 KiB

5 years ago
  1. #ifndef _FIFO_H
  2. #define _FIFO_H
  3. /* Main FIFO structure. Allocate memory for it yourself. */
  4. typedef struct fifo_t {
  5. void *head;
  6. void *tail;
  7. unsigned long long count;
  8. } fifo_t;
  9. typedef struct fifo_handle_t {
  10. void *next;
  11. } fifo_handle_t;
  12. /* Initializes fifo structure. */
  13. #define fifo_init(fifo) \
  14. do { \
  15. fifo_t *__q = fifo; \
  16. __q->head = NULL; \
  17. __q->tail = NULL; \
  18. __q->count = 0; \
  19. } while (0)
  20. #define __fifo_push(fifo, p, h) \
  21. do { \
  22. fifo_t *__q = fifo; \
  23. __typeof__ (p) e = p; \
  24. e->h.next = NULL; \
  25. if (__q->tail == NULL) { \
  26. __q->head = e; \
  27. } else { \
  28. __typeof__ (e) t = __q->tail; \
  29. t->h.next = e; \
  30. } \
  31. __q->tail = e; \
  32. __q->count++; \
  33. } while (0)
  34. /* Puts an element to the queue. */
  35. #define fifo_push(fifo, p) __fifo_push (fifo, p, fifo_handle)
  36. #define __fifo_pop(fifo, p, h) \
  37. do { \
  38. fifo_t *__q = fifo; \
  39. p = __q->head; \
  40. if (p != NULL) { \
  41. __q->count--; \
  42. __q->head = p->h.next; \
  43. if (__q->tail == p) \
  44. __q->tail = NULL; \
  45. } \
  46. } while (0)
  47. /* Pops the first element out of the queue. */
  48. #define fifo_pop(fifo, p) __fifo_pop (fifo, p, fifo_handle)
  49. #define __fifo_peak(fifo, p, h) \
  50. do { \
  51. p = (fifo)->head; \
  52. } while (0)
  53. /* Returns the first elemnt of the queue without removing. */
  54. #define fifo_peak(fifo, p) __fifo_peak (fifo, p, fifo_handle)
  55. /* Returns the length of the queue. */
  56. #define fifo_length(fifo) ((fifo)->count)
  57. /* Returns true if the queue is empty. */
  58. #define fifo_empty(fifo) ((fifo)->count == 0)
  59. #endif /* _FIFO_H */