mem.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * TAP-Windows -- A kernel driver to provide virtual tap
  3. * device functionality on Windows.
  4. *
  5. * This code was inspired by the CIPE-Win32 driver by Damion K. Wilson.
  6. *
  7. * This source code is Copyright (C) 2002-2010 OpenVPN Technologies, Inc.,
  8. * and is released under the GPL version 2 (see below).
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program (see the file COPYING included with this
  21. * distribution); if not, write to the Free Software Foundation, Inc.,
  22. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  23. */
  24. //------------------
  25. // Memory Management
  26. //------------------
  27. PVOID
  28. MemAlloc (ULONG p_Size, BOOLEAN zero)
  29. {
  30. PVOID l_Return = NULL;
  31. if (p_Size)
  32. {
  33. __try
  34. {
  35. if (NdisAllocateMemoryWithTag (&l_Return, p_Size, 'APAT')
  36. == NDIS_STATUS_SUCCESS)
  37. {
  38. if (zero)
  39. NdisZeroMemory (l_Return, p_Size);
  40. }
  41. else
  42. l_Return = NULL;
  43. }
  44. __except (EXCEPTION_EXECUTE_HANDLER)
  45. {
  46. l_Return = NULL;
  47. }
  48. }
  49. return l_Return;
  50. }
  51. VOID
  52. MemFree (PVOID p_Addr, ULONG p_Size)
  53. {
  54. if (p_Addr && p_Size)
  55. {
  56. __try
  57. {
  58. #if DBG
  59. NdisZeroMemory (p_Addr, p_Size);
  60. #endif
  61. NdisFreeMemory (p_Addr, p_Size, 0);
  62. }
  63. __except (EXCEPTION_EXECUTE_HANDLER)
  64. {
  65. }
  66. }
  67. }
  68. /*
  69. * Circular queue management routines.
  70. */
  71. #define QUEUE_BYTE_ALLOCATION(size) \
  72. (sizeof (Queue) + (size * sizeof (PVOID)))
  73. #define QUEUE_ADD_INDEX(var, inc) \
  74. { \
  75. var += inc; \
  76. if (var >= q->capacity) \
  77. var -= q->capacity; \
  78. MYASSERT (var < q->capacity); \
  79. }
  80. #define QUEUE_SANITY_CHECK() \
  81. MYASSERT (q != NULL && q->base < q->capacity && q->size <= q->capacity)
  82. #define QueueCount(q) (q->size)
  83. #define UPDATE_MAX_SIZE() \
  84. { \
  85. if (q->size > q->max_size) \
  86. q->max_size = q->size; \
  87. }
  88. Queue *
  89. QueueInit (ULONG capacity)
  90. {
  91. Queue *q;
  92. MYASSERT (capacity > 0);
  93. q = (Queue *) MemAlloc (QUEUE_BYTE_ALLOCATION (capacity), TRUE);
  94. if (!q)
  95. return NULL;
  96. q->base = q->size = 0;
  97. q->capacity = capacity;
  98. q->max_size = 0;
  99. return q;
  100. }
  101. VOID
  102. QueueFree (Queue *q)
  103. {
  104. if (q)
  105. {
  106. QUEUE_SANITY_CHECK ();
  107. MemFree (q, QUEUE_BYTE_ALLOCATION (q->capacity));
  108. }
  109. }
  110. PVOID
  111. QueuePush (Queue *q, PVOID item)
  112. {
  113. ULONG dest;
  114. QUEUE_SANITY_CHECK ();
  115. if (q->size == q->capacity)
  116. return NULL;
  117. dest = q->base;
  118. QUEUE_ADD_INDEX (dest, q->size);
  119. q->data[dest] = item;
  120. ++q->size;
  121. UPDATE_MAX_SIZE();
  122. return item;
  123. }
  124. PVOID
  125. QueuePop (Queue *q)
  126. {
  127. ULONG oldbase;
  128. QUEUE_SANITY_CHECK ();
  129. if (!q->size)
  130. return NULL;
  131. oldbase = q->base;
  132. QUEUE_ADD_INDEX (q->base, 1);
  133. --q->size;
  134. UPDATE_MAX_SIZE();
  135. return q->data[oldbase];
  136. }
  137. PVOID
  138. QueueExtract (Queue *q, PVOID item)
  139. {
  140. ULONG src, dest, count, n;
  141. QUEUE_SANITY_CHECK ();
  142. n = 0;
  143. src = dest = q->base;
  144. count = q->size;
  145. while (count--)
  146. {
  147. if (item == q->data[src])
  148. {
  149. ++n;
  150. --q->size;
  151. }
  152. else
  153. {
  154. q->data[dest] = q->data[src];
  155. QUEUE_ADD_INDEX (dest, 1);
  156. }
  157. QUEUE_ADD_INDEX (src, 1);
  158. }
  159. if (n)
  160. return item;
  161. else
  162. return NULL;
  163. }
  164. #undef QUEUE_BYTE_ALLOCATION
  165. #undef QUEUE_ADD_INDEX
  166. #undef QUEUE_SANITY_CHECK
  167. #undef UPDATE_MAX_SIZE