1 /*
2 * Copyright (C) 2019 The Android Open Source Project
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 #include <malloc.h>
18 #include <stdint.h>
19 #include <stdio.h>
20 #include <unistd.h>
21
22 #include <memory_trace/MemoryTrace.h>
23
24 #include "Alloc.h"
25 #include "Pointers.h"
26 #include "Utils.h"
27
AllocDoesFree(const memory_trace::Entry & entry)28 bool AllocDoesFree(const memory_trace::Entry& entry) {
29 switch (entry.type) {
30 case memory_trace::MALLOC:
31 case memory_trace::CALLOC:
32 case memory_trace::MEMALIGN:
33 case memory_trace::THREAD_DONE:
34 return false;
35
36 case memory_trace::FREE:
37 return entry.ptr != 0;
38
39 case memory_trace::REALLOC:
40 return entry.u.old_ptr != 0;
41 }
42 }
43
MallocExecute(const memory_trace::Entry & entry,Pointers * pointers)44 static uint64_t MallocExecute(const memory_trace::Entry& entry, Pointers* pointers) {
45 int pagesize = getpagesize();
46 uint64_t time_nsecs = Nanotime();
47 void* memory = malloc(entry.size);
48 MakeAllocationResident(memory, entry.size, pagesize);
49 time_nsecs = Nanotime() - time_nsecs;
50
51 pointers->Add(entry.ptr, memory);
52
53 return time_nsecs;
54 }
55
CallocExecute(const memory_trace::Entry & entry,Pointers * pointers)56 static uint64_t CallocExecute(const memory_trace::Entry& entry, Pointers* pointers) {
57 int pagesize = getpagesize();
58 uint64_t time_nsecs = Nanotime();
59 void* memory = calloc(entry.u.n_elements, entry.size);
60 MakeAllocationResident(memory, entry.u.n_elements * entry.size, pagesize);
61 time_nsecs = Nanotime() - time_nsecs;
62
63 pointers->Add(entry.ptr, memory);
64
65 return time_nsecs;
66 }
67
ReallocExecute(const memory_trace::Entry & entry,Pointers * pointers)68 static uint64_t ReallocExecute(const memory_trace::Entry& entry, Pointers* pointers) {
69 void* old_memory = nullptr;
70 if (entry.u.old_ptr != 0) {
71 old_memory = pointers->Remove(entry.u.old_ptr);
72 }
73
74 int pagesize = getpagesize();
75 uint64_t time_nsecs = Nanotime();
76 void* memory = realloc(old_memory, entry.size);
77 MakeAllocationResident(memory, entry.size, pagesize);
78 time_nsecs = Nanotime() - time_nsecs;
79
80 pointers->Add(entry.ptr, memory);
81
82 return time_nsecs;
83 }
84
MemalignExecute(const memory_trace::Entry & entry,Pointers * pointers)85 static uint64_t MemalignExecute(const memory_trace::Entry& entry, Pointers* pointers) {
86 int pagesize = getpagesize();
87 uint64_t time_nsecs = Nanotime();
88 void* memory = memalign(entry.u.align, entry.size);
89 MakeAllocationResident(memory, entry.size, pagesize);
90 time_nsecs = Nanotime() - time_nsecs;
91
92 pointers->Add(entry.ptr, memory);
93
94 return time_nsecs;
95 }
96
FreeExecute(const memory_trace::Entry & entry,Pointers * pointers)97 static uint64_t FreeExecute(const memory_trace::Entry& entry, Pointers* pointers) {
98 if (entry.ptr == 0) {
99 return 0;
100 }
101
102 void* memory = pointers->Remove(entry.ptr);
103 uint64_t time_nsecs = Nanotime();
104 free(memory);
105 return Nanotime() - time_nsecs;
106 }
107
AllocExecute(const memory_trace::Entry & entry,Pointers * pointers)108 uint64_t AllocExecute(const memory_trace::Entry& entry, Pointers* pointers) {
109 switch (entry.type) {
110 case memory_trace::MALLOC:
111 return MallocExecute(entry, pointers);
112 case memory_trace::CALLOC:
113 return CallocExecute(entry, pointers);
114 case memory_trace::REALLOC:
115 return ReallocExecute(entry, pointers);
116 case memory_trace::MEMALIGN:
117 return MemalignExecute(entry, pointers);
118 case memory_trace::FREE:
119 return FreeExecute(entry, pointers);
120 default:
121 return 0;
122 }
123 }
124