summaryrefslogtreecommitdiffhomepage
path: root/src/blitter/ntapi_blt_block.c
blob: 12dc2f78a631b50c6d140c5f0732a3f319edce16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/********************************************************/
/*  ntapi: Native API core library                      */
/*  Copyright (C) 2013--2017  Z. Gilboa                 */
/*  Released under GPLv2 and GPLv3; see COPYING.NTAPI.  */
/********************************************************/

#include <ntapi/nt_status.h>
#include <ntapi/nt_blitter.h>
#include <ntapi/nt_sync.h>
#include <ntapi/ntapi.h>
#include <ntapi/nt_atomic.h>
#include "ntapi_blitter.h"
#include "ntapi_impl.h"

static int32_t __fastcall __blt_bitbite(
	__in	nt_blitter *	blitter,
	__in	unsigned int	bit,
	__in	size_t		byte)
{
	uint32_t	locktry;
	uintptr_t	test;
	uintptr_t	cmp;
	uintptr_t	xchg;
	uintptr_t	mask;

	mask    = ((uintptr_t)1 << bit);
	locktry = blitter->params.lock_tries;

	for (; locktry; locktry--) {
		cmp  = blitter->bitmap[byte] | mask;
		xchg = cmp ^ mask;

		test  = at_locked_cas(
			(intptr_t *)&blitter->bitmap[byte],
			cmp,xchg);

		if (test == cmp) {
			at_locked_dec(&blitter->info.blocks_avail);
			at_locked_inc(&blitter->info.blocks_used);
			return NT_STATUS_SUCCESS;

		} else if (test ^ mask)
			return NT_STATUS_TRANSACTIONAL_CONFLICT;
	}

	if (!locktry) {
		blitter->info.busy = 1;
		blitter->info.lock_tries = blitter->params.lock_tries;
		return NT_STATUS_DEVICE_BUSY;
	}

	return NT_STATUS_MORE_PROCESSING_REQUIRED;
}

static int32_t __fastcall __blt_acquire(
	__in	nt_blitter *	blitter,
	__out	intptr_t *	blkid)
{
	unsigned int	bit;
	uintptr_t		i,n;

	if (blitter->info.blocks_avail == 0)
		return NT_STATUS_ALLOCATE_BUCKET;

	for (n=0,bit=0; blitter->info.blocks_avail && (n < blitter->params.round_trips); n++) {
		for (i=*blkid/(8*sizeof(size_t)); (i<blitter->ptrs); i++)
			if (at_bsf(&bit,blitter->bitmap[i]))
				break;

		if (i == blitter->ptrs)
			return NT_STATUS_ALLOCATE_BUCKET;

		switch (__blt_bitbite(blitter,bit,i)) {
			case NT_STATUS_SUCCESS:
				*blkid = bit + (i * 8 * sizeof(size_t));
				return NT_STATUS_SUCCESS;

			case NT_STATUS_DEVICE_BUSY:
				return NT_STATUS_DEVICE_BUSY;

			default:
				break;
		}
	}

	return NT_STATUS_ALLOCATE_BUCKET;
}


int32_t __fastcall __ntapi_blt_obtain(
	__in	nt_blitter *	blitter,
	__out	intptr_t *	blkid)
{
	unsigned int	bit;
	uintptr_t	i,n;
	uintptr_t	mask;

	if (blitter->info.blocks_avail == 0)
		return NT_STATUS_ALLOCATE_BUCKET;
	else if ((bit = *blkid % (8*sizeof(size_t))) == 0)
		return __ntapi_blt_acquire(blitter,blkid);

	for (n=0,mask=(uintptr_t)-1; n<bit; n++)
		mask ^= ((size_t)1 << n);

	i = *blkid / (8*sizeof(size_t));

	for (n=0; blitter->info.blocks_avail && (n < blitter->params.round_trips); n++) {
		if (!(at_bsf(&bit,(mask & blitter->bitmap[i]))))
			break;

		switch (__blt_bitbite(blitter,bit,i)) {
			case NT_STATUS_SUCCESS:
				*blkid = bit + (i * 8 * sizeof(size_t));
				return NT_STATUS_SUCCESS;

			case NT_STATUS_DEVICE_BUSY:
				return NT_STATUS_DEVICE_BUSY;

			default:
				break;
		}
	}

	*blkid = ++i * 8 * sizeof(size_t);
	return __blt_acquire(blitter,blkid);
}


int32_t __fastcall __ntapi_blt_possess(
	__in	nt_blitter *	blitter,
	__out	intptr_t *	blkid)
{
	int		bit;
	size_t		byte;
	uintptr_t 	test;
	uintptr_t	mask;

	bit  = *blkid % (8*sizeof(size_t));
	byte = *blkid / (8*sizeof(size_t));

	mask = ((uintptr_t)1 << bit);
	test  = at_locked_and(
		(intptr_t *)&blitter->bitmap[byte],
		~mask);

	if (test & mask) {
		at_locked_dec(&blitter->info.blocks_avail);
		at_locked_inc(&blitter->info.blocks_used);
	}

	return NT_STATUS_SUCCESS;
}


int32_t __fastcall __ntapi_blt_acquire(
	__in	nt_blitter *	blitter,
	__out	intptr_t *	blkid)
{
	*blkid = 0;
	return __blt_acquire(blitter,blkid);
}


int32_t __fastcall	__ntapi_blt_release(
	__in	nt_blitter *	blitter,
	__out	intptr_t	blkid)
{
	size_t		i;
	unsigned int	idx;
	uintptr_t	bit;

	i   = blkid / (8 * sizeof(uintptr_t));
	idx = blkid % (8 * sizeof(uintptr_t));
	bit = ((uintptr_t)1 << idx);

	at_locked_or((intptr_t *)&blitter->bitmap[i],bit);
	at_locked_dec(&blitter->info.blocks_used);
	at_locked_inc(&blitter->info.blocks_avail);

	return NT_STATUS_SUCCESS;
}


void * __fastcall __ntapi_blt_get(
	__in	const nt_blitter *	blitter,
	__in	intptr_t		block_id)
{
	size_t * addr = (size_t *)blitter->info.region_addr;
	addr += block_id;
	return addr;
}


void __fastcall __ntapi_blt_set(
	__in	const nt_blitter *	blitter,
	__in	intptr_t		block_id,
	__in	void *			val)
{
	size_t * addr = (size_t *)blitter->info.region_addr;
	addr += block_id;
	*addr = (size_t)val;
	return;
}