![]() |
|
|||
File indexing completed on 2025-08-27 09:30:24
0001 /* 0002 * Copyright 2021 Google Inc. All rights reserved. 0003 * 0004 * Licensed under the Apache License, Version 2.0 (the "License"); 0005 * you may not use this file except in compliance with the License. 0006 * You may obtain a copy of the License at 0007 * 0008 * http://www.apache.org/licenses/LICENSE-2.0 0009 * 0010 * Unless required by applicable law or agreed to in writing, software 0011 * distributed under the License is distributed on an "AS IS" BASIS, 0012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 0013 * See the License for the specific language governing permissions and 0014 * limitations under the License. 0015 */ 0016 0017 #ifndef FLATBUFFERS_ALLOCATOR_H_ 0018 #define FLATBUFFERS_ALLOCATOR_H_ 0019 0020 #include "flatbuffers/base.h" 0021 0022 namespace flatbuffers { 0023 0024 // Allocator interface. This is flatbuffers-specific and meant only for 0025 // `vector_downward` usage. 0026 class Allocator { 0027 public: 0028 virtual ~Allocator() {} 0029 0030 // Allocate `size` bytes of memory. 0031 virtual uint8_t *allocate(size_t size) = 0; 0032 0033 // Deallocate `size` bytes of memory at `p` allocated by this allocator. 0034 virtual void deallocate(uint8_t *p, size_t size) = 0; 0035 0036 // Reallocate `new_size` bytes of memory, replacing the old region of size 0037 // `old_size` at `p`. In contrast to a normal realloc, this grows downwards, 0038 // and is intended specifcally for `vector_downward` use. 0039 // `in_use_back` and `in_use_front` indicate how much of `old_size` is 0040 // actually in use at each end, and needs to be copied. 0041 virtual uint8_t *reallocate_downward(uint8_t *old_p, size_t old_size, 0042 size_t new_size, size_t in_use_back, 0043 size_t in_use_front) { 0044 FLATBUFFERS_ASSERT(new_size > old_size); // vector_downward only grows 0045 uint8_t *new_p = allocate(new_size); 0046 memcpy_downward(old_p, old_size, new_p, new_size, in_use_back, 0047 in_use_front); 0048 deallocate(old_p, old_size); 0049 return new_p; 0050 } 0051 0052 protected: 0053 // Called by `reallocate_downward` to copy memory from `old_p` of `old_size` 0054 // to `new_p` of `new_size`. Only memory of size `in_use_front` and 0055 // `in_use_back` will be copied from the front and back of the old memory 0056 // allocation. 0057 void memcpy_downward(uint8_t *old_p, size_t old_size, uint8_t *new_p, 0058 size_t new_size, size_t in_use_back, 0059 size_t in_use_front) { 0060 memcpy(new_p + new_size - in_use_back, old_p + old_size - in_use_back, 0061 in_use_back); 0062 memcpy(new_p, old_p, in_use_front); 0063 } 0064 }; 0065 0066 } // namespace flatbuffers 0067 0068 #endif // FLATBUFFERS_ALLOCATOR_H_
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
![]() ![]() |