Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2016 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2016 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 NET_BASE_ARENA_H_ | 5 #ifndef NET_BASE_ARENA_H_ |
| 6 #define NET_BASE_ARENA_H_ | 6 #define NET_BASE_ARENA_H_ |
| 7 | 7 |
| 8 #include <memory> | 8 #include <memory> |
| 9 #include <vector> | 9 #include <vector> |
| 10 | 10 |
| 11 #include "net/base/net_export.h" | 11 #include "net/base/net_export.h" |
| 12 | 12 |
| 13 namespace net { | 13 namespace net { |
| 14 | 14 |
| 15 // Allocates large blocks of memory, and doles them out in smaller chunks. | 15 // Allocates large blocks of memory, and doles them out in smaller chunks. |
| 16 // Not thread-safe. | 16 // Not thread-safe. |
| 17 class NET_EXPORT_PRIVATE UnsafeArena { | 17 class NET_EXPORT_PRIVATE UnsafeArena { |
| 18 public: | 18 public: |
| 19 class Status { | |
| 20 private: | |
| 21 friend class UnsafeArena; | |
| 22 size_t bytes_allocated_; | |
| 23 | |
| 24 public: | |
| 25 Status() : bytes_allocated_(0) {} | |
| 26 size_t bytes_allocated() const { return bytes_allocated_; } | |
| 27 }; | |
|
Ryan Hamilton
2017/01/10 22:46:12
Instead of a class, I wonder if a struct might be
Bence
2017/01/11 14:25:13
I would love that, but unfortunately Status is use
Ryan Hamilton
2017/01/11 15:05:06
Yes, I meant doing this in shared code.
| |
| 28 | |
| 19 // Blocks allocated by this arena will be at least |block_size| bytes. | 29 // Blocks allocated by this arena will be at least |block_size| bytes. |
| 20 explicit UnsafeArena(size_t block_size); | 30 explicit UnsafeArena(size_t block_size); |
| 21 ~UnsafeArena(); | 31 ~UnsafeArena(); |
| 22 | 32 |
| 23 // Copy and assign are not allowed. | 33 // Copy and assign are not allowed. |
| 24 UnsafeArena() = delete; | 34 UnsafeArena() = delete; |
| 25 UnsafeArena(const UnsafeArena&) = delete; | 35 UnsafeArena(const UnsafeArena&) = delete; |
| 26 UnsafeArena& operator=(const UnsafeArena&) = delete; | 36 UnsafeArena& operator=(const UnsafeArena&) = delete; |
| 27 | 37 |
| 28 // Move is allowed. | 38 // Move is allowed. |
| 29 UnsafeArena(UnsafeArena&& other); | 39 UnsafeArena(UnsafeArena&& other); |
| 30 UnsafeArena& operator=(UnsafeArena&& other); | 40 UnsafeArena& operator=(UnsafeArena&& other); |
| 31 | 41 |
| 32 char* Alloc(size_t size); | 42 char* Alloc(size_t size); |
| 33 char* Realloc(char* original, size_t oldsize, size_t newsize); | 43 char* Realloc(char* original, size_t oldsize, size_t newsize); |
| 34 char* Memdup(const char* data, size_t size); | 44 char* Memdup(const char* data, size_t size); |
| 35 | 45 |
| 36 // If |data| and |size| describe the most recent allocation made from this | 46 // If |data| and |size| describe the most recent allocation made from this |
| 37 // arena, the memory is reclaimed. Otherwise, this method is a no-op. | 47 // arena, the memory is reclaimed. Otherwise, this method is a no-op. |
| 38 void Free(char* data, size_t size); | 48 void Free(char* data, size_t size); |
| 39 | 49 |
| 40 void Reset(); | 50 void Reset(); |
| 41 | 51 |
| 52 Status status() const { return status_; } | |
| 53 | |
| 42 private: | 54 private: |
| 43 struct Block { | 55 struct Block { |
| 44 std::unique_ptr<char[]> data; | 56 std::unique_ptr<char[]> data; |
| 45 size_t size = 0; | 57 size_t size = 0; |
| 46 size_t used = 0; | 58 size_t used = 0; |
| 47 | 59 |
| 48 explicit Block(size_t s); | 60 explicit Block(size_t s); |
| 49 ~Block(); | 61 ~Block(); |
| 50 | 62 |
| 51 Block(Block&& other); | 63 Block(Block&& other); |
| 52 Block& operator=(Block&& other); | 64 Block& operator=(Block&& other); |
| 53 }; | 65 }; |
| 54 | 66 |
| 55 void Reserve(size_t additional_space); | 67 void Reserve(size_t additional_space); |
| 56 void AllocBlock(size_t size); | 68 void AllocBlock(size_t size); |
| 57 | 69 |
| 58 size_t block_size_; | 70 size_t block_size_; |
| 59 std::vector<Block> blocks_; | 71 std::vector<Block> blocks_; |
| 72 Status status_; | |
| 60 }; | 73 }; |
| 61 | 74 |
| 62 } // namespace net | 75 } // namespace net |
| 63 | 76 |
| 64 #endif // NET_BASE_ARENA_H_ | 77 #endif // NET_BASE_ARENA_H_ |
| OLD | NEW |