LinuxÒÁµéÔ°ÂÛ̳'s Archiver

rensheng05 ·¢±íÓÚ 2005-6-29 01:08

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]

rensheng05 ·¢±íÓÚ 2005-6-29 01:09

ÇóÇó¸÷λ´ó¸çÁË£¬¿ìµã°ïÎÒ½â¾öÕâ¸öÎÊÌâ°É£¬ÎÒÊǼ±µÄÏëÌøÂ¥Á˰¡£¡

rensheng05 ·¢±íÓÚ 2005-6-29 01:24

ÎÒ¿´µ½Ò»¶ÎÀÛÊÀµÄ´úÂ룺
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 Õâ¸öÀàÐÍÄØ£¿

flag ·¢±íÓÚ 2005-6-29 09:35

[url]http://dev.csdn.net/Develop/article/17/17946.shtm[/url]
¹ØÓÚSTL²»±»Ö§³ÖµÄÎÊÌâºÜ¶à£¬Èç¹û³öÏÖÄã˵µÄÕâÖÖÇé¿öÒ²²»ÊÇºÜÆæ¹ÖÁË¡£

dearvoid ·¢±íÓÚ 2005-6-29 10:05

Allocator a ÒѾ­ÓÉËüµÄ constructor ³õʼ»¯ºÃÁË, ²»Óø³Öµ, Ö±½ÓʹÓü´¿É

dearvoid ·¢±íÓÚ 2005-6-29 10:12

¾ÙÀý:
[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]

rensheng05 ·¢±íÓÚ 2005-6-29 17:48

ÄÇÔõô×ö²ÅÄÜʹSTL±»Ö§³ÖÄØ£¿

rensheng05 ·¢±íÓÚ 2005-6-29 17:50

ÄѵÀûÄĸöC++µÄ°æ±¾Ö§³ÖÂð£¿
Èç¹ûÎÒ»»¸ö±àÒëµÄ»·¾³£¬ÊDz»ÊÇ¿ÉÒÔ½â¾öÕâ¸öÎÊÌâÄØ£¿

dearvoid ·¢±íÓÚ 2005-6-29 17:51

ʲôÒâ˼? ÏÖÔڵıàÒëÆ÷Ò»°ã¶¼Ö§³Ö STL °¡

rensheng05 ·¢±íÓÚ 2005-6-29 17:52

ÄÇÎÒÏë×Ô¼º×ö¸öSTL£¬ÄѵÀ¾Íû°ì·¨ÑÏÕûËüµÄÕýÈ·Óë·ñÁË£¿

dearvoid ·¢±íÓÚ 2005-6-29 18:03

Ô½·¢ºýÍ¿ÁË...:(

rensheng05 ·¢±íÓÚ 2005-6-29 20:32

¿ÉÔõôÎÒ°ÑÄãµÄ³ÌÐòÖ±½Ó¸´ÖÆÉÏÈ¥Ò²±àÒë²»³É¹¦ÄØ£¿ÊDz»ÊDz»Ö§³ÖSTL ÄØ£¿

dearvoid ·¢±íÓÚ 2005-6-29 21:00

Äãʲôϵͳ£¿gcc ʲô°æ±¾£¿

rensheng05 ·¢±íÓÚ 2005-6-29 21:15

WindowµÄ°¡

rensheng05 ·¢±íÓÚ 2005-6-29 21:17

C++6¡£0µÄ°æ±¾°¡

dearvoid ·¢±íÓÚ 2005-6-29 21:29

ÕâÀïĬÈÏÊÇ Linux :(

Clerk_9919 ·¢±íÓÚ 2006-5-15 15:55

VC6 ¶ÔC++±ê×¼Ö§³ÖµÄ²»ºÃÓÈÆäÊÇÄ£°å

Ò³: [1]

Powered by Discuz! Archiver 6.1.0  © 2001-2007 Comsenz Inc.