]>
git.saurik.com Git - apple/security.git/blob - cdsa/cdsa_utilities/handleobject.cpp
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
20 // handleobject - give an object a process-global unique handle
23 #define _CPP_HANDLEOBJECT
25 #include <Security/handleobject.h>
29 // Static members of HandleObject
31 ModuleNexus
<HandleObject::State
> HandleObject::state
;
35 // Bring the State constructor out of line
37 HandleObject::State::State()
42 // Assign a HandleObject's (new) Handle.
44 void HandleObject::State::make(HandleObject
*obj
)
46 StLock
<Mutex
> _(mLock
);
48 Handle handle
= reinterpret_cast<uint32
>(obj
) ^ (++sequence
<< 19);
49 if (handleMap
[handle
] == NULL
) {
50 debug("handleobj", "create 0x%lx for %p", handle
, obj
);
51 obj
->setHandle(handle
);
52 handleMap
[handle
] = obj
;
60 // Clean up a HandleObject that dies.
61 // Note that an object MAY clear its handle before (in which case we do nothing).
62 // In particular, killHandle will do this.
64 void HandleObject::State::erase(HandleObject
*obj
)
66 StLock
<Mutex
> _(mLock
);
67 if (obj
->validHandle())
68 handleMap
.erase(obj
->handle());
73 // This is the main locator driver. It translates an object handle
74 // into an object pointer, on the way atomically locking it and/or
75 // removing it from the handle map for atomic deletion.
77 HandleObject
*HandleObject::State::locate(CSSM_HANDLE h
, LocateMode mode
, CSSM_RETURN error
)
81 StLock
<Mutex
> _(mLock
);
82 HandleMap::iterator it
= handleMap
.find(h
);
83 if (it
== handleMap
.end())
84 CssmError::throwMe(error
);
85 HandleObject
*obj
= it
->second
;
86 if (obj
== NULL
|| obj
->handle() != h
)
87 CssmError::throwMe(error
);
88 if (mode
== findTarget
)
89 return obj
; // that's all, folks
90 // atomic find-and-lock requested (implicit in remove operation)
92 // got object lock - assured of exit path
93 if (mode
== removeTarget
) {
94 debug("handleobj", "killing %p", obj
);
100 // obj is busy; relinquish maplock and try again later
101 debug("handleobj", "object %p (handle 0x%lx) is busy - backing off",
104 #if _USE_THREADS == _USE_NO_THREADS
105 assert(false); // impossible; tryLock above always succeeds
106 #else // real threads
108 #endif // real threads
114 // The default locking virtual methods do nothing and succeed.
116 void HandleObject::lock() { }
118 bool HandleObject::tryLock() { return true; }