1 : /*
2 : * Copyright (c) 2011 The Native Client Authors. All rights reserved.
3 : * Use of this source code is governed by a BSD-style license that can be
4 : * found in the LICENSE file.
5 : */
6 : #ifndef NATIVE_CLIENT_SRC_TRUSTED_SEL_LDR_UNIVERSAL_PEPPER_EMU_HELPER_H_
7 : #define NATIVE_CLIENT_SRC_TRUSTED_SEL_LDR_UNIVERSAL_PEPPER_EMU_HELPER_H_
8 :
9 : #include <string>
10 :
11 : // The classes below associate ranges of resource handles with
12 : // data structures containing actual information about
13 : // the handle.
14 : // This is still quite primitive and will change over time.
15 : class ResourceBase {
16 : public:
17 : ResourceBase(int num_handles, const char* type);
18 : ~ResourceBase();
19 : const char* GetType() { return type_; }
20 : // Find a free handle (refcount == 0).
21 : int Alloc();
22 : // Get refcount for handle
23 : int GetRefCount(int handle);
24 : // Increment refcount of handle.
25 : bool IncRefCount(int handle);
26 : // Decrement refcount of handle,
27 : bool DecRefCount(int handle);
28 :
29 : static ResourceBase* FindResource(int handle);
30 :
31 : private:
32 : static ResourceBase* chain_;
33 : // Last used handle. This maybe rounded up - so there can be gaps).
34 : static int chain_handle_last_;
35 :
36 : protected:
37 : // Next ResourceBase in chain starting with chain_
38 : ResourceBase* next_;
39 : // First handle in this range.
40 : const int first_handle_;
41 : // Width of the range.
42 : const int num_handles_;
43 : // Resource type respresent as a string.
44 : const char* type_;
45 : // Refcount for each handle in the range - zero means unused.
46 : int* const used_;
47 : };
48 :
49 :
50 : template<class Data> class Resource : public ResourceBase {
51 : public:
52 77 : Resource(int num_handles, const char* type)
53 77 : : ResourceBase(num_handles, type), data_(new Data[num_handles]) {}
54 :
55 77 : ~Resource() {
56 77 : delete [] data_;
57 : }
58 :
59 : // Get pointer to data structure associated with handle
60 0 : Data* GetDataForHandle(int handle) {
61 0 : const int pos = handle - first_handle_;
62 0 : if (pos < 0 || pos >= num_handles_) return NULL;
63 0 : if (used_[pos] <= 0) {
64 0 : NaClLog(LOG_FATAL, "using released resource of type %s\n", type_);
65 0 : return NULL;
66 : }
67 0 : return &data_[pos];
68 : }
69 :
70 : private:
71 : Data* const data_;
72 : };
73 :
74 :
75 : struct NaClSrpcArg;
76 : // When the a nexe needs to make ppapi calls via srpc,
77 : // argument of type "struct PP_Var" are "srpc serialized".
78 : // So the underlying value is wrapped twice.
79 : // The helper functions below help with extracting and setting the actual value.
80 : std::string GetMarshalledJSString(NaClSrpcArg* arg);
81 : int GetMarshalledJSInt(NaClSrpcArg* arg);
82 : bool GetMarshalledJSBool(NaClSrpcArg* arg);
83 :
84 : void SetMarshalledJSInt(NaClSrpcArg* arg, int val);
85 : #endif /* NATIVE_CLIENT_SRC_TRUSTED_SEL_LDR_UNIVERSAL_PEPPER_EMU_HELPER_H_ */
|