Subversion Repositories Kolibri OS

Rev

Rev 2300 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1159 hidnplayr 1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2
;;                                                              ;;
1514 hidnplayr 3
;; Copyright (C) KolibriOS team 2004-2010. All rights reserved. ;;
1159 hidnplayr 4
;; Distributed under terms of the GNU General Public License    ;;
5
;;                                                              ;;
6
;;  queue.inc                                                   ;;
7
;;                                                              ;;
8
;;    Written by hidnplayr@kolibrios.org                        ;;
9
;;                                                              ;;
10
;;          GNU GENERAL PUBLIC LICENSE                          ;;
11
;;             Version 2, June 1991                             ;;
12
;;                                                              ;;
13
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
14
 
1206 hidnplayr 15
$Revision: 2305 $
1159 hidnplayr 16
 
1514 hidnplayr 17
; The Queues implemented by these macros form a ring-buffer.
1257 hidnplayr 18
; The data to these queue's always looks like this:
19
;
20
; At top, you have the queue struct, wich has the size (number of currently queued packets, read and write pointers.
21
; This struct is followed by a number of slots wich you can read and write to using the macros.
22
; How these slots look like is up to you to chose, normally they should have at least a pointer to where the real data is.
23
; (you can see some examples below)
24
 
25
 
1159 hidnplayr 26
struct	queue
2305 hidnplayr 27
 
28
	size	       dd ?    ; number of queued packets in this queue
29
	w_ptr	       dd ?    ; current writing pointer in queue
30
	r_ptr	       dd ?    ; current reading pointer
31
 
1159 hidnplayr 32
ends
33
 
1257 hidnplayr 34
; The following macros share these inputs:
35
 
36
; ptr           = pointer to where the queue data is located
37
; size          = number of slots/entrys in the queue
38
; entry_size    = size of one slot, in bytes
39
; failaddr      = the address where macro will jump to when there is no data in the queue
40
 
41
; additionally, add_to_queue requires you to set esi to the data wich you want to queue
42
; get_from_queue on the other hand will return a pointer in esi, to the entry you're interessed in
43
; PS: macros WILL destroy ecx and edi
44
 
1249 hidnplayr 45
macro add_to_queue ptr, size, entry_size, failaddr {
1159 hidnplayr 46
 
1257 hidnplayr 47
	cmp	[ptr + queue.size], size	; Check if queue isnt full
2300 hidnplayr 48
	jae	failaddr
1159 hidnplayr 49
 
1257 hidnplayr 50
	inc	[ptr + queue.size]		; if not full, queue one more
1159 hidnplayr 51
 
1257 hidnplayr 52
	mov	edi, [ptr + queue.w_ptr]	; Current write pointer (FIFO!)
1249 hidnplayr 53
	mov	ecx, entry_size/4		; Write the queue entry
54
	rep	movsd				;
1159 hidnplayr 55
 
2305 hidnplayr 56
	lea	ecx, [size*entry_size+ptr+sizeof.queue]
1257 hidnplayr 57
	cmp	edi, ecx			; entry size
2300 hidnplayr 58
	jb	.no_wrap
1159 hidnplayr 59
 
1249 hidnplayr 60
	sub	edi, size*entry_size
1159 hidnplayr 61
 
62
  .no_wrap:
1249 hidnplayr 63
	mov	[ptr + queue.w_ptr], edi
1159 hidnplayr 64
 
65
}
66
 
67
 
68
 
1249 hidnplayr 69
macro get_from_queue ptr, size, entry_size,  failaddr {
1159 hidnplayr 70
 
1257 hidnplayr 71
	cmp	[ptr + queue.size], 0		; any packets queued?
1249 hidnplayr 72
	je	failaddr
1159 hidnplayr 73
 
1257 hidnplayr 74
	dec	[ptr + queue.size]		; if so, dequeue one
1159 hidnplayr 75
 
76
	mov	esi, [ptr + queue.r_ptr]
1249 hidnplayr 77
	push	esi
1159 hidnplayr 78
 
1249 hidnplayr 79
	add	esi, entry_size
1159 hidnplayr 80
 
2305 hidnplayr 81
	lea	ecx, [size*entry_size+ptr+sizeof.queue]
1257 hidnplayr 82
	cmp	esi, ecx			; entry size
2300 hidnplayr 83
	jb	.no_wrap
1159 hidnplayr 84
 
1249 hidnplayr 85
	sub	esi, size*entry_size
1159 hidnplayr 86
 
87
  .no_wrap:
88
	mov	dword [ptr + queue.r_ptr], esi
89
 
1249 hidnplayr 90
	pop	esi
91
 
1159 hidnplayr 92
}
93
 
1257 hidnplayr 94
macro init_queue ptr {
1249 hidnplayr 95
 
1257 hidnplayr 96
	mov	[ptr + queue.size] , 0
2305 hidnplayr 97
	lea	edi, [ptr + sizeof.queue]
1257 hidnplayr 98
	mov	[ptr + queue.w_ptr], edi
99
	mov	[ptr + queue.r_ptr], edi
1249 hidnplayr 100
}