summaryrefslogtreecommitdiffhomepage
path: root/src/blitter/ntapi_blt_alloc.c
blob: 4e050c1188aaf35c639e0dd667ed248f91c4ffa2 (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
/********************************************************/
/*  ntapi: Native API core library                      */
/*  Copyright (C) 2013--2016  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 int __blt_popcount(uintptr_t mask)
{
	/* todo: check cpuid, use at_popcount */
	uintptr_t i;
	int       ret;

	for (i=0,ret=0; i<8*sizeof(uintptr_t); i++)
		if (mask & ((uintptr_t)1<<i))
			ret++;

	return ret;
}


int32_t __fastcall __ntapi_blt_alloc(
	__out	nt_blitter **		blitter,
	__in	nt_blitter_params *	params)
{
	int32_t		status;
	nt_blitter *	blt_ctx;
	size_t		blt_ctx_size;
	size_t		params_size;
	size_t		ptrs,i;

	/* alignment */
	if ((params->block_size % sizeof(uintptr_t)) || (params->block_count % sizeof(uintptr_t)))
		return NT_STATUS_INVALID_PARAMETER;

	/* blt control block allocation */
	ptrs		= params->block_count / (8 * sizeof(uintptr_t));
	blt_ctx		= (nt_blitter *)0;
	blt_ctx_size	= (size_t)&((nt_blitter *)0)->bits;

	/* user-provided bitmap? */
	if (!params->bitmap)
		blt_ctx_size += ptrs * sizeof(uintptr_t);

	/* alloc */
	status = __ntapi->zw_allocate_virtual_memory(
		NT_CURRENT_PROCESS_HANDLE,
		(void **)&blt_ctx,
		0,
		&blt_ctx_size,
		NT_MEM_COMMIT,
		NT_PAGE_READWRITE);

	if (status) return (status);

	/* init control block */
	__ntapi->tt_aligned_block_memset(
		blt_ctx,
		0,(size_t)&((nt_blitter *)0)->bits);

	blt_ctx->addr	= blt_ctx;
	blt_ctx->size	= blt_ctx_size;
	blt_ctx->ptrs	= ptrs;

	/* init bitmap */
	blt_ctx->bitmap = params->bitmap
		? (uintptr_t *)params->bitmap
		: blt_ctx->bits;

	if (!(params->flags & NT_BLITTER_PRESERVE_BITS))
		__ntapi->tt_aligned_block_memset(
			blt_ctx->bitmap,
			0xFFFFFFFFFFFFFFFF,
			ptrs * sizeof(uintptr_t));

	/* info structure */
	blt_ctx->info.info_size   = sizeof(nt_blitter_info);
	blt_ctx->info.block_count = params->block_count;
	blt_ctx->info.block_size  = params->block_size;

	if (params->flags & NT_BLITTER_ENABLE_BLOCK_ARRAY)
		/* allocate in place */
		blt_ctx->info.region_size = params->block_count * params->block_size;
	else
		/* use pointer array */
		blt_ctx->info.region_size = params->block_count * sizeof(uintptr_t);

	/* allocate region */
	if (params->region)
		blt_ctx->info.region_addr = params->region;
	else
		status = __ntapi->zw_allocate_virtual_memory(
			NT_CURRENT_PROCESS_HANDLE,
			&blt_ctx->info.region_addr,
			0,
			&blt_ctx->info.region_size,
			NT_MEM_COMMIT,
			NT_PAGE_READWRITE);

	if (status) {
		__ntapi->blt_free(blt_ctx);
		return status;
	}

	if (params->flags & NT_BLITTER_PRESERVE_BITS)
		for (i=0,blt_ctx->info.blocks_avail=0; i<ptrs; i++)
			blt_ctx->info.blocks_avail += __blt_popcount(blt_ctx->bitmap[i]);
	else
		blt_ctx->info.blocks_avail = params->block_count;

	if (params->flags & NT_BLITTER_ENABLE_BLOCK_ARRAY)
		blt_ctx->info.blocks_cached = params->block_count;

	/* init block array */
	if (!params->region)
		__ntapi->tt_aligned_block_memset(
			blt_ctx->info.region_addr,
			0,blt_ctx->info.region_size);

	/* copy params */
	if (params->params_size < sizeof(nt_blitter_params))
		params_size = params->params_size;
	else
		params_size = sizeof(nt_blitter_params);

	__ntapi->tt_aligned_block_memcpy(
		(uintptr_t *)&blt_ctx->params,
		(uintptr_t *)params,
		params_size);

	/* update params */
	blt_ctx->params.lock_tries = params->lock_tries
				? params->lock_tries
				: __NT_BLITTER_DEFAULT_LOCK_TRIES;

	blt_ctx->params.round_trips = params->round_trips
				? params->round_trips
				: __NT_BLITTER_DEFAULT_ROUND_TRIPS;

	*blitter = blt_ctx;

	return NT_STATUS_SUCCESS;
}