@@ -10,23 +10,27 @@ namespace NKikimr::NMiniKQL {
10
10
11
11
class TMemoryUsageReporter {
12
12
public:
13
- using TReportCallback = std::function<void (ui64)>;
13
+ using TAllocateReportCallback = std::function<bool (ui64)>;
14
+ using TFreeReportCallback = std::function<void (ui64)>;
14
15
using TPtr = std::shared_ptr<TMemoryUsageReporter>;
15
16
public:
16
- TMemoryUsageReporter (TReportCallback reportAllocateCallback, TReportCallback reportFreeCallback): ReportAllocateCallback_(reportAllocateCallback), ReportFreeCallback_(reportFreeCallback) {}
17
+ TMemoryUsageReporter (TAllocateReportCallback reportAllocateCallback, TFreeReportCallback reportFreeCallback): ReportAllocateCallback_(reportAllocateCallback), ReportFreeCallback_(reportFreeCallback) {}
17
18
void ReportAllocate (ui64 bytes) {
18
19
// Cerr << "[MISHA][ALLOC]: " << bytes << ", Total: " << BytesAllocated_ << Endl;
19
20
Y_ENSURE (ReportAllocateCallback_ != nullptr );
20
- ReportAllocateCallback_ (bytes);
21
- BytesAllocated_ += bytes;
21
+ if (ReportAllocateCallback_ (bytes)) {
22
+ BytesAllocated_ += bytes;
23
+ }
22
24
}
23
25
24
26
void ReportFree (ui64 bytes) {
25
27
// Cerr << "[MISHA][FREE]: " << bytes << ", Total: " << BytesAllocated_ << Endl;
26
28
Y_ENSURE (ReportFreeCallback_ != nullptr );
27
- ReportFreeCallback_ (bytes);
28
- Y_ENSURE (BytesAllocated_ >= bytes, " Trying to free more bytes than allocated" );
29
- BytesAllocated_ -= bytes;
29
+ ui64 toFree = std::min (BytesAllocated_, bytes);
30
+ if (toFree) {
31
+ ReportFreeCallback_ (bytes);
32
+ }
33
+ BytesAllocated_ -= toFree;
30
34
}
31
35
32
36
~TMemoryUsageReporter () {
@@ -43,8 +47,8 @@ class TMemoryUsageReporter {
43
47
44
48
private:
45
49
ui64 BytesAllocated_{0 };
46
- TReportCallback ReportAllocateCallback_{nullptr };
47
- TReportCallback ReportFreeCallback_{nullptr };
50
+ TAllocateReportCallback ReportAllocateCallback_{nullptr };
51
+ TFreeReportCallback ReportFreeCallback_{nullptr };
48
52
};
49
53
50
54
}// namespace NKikimr::NMiniKQL
0 commit comments