-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* string ref counter wrapped in class and made static * removed manual addRef() * assuming ref=1 for untracked refs * synchronized read and write access to ref counter
- Loading branch information
1 parent
05d217d
commit f0a0881
Showing
14 changed files
with
159 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright 2024 The DAPHNE Consortium | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include "StringRefCount.h" | ||
|
||
void StringRefCounter::add(const char* arg) { | ||
auto ptr = reinterpret_cast<uintptr_t>(arg); | ||
const std::lock_guard<std::mutex> lock(mtxStrRefCnt); | ||
stringRefCount.insert({ptr, 2}); | ||
logger->info("StringRefCount: Added ptr={}; arg={}", ptr, arg); | ||
} | ||
|
||
bool StringRefCounter::inc(const char* arg) { | ||
auto ptr = reinterpret_cast<uintptr_t>(arg); | ||
const std::lock_guard<std::mutex> lock(mtxStrRefCnt); | ||
if(auto found = stringRefCount.find(ptr); found != stringRefCount.end()) { | ||
found->second++; | ||
std::cerr << "IncRef: " << ptr << " (found) - count incremented to " << found->second << std::endl; | ||
logger->info("IncRef: ptr={}; arg={}", ptr, arg); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
bool StringRefCounter::dec(const char* arg) { | ||
auto ptr = reinterpret_cast<uintptr_t>(arg); | ||
const std::lock_guard<std::mutex> lock(mtxStrRefCnt); | ||
if(auto found = stringRefCount.find(ptr); found != stringRefCount.end()) { | ||
found->second--; | ||
std::cerr << "DecRef: " << ptr << " (found) - count decremented to " << found->second << std::endl; | ||
if(found->second == 1) { | ||
logger->debug("Removing from StringRefCounter: ptr={}; arg={}", ptr, arg); | ||
// delete [] reinterpret_cast<char *>(found->first); | ||
stringRefCount.erase(found); | ||
} | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
StringRefCounter& StringRefCounter::instance() { | ||
static StringRefCounter INSTANCE; | ||
return INSTANCE; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
* Copyright 2024 The DAPHNE Consortium | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <spdlog/spdlog.h> | ||
|
||
#include <iostream> | ||
#include <map> | ||
#include <memory> | ||
|
||
class StringRefCounter { | ||
|
||
std::shared_ptr<spdlog::logger> logger; | ||
|
||
// This map keeps track of string allocations to be able to remove them when they are not needed anymore | ||
std::map<uintptr_t, size_t> stringRefCount; | ||
std::mutex mtxStrRefCnt; | ||
|
||
public: | ||
StringRefCounter() { | ||
logger = spdlog::get("runtime"); | ||
} | ||
|
||
~StringRefCounter() { | ||
if(!stringRefCount.empty()) { | ||
// This should not happen | ||
std::cerr << "Deleting " << stringRefCount.size() << " remaining string refs in ~DaphneContext()" << std::endl; | ||
logger->warn("{} string refs still present while destroying DaphneContext - this should not happen.", stringRefCount.size()); | ||
} | ||
} | ||
|
||
/** | ||
* @brief This method adds a numeric representation of a char* and an initial reference count | ||
* to delete the string/char* later on in the DecRef kernel. | ||
* | ||
* @param str The char* to keep track of | ||
* | ||
*/ | ||
void add(const char* arg); | ||
|
||
bool inc(const char* arg); | ||
|
||
bool dec(const char* arg); | ||
|
||
static StringRefCounter& instance(); | ||
}; | ||
|