SOS ½ô¼±ÇóÖú£¬¸÷λ´ó¸ç£¬°ï°ïæ°É (¹ØÓÚ allocator)
ÎÒÔÚÔËÐгÌÐòʱÓöµ½Ò»¸öÎÊÌ⣬Çë½Ì¸÷λ´ó¸ç£ºÉè¼ÆÒ»¸ö¿É±äµÄÊý×飬ÐèÒªÓõ½Ä¬ÈϵķÖÅäÆ÷£¬ÇëÎÊÈçºÎ¸³Öµ°¡£¿
²¿·Ö´úÂëÈçÏ£º
[font=ËÎÌå][size=3][font=ËÎÌå]template<class T, class Allocator = allocator<T> > [/font][/size][/font]
[font=ËÎÌå][size=3][font=ËÎÌå]class RangeArray { [/font][/size][/font]
[font=ËÎÌå][size=3][font=ËÎÌå] T *arrayptr; // pointer to array that underlies the container [/font][/size][/font]
[font=ËÎÌå][size=3][font=ËÎÌå] [/font][/size][/font]
[font=ËÎÌå][size=3][font=ËÎÌå] unsigned len; // holds length of the container [/font][/size][/font]
[font=ËÎÌå][size=3][font=ËÎÌå] int upperbound; // lower bound [/font][/size][/font]
[font=ËÎÌå][size=3][font=ËÎÌå] int lowerbound; // upper bound [/font][/size][/font]
[font=ËÎÌå][size=3][font=ËÎÌå] [/font][/size][/font]
[font=ËÎÌå][size=3][font=ËÎÌå] Allocator a; // allocator [/font][/size][/font]
[font=ËÎÌå][size=3][font=ËÎÌå]¡£¡£¡£¡£¡£¡£[/font][/size][/font]
[font=ËÎÌå][size=3][font=ËÎÌå]// Default constructor. [/font][/size][/font]
[font=ËÎÌå][size=3][font=ËÎÌå] RangeArray() [/font][/size][/font]
[font=ËÎÌå][size=3][font=ËÎÌå] { [/font][/size][/font]
[font=ËÎÌå][size=3][font=ËÎÌå] upperbound = lowerbound = 0; [/font][/size][/font]
[font=ËÎÌå][size=3][font=ËÎÌå] len = 0; [/font][/size][/font]
[font=ËÎÌå][size=3][font=ËÎÌå] arrayptr = a.allocate(0); [/font][/size][/font]
[font=ËÎÌå][size=3][font=ËÎÌå] } [/font][/size][/font]
[font=ËÎÌå][font=ËÎÌå][size=3]¡£¡£¡£¡£¡£[/size][/font][/font]
[font=ËÎÌå][font=ËÎÌå][size=3][/size][/font][/font]
[font=ËÎÌå][size=3][font=ËÎÌå]ÊéÉÏ˵Õâ¸öSTLÓÐĬÈϵķÖÅäÆ÷£¬¿ÉÈçºÎ¸øa¸³ÖµÄØ£¿°´ÕÕÎÒ¸øµÄ³ÌÐòÕâÑù×öµÄʱºò£¬±àÒëÀÏÊdzö´í£¬Ôõô¸Ã°¡£¿[/font][/size][/font]
[font=ËÎÌå][size=3][font=ËÎÌå]ÇóÇó¸÷λ´ó¸ç°ïÖú°ïÖúСµÜ°¡£¬Ð»Ð»ÁË£¡[/font][/size][/font] ÇóÇó¸÷λ´ó¸çÁË£¬¿ìµã°ïÎÒ½â¾öÕâ¸öÎÊÌâ°É£¬ÎÒÊǼ±µÄÏëÌøÂ¥Á˰¡£¡ ÎÒ¿´µ½Ò»¶ÎÀÛÊÀµÄ´úÂ룺
template<template <typename> class _Alloc>
class ACE_STL_Allocator : public ACE_Allocator
{
public:
typedef _Alloc<char> alloc_type;
public:
/// These methods are defined.
virtual void *malloc (size_t nbytes);
virtual void *calloc (size_t nbytes, char initial_value = '\0');
virtual void *calloc (size_t n_elem, size_t elem_size, char initial_value = '\0');
virtual void free (void *ptr);
/// These methods are no-ops.
virtual int remove (void);
virtual int bind (const char *name, void *pointer, int duplicates = 0);
virtual int trybind (const char *name, void *&pointer);
virtual int find (const char *name, void *&pointer);
virtual int find (const char *name);
virtual int unbind (const char *name);
virtual int unbind (const char *name, void *&pointer);
virtual int sync (ssize_t len = -1, int flags = MS_SYNC);
virtual int sync (void *addr, size_t len, int flags = MS_SYNC);
virtual int protect (ssize_t len = -1, int prot = PROT_RDWR);
virtual int protect (void *addr, size_t len, int prot = PROT_RDWR);
#if defined (ACE_HAS_MALLOC_STATS)
virtual void print_stats (void) const;
#endif /* ACE_HAS_MALLOC_STATS */
virtual void dump (void) const;
private:
alloc_type allocator;
};
ÇëÎÊÊDz»ÊÇÕâôÓõİ¡£¿
ÎÒ±àÒëµÄʱºòΪʲô˵ûalloc_type Õâ¸öÀàÐÍÄØ£¿ [url]http://dev.csdn.net/Develop/article/17/17946.shtm[/url]
¹ØÓÚSTL²»±»Ö§³ÖµÄÎÊÌâºÜ¶à£¬Èç¹û³öÏÖÄã˵µÄÕâÖÖÇé¿öÒ²²»ÊÇºÜÆæ¹ÖÁË¡£ Allocator a ÒѾÓÉËüµÄ constructor ³õʼ»¯ºÃÁË, ²»Óø³Öµ, Ö±½ÓʹÓü´¿É ¾ÙÀý:
[code]
[color=blue]-(guest@mac:tty1)-(cpp)-
[5653 0] %[/color] cat allocator.cc
#include <memory>
using namespace std;
template <class T, class Allocator = allocator<T> >
class Array
{
public:
Array(unsigned _len)
{
len = _len;
arrayptr = a.allocate(len);
}
~Array()
{
a.deallocate(arrayptr, len);
}
private:
Allocator a;
unsigned len;
T * arrayptr;
};
int main(int argc, char *argv[])
{
Array<int> a(10);
Array<int, allocator<int> > b(10);
}
[color=blue]-(guest@mac:tty1)-(cpp)-
[5653 0] %[/color] g++ allocator.cc
[color=blue]-(guest@mac:tty1)-(cpp)-
[5653 0] %[/color]
[/code] ÄÇÔõô×ö²ÅÄÜʹSTL±»Ö§³ÖÄØ£¿ ÄѵÀûÄĸöC++µÄ°æ±¾Ö§³ÖÂð£¿
Èç¹ûÎÒ»»¸ö±àÒëµÄ»·¾³£¬ÊDz»ÊÇ¿ÉÒÔ½â¾öÕâ¸öÎÊÌâÄØ£¿ ʲôÒâ˼? ÏÖÔڵıàÒëÆ÷Ò»°ã¶¼Ö§³Ö STL °¡ ÄÇÎÒÏë×Ô¼º×ö¸öSTL£¬ÄѵÀ¾Íû°ì·¨ÑÏÕûËüµÄÕýÈ·Óë·ñÁË£¿ Ô½·¢ºýÍ¿ÁË...:( ¿ÉÔõôÎÒ°ÑÄãµÄ³ÌÐòÖ±½Ó¸´ÖÆÉÏÈ¥Ò²±àÒë²»³É¹¦ÄØ£¿ÊDz»ÊDz»Ö§³ÖSTL ÄØ£¿ Äãʲôϵͳ£¿gcc ʲô°æ±¾£¿ WindowµÄ°¡ C++6¡£0µÄ°æ±¾°¡ ÕâÀïĬÈÏÊÇ Linux :( VC6 ¶ÔC++±ê×¼Ö§³ÖµÄ²»ºÃÓÈÆäÊÇÄ£°å
Ò³:
[1]