This source file includes following definitions.
- FillVec
- FreeSrpcArg
- FreeArguments
- Init
- FreeAll
#include "ppapi/native_client/src/trusted/plugin/srpc_params.h"
#include <stdlib.h>
#include "native_client/src/shared/srpc/nacl_srpc.h"
namespace plugin {
namespace {
bool FillVec(NaClSrpcArg* vec[], const char* types) {
const size_t kLength = strlen(types);
if (kLength > NACL_SRPC_MAX_ARGS) {
return false;
}
if (kLength > 0) {
NaClSrpcArg* args =
reinterpret_cast<NaClSrpcArg*>(malloc(kLength * sizeof(*args)));
if (NULL == args) {
return false;
}
memset(static_cast<void*>(args), 0, kLength * sizeof(*args));
for (size_t i = 0; i < kLength; ++i) {
vec[i] = &args[i];
args[i].tag = static_cast<NaClSrpcArgType>(types[i]);
}
}
vec[kLength] = NULL;
return true;
}
void FreeSrpcArg(NaClSrpcArg* arg) {
switch (arg->tag) {
case NACL_SRPC_ARG_TYPE_CHAR_ARRAY:
free(arg->arrays.carr);
break;
case NACL_SRPC_ARG_TYPE_DOUBLE_ARRAY:
free(arg->arrays.darr);
break;
case NACL_SRPC_ARG_TYPE_HANDLE:
break;
case NACL_SRPC_ARG_TYPE_INT_ARRAY:
free(arg->arrays.iarr);
break;
case NACL_SRPC_ARG_TYPE_LONG_ARRAY:
free(arg->arrays.larr);
break;
case NACL_SRPC_ARG_TYPE_STRING:
free(arg->arrays.str);
break;
case NACL_SRPC_ARG_TYPE_VARIANT_ARRAY:
if (arg->arrays.varr) {
for (uint32_t i = 0; i < arg->u.count; i++) {
FreeSrpcArg(&arg->arrays.varr[i]);
}
}
break;
case NACL_SRPC_ARG_TYPE_OBJECT:
break;
case NACL_SRPC_ARG_TYPE_BOOL:
case NACL_SRPC_ARG_TYPE_DOUBLE:
case NACL_SRPC_ARG_TYPE_INT:
case NACL_SRPC_ARG_TYPE_LONG:
case NACL_SRPC_ARG_TYPE_INVALID:
default:
break;
}
}
void FreeArguments(NaClSrpcArg* vec[]) {
if (NULL == vec[0]) {
return;
}
for (NaClSrpcArg** argp = vec; *argp; ++argp) {
FreeSrpcArg(*argp);
}
free(vec[0]);
}
}
bool SrpcParams::Init(const char* in_types, const char* out_types) {
if (!FillVec(ins_, in_types)) {
return false;
}
if (!FillVec(outs_, out_types)) {
FreeArguments(ins_);
return false;
}
return true;
}
void SrpcParams::FreeAll() {
FreeArguments(ins_);
FreeArguments(outs_);
memset(ins_, 0, sizeof(ins_));
memset(outs_, 0, sizeof(outs_));
}
}