| 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"cxx . . $ `<file>/melder/melder.h`/ // include early, so that size_t is defined |
| 20 | #include "ggml-memory-pool.h"cxx . . $ `<file>/external/whispercpp/ggml-memory-pool.h`/ |
| 21 | #include "ggml-impl.h"cxx . . $ `<file>/external/whispercpp/ggml-impl.h`/ |
| 22 | #include "SpeechRecognizer.h"cxx . . $ `<file>/fon/SpeechRecognizer.h`/ |
| 23 | #include "whisper.h"cxx . . $ `<file>/external/whispercpp/whisper.h`/ |
| 24 | |
| 25 | GgmlMemoryPoolcxx . . $ GgmlMemoryPool# theGgmlMemoryPoolcxx . . $ theGgmlMemoryPool.; |
| 26 | |
| 27 | void GgmlMemoryPoolcxx . . $ GgmlMemoryPool# :: addcxx . . $ GgmlMemoryPool#add(3aa184a1f7b8f66e).(void *ptr, size_t size, bool aligned) { |
| 28 | //TRACE |
| 29 | if (! ptr) { |
| 30 | tracecxx . . $ `melder/melder_debug.h:60:10`! (U"Trying to add nullptr to allocations)"); |
| 31 | return; |
| 32 | } |
| 33 | if (allocationscxx . . $ GgmlMemoryPool#allocations..countcxx . . $ std/unordered_map#count(b71ff106d381144b). (ptr)) |
| 34 | tracecxx . . $ `melder/melder_debug.h:60:10`! (U"Something is very wrong: pointer already in allocations (possible double allocation or missing remove)"); |
| 35 | |
| 36 | allocationscxx . . $ GgmlMemoryPool#allocations. [cxx . . $ std/unordered_map#`operator[]`(4f10b6009f0ee734).ptr] =cxx . . $ Allocation#`operator=`(276c120a0c73d81c). { size, aligned }; |
| 37 | } |
| 38 | |
| 39 | bool GgmlMemoryPoolcxx . . $ GgmlMemoryPool# :: removecxx . . $ GgmlMemoryPool#remove(db00c6a08f501612).(void *ptr) { |
| 40 | //TRACE |
| 41 | if (! ptr) { |
| 42 | tracecxx . . $ `melder/melder_debug.h:60:10`! (U"Trying to remove nullptr from allocations)"); |
| 43 | return false; |
| 44 | } |
| 45 | auto it = allocationscxx . . $ GgmlMemoryPool#allocations..findcxx . . $ std/unordered_map#find(f711f2f81556fe93). (ptr); |
| 46 | if (it ==cxx . . $ std/__detail/`operator==`(6ab342297d2d0253). allocationscxx . . $ GgmlMemoryPool#allocations..endcxx . . $ std/unordered_map#end(58ae48d364eea91c). ()) |
| 47 | return false; |
| 48 | allocationscxx . . $ GgmlMemoryPool#allocations.. erasecxx . . $ std/unordered_map#erase(d465664efb4c2d93). (it); |
| 49 | return true; |
| 50 | } |
| 51 | |
| 52 | bool GgmlMemoryPoolcxx . . $ GgmlMemoryPool# :: removecxx . . $ GgmlMemoryPool#remove(ba504f6f2e89cb92).(void *ptr, size_t size) { |
| 53 | //TRACE |
| 54 | if (! ptr) { |
| 55 | tracecxx . . $ `melder/melder_debug.h:60:10`! (U"Trying to remove nullptr from allocations)"); |
| 56 | return false; |
| 57 | } |
| 58 | |
| 59 | auto it = allocationscxx . . $ GgmlMemoryPool#allocations..findcxx . . $ std/unordered_map#find(f711f2f81556fe93). (ptr); |
| 60 | if (it ==cxx . . $ std/__detail/`operator==`(6ab342297d2d0253). allocationscxx . . $ GgmlMemoryPool#allocations..endcxx . . $ std/unordered_map#end(58ae48d364eea91c). ()) |
| 61 | return false; |
| 62 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (size == it->second.size); // otherwise something weird: size does not match |
| 63 | allocationscxx . . $ GgmlMemoryPool#allocations.. erasecxx . . $ std/unordered_map#erase(d465664efb4c2d93). (it); |
| 64 | return true; |
| 65 | } |
| 66 | |
| 67 | void GgmlMemoryPoolcxx . . $ GgmlMemoryPool# :: clearcxx . . $ GgmlMemoryPool#clear(49f6e7a06ebc5aa8).() { |
| 68 | //TRACE |
| 69 | tracecxx . . $ `melder/melder_debug.h:60:10`! (U"--- WE ARE IN THE EMERGENCY CLEAN UP ---"); |
| 70 | tracecxx . . $ `melder/melder_debug.h:60:10`! (U"Number of allocations in the memory pool is ", n_allocations()); |
| 71 | tracecxx . . $ `melder/melder_debug.h:60:10`! (U"Total memory in bytes is ", sizeInBytes()); |
| 72 | tracecxx . . $ `melder/melder_debug.h:60:10`! (U"Destroying all living SpeechRecognizers..."); |
| 73 | for (auto & speechRecognizer : theLivingSpeechRecognizerscxx . . $ theLivingSpeechRecognizers.) { |
| 74 | if (speechRecognizer -> whisperContextcxx . . $ structSpeechRecognizer#whisperContext..ptrcxx . . $ autoWhisperContext#ptr.) { |
| 75 | whisper_free (speechRecognizer -> whisperContextcxx . . $ structSpeechRecognizer#whisperContext..ptrcxx . . $ autoWhisperContext#ptr.); |
| 76 | speechRecognizer -> whisperContextcxx . . $ structSpeechRecognizer#whisperContext..ptrcxx . . $ autoWhisperContext#ptr. = nullptr; |
| 77 | } |
| 78 | tracecxx . . $ `melder/melder_debug.h:60:10`! (U"speechRecognizer destroyed"); |
| 79 | } |
| 80 | |
| 81 | tracecxx . . $ `melder/melder_debug.h:60:10`! (U"Number of allocations in the memory pool is ", n_allocations()); |
| 82 | tracecxx . . $ `melder/melder_debug.h:60:10`! (U"Total memory in bytes is ", sizeInBytes()); |
| 83 | tracecxx . . $ `melder/melder_debug.h:60:10`! (U"Clearing allocations..."); |
| 84 | for (auto & allocation_pair : allocationscxx . . $ GgmlMemoryPool#allocations.) { |
| 85 | if (! allocation_pair.firstcxx . . $ std/pair#first.) |
| 86 | continue; |
| 87 | if (allocation_pair.secondcxx . . $ std/pair#second..alignedcxx . . $ Allocation#aligned.) |
| 88 | ggml_aligned_free (allocation_pair.firstcxx . . $ std/pair#first., allocation_pair.secondcxx . . $ std/pair#second..sizecxx . . $ Allocation#size., false); |
| 89 | else |
| 90 | ggml_raw_free (allocation_pair.firstcxx . . $ std/pair#first., false); |
| 91 | } |
| 92 | allocationscxx . . $ GgmlMemoryPool#allocations..clearcxx . . $ std/unordered_map#clear(ced63f7c635d850d).(); |
| 93 | tracecxx . . $ `melder/melder_debug.h:60:10`! (U"Allocations cleared"); |
| 94 | tracecxx . . $ `melder/melder_debug.h:60:10`! (U"Number of allocations in the memory pool is ", n_allocations()); |
| 95 | tracecxx . . $ `melder/melder_debug.h:60:10`! (U"Total memory in bytes is ", sizeInBytes()); |
| 96 | } |
| 97 | |
| 98 | size_t GgmlMemoryPoolcxx . . $ GgmlMemoryPool# :: n_allocationscxx . . $ GgmlMemoryPool#n_allocations(fe4d2d262d4164a8).() const { |
| 99 | return allocationscxx . . $ GgmlMemoryPool#allocations..sizecxx . . $ std/unordered_map#size(fe713de6d184c8a).(); |
| 100 | } |
| 101 | |
| 102 | size_t GgmlMemoryPoolcxx . . $ GgmlMemoryPool# :: sizeInBytescxx . . $ GgmlMemoryPool#sizeInBytes(fe4d2d262d4164a8).() const { |
| 103 | size_t size = 0; |
| 104 | for (auto & allocation_pair : allocationscxx . . $ GgmlMemoryPool#allocations.) { |
| 105 | if (allocation_pair.firstcxx . . $ std/pair#first.) |
| 106 | size += allocation_pair.secondcxx . . $ std/pair#second..sizecxx . . $ Allocation#size.; |
| 107 | } |
| 108 | return size; |
| 109 | } |
| 110 | |
| 111 | /* End of file ggml-memory-pool.cpp */ |