<commit_before>#define _GNU_SOURCE

#include <dlfcn.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>

static int fd_out = 1;
static void *(*real_calloc)(size_t, size_t) = NULL;
static void *(*real_malloc)(size_t) = NULL;
static void *(*real_realloc)(size_t) = NULL;
static void (*real_free)(void *) = NULL;

void flushLog(){
	close(fd_out);
	sigset(SIGSTOP, SIG_DFL);
	sigset(SIGTSTP, SIG_DFL);
	_exit(0);
}

void flushLogWrapper(int x){
	flushLog();
}

int recordLog(char* format, ...) {
	char buf[1024] = {0};
	int bytesWritten = 0;
	
	if (format){
		va_list valist;
		va_start(valist, format);
		bytesWritten = vsnprintf(buf, 1024, format, valist);
		va_end(valist);
	}
	
	write(fd_out, buf, bytesWritten);
}

__attribute__((constructor)) void mallocCounter() {
	do {
		real_malloc = dlsym(RTLD_NEXT, "malloc");
		real_calloc = dlsym(RTLD_NEXT, "calloc");
		real_realloc = dlsym(RTLD_NEXT, "realloc");
		real_free = dlsym(RTLD_NEXT, "free");
	} while (!real_malloc || !real_calloc || !real_realloc || !real_free);
	
	fd_out = creat("libmemchecker_log.txt", 0664);
    
	sigset(SIGTSTP, flushLogWrapper);
    atexit(flushLog);
}

void *calloc(size_t nmemb, size_t size){
	void *lptr = real_calloc(nmemb, size);
	recordLog("+\t%lu\t%p\tcalloc\n", (long unsigned) size * nmemb, lptr);
	return lptr;
}

void *malloc(size_t size){
	void *lptr = real_malloc(size);
	recordLog("+\t%lu\t%p\tmalloc\n", (long unsigned) size, lptr);
	return lptr;
}

void *realloc(void *ptr, size_t size){
    void *lptr = real_realloc(ptr, size);
	fprintf(fd_output, "~\t%lu\t%p\t%p\trealloc\n", (long unsigned) size, ptr, lptr);
    return lptr;
}

void free(void *ptr){
    fprintf(fd_output, "-\t%p\n", ptr);
    real_free(ptr);
}<commit_msg>The oldest malloccounter.c<commit_after>#define _GNU_SOURCE

#include <dlfcn.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#define SECRET_FD 17

static long counter = 0;

#define GET_REAL(f) do { \
    if (real_ ## f == NULL) { \
        real_ ## f = dlsym(RTLD_NEXT, #f); \
    } \
} while (0)

void printCount()
{
    char buf[1024];
    int sz;
    sz = sprintf(buf, "%ld\n", counter);
    sz = write(SECRET_FD, buf, sz);
    (void) sz;
}

__attribute__((constructor))
void mallocCounter()
{
    dup2(1, SECRET_FD);
    close(1);
    close(2);
    atexit(printCount);
}

void *calloc(size_t nmemb, size_t size)
{
    static void *(*real_calloc)(size_t, size_t) = NULL;
    counter++;
    GET_REAL(calloc);
    return real_calloc(nmemb, size);
}

void *malloc(size_t size)
{
    static void *(*real_malloc)(size_t) = NULL;
    counter++;
    GET_REAL(malloc);
    return real_malloc(size);
}

void free(void *ptr)
{
    static void (*real_free)(void *) = NULL;
    counter--;
    GET_REAL(free);
    real_free(ptr);
}
{'fraction_non_alphanumeric': 0.1678094550703717, 'fraction_numerical': 0.010104655359076146, 'mean_word_length': 4.259962049335863, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6671547', 'n_tokens_mistral': 1179, 'n_tokens_neox': 1133, 'n_words': 241}