dqueue.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* $Id: dqueue.c,v 1.5 2003/04/12 21:40:49 schindler Exp $
  2. *
  3. * Driver for Eicon DIVA Server ISDN cards.
  4. * User Mode IDI Interface
  5. *
  6. * Copyright 2000-2003 by Armin Schindler (mac@melware.de)
  7. * Copyright 2000-2003 Cytronics & Melware (info@melware.de)
  8. *
  9. * This software may be used and distributed according to the terms
  10. * of the GNU General Public License, incorporated herein by reference.
  11. */
  12. #include "platform.h"
  13. #include "dqueue.h"
  14. int
  15. diva_data_q_init(diva_um_idi_data_queue_t *q,
  16. int max_length, int max_segments)
  17. {
  18. int i;
  19. q->max_length = max_length;
  20. q->segments = max_segments;
  21. for (i = 0; i < q->segments; i++) {
  22. q->data[i] = NULL;
  23. q->length[i] = 0;
  24. }
  25. q->read = q->write = q->count = q->segment_pending = 0;
  26. for (i = 0; i < q->segments; i++) {
  27. if (!(q->data[i] = diva_os_malloc(0, q->max_length))) {
  28. diva_data_q_finit(q);
  29. return (-1);
  30. }
  31. }
  32. return (0);
  33. }
  34. int diva_data_q_finit(diva_um_idi_data_queue_t *q)
  35. {
  36. int i;
  37. for (i = 0; i < q->segments; i++) {
  38. if (q->data[i]) {
  39. diva_os_free(0, q->data[i]);
  40. }
  41. q->data[i] = NULL;
  42. q->length[i] = 0;
  43. }
  44. q->read = q->write = q->count = q->segment_pending = 0;
  45. return (0);
  46. }
  47. int diva_data_q_get_max_length(const diva_um_idi_data_queue_t *q)
  48. {
  49. return (q->max_length);
  50. }
  51. void *diva_data_q_get_segment4write(diva_um_idi_data_queue_t *q)
  52. {
  53. if ((!q->segment_pending) && (q->count < q->segments)) {
  54. q->segment_pending = 1;
  55. return (q->data[q->write]);
  56. }
  57. return NULL;
  58. }
  59. void
  60. diva_data_q_ack_segment4write(diva_um_idi_data_queue_t *q, int length)
  61. {
  62. if (q->segment_pending) {
  63. q->length[q->write] = length;
  64. q->count++;
  65. q->write++;
  66. if (q->write >= q->segments) {
  67. q->write = 0;
  68. }
  69. q->segment_pending = 0;
  70. }
  71. }
  72. const void *diva_data_q_get_segment4read(const diva_um_idi_data_queue_t *
  73. q)
  74. {
  75. if (q->count) {
  76. return (q->data[q->read]);
  77. }
  78. return NULL;
  79. }
  80. int diva_data_q_get_segment_length(const diva_um_idi_data_queue_t *q)
  81. {
  82. return (q->length[q->read]);
  83. }
  84. void diva_data_q_ack_segment4read(diva_um_idi_data_queue_t *q)
  85. {
  86. if (q->count) {
  87. q->length[q->read] = 0;
  88. q->count--;
  89. q->read++;
  90. if (q->read >= q->segments) {
  91. q->read = 0;
  92. }
  93. }
  94. }