‫Hello and welcome.
‫And in this lecture, we're going to create a disk streamer, so you know how we currently read from
‫the disk at one sector at a time.
‫This is all fine, but it means we have to rewind 10, 12 bytes every single time.
‫And it's just a complete pain when we only want to load, let's say, 20 bytes from the disk, we have
‫to load 512 bytes and discard the bytes that we're not using.
‫It's a lot of effort, and because of the way the disk works, we have to do it like that.
‫However, if we create a special disk streamer that does this for us, then we can happily read five
‫bytes at a time and allow this implementation to skip the other bytes that we don't need a much cleaner
‫solution.
‫So we're going to be implementing that in this lecture today.
‫So the very first thing to do then is go to source and disk and we're going to create a new file called
‫Streamer Domeij.
‫And we're going to go hashtag F and F strimmer page to find a stream of hate.
‫And you know what?
‫Let's call it disc.
‫OK, we are now going to include distant stage and we're going to go struct desk stream and it has struck
‫desk desk.
‫So this past variable represents the position we're currently at in the stream.
‫So if we've just created a disk stream, then the position will be zero.
‫So if we create a disk stream and bind it to a desk and we read the first byte, then that's going to
‫read from the very first sector in the disk and return us however amount of bytes we wanted to read.
‫That's essentially put the position here.
‫The past variable represents the position we're currently seeking at in the desk.
‫And this is not a sector position.
‫This is a pipe position.
‫So, for example, if the position was equal to 513, then we would be reading from the second sector
‫in the desk, essentially.
‫OK, so we're going to create a new file called Streambeds C and we're going to include streamer dot
‫org.
‫OK, now it's pretty simple to create this stream.
‫So just follow what I'm doing.
‫We're going to include memory.
‫Keep keep.
‫Org OK, because we're going to be using our heap.
‫Now we're going to go struct disk streamer disk stream new in it disk ID.
‫OK, now we're going to go struct desk disk equals disk get discarded.
‫If the desk is no, then they've passed us an invalid disguised, so return now, OK, otherwise we
‫need to create a new desk streamer.
‫OK, and we're going to stream.
‫And before I forget, guys, this should actually be disc's stream.
‫OK, just stream.
‫So change this stream into this stream like that.
‫So we've just created the new dish stream now and now we're going to say stream pass equals zero stream
‫desk equals desk, and then we're just going to return our stream.
‫OK, so this creates a new disk stream.
‫OK, and that returns a new disk stream, not a new disk stream.
‫So now we're going to say in it, disk, streamer, seek in it pass.
‫And we're also going to accept a disk streaming here, too, guys.
‫So this will reposition the position in the disk stream.
‫So the position provided is so easy.
‫We just do stream pass equals pass and we're just going to return zero.
‫OK, so we're now going to create the read function, which is the most complicated function in the
‫disk stream.
‫So we're going to go in it just stream arete struct this stream
‫void out and it total.
‫So simply what's going to happen here is it's going to read from the disk stream X amount of bytes represented
‫by total and it's going to read them into the outpointed by here, OK.
‫And if everything went OK, we'll return zero, otherwise it will return a negative value.
‫OK, so let's calculate the sector that we should be using.
‫So we go in it sector extreme position divided by sect size.
‫OK, so we're probably going to have to include configuration here as well.
‫OK, now we're going to go in it offset extreme position modulus, PCOS, sect size.
‫Now we're going to go shabbath post sector size.
‫So we're going to store this on the stack, guys.
‫So right now, we're going to go in it.
‫Rascal's desk, Reid Block, Dream Desk, Sector one puff.
‫OK, so that'll be one sector into this local variable we've created here.
‫OK, so if the if the response is below zero, then we need to go to the out because there's an error
‫that's happened and we're going to create the out label by here.
‫We're just going to return rest's.
‫OK, so now under here we need to calculate the total to reach.
‫So in it total to read equals total above PTW sector size.
‫Then we should use our sector size.
‫So we're doing a Tenebrae here.
‫OK, otherwise we use the total.
‫So if the total they provided to us is more than a sector, then we should use the sector size, otherwise
‫we'll use the total they provided to us.
‫Now we're going to have a full loop for an entire equals zero ie below total to read I plus plus and
‫now we're going to cast the out to a char pointer.
‫And we're going to have an officer plus side there as well.
‫OK, so that that basically reads from the Buffa where we loaded the sector into our out.
‫OK, so let me just explain this before we go any further.
‫We can't just load straight into the out because they are my only hold buffa for five bites and we can
‫only read 512 bytes at a time.
‫So what do we do?
‫We load the sector into this local variable path, OK?
‫And then we loop through that buffer and we set the out only the amount they're asking for.
‫So we never overflow the buffer.
‫So it's quite clever way of doing it.
‫Right.
‫But now we still have the problem.
‫The what if they ask three more than a sector while we're getting to that bit now?
‫So the first thing we do is we adjust the string because we've just read the total to read into the
‫out variable for them.
‫Right.
‫So we need to account for that.
‫So we go stream past plus equals total soreheads.
‫OK, and now we need to check if we need to repassed to sector.
‫OK, so to do that we say if total is above Peatross sector size then rascal's this stream arete stream
‫out total minus prosector sector size.
‫So you can see we use recursion here to allow us to read past the first sector and the out variable
‫here.
‫It points to the end of the sector that we've just read here at all times.
‫OK, because you can see we go out plus plus.
‫So by the time we reach here, the output points to the next sector to understand.
‫So that's how that works.
‫And then obviously we minus the sector size from our total before passing it to them.
‫So now they don't have as much to read.
‫So let's say that they pass a total of 513.
‫OK, what's going to happen is we're going to re 512 bytes one sect up into the buffer.
‫OK, that's what's going to happen now is total above the sector size.
‫Yes, it is.
‫Kasar totals five June 13.
‫So that's Tenebrae says that we should set the total read Topeka's sector size, which is 512.
‫OK, so total is five June 13.
‫The total to read is 512.
‫So we loop through Buffa where we load that sector and we set 512 bytes into the outpoint here and we
‫incremented by one each time.
‫OK, so now by the time we get to the at the adjust the streambed here, our outpoint is now 512 bytes
‫ahead, understand.
‫So now the next byte it reads will be by five and 13.
‫Now remember, at this point in time, total is still equal to five in 13.
‫Total to read is five, 10, 12.
‫So we adjust the stream here by going stream pass plus equals total to.
‫Right, OK, so our stream is now equal to five June 12 now totals five June 13.
‫So that's above the sector size.
‫So we know there's more to read.
‫We have one more byte to read, in fact.
‫So we going here, we passed in our stream out and I remember out suggested five, 10, 12 bytes ahead
‫now and then we get our total, which is five and 13.
‫We subtract 512, which is a sector size, which means the total that gets passed is one byte because
‫there's only one more byte left to read.
‫OK, good.
‫So now we're going to go through this again.
‫So we pass one here.
‫We're now back in this function here.
‫We read one sector because we can't read one bite from industry.
‫Have to read a whole sector in.
‫OK, now we're back in this statement now at all is one at this point is one above the sector decides.
‫No, it isn't.
‫So we use the total.
‫So total to read is total, which is one.
‫So we read one bytes from above four into the out and then we adjust the stream by one.
‫Is the total above the sector size.
‫Well one is not above five, ten, twelve.
‫So there's no more to read.
‫So we return the response to the.
‫Now this implementation can get dangerous because we rely on recursion.
‫Now everyone knows the recursion you have to be careful with because you can overflow the stack and
‫that's a very nasty thing.
‫However, this is a pretty decent solution.
‫It's a nice solution.
‫But there's always that one more chance that we write one more byte, the stack.
‫OK, so you need to be careful with recursion like this.
‫Make sure you have a very big stack.
‫OK, now this implementation can be improved, but is very simple to implement and simple code is always
‫the best.
‫OK, but yes, there is a minor concern that we could overflow the stack.
‫So you do need to be a little careful with recursion.
‫OK, so the final thing to do, we need to create the ability to close the stream so we just go void
‫this stream, a close struct stream and then we just go free stream, nice and simple.
‫OK, so we now need to put some of these things in the head of SodaStream, and you should go in the
‫header file, we want the prototype there, uh, just sike stream of seke.
‫OK, this stream arete.
‫Absolutely.
‫Um, just close.
‫OK, so what we now need to do is we need to add this to our makefile, so just go to the makefile here
‫and we're just going to go Bel's desk stream.
‫Adado just to the top here, OK?
‫And if we find desk Dotto by here, we can copy and paste that one down here.
‫And we're just going to go this strimmer, see?
‫This stream, Tazi, and there's no need to create a dis directory in the bill because we've already
‫created one because we've created to start over for.
‫So, yeah, you know, the drill.
‫So that should be OK.
‫And now if we now go make clean bill that S.H..
‫You can see that Bill is absolutely fine.
‫So let's now test on new functionality.
‫So if we just go to seed Ben and we're going to open a blast and we stop in by here, we're going to
‫pick a nice bite that we want to read.
‫So ideally past the first sector to test this.
‫Right.
‫So the first sector ends here.
‫So let's read byte Xerox 201, which is Bates', which is decimal 184.
‫OK, so let's just write that down.
‫And, you know, guys, if you can't find the same bytes as me, you just pick any bite.
‫We just need to test it.
‫So this is the office that I'm going to read Xerox to one, and it should be equal to one eight four.
‫OK, excellent.
‫So, yeah, we're going to go to kind of that see now and we're going to use our implementation and
‫see if it works.
‫OK, so we're going to scroll to the top in here.
‫We're going to include desk stream dot org.
‫And now inside.
‫Here, we're going to get rid of this because we don't need this now and we're just going to go just
‫stream anew and we're going to put zero in here because we only really care about the first.
‫Yeah, there is no other desks.
‫So we're going to go struck this streamer now struck this stream.
‫I mean, stream equals jet stream a new zero.
‫OK, so that creates the new disc streamer.
‫We now need to go disk stream seke.
‫And the address we had was zero to one.
‫So this will move the position of the stream.
‫OK, and we're now going to go chaussy equals zero disk stream arete stream ampersands C and we're going
‫to be one bytes.
‫OK, and just change char to one side char as well guys.
‫OK, well now we're going to go while one creates the infinite loop and obviously our sike needs to
‫also take in the stream by there as well guys.
‫OK, so if we now go back we're going to make clean build that S.H. can see that works perfectly fine.
‫We're going to go in and now we're going to debug this.
‫OK, so we're going to go JDB at symbol file dot dot forward slash build forward slash kernel full theta
‫zero one one, two, three, four, five.
‫You know, five of those zeros.
‫We're going to load that.
‫There we are.
‫This read the symbols now we're going to go break Col Dot C 96, OK, because line ninety six and now
‫we're going to go target remote.
‫I'm going to pipe out kumu.
‫You know the drill just like that.
‫OK, we're going to press enter now.
‫We're going to go see to continue.
‫We can see when I broke here we're going to go next and we can see we print stream that's allocated
‫to the heap correctly.
‫We're going to press next.
‫Now that's repositioned the pointer and we're going to go next.
‫And that creates our variable C and we're going to go next again.
‫And then by doing next again, we've read one bite into the variable and now we're going to print C
‫that gives us one hundred eighty four.
‫If we check the text so that we can see the zero to one is one hundred eighty four decimal.
‫So we've successfully made the disk stream which will make reading from the disk far easier down the
‫line.
‫We no longer have to read in sectors anymore.
‫That's all abstracted out at the lower levels.
‫Thanks for watching.