Impl. checking calloc as well

This commit is contained in:
Stephen Seo 2024-06-04 15:15:05 +09:00
parent 0e447dc3be
commit eb716bf65a
3 changed files with 30 additions and 0 deletions

View file

@ -18,6 +18,7 @@ namespace SC_AM_Internal {
} }
void *(*real_malloc)(std::size_t) = nullptr; void *(*real_malloc)(std::size_t) = nullptr;
void *(*real_calloc)(std::size_t, std::size_t) = nullptr;
void (*real_free)(void*) = nullptr; void (*real_free)(void*) = nullptr;
Malloced::Malloced() : address(nullptr), size(0) {} Malloced::Malloced() : address(nullptr), size(0) {}
@ -80,6 +81,18 @@ namespace SC_AM_Internal {
return address; return address;
} }
void *Stats::do_calloc(std::size_t n, std::size_t size) {
void *address = real_calloc(n, size);
if (address != nullptr) {
Malloced *data = reinterpret_cast<Malloced*>(real_malloc(sizeof(Malloced)));
data->address = address;
data->size = size;
ListNode::add_to_list(malloced_list_tail, data);
}
return address;
}
void Stats::do_free(void *ptr) { void Stats::do_free(void *ptr) {
if (ptr) { if (ptr) {
if(!ListNode::remove_from_list(malloced_list_head, ptr)) { if(!ListNode::remove_from_list(malloced_list_head, ptr)) {

View file

@ -13,6 +13,7 @@ namespace SC_AM_Internal {
constexpr unsigned int DATA_ITEMS_SIZE = 500; constexpr unsigned int DATA_ITEMS_SIZE = 500;
extern void *(*real_malloc)(std::size_t size); extern void *(*real_malloc)(std::size_t size);
extern void *(*real_calloc)(std::size_t n, std::size_t size);
extern void (*real_free)(void *ptr); extern void (*real_free)(void *ptr);
struct Malloced { struct Malloced {
@ -45,6 +46,7 @@ namespace SC_AM_Internal {
void initialize(); void initialize();
void *do_malloc(std::size_t size); void *do_malloc(std::size_t size);
void *do_calloc(std::size_t n, std::size_t size);
void do_free(void *ptr); void do_free(void *ptr);
void print_status() const; void print_status() const;

View file

@ -22,6 +22,21 @@ extern "C" {
return SC_AM_Internal::stats->do_malloc(size); return SC_AM_Internal::stats->do_malloc(size);
} }
void *calloc(std::size_t n, std::size_t size) {
std::clog << "attempting to calloc size: " << size << "...\n";
if (SC_AM_Internal::real_calloc == nullptr) {
SC_AM_Internal::real_calloc = reinterpret_cast<void*(*)(std::size_t, std::size_t)>(
dlsym(RTLD_NEXT, "calloc"));
}
if (SC_AM_Internal::stats == nullptr) {
SC_AM_Internal::stats = SC_AM_Internal::get_init_stats();
}
return SC_AM_Internal::stats->do_calloc(n, size);
}
void free(void *ptr) { void free(void *ptr) {
std::clog << "attempting to free...\n"; std::clog << "attempting to free...\n";