| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium 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 #ifndef COURGETTE_MEMORY_ALLOCATOR_H_ | 5 #ifndef COURGETTE_MEMORY_ALLOCATOR_H_ |
| 6 #define COURGETTE_MEMORY_ALLOCATOR_H_ | 6 #define COURGETTE_MEMORY_ALLOCATOR_H_ |
| 7 | 7 |
| 8 #include <stddef.h> | 8 #include <cstddef> |
| 9 #include <stdint.h> | 9 #include <cstdint> |
| 10 #include <stdlib.h> | 10 #include <cstdlib> |
| 11 | 11 |
| 12 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
| 13 #include "base/files/file.h" | 13 #include "base/files/file.h" |
| 14 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
| 15 #include "base/logging.h" | 15 #include "base/logging.h" |
| 16 #include "base/process/memory.h" | 16 #include "base/process/memory.h" |
| 17 | 17 |
| 18 #ifndef NDEBUG | 18 #ifndef NDEBUG |
| 19 | 19 |
| 20 // A helper class to track down call sites that are not handling error cases. | 20 // A helper class to track down call sites that are not handling error cases. |
| (...skipping 303 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 324 #endif // OS_WIN | 324 #endif // OS_WIN |
| 325 | 325 |
| 326 // Manages a growable buffer. The buffer allocation is done by the | 326 // Manages a growable buffer. The buffer allocation is done by the |
| 327 // MemoryAllocator class. This class will not throw exceptions so call sites | 327 // MemoryAllocator class. This class will not throw exceptions so call sites |
| 328 // must be prepared to handle memory allocation failures. | 328 // must be prepared to handle memory allocation failures. |
| 329 // The interface is STL inspired to avoid having to make too many changes | 329 // The interface is STL inspired to avoid having to make too many changes |
| 330 // to code that previously was using STL. | 330 // to code that previously was using STL. |
| 331 template<typename T, class Allocator = MemoryAllocator<T> > | 331 template<typename T, class Allocator = MemoryAllocator<T> > |
| 332 class NoThrowBuffer { | 332 class NoThrowBuffer { |
| 333 public: | 333 public: |
| 334 typedef T value_type; | 334 using value_type = T; |
| 335 using iterator = T*; |
| 336 using const_iterator = const T*; |
| 337 |
| 335 static const size_t kAllocationFailure = 0xffffffff; | 338 static const size_t kAllocationFailure = 0xffffffff; |
| 336 static const size_t kStartSize = sizeof(T) > 0x100 ? 1 : 0x100 / sizeof(T); | 339 static const size_t kStartSize = sizeof(T) > 0x100 ? 1 : 0x100 / sizeof(T); |
| 337 | 340 |
| 338 NoThrowBuffer() : buffer_(NULL), size_(0), alloc_size_(0) { | 341 NoThrowBuffer() : buffer_(NULL), size_(0), alloc_size_(0) { |
| 339 } | 342 } |
| 340 | 343 |
| 341 ~NoThrowBuffer() { | 344 ~NoThrowBuffer() { |
| 342 clear(); | 345 clear(); |
| 343 } | 346 } |
| 344 | 347 |
| (...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 436 } | 439 } |
| 437 | 440 |
| 438 const T& back() const { | 441 const T& back() const { |
| 439 return buffer_[size_ - 1]; | 442 return buffer_[size_ - 1]; |
| 440 } | 443 } |
| 441 | 444 |
| 442 T& back() { | 445 T& back() { |
| 443 return buffer_[size_ - 1]; | 446 return buffer_[size_ - 1]; |
| 444 } | 447 } |
| 445 | 448 |
| 446 const T* begin() const { | 449 const_iterator begin() const { |
| 447 if (!size_) | 450 if (!size_) |
| 448 return NULL; | 451 return NULL; |
| 449 return buffer_; | 452 return buffer_; |
| 450 } | 453 } |
| 451 | 454 |
| 452 T* begin() { | 455 iterator begin() { |
| 453 if (!size_) | 456 if (!size_) |
| 454 return NULL; | 457 return NULL; |
| 455 return buffer_; | 458 return buffer_; |
| 456 } | 459 } |
| 457 | 460 |
| 458 const T* end() const { | 461 const_iterator end() const { |
| 459 if (!size_) | 462 if (!size_) |
| 460 return NULL; | 463 return NULL; |
| 461 return buffer_ + size_; | 464 return buffer_ + size_; |
| 462 } | 465 } |
| 463 | 466 |
| 464 T* end() { | 467 iterator end() { |
| 465 if (!size_) | 468 if (!size_) |
| 466 return NULL; | 469 return NULL; |
| 467 return buffer_ + size_; | 470 return buffer_ + size_; |
| 468 } | 471 } |
| 469 | 472 |
| 470 const T& operator[](size_t index) const { | 473 const T& operator[](size_t index) const { |
| 471 DCHECK(index < size_); | 474 DCHECK(index < size_); |
| 472 return buffer_[index]; | 475 return buffer_[index]; |
| 473 } | 476 } |
| 474 | 477 |
| (...skipping 22 matching lines...) Expand all Loading... |
| 497 protected: | 500 protected: |
| 498 T* buffer_; | 501 T* buffer_; |
| 499 size_t size_; // how much of the buffer we're using. | 502 size_t size_; // how much of the buffer we're using. |
| 500 size_t alloc_size_; // how much space we have allocated. | 503 size_t alloc_size_; // how much space we have allocated. |
| 501 Allocator alloc_; | 504 Allocator alloc_; |
| 502 }; | 505 }; |
| 503 | 506 |
| 504 } // namespace courgette | 507 } // namespace courgette |
| 505 | 508 |
| 506 #endif // COURGETTE_MEMORY_ALLOCATOR_H_ | 509 #endif // COURGETTE_MEMORY_ALLOCATOR_H_ |
| OLD | NEW |