Coverage Report

Created: 2025-12-31 06:48

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/MapServer/src/flatgeobuf/include/flatbuffers/allocator.h
Line
Count
Source
1
/*
2
 * Copyright 2021 Google Inc. All rights reserved.
3
 *
4
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 * you may not use this file except in compliance with the License.
6
 * You may obtain a copy of the License at
7
 *
8
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 *
10
 * Unless required by applicable law or agreed to in writing, software
11
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 * See the License for the specific language governing permissions and
14
 * limitations under the License.
15
 */
16
17
#ifndef FLATBUFFERS_ALLOCATOR_H_
18
#define FLATBUFFERS_ALLOCATOR_H_
19
20
#include "flatbuffers/base.h"
21
22
namespace mapserver {
23
namespace flatbuffers {
24
25
// Allocator interface. This is flatbuffers-specific and meant only for
26
// `vector_downward` usage.
27
class Allocator {
28
 public:
29
0
  virtual ~Allocator() {}
30
31
  // Allocate `size` bytes of memory.
32
  virtual uint8_t *allocate(size_t size) = 0;
33
34
  // Deallocate `size` bytes of memory at `p` allocated by this allocator.
35
  virtual void deallocate(uint8_t *p, size_t size) = 0;
36
37
  // Reallocate `new_size` bytes of memory, replacing the old region of size
38
  // `old_size` at `p`. In contrast to a normal realloc, this grows downwards,
39
  // and is intended specifcally for `vector_downward` use.
40
  // `in_use_back` and `in_use_front` indicate how much of `old_size` is
41
  // actually in use at each end, and needs to be copied.
42
  virtual uint8_t *reallocate_downward(uint8_t *old_p, size_t old_size,
43
                                       size_t new_size, size_t in_use_back,
44
0
                                       size_t in_use_front) {
45
0
    FLATBUFFERS_ASSERT(new_size > old_size);  // vector_downward only grows
46
0
    uint8_t *new_p = allocate(new_size);
47
0
    memcpy_downward(old_p, old_size, new_p, new_size, in_use_back,
48
0
                    in_use_front);
49
0
    deallocate(old_p, old_size);
50
0
    return new_p;
51
0
  }
52
53
 protected:
54
  // Called by `reallocate_downward` to copy memory from `old_p` of `old_size`
55
  // to `new_p` of `new_size`. Only memory of size `in_use_front` and
56
  // `in_use_back` will be copied from the front and back of the old memory
57
  // allocation.
58
  void memcpy_downward(uint8_t *old_p, size_t old_size, uint8_t *new_p,
59
                       size_t new_size, size_t in_use_back,
60
0
                       size_t in_use_front) {
61
0
    memcpy(new_p + new_size - in_use_back, old_p + old_size - in_use_back,
62
0
           in_use_back);
63
0
    memcpy(new_p, old_p, in_use_front);
64
0
  }
65
};
66
67
}  // namespace flatbuffers
68
}  // namespace mapserver
69
70
#endif  // FLATBUFFERS_ALLOCATOR_H_