Back to home page

EIC code displayed by LXR

 
 

    


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_DEFAULT_ALLOCATOR_H_
0018 #define FLATBUFFERS_DEFAULT_ALLOCATOR_H_
0019 
0020 #include "flatbuffers/allocator.h"
0021 #include "flatbuffers/base.h"
0022 
0023 namespace flatbuffers {
0024 
0025 // DefaultAllocator uses new/delete to allocate memory regions
0026 class DefaultAllocator : public Allocator {
0027  public:
0028   uint8_t *allocate(size_t size) FLATBUFFERS_OVERRIDE {
0029     return new uint8_t[size];
0030   }
0031 
0032   void deallocate(uint8_t *p, size_t) FLATBUFFERS_OVERRIDE { delete[] p; }
0033 
0034   static void dealloc(void *p, size_t) { delete[] static_cast<uint8_t *>(p); }
0035 };
0036 
0037 // These functions allow for a null allocator to mean use the default allocator,
0038 // as used by DetachedBuffer and vector_downward below.
0039 // This is to avoid having a statically or dynamically allocated default
0040 // allocator, or having to move it between the classes that may own it.
0041 inline uint8_t *Allocate(Allocator *allocator, size_t size) {
0042   return allocator ? allocator->allocate(size)
0043                    : DefaultAllocator().allocate(size);
0044 }
0045 
0046 inline void Deallocate(Allocator *allocator, uint8_t *p, size_t size) {
0047   if (allocator)
0048     allocator->deallocate(p, size);
0049   else
0050     DefaultAllocator().deallocate(p, size);
0051 }
0052 
0053 inline uint8_t *ReallocateDownward(Allocator *allocator, uint8_t *old_p,
0054                                    size_t old_size, size_t new_size,
0055                                    size_t in_use_back, size_t in_use_front) {
0056   return allocator ? allocator->reallocate_downward(old_p, old_size, new_size,
0057                                                     in_use_back, in_use_front)
0058                    : DefaultAllocator().reallocate_downward(
0059                          old_p, old_size, new_size, in_use_back, in_use_front);
0060 }
0061 
0062 }  // namespace flatbuffers
0063 
0064 #endif  // FLATBUFFERS_DEFAULT_ALLOCATOR_H_