Is there a way to associate a 'struct file' structure with 'struct platform_device'? - linux-device-driver

I'm writing a kernel module that has private attributes for each probed instance. When performing different file operations, is it possible to access that private data?
The private data I'm referring to is stored using:
void platform_set_drvdata(struct platform_device *, void *);
and would like to be able to access that data from, say, a read file operation:
static ssize_t read(struct file *, char __user *, size_t , loff_t *);
I feel as though I've asked this before, but can't find the question: Is there a way to map a struct file object to a struct platform_device object (preferably without resorting to global variables)?
EDIT
I looked through the drivers/platform directory of the kernel for an example of code that had struct file_operations object that had members using per-probed instance data. The code I found seemed rather circular.
As of writing this, my platform instance data object now contains a struct file_operations fops member, which, when the open() is called I use the container_of() macro to get my instance data.
In the probe() function, I do:
static int am_probe(struct platform_device *pdev) {
struct am_instance * instance = devm_kzalloc(dev, sizeof(struct am_instance), GFP_KERNEL);
...
/* am_fops is in .rodata (and not a pointer) */
instance->fops = am_fops;
rv = register_chrdev(0, instance->device_name, &instance->fops);
...
platform_set_drvdata(pdev, instance);
...
Then, in the open() method I do this:
static int am_open(struct inode *inode, struct file *file) {
file->private_data = container_of(file->f_op, struct am_instance, fops);
return 0;
}
The above works, in that from the read() function, I can access the instance data by examining the file->private_data field with an appropriate cast.

Related

In Objective-c++, is there a way to move static c functions to another file?

At work, I see lots of ObjC++ files that have dozens of static C helper functions at the top. I'd like to break these static C helper functions out into a separate file and just import the header to ease readability.
Instead of:
static int _AddEm(const int a, const int b) { return a + b }
...
...
...
x 300
Something like:
#import "MyClassHelpers.h"
That would contain all the static C helper functions. The issue is that static seems to only work in the declared file (I'm coming from a Swift and Java background), so I can't just declare a static class in c++ world and bring it and it's helpers over. I'd like to maintain the speed and binary size benefits of using static. What's the best practice for this?
Thanks!
In C, putting static on a function means that the function is restricted to the file where it's declared. (And as a result you can have static functions with the same name in multiple files without conflicting, whereas without static the linker may complain of multiple functions with the same name.) In a way, you can think of it as making the function "private" to the file (in the sense of private members of a class in object-oriented languages you might be familiar with).
If you want to define the code of a function once and use it in multiple files, you want the opposite -- make it not static, and then declare the function signature in the header with extern, like this:
// in MyClassHelpers.h
extern int _AddEm(const int a, const int b);
// in MyClassHelpers.c
int _AddEm(const int a, const int b) { return a + b }
This way, other files that include MyClassHelpers.h can use this function, and the linker will link it with the the single implementation of it compiled from MyClassHelpers.c.
If you had put a static function definition in the header like this:
// in MyClassHelpers.h
static int _AddEm(const int a, const int b) { return a + b }
then each implementation file that includes this header will effectively have its own "private" copy of this function, potentially resulting in the same code being repeated numerous times. (Including a header is basically the same as copy-pasting the contents of the header into the file it is included in, so it is if you wrote this static function definition in every file.)
It occurs to me that perhaps what you're trying to do with these tiny functions is to have calls to them be inlined rather than be called as separate function calls. In that case, perhaps declaring it static and inline in the header will work:
// in MyClassHelpers.h
static inline int _AddEm(const int a, const int b) { return a + b }

Are only types with trivial destructor suited for storage for placement new?

The examples for placement new often use unsigned char arrays as the underlying storage. The steps can be:
create the unsigned char array with new
create an object in this storage with placement new
use object
destroy object
call delte for the unsigned char array to free the array
Point 5. seems only to work if we use a type for the underlying storage with a trivial destructor. Otherwise, we would call the destructor of the underlying storage type but with no object existing there. Technically, we are destructing a bunch of unsigned chars which are not present and we are lucky that the desturctor of the unsigned char type is trivial and so no-op.
What about the following code:
struct A{ /* some members... */ };
struct B{ /* some members... B shall be same size as A */ };
int main()
{
auto ptr_to_a = new A; // A object lives # ptr_to_a
ptr_to_a->~A(); // A object destroyed. no object living # ptr_to_a, but storage is preserved
new (ptr_to_a) B; // B object living # ptr_to_a.
std::launder(reinterpret_cast<b*>(ptr_to_a))->/*...*/; // use B. for this purpose we need std::launder in C++17 or we would store the pointer returned by the placement new and use it without std::launder
std::launder(reinterpret_cast<b*>(ptr_to_a))->~B(); // B object destroyed. no object living # ptr_to_a, but storage is preserved
// at this point there is no object living # ptr_to_a, but we need to hand back the occupied storage.
// a)
delete ptr_to_a; // undefined behavior because no object is sitting # ptr_to_a
// b)
new (ptr_to_a) A; // create an object again to make behavior defined. but this seems odd.
delete ptr_to_a;
// c)
// some method to just free the memory somehow without invoking destructors?
return 0;
}
On https://en.cppreference.com/w/cpp/language/lifetime is written:
As a special case, objects can be created in arrays of unsigned char or std::byte (in which case it is said that the array provides storage for the object) if... .
Does this imply, that its only allowed to use placement new on unsigned char and byte arrays and because they have a trivial destructor my code sample is obsolete?
Otherwise, how about my codesample? Is option b) the only valid solution?
Edit: second examlpe:
struct A{ /* some members... */ };
struct alignas(alignof(A)) B{ /* some members... */ };
int main()
{
static_assert(sizeof(A) == sizeof(B));
A a;
a.~A();
auto b_ptr = new (&a) B;
b_ptr->~B();
return 0;
// undefined behavior because a's destructor gets called but no A object is "alive" (assuming non trivial destructor)
// to make it work, we need to placement new a new A into a?
}
Generally, you wouldn't use the storage returned by an allocation of an unrelated class A to put your B in. You don't even have to have an allocation at all
int main ()
{
char storage[sizeof(B)];
std::aligned_storage<sizeof(B), alignof(B)>::type aligned_storage;
auto b_ptr1 = new (&storage) B; // potentially unaligned
auto b_ptr2 = new (&aligned_storage) B; // guaranteed safe
// use b_ptr1, b_ptr2
b_ptr1->~B();
b_ptr2->~B();
// storage ceases to exist when main returns
}
If you do need to dynamically allocate, I would suggest wrapping the storage in a holder struct, so that you don't end the lifetime of the thing you newed.
struct B_holder
{
std::aligned_storage<sizeof(B), alignof(B)>::type storage;
B * make_B() { return new(&storage) B; }
}
int main()
{
auto holder = std::make_unique<B_holder>();
auto * B_ptr = B_holder->make_B();
// use B_ptr
B_ptr->~B();
// holder ceases to exist when main returns
}
For the most part yes.
You can however do something like this.
struct placed {
char stuff[100];
};
struct stupid {
std::aligned_storage_t<sizeof(placed), alignof(placed)> data;
~stupid() {
std::cout << "stupid gone\n";
}
};
int main() {
auto* pstupid = new stupid;
auto* pplaced = ::new( (void*)&pstupid->data ) placed;
pplaced->~placed();
auto* pstupid2 = ::new( (void*)pstupid ) stupid;
delete pstupid2;
}
but that is, as implied by the type name, pretty stupid. And exceedingly hard to make exception safe without a lot of noexcept guarantees I didn't include above.
I am also not completely certain if delete pstupid2 is legal, as it was created via placement new and not via a simple new expression.

PostgreSQL + lexical definition of GROUP_P

Where can I find lexical definitions of all PostgreSQL keywords / tokens, like GROUP_P?
GROUP_P is not a keyword, it's a symbol in the context of the C compiler and it doesn't have lexical significance outside of the C source code.
In this declaration from parser/kwlist.h:
PG_KEYWORD("group", GROUP_P, RESERVED_KEYWORD)
it is the first argument "group" that is the keyword, the 2nd argument GROUP_P being typically an enum field or a #define (it's up to the includer).
The header file src/include/parser/keywords.h provides a struct type ScanKeyword that can be directly mapped to the PG_KEYWORD macro, field by field:
typedef struct ScanKeyword
{
const char *name; /* in lower case */
int16 value; /* grammar's token code */
int16 category; /* see codes above */
} ScanKeyword;
For a concrete example of use, see how ECPG does it in src/interfaces/ecpg/preproc/keywords.c

Is it possible to have an exported function in a DLL return a static member of an exported class?

I'm working on a plug-in protocol of sorts. The idea is that there's a base clase PCOperatorBase and that plugins would subclass this base class to provide specific functionality through a "process" virtual method that they override. The subclasses also should have a static struct member that hold typical plugin info: plug name, type and subtype. This info is used by another class (a PCOperatorManager) to know what types of plugins is dealing with. And I thought about having it be a static member so that there's no need to instantiate a plug to merely find out about the type of operator it is.
I have the following classes:
PCOperatorBase.h the superclass from which to derive all other plugs:
#ifdef PCOPERATORBASE_EXPORTS
#define PCOPERATORBASE_API __declspec(dllexport)
#else
#define PCOPERATORBASE_API __declspec(dllimport)
#endif
class PCOPERATORBASE_API PCOperatorBase
{
public:
typedef struct OperatorInfo
{
wchar_t* name;
wchar_t* type;
wchar_t* subtype;
} OperatorInfo;
PCOperatorBase(void);
virtual ~PCOperatorBase(void);
virtual int process(int* inBuffer, int* outBuffer, int bufferSize);
};
And then, for instance, a subclass:
BlackNWhite.h: a RGB->black / white operator -- yes, these are going to be graphics plugs, and disregard the types for the in / out buffers.. they are merely placeholders at this point.
#ifdef BLACKNWHITE_EXPORTS
#define BLACKNWHITE_API __declspec(dllexport)
#else
#define BLACKNWHITE_API __declspec(dllimport)
#endif
// This class is exported from the BlackNWhite.dll
class BLACKNWHITE_API CBlackNWhite : PCOperatorBase
{
public:
static PCOperatorBase::OperatorInfo* operatorInfo;
CBlackNWhite(void);
virtual ~CBlackNWhite(void);
//virtual int process(int* inBuffer, int* outBuffer, int bufferSize);
};
BLACKNWHITE_API CBlackNWhite* getOperatorInstance();
BLACKNWHITE_API const PCOperatorBase::OperatorInfo* getOperatorInfo();
And here the implementation file:
BlackNWhite.cpp:
#include "stdafx.h"
#include "BlackNWhite.h"
BLACKNWHITE_API CBlackNWhite* getOperatorInstance()
{
return new CBlackNWhite();
}
BLACKNWHITE_API const PCOperatorBase::OperatorInfo* getOperatorInfo()
{
return CBlackNWhite::operatorInfo;
}
CBlackNWhite::CBlackNWhite()
{
}
CBlackNWhite::~CBlackNWhite()
{
}
Now, I've tried a few approaches but I can't get the DLL to compile, because of the static member.The linker throws:
\BlackNWhite.lib and object c:\Projects\BlackNWhite\Debug\BlackNWhite.exp
1>BlackNWhite.obj : error LNK2001: unresolved external symbol "public: static struct PCOperatorBase::OperatorInfo * CBlackNWhite::operatorInfo" (?operatorInfo#CBlackNWhite##2PAUOperatorInfo#PCOperatorBase##A)
1>c:\Projects\BlackNWhite\Debug\BlackNWhite.dll : fatal error LNK1120: 1 unresolved externals
I thought that since the struct is defined inside the base class and the base class is exporting, the struct would export too. But I guess I'm wrong?
So how should I be doing it?
And regardless, is there a better approach to having the plugs' factory export their name,type and subtype without the need for instantiation than a static class member? For example, a resource file? or even another extern "C" function could return this info.. But I just felt that since it's C++ it makes the most sense to encapsulate this data (which is about the class as a factory itself) within the class, whether through a member or a method.
It doesn't have anything to do with DLLs, you declared the static member but you forgot to define it. Add this line to BlackNWhite.cpp:
PCOperatorBase::OperatorInfo* CBlackNWhite::operatorInfo = NULL;

Using boost::program_options with own template class possible?

I'm currently start using boost::program_options for parsing command line options as well as configuration files.
Is it possible to use own template classes as option arguments? That means, something like
#include <iostream>
#include "boost/program_options.hpp"
namespace po = boost::program_options;
template <typename T>
class MyClass
{
private:
T* m_data;
size_t m_size;
public:
MyClass( size_t size) : m_size(size) { m_data = new T[size]; }
~MyClass() { delete[] m_data; }
T get( size_t i ) { return m_data[i]; }
void set( size_t i, T value ) { m_data[i] = value; }
};
int main (int argc, const char * argv[])
{
po::options_description generic("General options");
generic.add_options() ("myclass", po::value< MyClass<int>(2) >(),
"Read MyClass");
return 0;
}
Trying to compile this I get an Semantic Issue (No matching function for call to 'value'). I guess I need to provide some casting to an generalized type but I have no real idea.
Can anybody help?
Thanks
Aeon512
I wouldn't know if boost::program_options allows the use-case you are trying, but the error you are getting is because your are trying to pass an object as a template type to po::value<>. If the size is known at compile-time, you could have the size be passed in as a template parameter.
template< typename T, size_t size >
class MyClass {
T m_data[size];
public:
// ...
};
And then use it like so:
po::value< MyClass<int, 2> >()
You should also look into using Boost.Array instead that I guess fulfills what you are trying to implement.
I would write it like this:
MyClass<int> mine(2);
generic.add_options() ("myclass", po::value(&mine), "Read MyClass");
Then all that needs to be done is to define an input stream operator like this:
std::istream& operator >>(std::istream& source, MyClass& target);
Then Boost Program Options will invoke this stream operator when the myclass option is used, and your object will be automatically populated according to that operator's implementation, rather than having to later call one of the Program Options functions to extract the value.
If you don't prefer the above syntax, something like should work too:
generic.add_options() ("myclass", po::value<MyClass<int> >()->default_value(MyClass<int>(2)), "Read MyClass");
This way you would be creating the instance of your class directly with your desired constructor argument outside of the template part where runtime behavior isn't allowed. I do not prefer this way because it's verbose and you end up needing to call more functions later to convert the value.