Tree @debian/1.12.1599.102-1 (Download .tar.gz)
- ..
- protobuf
- android_jni_mock.cc
- android_jni_mock.h
- android_jni_proxy.cc
- android_jni_proxy.h
- android_jni_proxy_test.cc
- android_util.cc
- android_util.h
- android_util_test.cc
- base.gyp
- base.h
- base_test.gyp
- bitarray.h
- bitarray_test.cc
- clock_mock.cc
- clock_mock.h
- clock_mock_test.cc
- codegen_bytearray_stream.h
- codegen_bytearray_stream_test.cc
- compiler_specific.h
- config_file_stream.cc
- config_file_stream.h
- config_file_stream_test.cc
- const.h
- coordinates.h
- cpu_stats.cc
- cpu_stats.h
- cpu_stats_main.cc
- cpu_stats_test.cc
- crash_report_handler.cc
- crash_report_handler.h
- crash_report_handler_mac.mm
- crash_report_util.cc
- crash_report_util.h
- crash_report_util_test.cc
- debug.cc
- debug.h
- encryptor.cc
- encryptor.h
- encryptor_main.cc
- encryptor_test.cc
- file_stream.cc
- file_stream.h
- file_util.cc
- file_util.h
- file_util_test.cc
- flags.cc
- flags.h
- flags_test.cc
- freelist.h
- gen_character_set.py
- gen_config_file_stream_data.py
- hash.cc
- hash_tables.h
- hash_tables_test.cc
- iconv.cc
- init.cc
- init.h
- init_test.cc
- iterator_adapter.h
- iterator_adapter_test.cc
- japanese_util_rule.h
- linked_ptr.h
- logging.cc
- logging.h
- logging_test.cc
- mac_process.h
- mac_process.mm
- mac_util.h
- mac_util.mm
- mac_util_main.cc
- mac_util_test.mm
- mmap.cc
- mmap.h
- mmap_test.cc
- multifile.cc
- multifile.h
- multifile_test.cc
- mutex.cc
- mutex.h
- mutex_test.cc
- nacl_js_proxy.cc
- nacl_js_proxy.h
- namespace.h
- number_util.cc
- number_util.h
- number_util_test.cc
- openssl_config.sh
- password_manager.cc
- password_manager.h
- password_manager_main.cc
- password_manager_test.cc
- pepper_file_system_mock.cc
- pepper_file_system_mock.h
- pepper_file_util.cc
- pepper_file_util.h
- pepper_scoped_obj.h
- port.h
- process.cc
- process.h
- process_main.cc
- process_mutex.cc
- process_mutex.h
- process_mutex_main.cc
- process_mutex_test.cc
- run_level.cc
- run_level.h
- run_level_main.cc
- scheduler.cc
- scheduler.h
- scheduler_stub.cc
- scheduler_stub.h
- scheduler_stub_test.cc
- scheduler_test.cc
- scoped_cftyperef.h
- scoped_handle.cc
- scoped_handle.h
- scoped_nsobject.h
- scoped_ptr.h
- singleton.cc
- singleton.h
- singleton_test.cc
- stl_util.h
- stl_util_test.cc
- stopwatch.cc
- stopwatch.h
- stopwatch_main.cc
- stopwatch_test.cc
- string_piece.cc
- string_piece.h
- string_piece_test.cc
- system_util.cc
- system_util.h
- system_util_test.cc
- task_manager.cc
- task_manager.h
- task_manager_test.cc
- task_runner.cc
- task_runner.h
- task_runner_test.cc
- task_token.cc
- task_token.h
- task_token_test.cc
- text_converter.cc
- text_converter.h
- text_converter_compiler.cc
- text_normalizer.cc
- text_normalizer.h
- text_normalizer_test.cc
- thread.cc
- thread.h
- thread_annotations.h
- thread_test.cc
- timer.cc
- timer.h
- timer_main.cc
- timer_test.cc
- trie.h
- trie_test.cc
- unnamed_event.cc
- unnamed_event.h
- unnamed_event_test.cc
- update_checker.cc
- update_checker.h
- update_util.cc
- update_util.h
- update_util_test.cc
- url.cc
- url.h
- url_test.cc
- util.cc
- util.h
- util_test.cc
- version.cc
- version.h
- version_def_template.h
- version_test.cc
- win_api_test_helper.cc
- win_api_test_helper.h
- win_api_test_helper_test.cc
- win_font_test_helper.cc
- win_font_test_helper.h
- win_sandbox.cc
- win_sandbox.h
- win_sandbox_test.cc
- win_util.cc
- win_util.h
- winmain.h
freelist.h @debian/1.12.1599.102-1 — raw · history · blame
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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | // Copyright 2010-2013, Google Inc. // All rights reserved. // // Redistribution and use in source and binary forms, with or without // modification, are permitted provided that the following conditions are // met: // // * Redistributions of source code must retain the above copyright // notice, this list of conditions and the following disclaimer. // * Redistributions in binary form must reproduce the above // copyright notice, this list of conditions and the following disclaimer // in the documentation and/or other materials provided with the // distribution. // * Neither the name of Google Inc. nor the names of its // contributors may be used to endorse or promote products derived from // this software without specific prior written permission. // // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #ifndef MOZC_BASE_FREELIST_H_ #define MOZC_BASE_FREELIST_H_ #include <vector> #include "base/port.h" namespace mozc { // Note: The program will be crashed in the following code; // FreeList<int> freelist(10); // freelist.Alloc(5); // freelist.set_size(100); // int* array = freelist.Alloc(10); // array[8] = 0; // crashed. // Also this class runs unneeded T's constructor along with the memory chunk // allocation. // Please do take care to use this class. template <class T> class FreeList { public: explicit FreeList(size_t size) : current_index_(0), chunk_index_(0), size_(size) { } ~FreeList() { for (size_t i = 0; i < pool_.size(); ++i) { delete [] pool_[i]; } } void Reset() { chunk_index_ = current_index_ = 0; } void Free() { for (size_t i = 1; i < pool_.size(); ++i) { delete [] pool_[i]; } if (pool_.size() > 1) { pool_.resize(1); } current_index_ = 0; chunk_index_ = 0; } T* Alloc() { return Alloc(static_cast<size_t>(1)); } T* Alloc(size_t len) { if ((current_index_ + len) >= size_) { chunk_index_++; current_index_ = 0; } if (chunk_index_ == pool_.size()) { pool_.push_back(new T[size_]); } T* r = pool_[chunk_index_] + current_index_; current_index_ += len; return r; } void set_size(size_t size) { size_ = size; } private: vector<T *> pool_; size_t current_index_; size_t chunk_index_; size_t size_; DISALLOW_COPY_AND_ASSIGN(FreeList); }; template <class T> class ObjectPool { public: explicit ObjectPool(int size): freelist_(size) { } ~ObjectPool() { } void Free() { released_.clear(); freelist_.Free(); } T* Alloc() { if (!released_.empty()) { T *result = released_.back(); released_.pop_back(); return result; } return freelist_.Alloc(1); } void Release(T *ptr) { released_.push_back(ptr); } void set_size(int size) { freelist_.set_size(size); } private: vector<T *> released_; FreeList<T> freelist_; DISALLOW_COPY_AND_ASSIGN(ObjectPool); }; } // namespace mozc #endif // MOZC_BASE_FREELIST_H_ |