Debian Codegraph @

external/whispercpp/ggml-memory-pool.cpp

raw symbols SCIP
1 /* ggml-memory-pool.cpp
2 *
3 * Copyright (C) 2026 Anastasia Shchupak
4 *
5 * This code is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or (at
8 * your option) any later version.
9 *
10 * This code is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this work. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include "melder.h" // include early, so that size_t is defined
20 #include "ggml-memory-pool.h"
21 #include "ggml-impl.h"
22 #include "SpeechRecognizer.h"
23 #include "whisper.h"
24
25 GgmlMemoryPool theGgmlMemoryPool;
26
27 void GgmlMemoryPool :: add(void *ptr, size_t size, bool aligned) {
28 //TRACE
29 if (! ptr) {
30 trace (U"Trying to add nullptr to allocations)");
31 return;
32 }
33 if (allocations.count (ptr))
34 trace (U"Something is very wrong: pointer already in allocations (possible double allocation or missing remove)");
35
36 allocations [ptr] = { size, aligned };
37 }
38
39 bool GgmlMemoryPool :: remove(void *ptr) {
40 //TRACE
41 if (! ptr) {
42 trace (U"Trying to remove nullptr from allocations)");
43 return false;
44 }
45 auto it = allocations.find (ptr);
46 if (it == allocations.end ())
47 return false;
48 allocations. erase (it);
49 return true;
50 }
51
52 bool GgmlMemoryPool :: remove(void *ptr, size_t size) {
53 //TRACE
54 if (! ptr) {
55 trace (U"Trying to remove nullptr from allocations)");
56 return false;
57 }
58
59 auto it = allocations.find (ptr);
60 if (it == allocations.end ())
61 return false;
62 Melder_assert (size == it->second.size); // otherwise something weird: size does not match
63 allocations. erase (it);
64 return true;
65 }
66
67 void GgmlMemoryPool :: clear() {
68 //TRACE
69 trace (U"--- WE ARE IN THE EMERGENCY CLEAN UP ---");
70 trace (U"Number of allocations in the memory pool is ", n_allocations());
71 trace (U"Total memory in bytes is ", sizeInBytes());
72 trace (U"Destroying all living SpeechRecognizers...");
73 for (auto & speechRecognizer : theLivingSpeechRecognizers) {
74 if (speechRecognizer -> whisperContext.ptr) {
75 whisper_free (speechRecognizer -> whisperContext.ptr);
76 speechRecognizer -> whisperContext.ptr = nullptr;
77 }
78 trace (U"speechRecognizer destroyed");
79 }
80
81 trace (U"Number of allocations in the memory pool is ", n_allocations());
82 trace (U"Total memory in bytes is ", sizeInBytes());
83 trace (U"Clearing allocations...");
84 for (auto & allocation_pair : allocations) {
85 if (! allocation_pair.first)
86 continue;
87 if (allocation_pair.second.aligned)
88 ggml_aligned_free (allocation_pair.first, allocation_pair.second.size, false);
89 else
90 ggml_raw_free (allocation_pair.first, false);
91 }
92 allocations.clear();
93 trace (U"Allocations cleared");
94 trace (U"Number of allocations in the memory pool is ", n_allocations());
95 trace (U"Total memory in bytes is ", sizeInBytes());
96 }
97
98 size_t GgmlMemoryPool :: n_allocations() const {
99 return allocations.size();
100 }
101
102 size_t GgmlMemoryPool :: sizeInBytes() const {
103 size_t size = 0;
104 for (auto & allocation_pair : allocations) {
105 if (allocation_pair.first)
106 size += allocation_pair.second.size;
107 }
108 return size;
109 }
110
111 /* End of file ggml-memory-pool.cpp */