Skip to content
4 changes: 2 additions & 2 deletions 04_Memory_Management/05_Heap_Allocation.md
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ What the heap will look like after the code above?
| 6 | F | X | .. | X | 6 | F | X | .. | X | 6 | F | .. | X | | |


Now, all of the memory in the heap is available to allocate (except for the overhead used to store the status of each chunk), and everything looks perfectly fine. But now the code keeps executing, and it will arrive at the following instruction:
Now, all of the memory in the heap is available to allocate (except for the overhead used to store the status of each chunk), and everything looks perfectly fine. But the code keeps executing, and it will arrive at the following instruction:

```c
alloc(7);
Expand Down Expand Up @@ -416,7 +416,7 @@ This means that the allocator (before marking this location as free and returnin

![The heap status after the merge](/Images/heap_example_after_merge.png)

The fields in bold are the fields that are changed. The exact implementation of this code is left to the reader.
The fields in bold are the fields that are changed. The exact implementation of this code is left to the reader. Please note that some books refers to this technique, calling it _coalescing_, but is the same concepts.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should just introduce it as coalescing in the first place. It is already standard terminology in undergraduate curricula. So maybe early on, we could ask the question: what are some things we want to do with (free) heap blocks? The two natural answers are:

  1. Splitting -> motivate with an example
  2. Coalescing -> motivate with an example
    The examples should show that splitting and coalescing go hand in hand. Imagine you only split, then your block sizes will be non-increasing, which means harder chance to repurpose free blocks. This is why coalescing is helpful.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just reread the whole paragraph, and the main thing is that the approach is basicall incremental, so the "coalesce" concept is only introduced by consecutive examples expanding the previous implementation, and i haven´t found a better place of where to introduce that name earlier. I'll try anyway to rephrase the paragraph.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to introduce the coalescing term at the beginning of the chapter.

Also I think that the splitting and merging are explained in the way you suggested.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the term is introduced above, this line isn't needed anymore.


### Part 6: Splitting

Expand Down
32 changes: 32 additions & 0 deletions 99_Appendices/C_Language_Info.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,38 @@ It is worth mentioning that inline assembly syntax is the At&t syntax, so the us
asm("movl $5, %rcx;");
```

## Dealing With Literals and Bitwise Operation

This is one of the most misleading and subtle issue we can face while osdeving. And most of the time we face it the hard way.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Content is fine overall, just some general comments:

  • use capitals are the start of sentence and not starting with conjunctions (e.g. and).
  • there's some baked-in assumptions about type sizes in C, int isnt necessarily 32-bits. The issue is that int is smaller than the fixed-size uint64_t type.
  • it's overall quite wordy, the same example is given twice and the comparison to languages other than c/c++ isnt needed I think.
  • the real solution is to ensure that the immediate value is of the correct type for the next operation, which you give examples of but dont actually say that.
  • The introduction is quite ominous haha. Perhaps consider something like:

There are some subtle bugs that can be encountered when when using immediate values in C, due to operator precedence and integer promotion rules.


So what is the problem? let's imagine we have a 64 bit variable, and we need some to do a bitwise operation like `setting` the bit at the position `x`, this is easily achieved using the _left shift_ (`|=`) operator combined with a _xor_ (`|=`), like in the following pseudocode:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or, not xor (^=)


```
variable example_var |= (1 << x)
```

And that is correct, and it probably could work in many languages (of course changing the syntax), but let's see what happens with C and C++ languages.
In _C_, the statement above become:

```c
uint64_t example_var |= (1 << x);
```

And we do few test, for `x=1, 2, 10, 20, 31`, everything works fine, so what is the issue? The issue is when the shift is above 31, because of the C _Integer promotion rule_.

In the above example, `1` is a literal, and by default C converts it to `int`, and this type in C is 32 bits, the bitwise operation is executed using the type of the left operand, so we are trying to shift left a bit of a number of position that is higher, than the size of the variable, causing an undefined behavior.

Then what are the solutions? Below few example of how to potentially fix it:

```c
#define ONE 1ULL
const uint64_t one = 1;

uint64_t example_one |= one << 42;
uint64_t example_two |= ONE << 42;
uint64_t example_three |= 1ULL << 42;
```

## C +(+) assembly together - Calling Conventions

Different C compilers feature a number of [calling conventions](https://en.wikipedia.org/wiki/X86_calling_conventions),
Expand Down
1 change: 1 addition & 0 deletions 99_Appendices/J_Updates.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,4 @@ Sixth Book Release
* _Stivale 2_ protocol sections have been replaced with Limine protocol, since _stivale2_ has been deprecated.
* Add a complete exammple of how to create an ELF executable for our kernel
* Typo and error fixes
* New short paragraph to explain the behaviour of literals with bitwise operators.