[Autogenerated] there is a hip command available in windy PG that can
show you some stuff related to hips in the process.
So for example,
let me run CPU stress here just as an application which is
not UWP application and then launch win debug and attached to
it is attached to process here, find CPU stress and attached to it.
One thing we can do is use the heap command.
If I just use the heap command,
you can see this of hips in the process right now.
As you can see,
the type of hip is shown here in the beginning address
of the hip is shown on the left.
And because this is a normal application didn't opt
in into using the segment heap, it uses the nt heap.
There are several hips already created by entities within this process.
You can use the dash s option to show some more details,
some summary details about the hips such as the reserve size,
the committed size, which one of these hips,
you can see whether the L.
F.
H.
Has been enabled for these hip or hips and there are many other options.
In fact for the heat command,
I encourage you to look up these options in the wind,
debug documentation.
Let's try something else such as Notepad Now, Notepad in Windows 11.
The new Notepad is W W.
P.
Applications coming from the store technically.
And so it should be slightly different in terms of heaps,
Let me launch another windy bug and attached to that.
Notepad isn't attached to process again, find Notepad and attaching to it.
If I tried the same thing,
we'll be able to see that we have several hips here in the process,
some of them are segment hip, you can see the term segment here,
indicates this is a segment heap implementation.
There are also some native normal anti hips in the process.
That's the classic hip is as just we've seen earlier,
we can we can use hip dash as to see some summary of
these hips as they are provided, at least to some extent.
So let's see how we can use the heap in API Let me just minimize
all this stuff here and go back to visual studio.
So I'm going to create a new process here.
Yeah.
And that sort of new application,
that's going to be a simple console application as we've
done multiple times in this course, let's call that hips just for fun.
What I want to do here is to show you how to create a separate hips for
certain size allocation based on some data structure,
which is one of the motivations I mentioned for creating separate hips.
So I want to use Windows dot h here.
I'm going to have S D I.
O just in case that they needed.
So here's the way this is going to work.
I'm going to create my own class here and let's say that this kind of
class or structure is used a lot in my process.
There are lots of allocations and the allocations in order to to remove
fragmentation and to optimize memory allocations without having work
with a different process hip I would like to create my own heap that's
going to be used for managing this, my data objects so to speak.
So I have this class here called my data,
it could be class could be struck,
it doesn't really matter and I might have some data
there so here's some data called X, maybe some data called Y.
It doesn't really matter what I'm going to store here.
It's really not the point.
The point is that might have of course some other functions like do work,
they're going to do something whatever that may be but what I want to do
is to have all these instances of my data being created from the heap if
you're using just pure C or the only thing we can do here um is really
just uh try to use the heap APIs So it's possible for me to do something
like a hip I'll here,
try to allocate star from from some heat but before that I might even want
to call him create to create a new hip but that would be a messy and not
very convenient because the client needs to know that needs to call these
APIs maybe some rappers I provide.
What I want to do is to make things even easier when using
C++ so I can do something like this,
I can add my data dot h here that I can do something like my data star and
just allocate my data using normal C++ code while also perhaps passing
arguments to the con instructor and in fact using the power of C++ here to
create objects in the normal way.
And of course we can do that using the make unique or make shared
APIs C++ 11 or later to make these things even more interesting for
for our purposes but for now it's going to be good enough to do a
simple allocation here and here.
I'm going to just go ahead and delete that object and in the
meantime we can use something like do work and do something with
the object so they want to happen here.
I would like to have this allocation happening from
a different hip for its own hip.
So you can use C++ features here to make it transparent so that
the client doesn't need to know that these objects actually
being created from a separate hip.
So here's how we can do that,
we can have some statics here that are going to hold the handle to the hip.
This could also be of course the global variables maybe even also have
something like counting the number of objects that we have.
So we know when it's time to completely destroy the hip.
So these have to be static in the sense that they have to have a single
instance for any number of my data instances out there,
that's of course very important.
Also going to have my data here,
let's just have two values for the constructor so we can do
something a bit more interesting to see that I think these things
are actually working and do something useful.
Let me even go ahead and make these members of private.
So I need to have these statics here and the reel feature here
that makes all this work is the fact that I can overload the new
and delete operator in C plus plus.
So you can use operator new here and operator and you always accept a size.
That's the very least that it can have.
It can accept other parameters if you so desire,
but at least the size and here we'll have operator delete.
That's going to accept a pointer that we need to free.
So here's where all the magic is going to happen.
I'm going to press control dot to uh to add an entry for this
in the CPP file and just switch to that.
And also I'm going to add and include for Windows dot h because you're not
using a pre compiled header here so have to have every CPP file have access
to the Windows APIs So here's the new operator.
And what I want to do here is when the object I'm going to create is the first
one I'd like to create the heap up front so you can do something like this if
the number of objects one implementing that at this point because we're going
to allocate one very soon is zero.
It means that we are able to create our own hip by calling the hip,
create API with hip create we might have several options here.
We can use one of them.
Might say that we don't want to have any synchronization for the hip.
This is constant called hypno serialized.
But for now I'm just going to keep it simple and use
flags of zero to get all the default.
Then we can have an initial committed size that we select 64 kilobytes.
That's due to the 16th power that is 64 kilobytes.
And with the maximum size that can specify either zero,
which indicates the hip is unbounded so its global can
grow for as long as as much as possible,
as much as the address space and memory in the system would
allow or have some maximum size for that.
Let's just go with two megabytes just for demonstration purposes.
And the last thing I need to do is to call the hip a lock
function using that exact hip to allocate the exact size
bite that I have been given.
But this time use that new hip that I've created and in the meantime
I've also implemented the number of objects here.
So now we can implement the delete operator.
The operator is the exact opposite.
So first we need to free that allocation,
we've been given that would be the hip that we're using flags again,
don't matter in this particular case.
And the address itself, let's call that P.
And now we can subtract one of the objects here.
I want to count from the objects were managing.
And if we reach zero, perhaps a good time to call him,
destroy and destroy the hip entirely.
So that's basically it.
Let's see if that works.
Let's just complete some of the stuff here,
like the constructor here by using simple
initialization here to initialize those members.
And in do work.
Let's just go ahead and print the the values of these value variable.
So let's just write X equals X and Y equals Y.
Just display X and M Y.
Just for demonstration purposes.
Because that means we also have to have another include here for S T D I
O because I placed everything in in separate files,
going back to our main application.
Of course now we have to uh to change something here.
And of course we can create as many of these as we want.
Let's create another one for For flexibility and and
some variants called P two here.
And then we also need to do beat P two.
And to see whether that actually does what we're expecting
that to do will set up break points.
So we'll have a break point in operator new here.
And operator delete as well.
So let's build that, see if it builds successfully.
So we get to unresolved externals here because when we have a static members,
it is necessary to also define them globally with static here.
The other option is to use the in line keyword which is
available in C++ 17 to make that automatic.
So currently, because we just always defaults to C++ 14 in these days,
the Visual Studio here doesn't seem to be happy.
I'm going to go to the properties here,
select all platforms or configurations for simplicity and change the
standards of the C++ compile C++ 17 at the very least now it should
go without any issues so let's try it.
I'm going to press F five here.
Well actually five is not a good idea because that
would be our mini Excel application.
Let me just right click here and make that the startup project and then press
F five so you should hit the breakpoint fairly quickly.
Here goes the breakpoint notice the size is eight,
that's the size of my data, it makes sense.
It has two integers each 14 bytes in size so the size of eight
makes perfect sense as object is currently zero.
So we're going to create a hip here,
so here goes the hip is handle is null now but then we're going
to create a hip and we get back something so we have a hip in
the process and HIPAA lock will make the allocation if you
continue running with F Five.
We have another operator new here.
But of course we can look at the output and already we see the output
form P because P do work has been called as well.
Now we're actually allocating here.
The second object we can drop to maine to see where we are at.
So here is where where we are at.
We're initializing my data here.
So we're actually using a different hip.
And of course,
we can see that as well in VM map if you so desire using a different hip,
which is not the default process here, which is always available.
But we're not.
We don't want to use that in this example.
So continuing on with that, pressing a five.
We get to operate or delete here.
So this point as object is to before we make the detriment here,
we need to free this chunk here and we're going to do that.
So here we bring that.
We don't need to destroy the hip just yet.
Does this still another object that is using the hips.
So as objects drops to one to press the five again to continue,
we find ourselves here in the district operator again.
Um And and by the way,
we can of course look at the actual value here for P and see what the object is.
You can see the values here.
Here's five.
And here's two.
It's also possible to simply show that with four byte integers.
So here's five is too that's the data within this particular p
That's the value we're trying to free right now.
And uh and that's it.
Let's free that one.
And since now the number of object drops to zero,
the hip is going to be destroyed in its entirety.
Once we do that notice that the addresses has become inaccessible,
Everything is now gone because denier entire heap has been
released using the virtual free function as we've seen earlier
with the men release value there.
So this is a very nice and elegant way of using a custom heap or a different
hip when using C++ If you're not using C++ you might create your own allocate
and free functions that clients are supposed to call and all the magic happens
in the implementation of those functions,
you might use uh custom hip or whatever you'd like to do