Integer Pool

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?VHJlY2l1cw==?=

    Integer Pool

    Hello, Newsgroupians:

    I have a method that requires me to specify a client handle when I create
    the object. The object prototype is as follows...

    void Server.CreateMo nitor(string strName, int nClientHandle);

    When I create the monitor, I have specify a client handle. Therefore, when
    using other methods that came with the library, all I need to do is specify a
    client handle.

    I'd like to create a "handle pool" that will allow me to grab an integer
    from the pool and pass that into the function. This is synonymous to
    creating a handle in Win32; the handle is unique. Does anyone have any good
    resources as to where I can find some good information regarding creating a
    pool that will do this for me? I've been doing some searching, but all I can
    find is "thread pools" and not "handle pools." Perhaps my terminology is
    incorrect?

    How does Windows do it to create handles and make them unique?

    Thank you,


    Trecius
  • Ashutosh Bhawasinka

    #2
    Re: Integer Pool

    If there is a direct mapping of the Handle with some value, like it's
    type (which is not very clear in your description) you can use a
    Dictionary like this

    Dictionary<int, inthandles = new Dictionary<int, int>();
    or
    Dictionary<stri ng, inthandles = new Dictionary<stri ng, int>();

    Where the key will be the type of handle and it's value will be the
    actual handle
    Then you can add/set the handle like this

    handles["FileHandle "] = xyz;

    and you will retrieve or use it like this
    int handle = handles["FileHandle "];

    Thanks & Regards,
    Ashutosh

    Trecius wrote:
    Hello, Newsgroupians:
    >
    I have a method that requires me to specify a client handle when I create
    the object. The object prototype is as follows...
    >
    void Server.CreateMo nitor(string strName, int nClientHandle);
    >
    When I create the monitor, I have specify a client handle. Therefore, when
    using other methods that came with the library, all I need to do is specify a
    client handle.
    >
    I'd like to create a "handle pool" that will allow me to grab an integer
    from the pool and pass that into the function. This is synonymous to
    creating a handle in Win32; the handle is unique. Does anyone have any good
    resources as to where I can find some good information regarding creating a
    pool that will do this for me? I've been doing some searching, but all I can
    find is "thread pools" and not "handle pools." Perhaps my terminology is
    incorrect?
    >
    How does Windows do it to create handles and make them unique?
    >
    Thank you,
    >
    >
    Trecius
    >

    Comment

    • Peter Duniho

      #3
      Re: Integer Pool

      On Mon, 03 Nov 2008 10:50:06 -0800, Trecius
      <Trecius@discus sions.microsoft .comwrote:
      Hello, Newsgroupians:
      >
      I have a method that requires me to specify a client handle when I create
      the object. The object prototype is as follows...
      >
      void Server.CreateMo nitor(string strName, int nClientHandle);
      >
      When I create the monitor, I have specify a client handle. Therefore,
      when
      using other methods that came with the library, all I need to do is
      specify a
      client handle.
      >
      [...]
      How does Windows do it to create handles and make them unique?
      Handles for the unmanaged Windows API are related to the internal data
      structures for Windows. They are unique, because they refer to unique
      locations in those data structures.

      The library design seems suspect to me. It seems to me that if you're
      creating an object, like a "monitor" (whatever that is), one of two
      scenarios exist: there's a one-to-one mapping between the "nClientHan dle"
      and the created "monitor" or there may be multiple "monitor"s for each
      "nClientHandle" .

      In the former case, it seems to me that the reference to the monitor is
      itself the unique identifier you need. In the latter case, then you
      either have an instance of an object that's creating the monitor or you
      can create some kind of reference object for that purpose. In either
      case, the reference to that object can be provided as an appropriate
      identifier.

      Of course, all of that begs the question as to why you're supplying any
      kind of reference or identifier at all. A proper library, when you ask it
      to create something, would itself return a reference to that something.
      Making the _caller_ come up with the reference seems silly.

      Now, all of that would require fixing the library so that you can pass
      references (e.g. to Object) instead of an integer. If you've got a
      poorly-designed library where that's not an option, then I guess you're
      stuck having to come up with a unique integer. Approaches to that include:

      Using "unsafe" code you could just convert a reference to an integer, so
      that's one option.

      Just keep a counter that you increment each time you need a new int; that
      gives you 4 billion values before you run out, which in most cases would
      be enough.

      If 4 billion isn't enough, but you won't ever have nearly that many
      integers actually in use at once, then you could maintain a List<intof
      "retired" identifiers; as long as there's something in the list, use the
      first number available in the list, otherwise increment your counter. The
      counter will never go higher than the number of identifiers ever in use at
      once (but the length of the List<Twill have to be large enough to
      account for the difference between the largest number of identifiers ever
      in use at once and the largest retirement of identifiers).

      Without more information, it's hard to say what the best approach is.
      Except that fixing the library itself is the best solution. :)

      Pete

      Comment

      • =?Utf-8?B?VHJlY2l1cw==?=

        #4
        Re: Integer Pool

        Thank you, but it's not quite what I'm looking for. I may not know the
        strName until runtime. For example, if I were to just start the program, and
        I have one element. I want to get one item from the pool -- maybe it will be
        1. Now I know that no other item can be 1. Next, if I have another element
        at run time, I want to get another handle. I call the pool for another
        handle, and it gives me one -- maybe it will be 2. Now maybe I'm done with
        the first handle and no longer require it, so I release the handle back to
        the pool, so if I call the pool for another handle, it may return a 1 to me
        because I had already release it.


        Trecius

        "Ashutosh Bhawasinka" wrote:
        If there is a direct mapping of the Handle with some value, like it's
        type (which is not very clear in your description) you can use a
        Dictionary like this
        >
        Dictionary<int, inthandles = new Dictionary<int, int>();
        or
        Dictionary<stri ng, inthandles = new Dictionary<stri ng, int>();
        >
        Where the key will be the type of handle and it's value will be the
        actual handle
        Then you can add/set the handle like this
        >
        handles["FileHandle "] = xyz;
        >
        and you will retrieve or use it like this
        int handle = handles["FileHandle "];
        >
        Thanks & Regards,
        Ashutosh
        >
        Trecius wrote:
        Hello, Newsgroupians:

        I have a method that requires me to specify a client handle when I create
        the object. The object prototype is as follows...

        void Server.CreateMo nitor(string strName, int nClientHandle);

        When I create the monitor, I have specify a client handle. Therefore, when
        using other methods that came with the library, all I need to do is specify a
        client handle.

        I'd like to create a "handle pool" that will allow me to grab an integer
        from the pool and pass that into the function. This is synonymous to
        creating a handle in Win32; the handle is unique. Does anyone have any good
        resources as to where I can find some good information regarding creating a
        pool that will do this for me? I've been doing some searching, but all I can
        find is "thread pools" and not "handle pools." Perhaps my terminology is
        incorrect?

        How does Windows do it to create handles and make them unique?

        Thank you,


        Trecius
        >

        Comment

        • Ashutosh Bhawasinka

          #5
          Re: Integer Pool

          Handles are not just any items that you put into a pool and use any
          available one.

          Each handle (finally) maps to a kernel object in the system. So I am not
          sure if your usage is correct.

          If all your handles are for a single type of object and you are not
          bothered about a particular instance, then you can use a Stack or a
          Queue or List to store the handles.


          Trecius wrote:
          Thank you, but it's not quite what I'm looking for. I may not know the
          strName until runtime. For example, if I were to just start the program, and
          I have one element. I want to get one item from the pool -- maybe it will be
          1. Now I know that no other item can be 1. Next, if I have another element
          at run time, I want to get another handle. I call the pool for another
          handle, and it gives me one -- maybe it will be 2. Now maybe I'm done with
          the first handle and no longer require it, so I release the handle back to
          the pool, so if I call the pool for another handle, it may return a 1 to me
          because I had already release it.
          >
          >
          Trecius
          >
          "Ashutosh Bhawasinka" wrote:
          >
          >
          >If there is a direct mapping of the Handle with some value, like it's
          >type (which is not very clear in your description) you can use a
          >Dictionary like this
          >>
          >Dictionary<int , inthandles = new Dictionary<int, int>();
          >or
          >Dictionary<str ing, inthandles = new Dictionary<stri ng, int>();
          >>
          >Where the key will be the type of handle and it's value will be the
          >actual handle
          >Then you can add/set the handle like this
          >>
          >handles["FileHandle "] = xyz;
          >>
          >and you will retrieve or use it like this
          >int handle = handles["FileHandle "];
          >>
          >Thanks & Regards,
          >Ashutosh
          >>
          >Trecius wrote:
          >>
          >>Hello, Newsgroupians:
          >>>
          >>I have a method that requires me to specify a client handle when I create
          >>the object. The object prototype is as follows...
          >>>
          >>void Server.CreateMo nitor(string strName, int nClientHandle);
          >>>
          >>When I create the monitor, I have specify a client handle. Therefore, when
          >>using other methods that came with the library, all I need to do is specify a
          >>client handle.
          >>>
          >>I'd like to create a "handle pool" that will allow me to grab an integer
          >>from the pool and pass that into the function. This is synonymous to
          >>creating a handle in Win32; the handle is unique. Does anyone have any good
          >>resources as to where I can find some good information regarding creating a
          >>pool that will do this for me? I've been doing some searching, but all I can
          >>find is "thread pools" and not "handle pools." Perhaps my terminology is
          >>incorrect?
          >>>
          >>How does Windows do it to create handles and make them unique?
          >>>
          >>Thank you,
          >>>
          >>>
          >>Trecius
          >>>
          >>>

          Comment

          • Peter Duniho

            #6
            Re: Integer Pool

            On Mon, 03 Nov 2008 11:14:02 -0800, Trecius
            <Trecius@discus sions.microsoft .comwrote:
            Thank you, but it's not quite what I'm looking for. I may not know the
            strName until runtime. For example, if I were to just start the
            program, and
            I have one element. I want to get one item from the pool -- maybe it
            will be
            1. Now I know that no other item can be 1. Next, if I have another
            element
            at run time, I want to get another handle. I call the pool for another
            handle, and it gives me one -- maybe it will be 2. Now maybe I'm done
            with
            the first handle and no longer require it, so I release the handle back
            to
            the pool, so if I call the pool for another handle, it may return a 1 to
            me
            because I had already release it.
            Here's an implementation of the above, that is basically what I described
            as the third proposal in my previous reply:

            class IntPool
            {
            private int _iCur;
            private List<int_liReti red = new List<T>();

            public int GetNext()
            {
            int iRet;

            if (_liRetired.Cou nt 0)
            {
            iRet = _liRetired[_liRetired.Coun t - 1];
            _liRetired.Remo veAt(_liRetired .Count - 1);
            }
            else
            {
            unchecked { iRet = _iCur++ };
            if (_iCur == 0)
            {
            // All 4 billion integers are currently in use!
            throw new InvalidOperatio nException("no more ints
            available");
            }
            }

            return iRet;
            }

            public void Retire(int i)
            {
            _liRetired.Add( i);
            }
            }

            Apologies for typos and minor bugs...I didn't compile this, never mind
            test it.

            Pete

            Comment

            Working...