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

Side by Side Diff: core/src/fxcrt/fx_basic_buffer.cpp

Issue 372473003: Remove custom memory manager (Closed) Base URL: https://pdfium.googlesource.com/pdfium.git@master
Patch Set: Change malloc to calloc Created 6 years, 5 months 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/src/fxcrt/fx_basic_bstring.cpp ('k') | core/src/fxcrt/fx_basic_list.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 "../../include/fxcrt/fx_basic.h" 7 #include "../../include/fxcrt/fx_basic.h"
8 FX_STRSIZE FX_ftoa(FX_FLOAT f, FX_LPSTR buf); 8 FX_STRSIZE FX_ftoa(FX_FLOAT f, FX_LPSTR buf);
9 CFX_BinaryBuf::CFX_BinaryBuf(IFX_Allocator* pAllocator) 9 CFX_BinaryBuf::CFX_BinaryBuf()
10 : m_pAllocator(pAllocator) 10 : m_AllocStep(0)
11 , m_AllocStep(0)
12 , m_pBuffer(NULL) 11 , m_pBuffer(NULL)
13 , m_DataSize(0) 12 , m_DataSize(0)
14 , m_AllocSize(0) 13 , m_AllocSize(0)
15 { 14 {
16 } 15 }
17 CFX_BinaryBuf::CFX_BinaryBuf(FX_STRSIZE size, IFX_Allocator* pAllocator) 16 CFX_BinaryBuf::CFX_BinaryBuf(FX_STRSIZE size)
18 : m_pAllocator(pAllocator) 17 : m_AllocStep(0)
19 , m_AllocStep(0)
20 , m_DataSize(size) 18 , m_DataSize(size)
21 , m_AllocSize(size) 19 , m_AllocSize(size)
22 { 20 {
23 m_pBuffer = FX_Allocator_Alloc(m_pAllocator, FX_BYTE, size); 21 m_pBuffer = FX_Alloc(FX_BYTE, size);
24 } 22 }
25 CFX_BinaryBuf::~CFX_BinaryBuf() 23 CFX_BinaryBuf::~CFX_BinaryBuf()
26 { 24 {
27 if (m_pBuffer) { 25 if (m_pBuffer) {
28 FX_Allocator_Free(m_pAllocator, m_pBuffer); 26 FX_Free(m_pBuffer);
29 } 27 }
30 } 28 }
31 void CFX_BinaryBuf::Delete(int start_index, int count) 29 void CFX_BinaryBuf::Delete(int start_index, int count)
32 { 30 {
33 if (!m_pBuffer || start_index < 0 || start_index + count > m_DataSize) { 31 if (!m_pBuffer || start_index < 0 || start_index + count > m_DataSize) {
34 return; 32 return;
35 } 33 }
36 FXSYS_memmove32(m_pBuffer + start_index, m_pBuffer + start_index + count, m_ DataSize - start_index - count); 34 FXSYS_memmove32(m_pBuffer + start_index, m_pBuffer + start_index + count, m_ DataSize - start_index - count);
37 m_DataSize -= count; 35 m_DataSize -= count;
38 } 36 }
39 void CFX_BinaryBuf::Clear() 37 void CFX_BinaryBuf::Clear()
40 { 38 {
41 m_DataSize = 0; 39 m_DataSize = 0;
42 } 40 }
43 void CFX_BinaryBuf::DetachBuffer() 41 void CFX_BinaryBuf::DetachBuffer()
44 { 42 {
45 m_DataSize = 0; 43 m_DataSize = 0;
46 m_pBuffer = NULL; 44 m_pBuffer = NULL;
47 m_AllocSize = 0; 45 m_AllocSize = 0;
48 } 46 }
49 void CFX_BinaryBuf::AttachData(void* buffer, FX_STRSIZE size) 47 void CFX_BinaryBuf::AttachData(void* buffer, FX_STRSIZE size)
50 { 48 {
51 if (m_pBuffer) { 49 if (m_pBuffer) {
52 FX_Allocator_Free(m_pAllocator, m_pBuffer); 50 FX_Free(m_pBuffer);
53 } 51 }
54 m_DataSize = size; 52 m_DataSize = size;
55 m_pBuffer = (FX_LPBYTE)buffer; 53 m_pBuffer = (FX_LPBYTE)buffer;
56 m_AllocSize = size; 54 m_AllocSize = size;
57 } 55 }
58 void CFX_BinaryBuf::TakeOver(CFX_BinaryBuf& other) 56 void CFX_BinaryBuf::TakeOver(CFX_BinaryBuf& other)
59 { 57 {
60 AttachData(other.GetBuffer(), other.GetSize()); 58 AttachData(other.GetBuffer(), other.GetSize());
61 other.DetachBuffer(); 59 other.DetachBuffer();
62 } 60 }
(...skipping 16 matching lines...) Expand all
79 alloc_step = m_AllocSize / 4; 77 alloc_step = m_AllocSize / 4;
80 if (alloc_step < 128 ) { 78 if (alloc_step < 128 ) {
81 alloc_step = 128; 79 alloc_step = 128;
82 } 80 }
83 } else { 81 } else {
84 alloc_step = m_AllocStep; 82 alloc_step = m_AllocStep;
85 } 83 }
86 new_size = (new_size + alloc_step - 1) / alloc_step * alloc_step; 84 new_size = (new_size + alloc_step - 1) / alloc_step * alloc_step;
87 FX_LPBYTE pNewBuffer = m_pBuffer; 85 FX_LPBYTE pNewBuffer = m_pBuffer;
88 if (pNewBuffer) { 86 if (pNewBuffer) {
89 pNewBuffer = FX_Allocator_Realloc(m_pAllocator, FX_BYTE, m_pBuffer, new_ size); 87 pNewBuffer = FX_Realloc(FX_BYTE, m_pBuffer, new_size);
90 } else { 88 } else {
91 pNewBuffer = FX_Allocator_Alloc(m_pAllocator, FX_BYTE, new_size); 89 pNewBuffer = FX_Alloc(FX_BYTE, new_size);
92 } 90 }
93 if (pNewBuffer) { 91 if (pNewBuffer) {
94 m_pBuffer = pNewBuffer; 92 m_pBuffer = pNewBuffer;
95 m_AllocSize = new_size; 93 m_AllocSize = new_size;
96 } 94 }
97 } 95 }
98 void CFX_BinaryBuf::CopyData(const void* pStr, FX_STRSIZE size) 96 void CFX_BinaryBuf::CopyData(const void* pStr, FX_STRSIZE size)
99 { 97 {
100 if (size == 0) { 98 if (size == 0) {
101 m_DataSize = 0; 99 m_DataSize = 0;
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
136 if (!m_pBuffer) { 134 if (!m_pBuffer) {
137 return; 135 return;
138 } 136 }
139 FXSYS_memset8(m_pBuffer + m_DataSize, byte, count); 137 FXSYS_memset8(m_pBuffer + m_DataSize, byte, count);
140 m_DataSize += count; 138 m_DataSize += count;
141 } 139 }
142 CFX_ByteStringC CFX_BinaryBuf::GetByteString() const 140 CFX_ByteStringC CFX_BinaryBuf::GetByteString() const
143 { 141 {
144 return CFX_ByteStringC(m_pBuffer, m_DataSize); 142 return CFX_ByteStringC(m_pBuffer, m_DataSize);
145 } 143 }
146 void CFX_BinaryBuf::GetByteStringL(CFX_ByteStringL &str) const
147 {
148 str.Set(CFX_ByteStringC(m_pBuffer, m_DataSize), m_pAllocator);
149 }
150 CFX_ByteTextBuf& CFX_ByteTextBuf::operator << (FX_BSTR lpsz) 144 CFX_ByteTextBuf& CFX_ByteTextBuf::operator << (FX_BSTR lpsz)
151 { 145 {
152 AppendBlock((FX_LPCBYTE)lpsz, lpsz.GetLength()); 146 AppendBlock((FX_LPCBYTE)lpsz, lpsz.GetLength());
153 return *this; 147 return *this;
154 } 148 }
155 CFX_ByteTextBuf& CFX_ByteTextBuf::operator << (int i) 149 CFX_ByteTextBuf& CFX_ByteTextBuf::operator << (int i)
156 { 150 {
157 char buf[32]; 151 char buf[32];
158 FXSYS_itoa(i, buf, 10); 152 FXSYS_itoa(i, buf, 10);
159 AppendBlock(buf, (FX_STRSIZE)FXSYS_strlen(buf)); 153 AppendBlock(buf, (FX_STRSIZE)FXSYS_strlen(buf));
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
243 return *this; 237 return *this;
244 } 238 }
245 void CFX_WideTextBuf::operator =(FX_WSTR str) 239 void CFX_WideTextBuf::operator =(FX_WSTR str)
246 { 240 {
247 CopyData(str.GetPtr(), str.GetLength() * sizeof(FX_WCHAR)); 241 CopyData(str.GetPtr(), str.GetLength() * sizeof(FX_WCHAR));
248 } 242 }
249 CFX_WideStringC CFX_WideTextBuf::GetWideString() const 243 CFX_WideStringC CFX_WideTextBuf::GetWideString() const
250 { 244 {
251 return CFX_WideStringC((FX_LPCWSTR)m_pBuffer, m_DataSize / sizeof(FX_WCHAR)) ; 245 return CFX_WideStringC((FX_LPCWSTR)m_pBuffer, m_DataSize / sizeof(FX_WCHAR)) ;
252 } 246 }
253 void CFX_WideTextBuf::GetWideStringL(CFX_WideStringL& wideText) const
254 {
255 wideText.Set(CFX_WideStringC((FX_LPCWSTR)m_pBuffer, m_DataSize / sizeof(FX_W CHAR)), m_pAllocator);
256 }
257 CFX_ArchiveSaver& CFX_ArchiveSaver::operator << (FX_BYTE i) 247 CFX_ArchiveSaver& CFX_ArchiveSaver::operator << (FX_BYTE i)
258 { 248 {
259 if (m_pStream) { 249 if (m_pStream) {
260 m_pStream->WriteBlock(&i, 1); 250 m_pStream->WriteBlock(&i, 1);
261 } else { 251 } else {
262 m_SavingBuf.AppendByte(i); 252 m_SavingBuf.AppendByte(i);
263 } 253 }
264 return *this; 254 return *this;
265 } 255 }
266 CFX_ArchiveSaver& CFX_ArchiveSaver::operator << (int i) 256 CFX_ArchiveSaver& CFX_ArchiveSaver::operator << (int i)
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after
432 while (bit_left >= 8) { 422 while (bit_left >= 8) {
433 bit_left -= 8; 423 bit_left -= 8;
434 result |= m_pData[byte_pos++] << bit_left; 424 result |= m_pData[byte_pos++] << bit_left;
435 } 425 }
436 if (bit_left) { 426 if (bit_left) {
437 result |= m_pData[byte_pos] >> (8 - bit_left); 427 result |= m_pData[byte_pos] >> (8 - bit_left);
438 } 428 }
439 m_BitPos += nBits; 429 m_BitPos += nBits;
440 return result; 430 return result;
441 } 431 }
442 IFX_BufferArchive::IFX_BufferArchive(FX_STRSIZE size, IFX_Allocator* pAllocator) 432 IFX_BufferArchive::IFX_BufferArchive(FX_STRSIZE size)
443 : m_pAllocator(pAllocator) 433 : m_BufSize(size)
444 , m_BufSize(size)
445 , m_pBuffer(NULL) 434 , m_pBuffer(NULL)
446 , m_Length(0) 435 , m_Length(0)
447 { 436 {
448 } 437 }
449 void IFX_BufferArchive::Clear() 438 void IFX_BufferArchive::Clear()
450 { 439 {
451 m_Length = 0; 440 m_Length = 0;
452 if (m_pBuffer) { 441 if (m_pBuffer) {
453 FX_Allocator_Free(m_pAllocator, m_pBuffer); 442 FX_Free(m_pBuffer);
454 m_pBuffer = NULL; 443 m_pBuffer = NULL;
455 } 444 }
456 } 445 }
457 FX_BOOL IFX_BufferArchive::Flush() 446 FX_BOOL IFX_BufferArchive::Flush()
458 { 447 {
459 FX_BOOL bRet = DoWork(m_pBuffer, m_Length); 448 FX_BOOL bRet = DoWork(m_pBuffer, m_Length);
460 m_Length = 0; 449 m_Length = 0;
461 return bRet; 450 return bRet;
462 } 451 }
463 FX_INT32 IFX_BufferArchive::AppendBlock(const void* pBuf, size_t size) 452 FX_INT32 IFX_BufferArchive::AppendBlock(const void* pBuf, size_t size)
464 { 453 {
465 if (!pBuf || size < 1) { 454 if (!pBuf || size < 1) {
466 return 0; 455 return 0;
467 } 456 }
468 if (!m_pBuffer) { 457 if (!m_pBuffer) {
469 m_pBuffer = FX_Allocator_Alloc(m_pAllocator, FX_BYTE, m_BufSize); 458 m_pBuffer = FX_Alloc(FX_BYTE, m_BufSize);
470 if (!m_pBuffer) { 459 if (!m_pBuffer) {
471 return -1; 460 return -1;
472 } 461 }
473 } 462 }
474 FX_LPBYTE buffer = (FX_LPBYTE)pBuf; 463 FX_LPBYTE buffer = (FX_LPBYTE)pBuf;
475 FX_STRSIZE temp_size = (FX_STRSIZE)size; 464 FX_STRSIZE temp_size = (FX_STRSIZE)size;
476 while (temp_size > 0) { 465 while (temp_size > 0) {
477 FX_STRSIZE buf_size = FX_MIN(m_BufSize - m_Length, (FX_STRSIZE)temp_size ); 466 FX_STRSIZE buf_size = FX_MIN(m_BufSize - m_Length, (FX_STRSIZE)temp_size );
478 FXSYS_memcpy32(m_pBuffer + m_Length, buffer, buf_size); 467 FXSYS_memcpy32(m_pBuffer + m_Length, buffer, buf_size);
479 m_Length += buf_size; 468 m_Length += buf_size;
(...skipping 14 matching lines...) Expand all
494 FX_INT32 IFX_BufferArchive::AppendDWord(FX_DWORD i) 483 FX_INT32 IFX_BufferArchive::AppendDWord(FX_DWORD i)
495 { 484 {
496 char buf[32]; 485 char buf[32];
497 FXSYS_itoa(i, buf, 10); 486 FXSYS_itoa(i, buf, 10);
498 return AppendBlock(buf, (size_t)FXSYS_strlen(buf)); 487 return AppendBlock(buf, (size_t)FXSYS_strlen(buf));
499 } 488 }
500 FX_INT32 IFX_BufferArchive::AppendString(FX_BSTR lpsz) 489 FX_INT32 IFX_BufferArchive::AppendString(FX_BSTR lpsz)
501 { 490 {
502 return AppendBlock((FX_LPCBYTE)lpsz, lpsz.GetLength()); 491 return AppendBlock((FX_LPCBYTE)lpsz, lpsz.GetLength());
503 } 492 }
504 CFX_FileBufferArchive::CFX_FileBufferArchive(FX_STRSIZE size, IFX_Allocator* pAl locator) 493 CFX_FileBufferArchive::CFX_FileBufferArchive(FX_STRSIZE size)
505 : IFX_BufferArchive(size, pAllocator) 494 : IFX_BufferArchive(size)
506 , m_pFile(NULL) 495 , m_pFile(NULL)
507 , m_bTakeover(FALSE) 496 , m_bTakeover(FALSE)
508 { 497 {
509 } 498 }
510 CFX_FileBufferArchive::~CFX_FileBufferArchive() 499 CFX_FileBufferArchive::~CFX_FileBufferArchive()
511 { 500 {
512 Clear(); 501 Clear();
513 } 502 }
514 void CFX_FileBufferArchive::Clear() 503 void CFX_FileBufferArchive::Clear()
515 { 504 {
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
565 FX_BOOL CFX_FileBufferArchive::DoWork(const void* pBuf, size_t size) 554 FX_BOOL CFX_FileBufferArchive::DoWork(const void* pBuf, size_t size)
566 { 555 {
567 if (!m_pFile) { 556 if (!m_pFile) {
568 return FALSE; 557 return FALSE;
569 } 558 }
570 if (!pBuf || size < 1) { 559 if (!pBuf || size < 1) {
571 return TRUE; 560 return TRUE;
572 } 561 }
573 return m_pFile->WriteBlock(pBuf, size); 562 return m_pFile->WriteBlock(pBuf, size);
574 } 563 }
OLDNEW
« no previous file with comments | « core/src/fxcrt/fx_basic_bstring.cpp ('k') | core/src/fxcrt/fx_basic_list.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698