1 // Copyright 2018 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 #pragma once
15 
16 #include <inttypes.h>
17 #include <stddef.h>
18 #include <string.h>
19 
20 namespace android {
21 namespace base {
22 
23 class Stream;
24 
25 // Class to create sub-allocations in an existing buffer. Similar interface to
26 // Pool, but underlying mechanism is different as it's difficult to combine
27 // same-size heaps in Pool with a preallocated buffer.
28 class SubAllocator {
29 public:
30     // |pageSize| determines both the alignment of pointers returned
31     // and the multiples of space occupied.
32     SubAllocator(
33         void* buffer,
34         uint64_t totalSize,
35         uint64_t pageSize);
36 
37     // Memory is freed from the perspective of the user of
38     // SubAllocator, but the prealloced buffer is not freed.
39     ~SubAllocator();
40 
41     // Snapshotting
42     bool save(Stream* stream);
43     bool load(Stream* stream);
44     bool postLoad(void* postLoadBuffer);
45 
46     // returns null if the allocation cannot be satisfied.
47     void* alloc(size_t wantedSize);
48     void* allocFixed(size_t wantedSize, uint64_t offset);
49     // returns true if |ptr| came from alloc(), false otherwise
50     bool free(void* ptr);
51     void freeAll();
52     uint64_t getOffset(void* ptr);
53 
54     bool empty() const;
55 
56     // Convenience function to allocate an array
57     // of objects of type T.
58     template <class T>
allocArray(size_t count)59     T* allocArray(size_t count) {
60         size_t bytes = sizeof(T) * count;
61         void* res = alloc(bytes);
62         return (T*) res;
63     }
64 
strDup(const char * toCopy)65     char* strDup(const char* toCopy) {
66         size_t bytes = strlen(toCopy) + 1;
67         void* res = alloc(bytes);
68         memset(res, 0x0, bytes);
69         memcpy(res, toCopy, bytes);
70         return (char*)res;
71     }
72 
strDupArray(const char * const * arrayToCopy,size_t count)73     char** strDupArray(const char* const* arrayToCopy, size_t count) {
74         char** res = allocArray<char*>(count);
75 
76         for (size_t i = 0; i < count; i++) {
77             res[i] = strDup(arrayToCopy[i]);
78         }
79 
80         return res;
81     }
82 
dupArray(const void * buf,size_t bytes)83     void* dupArray(const void* buf, size_t bytes) {
84         void* res = alloc(bytes);
85         memcpy(res, buf, bytes);
86         return res;
87     }
88 
89 private:
90     class Impl;
91     Impl* mImpl = nullptr;
92 };
93 
94 } // namespace base
95 } // namespace android
96