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 } // namespace base
26 } // namespace android
27 
28 namespace android {
29 namespace base {
30 namespace guest {
31 
32 
33 // Class to create sub-allocations in an existing buffer. Similar interface to
34 // Pool, but underlying mechanism is different as it's difficult to combine
35 // same-size heaps in Pool with a preallocated buffer.
36 class SubAllocator {
37 public:
38     // |pageSize| determines both the alignment of pointers returned
39     // and the multiples of space occupied.
40     SubAllocator(
41         void* buffer,
42         uint64_t totalSize,
43         uint64_t pageSize);
44 
45     // Memory is freed from the perspective of the user of
46     // SubAllocator, but the prealloced buffer is not freed.
47     ~SubAllocator();
48 
49     // Snapshotting
50     bool save(Stream* stream);
51     bool load(Stream* stream);
52     bool postLoad(void* postLoadBuffer);
53 
54     // returns null if the allocation cannot be satisfied.
55     void* alloc(size_t wantedSize);
56     // returns true if |ptr| came from alloc(), false otherwise
57     bool free(void* ptr);
58     void freeAll();
59     uint64_t getOffset(void* ptr);
60 
61     bool empty() const;
62 
63     // Convenience function to allocate an array
64     // of objects of type T.
65     template <class T>
allocArray(size_t count)66     T* allocArray(size_t count) {
67         size_t bytes = sizeof(T) * count;
68         void* res = alloc(bytes);
69         return (T*) res;
70     }
71 
strDup(const char * toCopy)72     char* strDup(const char* toCopy) {
73         size_t bytes = strlen(toCopy) + 1;
74         void* res = alloc(bytes);
75         memset(res, 0x0, bytes);
76         memcpy(res, toCopy, bytes);
77         return (char*)res;
78     }
79 
strDupArray(const char * const * arrayToCopy,size_t count)80     char** strDupArray(const char* const* arrayToCopy, size_t count) {
81         char** res = allocArray<char*>(count);
82 
83         for (size_t i = 0; i < count; i++) {
84             res[i] = strDup(arrayToCopy[i]);
85         }
86 
87         return res;
88     }
89 
dupArray(const void * buf,size_t bytes)90     void* dupArray(const void* buf, size_t bytes) {
91         void* res = alloc(bytes);
92         memcpy(res, buf, bytes);
93         return res;
94     }
95 
96 private:
97     class Impl;
98     Impl* mImpl = nullptr;
99 };
100 
101 } // namespace guest
102 } // namespace base
103 } // namespace android
104