Linked lists with no data and a self referential pointer - linux-kernel

kernel/include/linux/types.h
.....
struct list_head {
struct list_head *next, *prev;
};
struct hlist_head {
struct hlist_node *first;
};
struct hlist_node {
struct hlist_node *next, **pprev;
};
What purpose these lists containing no data serve?

These are generic lists that can be used to link any data structure. Say for example, I have a structure
strut my_data {
type1;
type2;
...
struct list_head list;
...
typen;
};
And I have another list head,
struct list_head head;
I am initializing it to reference itself
INIT_LIST_HEAD(&head);
Now I can add an element to it
list_add_tail(&my_data.list, &head);
And the structure is linked now, to get the element back, we can use the list_entry API, which will internally call container_of function. There are other useful APIs used to loop through the list and perform operation, which can be found along with their implementation in kernel/include/list.h.

Related

Create an object from nested struct definition in GoLang?

Is there a way to create an object from a nested struct type
func main() {
car := Car{}
var wheel Car.Wheel
}
type Car struct {
Wheel struct {
name string
}
}
I have a deep nested json. I am interested in operating on many of these nested structs separately.
I want to access the struct definition through the “root” definition . Something like Car.Wheel instead of explicitly defining type Wheel struct for many of my nested objects in the json
Is there a way to create an object from a nested struct type
No, because there's no such thing as a "nested struct type". You don't have a type Car.Wheel, you have a type Car, with a field Wheel; that field's type is the unnamed type struct { name string }. You cannot refer to an unnamed type; it is unnamed. To refer to a type, you have to name it. You could do:
var wheel struct { name string }
And you'd be able to assign between wheel and Car.Wheel, because they're the same type; however, this is not particularly convenient (you'd have to write out the full type definition everywhere you use it), and it means you can't define any methods on the type, which may or may not be a limitation you care about.
Generally speaking, in Go, you just want to define a named type for each type you want to use, and those definitions would each be at the top level:
type Car struct {
Wheel Wheel
}
type Wheel struct {
name string
}
Are you looking for something like this:
package main
import "fmt"
type Car struct {
model string
wheel Wheel
}
type Wheel struct {
name string
}
func main() {
car := Car{model: "Toyota"}
car.wheel.name = "my wheel"
fmt.Println("car model: ", car.model)
fmt.Println("car wheel name:", car.wheel.name)
}

How to modify a field in a struct of an unknown type?

I have multiple structs that have one common field; let's call it common here
type Struct1 struct {
foo string
bar string
common string
}
type Struct2 struct {
baz int
qux string
common string
}
I want to create a function that takes an Interface as input and nullifies common. The available struct types won't be known at compile time, so I can't create a separate function for each type, and I can't use a switch statement.
P.S: In my use-case, I want to nullify common because it holds the creation time of each struct, and I want to track the history of the struct, so I will know if it changes. Having the creation time inside the struct will mess this up because the creation time will be different every time a new struct is generated even though its actual data may be the same.
Define a struct with the common fields and make it implement an interface which says that it is able to nullify the common fields. Then embed this struct into your other struct types that should be able to nullify the fields.
// CommonNullifier is able to nullify its common field(s)
type CommonNullifier interface {
NullifyCommon()
}
// StructCommon contains the common struct fields
type StructCommon struct {
Common string
}
func (sc *StructCommon) NullifyCommon() {
sc.Common = ""
}
// Struct1 embeds common fields, thus implements CommonNullifier
type Struct1 struct {
StructCommon
Foo string
}
// Struct2 also embeds common fields, thus also implements CommonNullifier
type Struct2 struct {
StructCommon
Bar string
}
// NullifyCommon nullfies the 'common' fields in the argument
func NullifyCommon(s CommonNullifier) {
s.NullifyCommon()
}
(See it on the Go Playground)
You could also use reflection, but using an interface is generally more readable.

adding a shared_ptr object on a weak_ptr container

My class is based on boost::asio tutorial.
This class has a private ctor, is derived from enable_shared_from_this.
its static member function return a shared_ptr from the object created.
I want to store those pointers on a list of weak_ptr, so the list don't need to worry about its life time, either prolong it.
The caller tcp_serve instantiate tcp_connection with create method:
tcp_server:
tcp_connection::pointer new_connection =
tcp_connection::create(acceptor_.get_io_service());
tcp_connection:
PUBLIC:
typedef boost::shared_ptr<tcp_connection> pointer;
static pointer create(boost::asio::io_service& io_service)
{
return pointer(new tcp_connection(io_service));
}
PRIVATE:
tcp_connection(boost::asio::io_service& io_service)
: _socket(io_service), _timer(io_service)
{
}
I am trying to create a list on the tcp_server, I tried many different kind of types, but I can't rightly added the object to the list:
std::list<std::weak_ptr<tcp_connection>> connections;
connections.push_back(new_connection);

Wrapping std::map and provide pointer to element

I am using a library which offers a function foo(Widget*).
My Widgets are stored in
struct WidgetManager {
std::map<int, Widget> dict;
??? getWidget(int id);
}
Originally I stored (raw) Widget pointers in the std::map just because it was convenient to pass them to foo.
If I want to store the actual Widgets in the map, what should the return type of getWidget be so that I can pass a pointer of the Widget to foo?
I am compelled to make it of type iterator, but I don't like that I have to access itr->second to get the Widget(pointer).
You can use & just before you pass your widget to the foo(Widget*) function to get a pointer to it.
struct WidgetManager {
std::map<int, Widget> dict;
Widget& getWidget(int id);
}
usage
WidgetManager wm;
//...
Widget& w = wm.getWidget(id);
foo(&w);
//...

golang initilize inner struct object in nested structs

I have a nested struct
type Posts struct {
Id int
CreatedAt time.Time
Data struct {
Title string
UserName string
}
}
I want to create a Data object but var innerData Posts.Data doesn't seem to work. I don't want to create separate Data struct because I'm planning to avoid name collusions by having multiple structs which will have different inner structs named Data.
You can't. Posts.Data isn't the name of a type. The only thing you could do is var innerData struct{ ... }, which I'm sure you don't want to because then you would have repetition and need to make changes in two places. Otherwise, you have to give it a name. That name doesn't have to be Data, it can be PostData or something to make it unique.

Resources