1// Copyright 2016 The SwiftShader Authors. All Rights Reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//    http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#include "Unknown.hpp"
16
17#include "Debug.hpp"
18
19namespace D3D9
20{
21	Unknown::Unknown()
22	{
23		referenceCount = 0;
24		bindCount = 0;
25	}
26
27	Unknown::~Unknown()
28	{
29		ASSERT(referenceCount == 0);
30		ASSERT(bindCount == 0);
31	}
32
33	long Unknown::QueryInterface(const IID &iid, void **object)
34	{
35		if(iid == IID_IUnknown)
36		{
37			AddRef();
38			*object = this;
39
40			return S_OK;
41		}
42
43		*object = 0;
44
45		return NOINTERFACE(iid);
46	}
47
48	unsigned long Unknown::AddRef()
49	{
50		return InterlockedIncrement(&referenceCount);
51	}
52
53	unsigned long Unknown::Release()
54	{
55		int current = referenceCount;
56
57		if(referenceCount > 0)
58		{
59			current = InterlockedDecrement(&referenceCount);
60		}
61
62		if(referenceCount == 0 && bindCount == 0)
63		{
64			delete this;
65		}
66
67		return current;
68	}
69
70	void Unknown::bind()
71	{
72		InterlockedIncrement(&bindCount);
73	}
74
75	void Unknown::unbind()
76	{
77		ASSERT(bindCount > 0);
78
79		InterlockedDecrement(&bindCount);
80
81		if(referenceCount == 0 && bindCount == 0)
82		{
83			delete this;
84		}
85	}
86}