Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(870)

Side by Side Diff: scoped_tss_type.h

Issue 6683071: TPM_ERROR and ScopedTssType: uses the first, creates the second. (Closed) Base URL: ssh://git@gitrw.chromium.org:9222/cryptohome.git@master
Patch Set: add warn_unused_result Created 9 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « SConstruct ('k') | tpm.cc » ('j') | tpm.cc » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2011 The Chromium OS Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // Implements a simple framework for scoping TSS values.
6 // Based on chrome's base/scoped_ptr_malloc implementation.
7 //
8 // Example usage:
9 // ScopedTssContext context;
gauravsh 2011/03/29 19:16:06 context_handle
Will Drewry 2011/03/29 19:42:22 Ugh. I hate that variable name, but okay. :)
gauravsh 2011/03/29 20:21:54 I just meant that you use context_handle later in
10 // TSS_RESULT result;
11 // if (!OpenAndConnectTpm(context_handle.get(), &result))
gauravsh 2011/03/29 20:21:54 As I was saying, you use context_handle here, just
12 // ...
13 // ScopedTssKey srk(*context);
gauravsh 2011/03/29 19:16:06 context_handle
Will Drewry 2011/03/29 19:42:22 Done.
14 // if (!LoadSrk(*context_handle, srk_handle.get(), &result))
15 // ...
16 //
17 // See the bottom of this file for common typedefs.
18
19 #include <assert.h>
20 #include <trousers/tss.h>
21 #include <trousers/trousers.h>
22
23 #include <base/compiler_specific.h>
24 #include <vector>
25
26 #ifndef CRYPTOHOME_SCOPED_TSS_TYPE_H_
27 #define CRYPTOHOME_SCOPED_TSS_TYPE_H_
28
29 namespace cryptohome {
30
31 class ScopedTssContextRelease {
32 public:
33 inline void operator()(TSS_HCONTEXT unused, TSS_HCONTEXT context) const {
34 // Usually, only |context| is used, but if the ScopedTssContext is
35 // used slightly differently, it may end up with a context in |unused|.
36 // For now, treat that as a bug.
37 assert(unused == 0);
38 if (context)
39 Tspi_Context_Close(context);
40 }
41 };
42
43 class ScopedTssMemoryRelease {
44 public:
45 inline void operator()(TSS_HCONTEXT context, BYTE* memory) const {
46 if (memory)
47 Tspi_Context_FreeMemory(context, memory);
gauravsh 2011/03/29 19:16:06 so in ScopedTssContextRelease, you check for (cont
Will Drewry 2011/03/29 19:42:22 For ContextRelease I assert it _is_ 0 and not that
48 }
49 };
50
51 class ScopedTssObjectRelease {
52 public:
53 inline void operator()(TSS_HCONTEXT context, TSS_HOBJECT handle) const {
54 Tspi_Context_CloseObject(context, handle);
55 }
56 };
57
58 // Provide a basic scoped container for TSS managed objects.
59 template<class TssType, class ReleaseProc = ScopedTssObjectRelease>
60 class ScopedTssType {
61 public:
62 explicit ScopedTssType(TSS_HCONTEXT c = 0, TssType t = 0) :
63 context_(c),
64 type_(t) {}
65 virtual ~ScopedTssType() {
66 release_(context_, type_);
67 }
68
69 // Allow pointer-like access for getting the value.
70 TssType& operator*() {
71 return type_;
72 }
73
74 // Return a pointer to the storage to allow in place changes.
75 // We could override &, but that seems like a bad idea.
76 virtual TssType* get() {
77 return &type_;
78 }
79
80 // Returns the assigned context.
81 virtual TSS_HCONTEXT context() {
82 return context_;
83 }
84
85 virtual TssType release() WARN_UNUSED_RESULT {
86 TssType tmp = type_;
87 type_ = 0;
88 context_ = 0;
89 return tmp;
90 }
91
92 virtual void reset(TSS_HCONTEXT c = 0, TssType t = 0) {
93 release_(context_, type_);
94 context_ = c;
95 type_ = t;
96 }
97
98 private:
99 static ReleaseProc const release_;
100 TSS_HCONTEXT context_;
101 TssType type_;
102 };
103
104 // Provide clear-cut helpers for the common cases.
105 typedef ScopedTssType<TSS_HCONTEXT, ScopedTssContextRelease> ScopedTssContext;
106 typedef ScopedTssType<BYTE*, ScopedTssMemoryRelease> ScopedTssMemory;
107
108 typedef ScopedTssType<TSS_HOBJECT> ScopedTssObject;
109 typedef ScopedTssType<TSS_HKEY> ScopedTssKey;
110 typedef ScopedTssType<TSS_HPOLICY> ScopedTssPolicy;
111 typedef ScopedTssType<TSS_HPCRS> ScopedTssPcrs;
112
113 } // namespace cryptohome
114
115 #endif // CRYPTOHOME_SCOPED_TSS_TYPE_H_
OLDNEW
« no previous file with comments | « SConstruct ('k') | tpm.cc » ('j') | tpm.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698