Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is Layout?

I want to reimplement some stdlib smart pointers in rust (mainly Box) to learn it better, i come from a C background which has the simple malloc and free functions, but rust's alloc and dealloc need some Layout. What is it? It's not really explained in the docs.

like image 424
Soft Waffle Avatar asked Sep 20 '20 17:09

Soft Waffle


People also ask

What does layout mean?

1 : the plan or design or arrangement of something laid out: such as. a : dummy sense 5b. b : final arrangement of matter to be reproduced especially by printing.

What is layout and examples?

The definition of a layout is an arrangement, plan or design. An example of a layout is a drawing of how a house will be built. noun. (informal) An establishment or property, especially a large residence or estate. noun.

What is a layout in a computer?

In computing, layout is the process of calculating the position of objects in space subject to various constraints. This functionality can be part of an application or packaged as a reusable component or library.

What does layout mean in writing?

(leɪaʊt ) Word forms: layouts. countable noun. The layout of a park, building, or piece of writing is the way in which the parts of it are arranged.

What is the definition of layout in architecture?

Definition of layout. (Entry 1 of 2) 1 : the plan or design or arrangement of something laid out: such as. a : dummy sense 5b. b : final arrangement of matter to be reproduced especially by printing. 2 : the act or process of planning or laying out in detail. 3 a : something that is laid out a model train layout.

What is a process layout?

A process layout is a type of facility layout. Organizations often use process layouts to design floor plans and arrange equipment for maximized efficiency. Plants with a process layout may arrange work stations, machinery, tools and other equipment in groups according to the functions they perform.

What is keyboard layout?

Keyboard layout, an arrangement of the keys on a typographic keyboard Model railroad layout, a diorama with tracks for operating scaled-down trains Layout or marking out, the transfer of a design onto a workpiece in manufacturing Plant layout study, an engineering study to analyze physical configurations for a manufacturing plant

What is the meaning of lay out?

lay·out. (lā′out′) n. 1. The act or an instance of laying out. 2. An arrangement or plan, especially the schematic arrangement of parts or areas: the layout of a factory; the layout of a printed circuit. 3.


1 Answers

As its documentation says, Layout describes a block of memory that is to be allocated or deallocated. In addition to memory size, it specifies alignment and optionally the trailing padding of the block.

C's malloc isn't told what alignment to use, so it has to assume worst-case alignment, possibly wasting memory. And even in C one sometimes needs memory of non-standard alignment, for which APIs outside of standard C had to be used until C11.

One final difference between Rust's and C's allocator interface is that Rust requires the layout to be passed to dealloc as well as to alloc - equivalent to C's free requiring the size that was passed to malloc. At first this sounds like a disadvantage because the user of the API must track the allocated size in order to be able to deallocate. But it turns out not to be an issue in practice because:

  • when allocating a single value such as Box<T> or array Box<[T; n]>, the size is determined at compile-time and thus known when Box::drop is invoked.

  • when allocating a dynamic array, such as Vec<T>, the capacity of the vector is tracked by the vector itself and thus also available in Vec::drop.

  • when boxing a slice, such as Box<[T]>, the slice cannot be resized and its capacity is equal to its length and again known to Box::drop.

So it turns out that it is the C-style allocation API that results in storing redundant size information. Passing the size to dealloc eliminates the redundancy, providing opportunity to save space, especially for small blocks where the size information is a non-negligible percentage of the memory used.

When comparing Rust's allocation interface to that of C, keep in mind that in Rust raw allocation is much more of a specialized tool, used only to implement safe abstractions such as Box, Rc or Vec. Unlike C, where a programmer is routinely expected to invoke malloc and free, most Rust programmers never need to invoke the global allocator directly.

like image 93
user4815162342 Avatar answered Oct 19 '22 12:10

user4815162342