Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(199)

Side by Side Diff: core/fxcrt/fx_basic_array.cpp

Issue 2477443002: Remove FX_BOOL from core (Closed)
Patch Set: Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « core/fxcrt/fx_basic.h ('k') | core/fxcrt/fx_basic_bstring.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 PDFium Authors. All rights reserved. 1 // Copyright 2014 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com 5 // Original code copyright 2014 Foxit Software Inc. http://www.foxitsoftware.com
6 6
7 #include "core/fxcrt/fx_basic.h" 7 #include "core/fxcrt/fx_basic.h"
8 #include "third_party/base/numerics/safe_math.h" 8 #include "third_party/base/numerics/safe_math.h"
9 9
10 CFX_BasicArray::CFX_BasicArray(int unit_size) 10 CFX_BasicArray::CFX_BasicArray(int unit_size)
11 : m_pData(nullptr), m_nSize(0), m_nMaxSize(0) { 11 : m_pData(nullptr), m_nSize(0), m_nMaxSize(0) {
12 if (unit_size < 0 || unit_size > (1 << 28)) { 12 if (unit_size < 0 || unit_size > (1 << 28)) {
13 m_nUnitSize = 4; 13 m_nUnitSize = 4;
14 } else { 14 } else {
15 m_nUnitSize = unit_size; 15 m_nUnitSize = unit_size;
16 } 16 }
17 } 17 }
18 CFX_BasicArray::~CFX_BasicArray() { 18 CFX_BasicArray::~CFX_BasicArray() {
19 FX_Free(m_pData); 19 FX_Free(m_pData);
20 } 20 }
21 FX_BOOL CFX_BasicArray::SetSize(int nNewSize) { 21 bool CFX_BasicArray::SetSize(int nNewSize) {
22 if (nNewSize <= 0) { 22 if (nNewSize <= 0) {
23 FX_Free(m_pData); 23 FX_Free(m_pData);
24 m_pData = nullptr; 24 m_pData = nullptr;
25 m_nSize = m_nMaxSize = 0; 25 m_nSize = m_nMaxSize = 0;
26 return 0 == nNewSize; 26 return 0 == nNewSize;
27 } 27 }
28 28
29 if (!m_pData) { 29 if (!m_pData) {
30 pdfium::base::CheckedNumeric<int> totalSize = nNewSize; 30 pdfium::base::CheckedNumeric<int> totalSize = nNewSize;
31 totalSize *= m_nUnitSize; 31 totalSize *= m_nUnitSize;
32 if (!totalSize.IsValid()) { 32 if (!totalSize.IsValid()) {
33 m_nSize = m_nMaxSize = 0; 33 m_nSize = m_nMaxSize = 0;
34 return FALSE; 34 return false;
35 } 35 }
36 m_pData = FX_Alloc(uint8_t, totalSize.ValueOrDie()); 36 m_pData = FX_Alloc(uint8_t, totalSize.ValueOrDie());
37 m_nSize = m_nMaxSize = nNewSize; 37 m_nSize = m_nMaxSize = nNewSize;
38 } else if (nNewSize <= m_nMaxSize) { 38 } else if (nNewSize <= m_nMaxSize) {
39 if (nNewSize > m_nSize) { 39 if (nNewSize > m_nSize) {
40 FXSYS_memset(m_pData + m_nSize * m_nUnitSize, 0, 40 FXSYS_memset(m_pData + m_nSize * m_nUnitSize, 0,
41 (nNewSize - m_nSize) * m_nUnitSize); 41 (nNewSize - m_nSize) * m_nUnitSize);
42 } 42 }
43 m_nSize = nNewSize; 43 m_nSize = nNewSize;
44 } else { 44 } else {
45 int nNewMax = nNewSize < m_nMaxSize ? m_nMaxSize : nNewSize; 45 int nNewMax = nNewSize < m_nMaxSize ? m_nMaxSize : nNewSize;
46 pdfium::base::CheckedNumeric<int> totalSize = nNewMax; 46 pdfium::base::CheckedNumeric<int> totalSize = nNewMax;
47 totalSize *= m_nUnitSize; 47 totalSize *= m_nUnitSize;
48 if (!totalSize.IsValid() || nNewMax < m_nSize) { 48 if (!totalSize.IsValid() || nNewMax < m_nSize) {
49 return FALSE; 49 return false;
50 } 50 }
51 uint8_t* pNewData = FX_Realloc(uint8_t, m_pData, totalSize.ValueOrDie()); 51 uint8_t* pNewData = FX_Realloc(uint8_t, m_pData, totalSize.ValueOrDie());
52 if (!pNewData) { 52 if (!pNewData) {
53 return FALSE; 53 return false;
54 } 54 }
55 FXSYS_memset(pNewData + m_nSize * m_nUnitSize, 0, 55 FXSYS_memset(pNewData + m_nSize * m_nUnitSize, 0,
56 (nNewMax - m_nSize) * m_nUnitSize); 56 (nNewMax - m_nSize) * m_nUnitSize);
57 m_pData = pNewData; 57 m_pData = pNewData;
58 m_nSize = nNewSize; 58 m_nSize = nNewSize;
59 m_nMaxSize = nNewMax; 59 m_nMaxSize = nNewMax;
60 } 60 }
61 return TRUE; 61 return true;
62 } 62 }
63 FX_BOOL CFX_BasicArray::Append(const CFX_BasicArray& src) { 63 bool CFX_BasicArray::Append(const CFX_BasicArray& src) {
64 int nOldSize = m_nSize; 64 int nOldSize = m_nSize;
65 pdfium::base::CheckedNumeric<int> newSize = m_nSize; 65 pdfium::base::CheckedNumeric<int> newSize = m_nSize;
66 newSize += src.m_nSize; 66 newSize += src.m_nSize;
67 if (m_nUnitSize != src.m_nUnitSize || !newSize.IsValid() || 67 if (m_nUnitSize != src.m_nUnitSize || !newSize.IsValid() ||
68 !SetSize(newSize.ValueOrDie())) { 68 !SetSize(newSize.ValueOrDie())) {
69 return FALSE; 69 return false;
70 } 70 }
71 71
72 FXSYS_memcpy(m_pData + nOldSize * m_nUnitSize, src.m_pData, 72 FXSYS_memcpy(m_pData + nOldSize * m_nUnitSize, src.m_pData,
73 src.m_nSize * m_nUnitSize); 73 src.m_nSize * m_nUnitSize);
74 return TRUE; 74 return true;
75 } 75 }
76 FX_BOOL CFX_BasicArray::Copy(const CFX_BasicArray& src) { 76 bool CFX_BasicArray::Copy(const CFX_BasicArray& src) {
77 if (!SetSize(src.m_nSize)) { 77 if (!SetSize(src.m_nSize)) {
78 return FALSE; 78 return false;
79 } 79 }
80 FXSYS_memcpy(m_pData, src.m_pData, src.m_nSize * m_nUnitSize); 80 FXSYS_memcpy(m_pData, src.m_pData, src.m_nSize * m_nUnitSize);
81 return TRUE; 81 return true;
82 } 82 }
83 uint8_t* CFX_BasicArray::InsertSpaceAt(int nIndex, int nCount) { 83 uint8_t* CFX_BasicArray::InsertSpaceAt(int nIndex, int nCount) {
84 if (nIndex < 0 || nCount <= 0) { 84 if (nIndex < 0 || nCount <= 0) {
85 return nullptr; 85 return nullptr;
86 } 86 }
87 if (nIndex >= m_nSize) { 87 if (nIndex >= m_nSize) {
88 if (!SetSize(nIndex + nCount)) { 88 if (!SetSize(nIndex + nCount)) {
89 return nullptr; 89 return nullptr;
90 } 90 }
91 } else { 91 } else {
92 int nOldSize = m_nSize; 92 int nOldSize = m_nSize;
93 if (!SetSize(m_nSize + nCount)) { 93 if (!SetSize(m_nSize + nCount)) {
94 return nullptr; 94 return nullptr;
95 } 95 }
96 FXSYS_memmove(m_pData + (nIndex + nCount) * m_nUnitSize, 96 FXSYS_memmove(m_pData + (nIndex + nCount) * m_nUnitSize,
97 m_pData + nIndex * m_nUnitSize, 97 m_pData + nIndex * m_nUnitSize,
98 (nOldSize - nIndex) * m_nUnitSize); 98 (nOldSize - nIndex) * m_nUnitSize);
99 FXSYS_memset(m_pData + nIndex * m_nUnitSize, 0, nCount * m_nUnitSize); 99 FXSYS_memset(m_pData + nIndex * m_nUnitSize, 0, nCount * m_nUnitSize);
100 } 100 }
101 return m_pData + nIndex * m_nUnitSize; 101 return m_pData + nIndex * m_nUnitSize;
102 } 102 }
103 FX_BOOL CFX_BasicArray::RemoveAt(int nIndex, int nCount) { 103 bool CFX_BasicArray::RemoveAt(int nIndex, int nCount) {
104 if (nIndex < 0 || nCount <= 0 || m_nSize < nIndex + nCount) { 104 if (nIndex < 0 || nCount <= 0 || m_nSize < nIndex + nCount) {
105 return FALSE; 105 return false;
106 } 106 }
107 int nMoveCount = m_nSize - (nIndex + nCount); 107 int nMoveCount = m_nSize - (nIndex + nCount);
108 if (nMoveCount) { 108 if (nMoveCount) {
109 FXSYS_memmove(m_pData + nIndex * m_nUnitSize, 109 FXSYS_memmove(m_pData + nIndex * m_nUnitSize,
110 m_pData + (nIndex + nCount) * m_nUnitSize, 110 m_pData + (nIndex + nCount) * m_nUnitSize,
111 nMoveCount * m_nUnitSize); 111 nMoveCount * m_nUnitSize);
112 } 112 }
113 m_nSize -= nCount; 113 m_nSize -= nCount;
114 return TRUE; 114 return true;
115 } 115 }
116 FX_BOOL CFX_BasicArray::InsertAt(int nStartIndex, 116 bool CFX_BasicArray::InsertAt(int nStartIndex,
117 const CFX_BasicArray* pNewArray) { 117 const CFX_BasicArray* pNewArray) {
118 if (!pNewArray) { 118 if (!pNewArray) {
119 return FALSE; 119 return false;
120 } 120 }
121 if (pNewArray->m_nSize == 0) { 121 if (pNewArray->m_nSize == 0) {
122 return TRUE; 122 return true;
123 } 123 }
124 if (!InsertSpaceAt(nStartIndex, pNewArray->m_nSize)) { 124 if (!InsertSpaceAt(nStartIndex, pNewArray->m_nSize)) {
125 return FALSE; 125 return false;
126 } 126 }
127 FXSYS_memcpy(m_pData + nStartIndex * m_nUnitSize, pNewArray->m_pData, 127 FXSYS_memcpy(m_pData + nStartIndex * m_nUnitSize, pNewArray->m_pData,
128 pNewArray->m_nSize * m_nUnitSize); 128 pNewArray->m_nSize * m_nUnitSize);
129 return TRUE; 129 return true;
130 } 130 }
131 const void* CFX_BasicArray::GetDataPtr(int index) const { 131 const void* CFX_BasicArray::GetDataPtr(int index) const {
132 if (index < 0 || index >= m_nSize || !m_pData) { 132 if (index < 0 || index >= m_nSize || !m_pData) {
133 return nullptr; 133 return nullptr;
134 } 134 }
135 return m_pData + index * m_nUnitSize; 135 return m_pData + index * m_nUnitSize;
136 } 136 }
OLDNEW
« no previous file with comments | « core/fxcrt/fx_basic.h ('k') | core/fxcrt/fx_basic_bstring.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698