How to Alloc/Dealloc Sync object.

Hi,

I want to know how to allocate and free sync object in runtime in WDM.( I am not sure whether it is in KDMF either ) What I see from the example code is that it always declare Sync object in DevExt and use it as below.

typedef struct _MyExt {

KEMUTEX m_mutex;

} MyEXT;

  1. wonder if it is available.
  2. if it is what would be the APIs that I can use to allocate/De-allocate. Especially I am interested in Mutex and Spinlock.

Thanks in Advance.
YY

xxxxx@hotmail.com wrote:

I want to know how to allocate and free sync object in runtime in WDM.( I am not sure whether it is in KDMF either ) What I see from the example code is that it always declare Sync object in DevExt and use it as below.

typedef struct _MyExt {

KEMUTEX m_mutex;

} MyEXT;

  1. wonder if it is available.
  2. if it is what would be the APIs that I can use to allocate/De-allocate. Especially I am interested in Mutex and Spinlock.

One of the nice things about kernel objects is that they don’t need to
be allocated and deallocated specially. YOU are responsible for
providing the memory. You then call a kernel API to fill in the fields,
but the memory still belongs to you. There is no hidden memory in the
background. When you’re through with the object, you just let the
memory go away like any other block of memory. If you have an object
that only needs to be used in a single function, you can even put the
memory on the stack.

This is quite different from user mode, where you call an API that
allocated a hidden memory block and returns you a handle. You have to
remember to close the handle to release the memory, otherwise the memory
hangs around.

In the shown case above, the memory will be released when your device
closes and the device extension is freed.


Tim Roberts, xxxxx@probo.com
Providenza & Boekelheide, Inc.

As Tim says you can allocate and de-allocate the objects, the one rule
you need to remember is you need to be sure that no thread is waiting on
the object or going to release the object once you de-allocate it.

Don Burn (MVP, Windows DKD)
Windows Filesystem and Driver Consulting
Website: http://www.windrvr.com
Blog: http://msmvps.com/blogs/WinDrvr

“Tim Roberts” wrote in message news:xxxxx@ntdev:

> xxxxx@hotmail.com wrote:
> > I want to know how to allocate and free sync object in runtime in WDM.( I am not sure whether it is in KDMF either ) What I see from the example code is that it always declare Sync object in DevExt and use it as below.
> >
> > typedef struct _MyExt {
> > …
> > KEMUTEX m_mutex;
> > …
> > } MyEXT;
> >
> >
> > 1. wonder if it is available.
> > 2. if it is what would be the APIs that I can use to allocate/De-allocate. Especially I am interested in Mutex and Spinlock.
>
> One of the nice things about kernel objects is that they don’t need to
> be allocated and deallocated specially. YOU are responsible for
> providing the memory. You then call a kernel API to fill in the fields,
> but the memory still belongs to you. There is no hidden memory in the
> background. When you’re through with the object, you just let the
> memory go away like any other block of memory. If you have an object
> that only needs to be used in a single function, you can even put the
> memory on the stack.
>
> This is quite different from user mode, where you call an API that
> allocated a hidden memory block and returns you a handle. You have to
> remember to close the handle to release the memory, otherwise the memory
> hangs around.
>
> In the shown case above, the memory will be released when your device
> closes and the device extension is freed.
>
> –
> Tim Roberts, xxxxx@probo.com
> Providenza & Boekelheide, Inc.

mm thanks Tim/Don,

then would it make sense to do in this way ?
typedef struct _MyExt {

PKEMUTEX m_pMutex; // or (PVOID m_pMutex;)

} MyEXT;

MySyncAllocRoutine()
{
MyExt->m_pMutex = ExAllocatePoolWithTag(NonPagedPool, sizeof(KMUTEX), ‘TEST’);
}

MySyncDeAllocRoutine()
{
MyExt->m_Mutex = ExFreePoolWithTag(MyExt->m_pMutex , ‘TEST’);
}

so then I can alloc/Dealloc the KMUTEX cleanly ?

Thanks
YY

xxxxx@hotmail.com wrote:

mm thanks Tim/Don,

then would it make sense to do in this way ?
typedef struct _MyExt {

PKEMUTEX m_pMutex; // or (PVOID m_pMutex;)

} MyEXT;

MySyncAllocRoutine()
{
MyExt->m_pMutex = ExAllocatePoolWithTag(NonPagedPool, sizeof(KMUTEX), ‘TEST’);
}

MySyncDeAllocRoutine()
{
MyExt->m_Mutex = ExFreePoolWithTag(MyExt->m_pMutex , ‘TEST’);
}

so then I can alloc/Dealloc the KMUTEX cleanly ?

That’s just an additional opportunity for errors. Why add the extra
complexity? Define it as a KMUTEX and forget about dynamic
allocation/deallocation. It’s only about 32 bytes, and you’ll use up
MORE memory by doing it dynamically.


Tim Roberts, xxxxx@probo.com
Providenza & Boekelheide, Inc.

Just use KEMUTEX and make it part of the device extension. There is no
reason to allocate this separately.

Don Burn (MVP, Windows DKD)
Windows Filesystem and Driver Consulting
Website: http://www.windrvr.com
Blog: http://msmvps.com/blogs/WinDrvr

xxxxx@hotmail.com” wrote in message
news:xxxxx@ntdev:

> mm thanks Tim/Don,
>
> then would it make sense to do in this way ?
> typedef struct _MyExt {
> …
> PKEMUTEX m_pMutex; // or (PVOID m_pMutex;)
> …
> } MyEXT;
>
> MySyncAllocRoutine()
> {
> MyExt->m_pMutex = ExAllocatePoolWithTag(NonPagedPool, sizeof(KMUTEX), ‘TEST’);
> }
>
> MySyncDeAllocRoutine()
> {
> MyExt->m_Mutex = ExFreePoolWithTag(MyExt->m_pMutex , ‘TEST’);
> }
>
> so then I can alloc/Dealloc the KMUTEX cleanly ?
>
> Thanks
> YY

Thanks Guys again.

I am porting some of old legacy “OS”-independent layer and found that it has been heavily using this dynamic Alloc/Dealloc on sync objects. This library will give a way to do the on-the-fly option.

I guess then the caller just add as many sync objects as they want and forget about deleting it would be best answer that I can get. :slight_smile:

xxxxx@hotmail.com wrote:

I am porting some of old legacy “OS”-independent layer and found that it has been heavily using this dynamic Alloc/Dealloc on sync objects. This library will give a way to do the on-the-fly option.

I guess then the caller just add as many sync objects as they want and forget about deleting it would be best answer that I can get. :slight_smile:

If you already have client code that expects this, the code you provided
will work fine. You can certainly allocate a KMUTEX and then initialize
it. (Remember to initialize it!)

For a new design, I would never do it that way, but if you’re fitting
existing code, maybe it’s the best.


Tim Roberts, xxxxx@probo.com
Providenza & Boekelheide, Inc.

> 2. if it is what would be the APIs that I can use to allocate/De-allocate.

Generally, this is allocation+init, like ExAllocatePoolWithTag+KeAcquireSpinLock.

But note that this creates the potential for races between wait and dealloc, so, in vast majority of cases, the sync objects in the kernel are used as fields of some other structures - not only devext, but IRP contexts and such.


Maxim S. Shatskih
Windows DDK MVP
xxxxx@storagecraft.com
http://www.storagecraft.com