OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #ifndef CHROME_BROWSER_SYNC_NOTIFIER_BASE_FASTALLOC_H_ |
| 6 #define CHROME_BROWSER_SYNC_NOTIFIER_BASE_FASTALLOC_H_ |
| 7 |
| 8 #include <assert.h> |
| 9 |
| 10 namespace notifier { |
| 11 |
| 12 template<class T, size_t Size> |
| 13 class FastAlloc { |
| 14 public: |
| 15 FastAlloc() : buffer_(NULL), size_(0) { }; |
| 16 ~FastAlloc() { freeBuffer(); } |
| 17 T* get_buffer(size_t size) { |
| 18 if (size_ != 0) { |
| 19 // We only allow one call to get_buffer. This makes the logic here |
| 20 // simpler, and the user has to worry less about someone else calling |
| 21 // get_buffer again on the same FastAlloc object and invalidating the |
| 22 // memory they were using. |
| 23 assert(false && "get_buffer may one be called once"); |
| 24 return NULL; |
| 25 } |
| 26 |
| 27 if (size <= Size) { |
| 28 buffer_ = internal_buffer_; |
| 29 } else { |
| 30 buffer_ = new T[size]; |
| 31 } |
| 32 |
| 33 if (buffer_ != NULL) { |
| 34 size_ = size; |
| 35 } |
| 36 return buffer_; |
| 37 } |
| 38 |
| 39 private: |
| 40 void freeBuffer() { |
| 41 #ifdef DEBUG |
| 42 memset(buffer_, 0xCC, size_ * sizeof(T)); |
| 43 #endif |
| 44 |
| 45 if (buffer_ != NULL && buffer_ != internal_buffer_) { |
| 46 delete[] buffer_; |
| 47 } |
| 48 buffer_ = NULL; |
| 49 size_ = 0; |
| 50 } |
| 51 |
| 52 T* buffer_; |
| 53 T internal_buffer_[Size]; |
| 54 size_t size_; |
| 55 }; |
| 56 |
| 57 } |
| 58 |
| 59 #endif // CHROME_BROWSER_SYNC_NOTIFIER_BASE_FASTALLOC_H_ |
OLD | NEW |