LCOV - code coverage report
Current view: directory - src/trusted/sel_universal - pepper_emu_helper.cc (source / functions) Found Hit Coverage
Test: coverage.lcov Lines: 61 12 19.7 %
Date: 2012-02-16 Functions: 0 0 -

       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                 : 
       7                 : #include <string>
       8                 : 
       9                 : #include "ppapi/c/pp_var.h"
      10                 : 
      11                 : #include "native_client/src/shared/platform/nacl_check.h"
      12                 : #include "native_client/src/shared/platform/nacl_log.h"
      13                 : 
      14                 : #include "native_client/src/shared/srpc/nacl_srpc.h"
      15                 : 
      16                 : #include "native_client/src/trusted/sel_universal/pepper_emu_helper.h"
      17                 : // This is not exactly like "struct PP_Var" so we do not pretend it is
      18                 : // c.f.: src/shared/ppapi_proxy/object_serialize.cc
      19                 : struct JSString {
      20                 :   uint32_t js_type;
      21                 :   uint32_t size;
      22                 :   char     payload[1];
      23                 : };
      24                 : 
      25                 : struct JSData {
      26                 :   uint32_t js_type;
      27                 :   uint32_t val;
      28                 : };
      29                 : 
      30                 : 
      31               0 : std::string GetMarshalledJSString(NaClSrpcArg* arg) {
      32               0 :   JSString* data = reinterpret_cast<JSString*>(arg->arrays.carr);
      33               0 :   CHECK(data->js_type == PP_VARTYPE_STRING);
      34               0 :   return std::string(data->payload, data->size);
      35                 : }
      36                 : 
      37               0 : int GetMarshalledJSInt(NaClSrpcArg* arg) {
      38               0 :   JSData* data = reinterpret_cast<JSData*>(arg->arrays.carr);
      39               0 :   CHECK(data->js_type == PP_VARTYPE_INT32);
      40               0 :   return data->val;
      41                 : }
      42                 : 
      43               0 : bool GetMarshalledJSBool(NaClSrpcArg* arg) {
      44               0 :   JSData* data = reinterpret_cast<JSData*>(arg->arrays.carr);
      45               0 :   CHECK(data->js_type == PP_VARTYPE_BOOL);
      46               0 :   return data->val != 0;
      47                 : }
      48                 : 
      49               0 : void SetMarshalledJSInt(NaClSrpcArg* arg, int val) {
      50               0 :   JSData* data = reinterpret_cast<JSData*>(malloc(sizeof *data));
      51               0 :   data->js_type = PP_VARTYPE_INT32;
      52               0 :   data->val = val;
      53                 : 
      54               0 :   arg->arrays.carr = reinterpret_cast<char*>(data);
      55               0 :   arg->u.count = sizeof *data;
      56               0 : }
      57                 : 
      58                 : // static
      59                 : ResourceBase* ResourceBase::chain_ = NULL;
      60                 : 
      61                 : // static
      62                 : // All handles will have 4 digits (or maybe more).
      63                 : // We also start each new resource type at a new thousand boundary.
      64                 : int ResourceBase::chain_handle_last_ = 1000;
      65                 : 
      66                 : // static
      67               0 : ResourceBase* ResourceBase::FindResource(int handle) {
      68               0 :   for (ResourceBase* res = chain_; res != NULL; res = res->next_) {
      69               0 :     if (res->first_handle_ <= handle &&
      70                 :         handle < (res->first_handle_ + res->num_handles_)) {
      71               0 :       return res;
      72                 :     }
      73                 :   }
      74               0 :   return NULL;
      75                 : }
      76                 : 
      77                 : 
      78              77 : static int RoundUp(int n, int r) {
      79              77 :   return (n + (r-1)) / r * r;
      80                 : }
      81                 : 
      82               0 : static bool IsUnused(int n) {
      83               0 :   return n <= 0;
      84                 : }
      85                 : 
      86              77 : ResourceBase::ResourceBase(int num_handles, const char* type)
      87                 :   : next_(chain_),
      88                 :     first_handle_(chain_handle_last_),
      89                 :     num_handles_(num_handles),
      90                 :     type_(type),
      91              77 :     used_(new int[num_handles]) {
      92           18799 :   for (int i = 0; i < num_handles; ++i) {
      93           18722 :     used_[i] = 0;
      94                 :   }
      95                 : 
      96              77 :   chain_handle_last_ = RoundUp(chain_handle_last_ + num_handles, 1000);
      97              77 :   chain_ = this;
      98              77 : }
      99                 : 
     100                 : 
     101              77 : ResourceBase::~ResourceBase() {
     102              77 :   delete [] used_;
     103              77 : }
     104                 : 
     105                 : 
     106               0 : bool ResourceBase::DecRefCount(int handle) {
     107               0 :   const int pos = handle - first_handle_;
     108               0 :   if (pos < 0 || pos >= num_handles_) return false;
     109               0 :   if (IsUnused(used_[pos])) return false;
     110               0 :   --used_[pos];
     111               0 :   return true;
     112                 : }
     113                 : 
     114                 : 
     115               0 : bool ResourceBase::IncRefCount(int handle) {
     116               0 :   const int pos = handle - first_handle_;
     117               0 :   if (pos < 0 || pos >= num_handles_) return false;
     118               0 :   if (IsUnused(used_[pos])) return false;
     119               0 :   ++used_[pos];
     120               0 :   return true;
     121                 : }
     122                 : 
     123                 : 
     124               0 : int ResourceBase::GetRefCount(int handle) {
     125               0 :   const int pos = handle - first_handle_;
     126               0 :   if (pos < 0 || pos >= num_handles_) return -1;
     127               0 :   return used_[pos];
     128                 : }
     129                 : 
     130                 : 
     131               0 : int ResourceBase::Alloc() {
     132               0 :   for (int i = 0; i < num_handles_; ++i) {
     133               0 :     if (IsUnused(used_[i])) {
     134               0 :         used_[i] = 1;
     135               0 :         return first_handle_ + i;
     136                 :       }
     137                 :   }
     138               0 :   NaClLog(LOG_FATAL, "Cannot alloc resource %s\n", type_);
     139               0 :   return -1;
     140                 : }

Generated by: LCOV version 1.7