lkml.org 
[lkml]   [1999]   [May]   [30]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
From
SubjectRe: How to write kernel modules using C++
Date

I think using C++ in modules is of quite limited use, but in some cases
it makes sense to snippets of existing code.

Since man people seem to be thinking about using C++ in modules, here
is my example, if you really need to do so. Comments welcome.

- Jan

--- cpp_mod.cpp ---

extern "C"
{
#include <linux/types.h>
#include <linux/module.h>
#include <linux/kernel.h>
}

/* can't include <linux/vmalloc.h>, because it in turn includes */
/* <linux/list.h> which uses C++ keywords a variable names */
extern "C" void * vmalloc(unsigned long size);
extern "C" void vfree(void * addr);
// extern "C" int atexit(void (*function)(void));

/* include this explicitely: g++ might optimize instance in module.h away !*/
static char kernel_version[] = UTS_RELEASE;

MODULE_AUTHOR("Jan Willamowius");
MODULE_DESCRIPTION("Demo for C++ usage in modules");
MODULE_PARM(param1, "i");

static int param1 = 0;

void * operator new(size_t amt)
{
void * pReturn = NULL;
pReturn = vmalloc(amt);
printk("NEW: size = %d returns 0x%p\n", amt, pReturn);
return pReturn;
}

void operator delete( void *p )
{
printk("FREE: ptr = 0x%p\n", p);
vfree(p);
}

/*
int atexit(void (*function)(void))
{
printk("ATEXIT\n");
return 0;
};
*/

class Myclass
{
public:
int c;
void printit();

Myclass();
~Myclass();
};

Myclass::Myclass()
{
c = 1;
printk("*******CONSTRUCTOR %d\n", c );
}

Myclass::~Myclass()
{
}

void Myclass::printit()
{
printk("******* PRINTIT %d\n", c );
}



int init_module()
{
// static Myclass mc;
// mc.printit();
Myclass * mc = new Myclass;
mc->printit();
delete mc;
return 0;
}

void cleanup_module()
{
}


--
Jan Willamowius, http://www.willamowius.de/
Microsoft has a year 2000 problem. I'm part of it. I'm running Linux.

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.rutgers.edu
Please read the FAQ at http://www.tux.org/lkml/

\
 
 \ /
  Last update: 2005-03-22 13:51    [W:0.019 / U:0.348 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site