Topic: Standard Allocators


Author: "Greg Colvin" <spam@me.not>
Date: 1998/06/30
Raw View
> Is there a reason why those objects did not make their way into the
> standard? They are very useful (I'd say, some are essential), they
> are almost trivial (nothing where a great debate could be based on),
> they don't add overhead if not used (maybe they increase compile
> time a little bit, but if that had been the concern, they could have
> been put into a header on their own), and given what you said, they
> were there from the very beginning (therefore they weren't invented
> after deadline).

In order to get the STL down to a size that was acceptable to
the committee a lot was cut out.  Those doing the cutting did
their best to keep the essentials, but of course some were left
out.  Also, the STL has continued to evolve outside the standards
process, as in Dave Musser's invention of introsort and his hash
based map proposal, which have been incorporated into many STL's
but were too late for the standard.

Greg Colvin


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "P.J. Plauger" <pjp@dinkumware.com>
Date: 1998/07/01
Raw View
Greg Colvin <spam@me.not> wrote in article <01bda3e5$87b13da0$049485cc@gcolvin-hpc>...
> In order to get the STL down to a size that was acceptable to
> the committee a lot was cut out.  Those doing the cutting did
> their best to keep the essentials, but of course some were left
> out.

Right. And one of the nice things about STL is that it defines an
extensible framework. You can always add back the bits that
you really like, without risking the integrity of the standardized
core.

>      Also, the STL has continued to evolve outside the standards
> process, as in Dave Musser's invention of introsort and his hash
> based map proposal, which have been incorporated into many STL's
> but were too late for the standard.

Nothing prevents any implementation from adopting introsort for sort.
We've done so in our standard-conforming implementation of STL,
and clearly to good advantage. We have also added hash tables
to our product, even though the Standard doesn't require them,
because they're so widely useful.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: Christopher Eltschka <celtschk@physik.tu-muenchen.de>
Date: 1998/06/29
Raw View
Joe Buck wrote:

[...]

> Given the many useful objects in the STL that were not included in the
> standard, I think it's important to reserve "STL" for the larger set,
> as designed by Stepanov, to avoid confusion.

Is there a reason why those objects did not make their way into the
standard? They are very useful (I'd say, some are essential), they
are almost trivial (nothing where a great debate could be based on),
they don't add overhead if not used (maybe they increase compile
time a little bit, but if that had been the concern, they could have
been put into a header on their own), and given what you said, they
were there from the very beginning (therefore they weren't invented
after deadline).
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: James Kuyper <kuyper@wizard.net>
Date: 1998/06/27
Raw View
Joe Buck wrote:
>
> In article <359133D9.446B@wizard.net>, James Kuyper  <kuyper@wizard.net> wrote:
> >> > What's "select1st" then?
> >>
> >> It's part of the Standard Template Library, or at least the SGI implementation
> >> of it.  See http://www.sgi.com/Technology/STL/select1st.html for a detailed
> >> explanation.
> >
> >It's not in the STL, though it may be in the SGI implementation.
>
> Rather, it is in the STL (the name "Standard Template Library" was coined
> by Stepanov, and he is the coauthor of both the original HP and the SGI
> versions, both of which have "select1st").  It was not included in the
> standard, but it was included in the STL.
...
> Given the many useful objects in the STL that were not included in the
> standard, I think it's important to reserve "STL" for the larger set,
> as designed by Stepanov, to avoid confusion.

Any recommendations for how to refer to the thing in the ISO standard?
"the standard" refers to the whole thing; we need a term that refers
specifically to the template library that is part of the standard.
"standard STL" feels redundant. "ISO STL" feels too NASA-ish ;-)
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: AllanW@my-dejanews.com
Date: 1998/06/24
Raw View
In article <898598651.24795.0.nnrp-08.9e98e8f6@news.demon.co.uk>,
  "Jim Barry" <jim.barry@bigfoot.com> wrote:
>
> AllanW@my-dejanews.com wrote in message
> <6mmu4b$o9p$1@nnrp1.dejanews.com>...
> >
> >An exercise for interested readers:  Rewrite the third parameter to
> >find_if in terms of select1st, bind2nd, equal_to, and other standard
> >functors.
>
> What's "select1st" then?

It's part of the Standard Template Library, or at least the SGI implementation
of it.  See http://www.sgi.com/Technology/STL/select1st.html for a detailed
explanation.

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/   Now offering spam-free web-based newsreading
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: James Kuyper <kuyper@wizard.net>
Date: 1998/06/24
Raw View
AllanW@my-dejanews.com wrote:
>
> In article <898598651.24795.0.nnrp-08.9e98e8f6@news.demon.co.uk>,
>   "Jim Barry" <jim.barry@bigfoot.com> wrote:
> >
> > AllanW@my-dejanews.com wrote in message
> > <6mmu4b$o9p$1@nnrp1.dejanews.com>...
> > >
> > >An exercise for interested readers:  Rewrite the third parameter to
> > >find_if in terms of select1st, bind2nd, equal_to, and other standard
> > >functors.
> >
> > What's "select1st" then?
>
> It's part of the Standard Template Library, or at least the SGI implementation
> of it.  See http://www.sgi.com/Technology/STL/select1st.html for a detailed
> explanation.

It's not in the STL, though it may be in the SGI implementation.
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: "Jim Barry" <jim.barry@bigfoot.com>
Date: 1998/06/24
Raw View
AllanW@my-dejanews.com wrote in message
<6morv1$7ml$1@nnrp1.dejanews.com>...
>In article <898598651.24795.0.nnrp-08.9e98e8f6@news.demon.co.uk>,
>  "Jim Barry" <jim.barry@bigfoot.com> wrote:
>> What's "select1st" then?
>
>It's part of the Standard Template Library, or at least the SGI
implementation
>of it.  See http://www.sgi.com/Technology/STL/select1st.html for a
detailed
>explanation.

Ah, a handy SGI extension - well, it's not in CD2 anyway. It
definitely ought to be in the Standard Library, as it is the missing
link between <map> and <algorithm>.

- Jim

--
Jim Barry, Thermoteknix Systems Ltd., Cambridge, UK.
http://www.thermoteknix.co.uk Queen's Award for Export 1998
Antispam address courtesy of http://www.bigfoot.com
Or e-mail direct: jim at thermoteknix dot co dot uk



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: jbuck@best.com (Joe Buck)
Date: 1998/06/25
Raw View
In article <359133D9.446B@wizard.net>, James Kuyper  <kuyper@wizard.net> wrote:
>> > What's "select1st" then?
>>
>> It's part of the Standard Template Library, or at least the SGI implementation
>> of it.  See http://www.sgi.com/Technology/STL/select1st.html for a detailed
>> explanation.
>
>It's not in the STL, though it may be in the SGI implementation.

Rather, it is in the STL (the name "Standard Template Library" was coined
by Stepanov, and he is the coauthor of both the original HP and the SGI
versions, both of which have "select1st").  It was not included in the
standard, but it was included in the STL.

What it does: it is a function object that selects the first element of
a pair.  There is also select2nd.  It's too bad that these function objects
were not included in the draft standard.

Example of use: function to print the keys of any map or multimap:

template <class MapType>
void dump_keys(const MapType& myMap, ostream& strm)
{
    transform(myMap.begin(), myMap.end(),
       ostream_iterator<typename MapType::key_type>(strm, "\n"),
       select1st<typename MapType::value_type>());
}

To print the values instead, select1st becomes select2nd.

Given the many useful objects in the STL that were not included in the
standard, I think it's important to reserve "STL" for the larger set,
as designed by Stepanov, to avoid confusion.

--
-- Joe Buck
   work: jbuck@synopsys.com, otherwise jbuck@welsh-buck.org or jbuck@best.com
http://www.welsh-buck.org/
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: AllanW@my-dejanews.com
Date: 1998/06/25
Raw View
In article <359133D9.446B@wizard.net>,
  James Kuyper <kuyper@wizard.net> wrote:
>
> AllanW@my-dejanews.com wrote:
> >
> > In article <898598651.24795.0.nnrp-08.9e98e8f6@news.demon.co.uk>,
> >   "Jim Barry" <jim.barry@bigfoot.com> wrote:
> > >
> > > AllanW@my-dejanews.com wrote in message
> > > <6mmu4b$o9p$1@nnrp1.dejanews.com>...
> > > >
> > > >An exercise for interested readers:  Rewrite the third parameter to
> > > >find_if in terms of select1st, bind2nd, equal_to, and other standard
> > > >functors.
> > >
> > > What's "select1st" then?
> >
> > It's part of the Standard Template Library, or at least the SGI
> > implementation of it.
> > See http://www.sgi.com/Technology/STL/select1st.html for a detailed
> > explanation.
>
> It's not in the STL, though it may be in the SGI implementation.

You're right, and I wish SGI's documentation had said so.  (It does say so on
a comment line in the actual header file itself, but who reads header files
when there's good documentation available?)

OTOH, it's easy to define, and I think it or something similar is required
for the exercise I stated above.  Otherwise, how would you access member
first from the pair?

template <class pair> struct select1st {
    typedef pair argument_type;
    typedef pair::first_type result_type;
    const result_type& operator()(const pair& p) const { return p.first; }
};


-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/   Now offering spam-free web-based newsreading
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: AllanW@my-dejanews.com
Date: 1998/06/23
Raw View
In article <6mit3e$as3$2@superb.csc.ti.com>,
  johna@daldd.sc.ti.com wrote:
>
> I'm trying to work myself into the std libraries/templates.
> In particular, I'm interested in standard allocators. For
> instance, is there an equivalent 'Pool' template/class (as discussed
> in Stroustrup-2nd edition) that I can overload new/delete in
> some object.e.g
>
>   class foo {
>     static pool my_pool;
>   public:
>     void* operator new(size_t){ return my_pool.alloc(); }
>     void  operator delete(void* p){ my_pool.free(p); }
>   }

That's what an allocator is.  Here is a trivial (but possibly instructive)
example:

    #include <algorithm>    // find_if()
    #include <iostream>     // std::cerr
    #include <list>         // list<>
    #include <utility>      // pair<>

    // assert() might be more appropriate...
    void broken(const char*err)
    {
        std::cerr << '\n' << err << std::endl;
        err = 0; // *** SET DEBUGGER BREAKPOINT HERE ***
    }

    class pool {
        typedef std::pair<void*, size_t> data;
        unsigned long aTot, aCount;

        // list, not slist.  We frequently delete from the middle.
        std::list<data> busy;

        struct findVoidPtr {
            void *ptr;
            findVoidPtr(void *p) : ptr(p) {}
            bool operator()(const data&d) { return d.first==ptr; }
        };

    public:
        pool() : aTot(0), aCount(0) {}
        ~pool() {
            if (!busy.empty())
                broken("POOL not empty on delete");
            else
                std::cerr << aTot << " bytes allocated in "
                    << aCount << " allocations." << std::endl;
        }
        void *alloc(size_t size) {
            void *ptr = malloc(size);
            if (ptr)
            {
                aTot += size;
                ++aCount;
                busy.push_back(data(ptr,size));
            }
            return ptr;
        }
        void free(void*ptr)
        {
            if (!ptr) return;
            std::list<data>::iterator i(
                std::find_if(
                    busy.begin(),
                    busy.end(),
                    findVoidPtr(ptr)));
            if (i==busy.end())
                broken("Free on non-allocated item");
            else
                busy.erase(i);
        }
    };
What I've done here is to create an allocation pool that tracks memory it
doled out.  If you free an element not allocated from the pool (or double
free it), you get an error.  If you destroy the pool while allocations are
still alive, you get an error.  Otherwise the destructor spits out some
statistics about memory usage.  Use it with your class foo like this:

    int main()
    {
        foo *foo1 = new foo;  // Allocates first instance
        foo1 = new foo;       // Second instance, first pointer lost
        //...
        delete foo1;          // Destroys second instance;
    }
As foo::my_pool is destroyed, a diagnostic is displayed which warns of
the memory leak.



An exercise for interested readers:  Rewrite the third parameter to
find_if in terms of select1st, bind2nd, equal_to, and other standard
functors.  No fair creating a new class or function; the idea is to
eliminate the need for embedded class findVoidPtr or any other custom
function or class.

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/   Now offering spam-free web-based newsreading


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Jim Barry" <jim.barry@bigfoot.com>
Date: 1998/06/23
Raw View
AllanW@my-dejanews.com wrote in message
<6mmu4b$o9p$1@nnrp1.dejanews.com>...
>
>An exercise for interested readers:  Rewrite the third parameter to
>find_if in terms of select1st, bind2nd, equal_to, and other standard
>functors.

What's "select1st" then?

- Jim

--
Jim Barry, Thermoteknix Systems Ltd., Cambridge, UK.
http://www.thermoteknix.co.uk Queen's Award for Export 1998
Antispam address courtesy of http://www.bigfoot.com
Or e-mail direct: jim at thermoteknix dot co dot uk



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: johna@daldd.sc.ti.com (John Apostol)
Date: 1998/06/21
Raw View
I'm trying to work myself into the std libraries/templates.
In particular, I'm interested in standard allocators. For
instance, is there an equivalent 'Pool' template/class (as discussed
in Stroustrup-2nd edition) that I can overload new/delete in
some object.e.g

  class foo {
    static pool my_pool;
  public:
    void* operator new(size_t){ return my_pool.alloc(); }
    void  operator delete(void* p){ my_pool.free(p); }
  }

Thanks
-john



[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]