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
19#include <assert.h>
20
21namespace D3D8
22{
23	Unknown::Unknown()
24	{
25		referenceCount = 0;
26		bindCount = 0;
27	}
28
29	Unknown::~Unknown()
30	{
31		ASSERT(referenceCount == 0);
32		ASSERT(bindCount == 0);
33	}
34
35	long Unknown::QueryInterface(const IID &iid, void **object)
36	{
37		if(iid == IID_IUnknown)
38		{
39			AddRef();
40			*object = this;
41
42			return S_OK;
43		}
44
45		*object = 0;
46
47		return NOINTERFACE(iid);
48	}
49
50	unsigned long Unknown::AddRef()
51	{
52		return InterlockedIncrement(&referenceCount);
53	}
54
55	unsigned long Unknown::Release()
56	{
57		int current = referenceCount;
58
59		if(referenceCount > 0)
60		{
61			current = InterlockedDecrement(&referenceCount);
62		}
63
64		if(referenceCount == 0 && bindCount == 0)
65		{
66			delete this;
67		}
68
69		return current;
70	}
71
72	void Unknown::bind()
73	{
74		InterlockedIncrement(&bindCount);
75	}
76
77	void Unknown::unbind()
78	{
79		ASSERT(bindCount > 0);
80
81		InterlockedDecrement(&bindCount);
82
83		if(referenceCount == 0 && bindCount == 0)
84		{
85			delete this;
86		}
87	}
88}