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
|
/*
* Copyright (C) 2006 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/** \file
This file consists of implementation of a class AdbObjectHandle that
encapsulates an internal API object that is visible to the outside
of the API through a handle.
*/
#include "stdafx.h"
#include "adb_api.h"
#include "adb_object_handle.h"
/// Global ADBAPIHANDLE -> AdbObjectHandle* map
AdbObjectHandleMap the_map;
/// Locker for the AdbObjectHandleMap instance
CComAutoCriticalSection the_map_locker;
/// Next adb handle value generator
ULONG_PTR next_adb_handle_value = 0;
AdbObjectHandle::AdbObjectHandle(AdbObjectType obj_type)
: adb_handle_(NULL),
object_type_(obj_type),
ref_count_(1) {
ATLASSERT(obj_type < AdbObjectTypeMax);
}
AdbObjectHandle::~AdbObjectHandle() {
ATLASSERT(0 == ref_count_);
ATLASSERT(NULL == adb_handle_);
}
LONG AdbObjectHandle::AddRef() {
ATLASSERT(ref_count_ > 0);
return InterlockedIncrement(&ref_count_);
}
LONG AdbObjectHandle::Release() {
ATLASSERT(ref_count_ > 0);
LONG ret = InterlockedDecrement(&ref_count_);
ATLASSERT(ret >= 0);
if (0 == ret) {
LastReferenceReleased();
delete this;
}
return ret;
}
ADBAPIHANDLE AdbObjectHandle::CreateHandle() {
ADBAPIHANDLE ret = NULL;
// We have to hold this lock while we're dealing with the handle
// and the table
the_map_locker.Lock();
ATLASSERT(!IsOpened());
if (!IsOpened()) {
try {
// Generate next handle value
next_adb_handle_value++;
ret = reinterpret_cast<ADBAPIHANDLE>(next_adb_handle_value);
// Add ourselves to the map
the_map[ret] = this;
// Save handle, addref and return
adb_handle_ = ret;
AddRef();
} catch (...) {
ret = NULL;
SetLastError(ERROR_OUTOFMEMORY);
}
} else {
// Signaling that this object is already opened
SetLastError(ERROR_GEN_FAILURE);
}
the_map_locker.Unlock();
return ret;
}
bool AdbObjectHandle::CloseHandle() {
bool ret = false;
// Addref just in case that last reference to this object is being
// held in the map
AddRef();
the_map_locker.Lock();
ATLASSERT(IsOpened());
if (IsOpened()) {
try {
// Look us up in the map.
AdbObjectHandleMap::iterator found = the_map.find(adb_handle());
ATLASSERT((found != the_map.end()) && (this == found->second));
if ((found != the_map.end()) && (this == found->second)) {
// Remove ourselves from the map, close and release the object
the_map.erase(found);
adb_handle_ = NULL;
Release();
ret = true;
} else {
SetLastError(ERROR_INVALID_HANDLE);
}
} catch (...) {
ret = false;
SetLastError(ERROR_OUTOFMEMORY);
}
} else {
SetLastError(ERROR_INVALID_HANDLE);
}
the_map_locker.Unlock();
Release();
return ret;
}
bool AdbObjectHandle::IsObjectOfType(AdbObjectType obj_type) const {
return (obj_type == object_type());
}
void AdbObjectHandle::LastReferenceReleased() {
ATLASSERT(!IsOpened());
}
AdbObjectHandle* AdbObjectHandle::Lookup(ADBAPIHANDLE adb_hndl) {
AdbObjectHandle* ret = NULL;
the_map_locker.Lock();
try {
// Look us up in the map.
AdbObjectHandleMap::iterator found = the_map.find(adb_hndl);
if (found != the_map.end()) {
ret = found->second;
ret->AddRef();
}
} catch (...) {
SetLastError(ERROR_OUTOFMEMORY);
}
the_map_locker.Unlock();
return ret;
}
|