| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2010 The Native Client Authors. All rights reserved. | 2 * Copyright 2010 The Native Client Authors. All rights reserved. |
| 3 * Use of this source code is governed by a BSD-style license that can | 3 * Use of this source code is governed by a BSD-style license that can |
| 4 * be found in the LICENSE file. | 4 * be found in the LICENSE file. |
| 5 */ | 5 */ |
| 6 | 6 |
| 7 #include "native_client/src/shared/ppapi_proxy/plugin_core.h" | 7 #include "native_client/src/shared/ppapi_proxy/plugin_core.h" |
| 8 #include <stdio.h> | 8 #include <stdio.h> |
| 9 #include <map> | 9 #include <map> |
| 10 #include "native_client/src/include/nacl_macros.h" | 10 #include "native_client/src/include/nacl_macros.h" |
| 11 #include "native_client/src/include/portability.h" | 11 #include "native_client/src/include/portability.h" |
| 12 #include "gen/native_client/src/shared/ppapi_proxy/ppb_rpc.h" | 12 #include "gen/native_client/src/shared/ppapi_proxy/ppb_rpc.h" |
| 13 #include "native_client/src/shared/ppapi_proxy/plugin_globals.h" | 13 #include "native_client/src/shared/ppapi_proxy/plugin_globals.h" |
| 14 #include "native_client/src/shared/ppapi_proxy/plugin_resource_tracker.h" |
| 14 #include "native_client/src/shared/ppapi_proxy/utility.h" | 15 #include "native_client/src/shared/ppapi_proxy/utility.h" |
| 15 #include "ppapi/c/ppb_core.h" | 16 #include "ppapi/c/ppb_core.h" |
| 16 #include "ppapi/c/pp_completion_callback.h" | 17 #include "ppapi/c/pp_completion_callback.h" |
| 17 #include "ppapi/c/pp_resource.h" | 18 #include "ppapi/c/pp_resource.h" |
| 18 #include "ppapi/cpp/common.h" | 19 #include "ppapi/cpp/common.h" |
| 19 | 20 |
| 20 using ppapi_proxy::DebugPrintf; | 21 using ppapi_proxy::DebugPrintf; |
| 21 | 22 |
| 22 // All of the methods here are invoked from the plugin's main (UI) thread, | 23 // All of the methods here are invoked from the plugin's main (UI) thread, |
| 23 // so no locking is done. | 24 // so no locking is done. |
| 24 | 25 |
| 25 namespace { | 26 namespace { |
| 26 #ifdef __native_client__ | 27 #ifdef __native_client__ |
| 27 __thread bool is_main_thread = false; | 28 __thread bool is_main_thread = false; |
| 28 bool main_thread_marked = false; | 29 bool main_thread_marked = false; |
| 29 #endif // __native_client__ | 30 #endif // __native_client__ |
| 30 | 31 |
| 31 std::map<PP_Resource, uint32_t>* local_ref_count; | 32 // Increment the reference count for a specified resource. This only takes |
| 32 | 33 // care of the plugin's reference count - the Resource should obtain the |
| 33 // Increment the reference count for a specified resource. This is done with | 34 // browser reference when it stores the browser's Resource id. When the |
| 34 // a plugin-side cache, maintained by this helper function. Only the first time | 35 // Resource's reference count goes to zero, the destructor should make sure |
| 35 // a resource has an AddRef call requires an RPC to the browser. | 36 // the browser reference is returned. |
| 36 void AddRefResource(PP_Resource resource) { | 37 void AddRefResource(PP_Resource resource) { |
| 37 DebugPrintf("PluginCore::AddRefResource: resource=%"NACL_PRIu64"\n", | 38 DebugPrintf("PluginCore::AddRefResource: resource=%"NACL_PRIu64"\n", |
| 38 resource); | 39 resource); |
| 39 if (local_ref_count == NULL) { | 40 if (ppapi_proxy::PluginResourceTracker::Get()->AddRefResource(resource)) |
| 40 local_ref_count = new std::map<PP_Resource, uint32_t>; | 41 DebugPrintf("Warning: AddRefResource()ing a nonexistent resource"); |
| 41 } | |
| 42 if (local_ref_count->find(resource) == local_ref_count->end()) { | |
| 43 // The first time we create a local ref count object we need to increment | |
| 44 // the reference count in the browser. | |
| 45 NaClSrpcChannel* channel = ppapi_proxy::GetMainSrpcChannel(); | |
| 46 (void) PpbCoreRpcClient::PPB_Core_AddRefResource(channel, resource); | |
| 47 // And add a local (cached) reference count object. | |
| 48 (*local_ref_count)[resource] = 1; | |
| 49 return; | |
| 50 } else { | |
| 51 // Otherwise, just update the local reference count. | |
| 52 (*local_ref_count)[resource] = (*local_ref_count)[resource] + 1; | |
| 53 } | |
| 54 } | 42 } |
| 55 | 43 |
| 56 void ReleaseResource(PP_Resource resource) { | 44 void ReleaseResource(PP_Resource resource) { |
| 57 DebugPrintf("PluginCore::ReleaseResource: resource=%"NACL_PRIu64"\n", | 45 DebugPrintf("PluginCore::ReleaseResource: resource=%"NACL_PRIu64"\n", |
| 58 resource); | 46 resource); |
| 59 if (local_ref_count == NULL || | 47 if (ppapi_proxy::PluginResourceTracker::Get()->UnrefResource(resource)) |
| 60 local_ref_count->find(resource) == local_ref_count->end()) { | 48 DebugPrintf("Warning: ReleaseRefResource()ing a nonexistent resource"); |
| 61 // ERROR: How can we decrement if there is no local (cached) reference | |
| 62 // count for the specified resource? | |
| 63 return; | |
| 64 } | |
| 65 if ((*local_ref_count)[resource] > 1) { | |
| 66 // Just update the local (cached) reference count. | |
| 67 (*local_ref_count)[resource] = (*local_ref_count)[resource] - 1; | |
| 68 return; | |
| 69 } else { | |
| 70 // When the local reference count goes to zero we decrement the reference | |
| 71 // count in the browser. | |
| 72 NaClSrpcChannel* channel = ppapi_proxy::GetMainSrpcChannel(); | |
| 73 (void) PpbCoreRpcClient::PPB_Core_ReleaseResource(channel, resource); | |
| 74 // Then remove the local reference count for the resource. | |
| 75 local_ref_count->erase(resource); | |
| 76 if (local_ref_count->size() == 0) { | |
| 77 // There are no locally reference counted objects, free the map. | |
| 78 delete local_ref_count; | |
| 79 local_ref_count = NULL; | |
| 80 } | |
| 81 } | |
| 82 } | 49 } |
| 83 | 50 |
| 84 void* MemAlloc(size_t num_bytes) { | 51 void* MemAlloc(size_t num_bytes) { |
| 85 DebugPrintf("PluginCore::MemAlloc: num_bytes=%"NACL_PRIuS"\n", num_bytes); | 52 DebugPrintf("PluginCore::MemAlloc: num_bytes=%"NACL_PRIuS"\n", num_bytes); |
| 86 return malloc(num_bytes); | 53 return malloc(num_bytes); |
| 87 } | 54 } |
| 88 | 55 |
| 89 void MemFree(void* ptr) { | 56 void MemFree(void* ptr) { |
| 90 DebugPrintf("PluginCore::MemFree: ptr=%p\n", ptr); | 57 DebugPrintf("PluginCore::MemFree: ptr=%p\n", ptr); |
| 91 free(ptr); | 58 free(ptr); |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 167 // Setting this once works because the main thread will call this function | 134 // Setting this once works because the main thread will call this function |
| 168 // before calling any pthread_creates. Hence the result is already | 135 // before calling any pthread_creates. Hence the result is already |
| 169 // published before other threads might attempt to call it. | 136 // published before other threads might attempt to call it. |
| 170 main_thread_marked = true; | 137 main_thread_marked = true; |
| 171 } | 138 } |
| 172 #endif // __native_client__ | 139 #endif // __native_client__ |
| 173 } | 140 } |
| 174 | 141 |
| 175 | 142 |
| 176 } // namespace ppapi_proxy | 143 } // namespace ppapi_proxy |
| OLD | NEW |