Windows System Software -- Consulting, Training, Development -- Unique Expertise, Guaranteed Results

Home NTDEV
Before Posting...
Please check out the Community Guidelines in the Announcements and Administration Category.

More Info on Driver Writing and Debugging


The free OSR Learning Library has more than 50 articles on a wide variety of topics about writing and debugging device drivers and Minifilters. From introductory level to advanced. All the articles have been recently reviewed and updated, and are written using the clear and definitive style you've come to expect from OSR over the years.


Check out The OSR Learning Library at: https://www.osr.com/osr-learning-library/


generic table usage

Pavel_SPavel_S Member Posts: 76

Hello guys.

Say I have generic table defined by myself (https://docs.microsoft.com/en-us/windows-hardware/drivers/ddi/content/ntddk/nf-ntddk-rtlinitializegenerictable) no matter this is AVL nor Splay.

Now, if my "content" of leaves in this tree contains some ULONGs or other simple POD style values I guess this is not a problem, but lets assume I have some strings in it. Is there any option to work on pointers to allocated memory instead of copying data every time I need to get some content?

I assume if I woudl return pointer to originally allocated memory (by allocate function I provided) it will be not thread safe right? Because I will never know if in another thread such content could be deleted... so i would need to have some ref counters to use otriginal memory OR I cna copy data all the time (but doing it under lock looks bit performance invasive). What is good way of handling this issue in general? how you address such issue?

Another question, more to confirm, if AVL tree needs rebalancing I guess under the hood there are no reallocations, but reassinging pointers "beloning" to tree right? I mean that AVL contains some structures and only those are "repinned" and memory allocated for content do not change?

Thank you.

Comments

  • Martin_DrábMartin_Dráb Member - All Emails Posts: 81

    You may work with pointers as elements (so the allocation routine would be called to allocate space for these pointers). Of course, you must ensure the pointers point to valid memory. As far as I read the documentation however, you need to synchronize access to the generic table since it is not thread-safe by its nature (e.g. an exclusive lock should be held when performing an insert operation).

    Another question, more to confirm, if AVL tree needs rebalancing I guess under the hood there are no reallocations, but reassinging pointers "beloning" to tree right? I mean that AVL contains some structures and only those are "repinned" and memory allocated for content do not change?

    I think rebalancing (in both AVL and splay tree cases) should not lead to any memory (de)allocations since tree rotations just work with pointers to left/right child and parent nodes.

    Martin Dráb

  • Pavel_SPavel_S Member Posts: 76
    edited June 2019

    Martin thank you for answer.
    I'd say direct usage of pointers depends on case. If however my data is connected to process my guess I cannot use it, because in another thread I may receive termination callback in which I would remove such data and if in another thread Im accessing this data (e.g ecuase I needed to check something there) there is no safe way to do it.

    Actually it doesn't need to be connected to process itself. I think in any scenario when you have multiple readers and multiple writers such scenario may happen.

  • Martin_DrábMartin_Dráb Member - All Emails Posts: 81

    I'd say direct usage of pointers depends on case. If however my data is connected to process my guess I cannot use it, because in another thread I may receive termination callback in which I would remove such data and if in another thread Im accessing this data (e.g ecuase I needed to check something there) there is no safe way to do it.

    I think that reference counting is probably the best option then, provided you can avoid circular references between objects. It is not so hard to implement and solves quite a lot of troubles for you.

    Martin Dráb

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Upcoming OSR Seminars
OSR has suspended in-person seminars due to the Covid-19 outbreak. But, don't miss your training! Attend via the internet instead!
Kernel Debugging 30 Mar 2020 OSR Seminar Space
Developing Minifilters 15 Jun 2020 LIVE ONLINE
Writing WDF Drivers 22 June 2020 LIVE ONLINE
Internals & Software Drivers 28 Sept 2020 Dulles, VA