Vol: core underlying usage-3

Source: Internet
Author: User
Document directory
  • Example of Memory Manager usage
Example of Memory Manager usage

The memory manager provides two methods of memory application/release, similar to the original C/C ++ memory application/release method,

Method C corresponds to: alloc (size)/Free (P)

The C ++ method corresponds to mnew/mdelete.

 

Similar to the above "Hello world !" Example: Create a new project in the original Solution

Stdafx. h contains the header file of the Memory Manager # include
"Mobject. H"

1. Example of C method

# Include
"Stdafx. H"

 

Using namespace vol;

 

Enum test_count

{

Testc_max = 64, // 32768,

};

Int _ tmain (INT argc, _ tchar * argv [])

{

Srand (: gettickcount ());

Int I = 0;

Int J = 0;

// Apply for memory in C mode:

// Use alloc (size) to apply for memory

// Use realloc (p, size)
Change Original applied memory size

// Use free (P)
Release memory

Void ** Pv =
Reinterpret_cast <void **> (: virtualalloc (null, testc_max *
Sizeof (void *),

Mem_reserve | mem_commit, page_readwrite ));

If (Pv! = NULL)

{

For (int K = 0; k <2; ++ K)

{

// 1. random memory allocation

For (I = 0; I <testc_max; ++ I)

{

If (Pv [I] = NULL)

{

PV [I] = alloc (I * (RAND () % 1024 ));

}

Else

{

PV [I] = realloc (Pv [I], I * rand ());

}

}

// Report the random memory block information. Dump transfers null and only reports the number of blocks cached by the Memory Manager.

I = rand () % testc_max;

Dump (Pv [I]);

 

// 2. Random release of memory

For (I = 0; I <testc_max; ++ I)

{

J = (RAND () % testc_max );

If (Pv [J]! = NULL)

{

Free (Pv [J]);

PV [J] = NULL;

}

}

// Report the random memory block information. Dump transfers null and only reports the number of blocks cached by the Memory Manager.

I = rand () % testc_max;

Dump (Pv [I]);

}

: Virtualfree (PV, 0, mem_release );

}

Return0;

}

 

2. c ++ example

When applying for a class object instance in C ++ mode, the class object must inherit from the mobject

# Include
"Stdafx. H"

 

Using namespace vol;

 

Enum test_count

{

Testc_max = 64, // 32768,

};

 

Class ctestmenbase:
Publicmobject

{

Public:

Ctestmenbase (uint ucopy)

: M_usizecopy (ucopy)

{

}

Virtual ~ Ctestmenbase (void)

{

}

Public:

Uint m_usizecopy;

};

 

Template <uint usize>

Class ctestmem:
Publicctestmenbase

{

Public:

Ctestmem (uint ucopy)

: Ctestmenbase (ucopy)

{

}

 

Voidprint (void)

{

Dev_info (Tf ("% d --> % d"), m_usizecopy, usize );

}

 

PRIVATE:

Void * m_pt [usize];

};

 

Typedef ctestmem <10> ctestmem10;

Typedef ctestmem <20> ctestmem20;

Typedef ctestmem <30> ctestmem30;

Typedef ctestmem <40> ctestmem40;

Typedef ctestmem <50> ctestmem50;

Typedef ctestmem <60> ctestmem60;

Typedef ctestmem <70> ctestmem70;

Typedef ctestmem <80> ctestmem80;

Typedef ctestmem <90> ctestmem90;

Typedef ctestmem <100> ctestmem100;

Typedef ctestmem <200> ctestmem200;

Typedef ctestmem <300> ctestmem300;

Typedef ctestmem <400> ctestmem400;

Typedef ctestmem <500> ctestmem500;

Typedef ctestmem <600> ctestmem600;

Typedef ctestmem <700> ctestmem700;

Typedef ctestmem <800> ctestmem800;

Typedef ctestmem <900> ctestmem900;

Ctestmenbase * getobj (void)

{

Switch (RAND () % 18)

{

Case 0:
Returnmnew ctestmem10 (10 );

Case 1:
Returnmnew ctestmem20 (20 );

Case 2:
Returnmnew ctestmem30 (30 );

Case 3:
Returnmnew ctestmem40 (40 );

Case 4:
Returnmnew ctestmem50 (50 );

Case 5:
Returnmnew ctestmem60 (60 );

Case 6:
Returnmnew ctestmem70 (70 );

Case 7:
Returnmnew ctestmem80 (80 );

Case 8:
Returnmnew ctestmem90 (90 );

Case 9:
Returnmnew ctestmem100 (100 );

Case10:
Return mnew ctestmem200 (200 );

Case11:
Return mnew ctestmem300 (300 );

Case12:
Return mnew ctestmem400 (400 );

Case13:
Return mnew ctestmem500 (500 );

Case14:
Return mnew ctestmem600 (600 );

Case15:
Return mnew ctestmem700 (700 );

Case16:
Return mnew ctestmem800 (800 );

Case17:
Return mnew ctestmem900 (900 );

}

Returnnull;

}

 

Int _ tmain (INT argc, _ tchar * argv [])

{

Srand (: gettickcount ());

Int I = 0;

Int J = 0;

// Memory application in C ++ mode:

// Use mnew to create a Class Object

// Use mdelete
Release Class Object

Ctestmenbase ** po = reinterpret_cast <ctestmenbase **> (: virtualalloc (null, testc_max *
Sizeof (ctestmenbase *), mem_reserve | mem_commit, page_readwrite ));

If (Po! = NULL)

{

For (int K = 0; k <2; ++ K)

{

// 1. random memory allocation

For (I = 0; I <testc_max; ++ I)

{

If (po [I] = NULL)

{

Po [I] = getobj ();

}

}

// Report the random memory block information. Dump transfers null and only reports the number of blocks cached by the Memory Manager.

I = rand () % testc_max;

Dump (po [I]);

 

// 2. Random release of memory

For (I = 0; I <testc_max; ++ I)

{

J = (RAND () % testc_max );

If (po [J]! = NULL)

{

Mdelete po [J];

Po [J] = NULL;

}

}

// Report the random memory block information. Dump transfers null and only reports the number of blocks cached by the Memory Manager.

I = rand () % testc_max;

Dump (po [I]);

}

: Virtualfree (PO, 0, mem_release );

}

 

Return0;

}

The Memory Manager methods that can be used at any time include dump (p) to report memory information, and check (p) to check whether the memory is valid.


If all debugging information is output in debug mode, the memory manager will output detailed information about the memory application/release.

[Trace] alloc from cachedchunklist [34] (0x00460000)'s unused block [0x0046e410, remian 19 blocks], size (2829) [alloc (3072)]

[Trace] alloc block by file: e: \ codeback \ wom \ trunk \ testcore3 \ testcore3.cpp (0x00419740), function: wmain (0x004197c0), line: 108

[Trace] alloc from cachedchunklist [46] (0x00530000)'s unused block [0x00530010, remian 0 blocks], size (23184) [alloc (24576)]

[Trace] cachedchunklist [46] (0x00530000)'s block is all alloc, it's Prev Chunk is 00000000

[Trace] alloc block by file: e: \ codeback \ wom \ trunk \ testcore3 \ testcore3.cpp (0x00419740), function: wmain (0x004197c0), line: 108

[Trace] alloc from newchunklist [49] (0x00560000)'s single block [0x00560010], size (32895) [alloc (36824)]

[Trace] alloc block by file: e: \ codeback \ wom \ trunk \ testcore3 \ testcore3.cpp (0x00419740), function: wmain (0x004197c0), line: 108

[Trace] alloc from new chunklist [46] (0x00570000)'s unused block [0x00576010, remian 1 blocks], size (21956) [alloc (24576)]

No matter whether the C or C ++ method is used, the unreleased memory information will be reported when the underlying exit,

Debug: detailed reports on the files in which the unreleased memory is applied for and the size of the memory.

Release: the report contains the unreleased memory start address and size.

 

Check the memory leakage reports of the Memory Manager and correct the changes.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.