aboutsummaryrefslogtreecommitdiff
path: root/modules/include/module_common_types.cc
blob: 86f753356dc73ca8df9057489698fc37d76d082c (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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
 *  Copyright (c) 2018 The WebRTC project authors. All Rights Reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#include "modules/include/module_common_types.h"

#include <string.h>

#include <cstdint>
#include <utility>

#include "rtc_base/numerics/safe_conversions.h"

namespace webrtc {

RTPFragmentationHeader::RTPFragmentationHeader()
    : fragmentationVectorSize(0),
      fragmentationOffset(nullptr),
      fragmentationLength(nullptr) {}

RTPFragmentationHeader::RTPFragmentationHeader(RTPFragmentationHeader&& other)
    : RTPFragmentationHeader() {
  swap(*this, other);
}

RTPFragmentationHeader& RTPFragmentationHeader::operator=(
    RTPFragmentationHeader&& other) {
  swap(*this, other);
  return *this;
}

RTPFragmentationHeader::~RTPFragmentationHeader() {
  delete[] fragmentationOffset;
  delete[] fragmentationLength;
}

void swap(RTPFragmentationHeader& a, RTPFragmentationHeader& b) {
  using std::swap;
  swap(a.fragmentationVectorSize, b.fragmentationVectorSize);
  swap(a.fragmentationOffset, b.fragmentationOffset);
  swap(a.fragmentationLength, b.fragmentationLength);
}

void RTPFragmentationHeader::CopyFrom(const RTPFragmentationHeader& src) {
  if (this == &src) {
    return;
  }

  if (src.fragmentationVectorSize != fragmentationVectorSize) {
    // new size of vectors

    // delete old
    delete[] fragmentationOffset;
    fragmentationOffset = nullptr;
    delete[] fragmentationLength;
    fragmentationLength = nullptr;

    if (src.fragmentationVectorSize > 0) {
      // allocate new
      if (src.fragmentationOffset) {
        fragmentationOffset = new size_t[src.fragmentationVectorSize];
      }
      if (src.fragmentationLength) {
        fragmentationLength = new size_t[src.fragmentationVectorSize];
      }
    }
    // set new size
    fragmentationVectorSize = src.fragmentationVectorSize;
  }

  if (src.fragmentationVectorSize > 0) {
    // copy values
    if (src.fragmentationOffset) {
      memcpy(fragmentationOffset, src.fragmentationOffset,
             src.fragmentationVectorSize * sizeof(size_t));
    }
    if (src.fragmentationLength) {
      memcpy(fragmentationLength, src.fragmentationLength,
             src.fragmentationVectorSize * sizeof(size_t));
    }
  }
}

void RTPFragmentationHeader::Resize(size_t size) {
  const uint16_t size16 = rtc::dchecked_cast<uint16_t>(size);
  if (fragmentationVectorSize < size16) {
    uint16_t oldVectorSize = fragmentationVectorSize;
    {
      // offset
      size_t* oldOffsets = fragmentationOffset;
      fragmentationOffset = new size_t[size16];
      memset(fragmentationOffset + oldVectorSize, 0,
             sizeof(size_t) * (size16 - oldVectorSize));
      // copy old values
      memcpy(fragmentationOffset, oldOffsets, sizeof(size_t) * oldVectorSize);
      delete[] oldOffsets;
    }
    // length
    {
      size_t* oldLengths = fragmentationLength;
      fragmentationLength = new size_t[size16];
      memset(fragmentationLength + oldVectorSize, 0,
             sizeof(size_t) * (size16 - oldVectorSize));
      memcpy(fragmentationLength, oldLengths, sizeof(size_t) * oldVectorSize);
      delete[] oldLengths;
    }
    fragmentationVectorSize = size16;
  }
}

}  // namespace webrtc