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 gfxstream {
21 namespace guest {
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     // returns true if |ptr| came from alloc(), false otherwise
49     bool free(void* ptr);
50     void freeAll();
51     uint64_t getOffset(void* ptr);
52 
53     bool empty() const;
54 
55     // Convenience function to allocate an array
56     // of objects of type T.
57     template <class T>
allocArray(size_t count)58     T* allocArray(size_t count) {
59         size_t bytes = sizeof(T) * count;
60         void* res = alloc(bytes);
61         return (T*) res;
62     }
63 
strDup(const char * toCopy)64     char* strDup(const char* toCopy) {
65         size_t bytes = strlen(toCopy) + 1;
66         void* res = alloc(bytes);
67         memset(res, 0x0, bytes);
68         memcpy(res, toCopy, bytes);
69         return (char*)res;
70     }
71 
strDupArray(const char * const * arrayToCopy,size_t count)72     char** strDupArray(const char* const* arrayToCopy, size_t count) {
73         char** res = allocArray<char*>(count);
74 
75         for (size_t i = 0; i < count; i++) {
76             res[i] = strDup(arrayToCopy[i]);
77         }
78 
79         return res;
80     }
81 
dupArray(const void * buf,size_t bytes)82     void* dupArray(const void* buf, size_t bytes) {
83         void* res = alloc(bytes);
84         memcpy(res, buf, bytes);
85         return res;
86     }
87 
88 private:
89     class Impl;
90     Impl* mImpl = nullptr;
91 };
92 
93 } // namespace guest
94 } // namespace gfxstream
95