summaryrefslogtreecommitdiffstats
path: root/native_client_sdk/src/libraries/nacl_io/socket/packet.cc
blob: 69a894f64f14a6bb29c7ec1f8d5362f4a649639f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// Copyright 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "nacl_io/socket/packet.h"

#include <assert.h>
#include <string.h>

#include "nacl_io/pepper_interface.h"

namespace nacl_io {

Packet::Packet(PepperInterface* ppapi)
    : ppapi_(ppapi), addr_(0), buffer_(NULL), len_(0) {
}

Packet::~Packet() {
  if ((NULL != ppapi_) && addr_)
    ppapi_->ReleaseResource(addr_);
  free(buffer_);
}

void Packet::Copy(const void* buffer, size_t len, PP_Resource addr) {
  addr_ = addr;
  len_ = len;
  buffer_ = (char*)malloc(len);
  assert(buffer_);

  memcpy(buffer_, buffer, len);
  if (addr && (NULL != ppapi_))
    ppapi_->AddRefResource(addr);
}

}  // namespace nacl_io