|
|
Subscribe / Log in / New account

What exactly is an object?

What exactly is an object?

Posted Feb 24, 2025 17:06 UTC (Mon) by dmv (subscriber, #168800)
Parent article: Slabs, sheaves, and barns

Super basic question, sorry it’s so stupid, but what exactly is an “object” in this context? As far as I can tell, it’s a chunk of memory that’s a certain size (smaller than a page, of course). So if you need 100 bytes for some common purpose P (say, tracking info about access credentials or whatever), the “object” is just that 100 byte chunk of memory. Is that right or are objects more granular than that—e.g., a certain-sized chunk of memory plus a constructor/destructor pair or whatever?


to post comments

What exactly is an object?

Posted Feb 24, 2025 17:10 UTC (Mon) by corbet (editor, #1) [Link] (5 responses)

An "object" is just a piece of memory, yes. No methods or other class-like stuff, this is C we're talking about :)

Sorry if that wasn't clear.

What exactly is an object?

Posted Feb 24, 2025 17:15 UTC (Mon) by dmv (subscriber, #168800) [Link]

No, not on you at all! I have learned that trying to track down the actual meanings of a fair bit of memory management terminology can lead to hours trawling through 1950s-1970s texts with no ultimate enlightenment at the end, so I figured I’d just ask here since I had been wondering about slab “objects”. Thanks!

What exactly is an object?

Posted Feb 24, 2025 22:18 UTC (Mon) by tialaramex (subscriber, #21167) [Link] (3 responses)

C is at least - in this respect - consistent. C just doesn't have methods, full stop.

What's weird is that C++ and Java for some reason give methods to some things but not others. There's no particular reason 'x'.is_lowercase() shouldn't work, which is why it does in Rust, there's no real "extra" work to deliver this compared with my_goose.honk() in both cases the compiler just has to figure out what type the object is and then go find the function to call (a "method") based on that type then fill in that object as a parameter to the function. In 1972 in a few hundred kilobytes of RAM I can well believe that's an unaffordable luxury for Dennis and so its absence from C is not a great surprise, but why implement this and then switch it off for the built-in types in a newer language? Search me.

While writing this comment and looking at the Rust docs for Kmalloc I found a doc bug - for a conventional Rust crate I'm pretty clear about how I can report or fix this. But it's not as obvious to me for the kernel code - please tell me I don't need to brave the LKML Jonathan?

The text "if `old_layout` is zero-sized `p` does not need to be a pointer returned by this [`Allocator`]." is unnecessary for free() as we do not provide an old_layout parameter. I can't tell (but hopefully the authors know) whether it is appropriate to simply remove this text, or whether in addition some other text should be provided about zero-size things being freed, or not freed as the case may be.

What exactly is an object?

Posted Feb 25, 2025 10:11 UTC (Tue) by Sesse (subscriber, #53779) [Link] (2 responses)

C++ does, in a sense, have this; it's just that the syntax is different. The philosophy is simple: Something can be a part of the class' interface without being a method (or member function, in C++ parlance). E.g., operator==(const Foo&, const Foo&) can be defined well outside of Foo, and then follows the normal rules for free functions. Something as simple as void whatever(const Foo&) can be seen a part of Foo's interface; in fact, it is commonly preferred if you can implement it not as a friend.

Of course, adding _data members_ to another class from the outside would be nearly impossible in C++'s compilation model (which it, of course, inherited from C).

What exactly is an object?

Posted Feb 25, 2025 20:09 UTC (Tue) by tialaramex (subscriber, #21167) [Link] (1 responses)

It doesn't seem useful to say "It's like method syntax except without the same syntax". That's just not method syntax. Some languages have Unified Method Call Syntax, so _all_ functions in the language which take at least one argument can be used as method calls. Rust only has the other side of that coin, all the methods in the language can also be treated as free functions with an extra argument - but not vice versa.

C++ ADL which maybe you were also gesturing at is just a mess. Your whatever function is indeed treated by C++ as if it's "part of Foo's interface" but only when it was defined in the same namespace so that ADL will find whatever when it is looking up Foo. This causes weird hard to understand issues in real world C++ software because what foo(bar) means will depend on the context in which you wrote it in non-obvious ways, as a result "turning off ADL" via arcane tricks is often needed & there have been attempts to reform ADL or to add explicit "No ADL please" features to the language.

I was serious about that docbug, who do I raise this with? I'd rather raise it as a bug rather than sending a patch because I'm certain those answering the bug ticket will know more than I do about kmalloc

What exactly is an object?

Posted Feb 27, 2025 9:28 UTC (Thu) by aviallon (subscriber, #157205) [Link]

You can simply open a bug on the kernel bugzilla

What exactly is an object?

Posted Feb 25, 2025 16:01 UTC (Tue) by rweikusat2 (subscriber, #117920) [Link]

That's true for kmalloc allocations. For actual slab allocations, an object is the pair (<size>, <type>). Bonwick's (SunOS) original slab allocator had constructors and destructors as the basic idea was to cache constructed objects. This doesn't seem to be the case for Linux, though (at least, I couldn't find a reference to something like this while looking. The type is an important bit of metainformation about usage patterns.


Copyright © 2025, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds