reference counting gcc plugin

From: Aravind Ceyardass
Date: Sat Jun 17 2023 - 18:22:12 EST


2nd attempt. I hope this is the right mailing list. :-)

Hello Kernel People,

I have developed a gcc plugin for implementing reference counting. I see that the kernel has implemented many different techniques over the years, but I couldn't find any with compiler support.

I would like your ideas, inputs before I release the plugin for everyone hoping that it would benefit the Linux kernel.

Here is a sample code to give you an idea of usage and the code transformation done by the plugin.


#include "hrcmm.h"
/*
REFTRACK_STRUCT macro defines the following declarations

struct foo;
void foo_addref(const struct foo *const);
void foo_removeref(const struct foo *const);
void foo_destroy(struct foo *const);
*/

REFTRACK_STRUCT(foo){
int bar;
};

/*
REFTRACK_EPILOG_WITH_DTOR macro calls foo_destroy when reference count is zero.
This is optional, if there is no special cleanup to be done, use REFTRACK_EPILOG.
*/
REFTRACK_EPILOG_WITH_DTOR(foo);

// This function is called when the reference count for object pointed to by p is zero
void foo_destroy(struct foo *p){
if (p)
printf("foo destroyed:%p\n", p);
}

typedef struct foo foo;

// statements commented out are injected by the plugin

void baz(foo *p){
printf("%d\n", p->bar);
// foo_removeref(p);
}

int main(int argc, char *argv[]){
foo *p = rc_malloc(sizeof(foo)); // rc_malloc is a default wrapper provided
// foo_addref(p);
p->bar = 123;

// foo_addref(p);
foo *q = p;

// foo_addref(q);
baz(q);

// foo_removeref(p);
// foo_removeref(q);
}


Aravind