Vol: core underlying usage-4

Source: Internet
Author: User
Document directory
  • Example of Local Memory Management
Example of Local Memory Management

Local Memory Management is built on the memory manager and has two main functions:

1. Optimized support for local scope memory Application

  1. In the method, we may need to use a relatively large memory block as a transit station for temporary data processing, and we cannot determine the size of this memory block, the general method is to apply for memory through new or malloc, so the extra guarantee of memory release is required.
  2. In the iteration method, the local variable is relatively large, and a few iterations cause the default stack overflow.

 

2. cache processing for fixed-size memory blocks

This is mainly for scenarios where a large number of memory blocks with a fixed upper limit need to be repeatedly used, such as the network data sending and receiving of servers.

 

Note: Vol: The Core single-piece platform, hash and memory manage in core can be directly used. Before using other single-piece, you need to initialize initcoreinstance.

 

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
"Memaid. H"

# Include
"Stdafx. H"

 

Using namespace vol;

 

Enum memaid_len

{

Memaid_len_max = 1024,

};

Class ctestmemaid:
Public mobject

{

Public:

Ctestmemaid (void ){}

~ Ctestmemaid (void ){}

Public:

Void * m_pt [1024];

};

 

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

{

// Call the initcoreinstance of Vol: core to initialize the non-core single piece of Vol: core.

// The first parameter of initcoreinstance indicates the maximum dynamic stack memory size that memaid can maintain. The unit is MB.

// This indicates the maximum dynamic stack memory m for initializing memaid (there is an algorithm based on the actual physical memory size and idle memory size)

Initcoreinstance (64 );

 

{

// The cmemaidstackscope object controls the collection of memory applied within its scope

// Multiple cmemaidstackscope objects can be created. Each object is only responsible for reclaiming the memory applied by it.

Cmemaidstackscope scope;

Cmemaidstackscope scope1;

Cmemaidstackscope scope2;

// Use one of the cmemaidstackscope objects to create a ctestmemaid object

// The scope of the object pointed to by Ptest is the same as that of scope.

Ctestmemaid * Ptest = gnew (scope. alloc <ctestmemaid> () ctestmemaid;

 

For (INT I = 0; I <1024; ++ I)

{

// Use one of the cmemaidstackscope objects scope3 to create a ctestmemaid object

Cmemaidstackscopescope3;

Ctestmemaid * Ptest = gnew (scope3.alloc <ctestmemaid> () ctestmemaid;

 

// Use one of the cmemaidstackscope objects scope4 to create data

Cmemaidstackscope scope4;

Pbyte pdata = scope4.alloc <byte> (256 );

}

 

Pbyte pdata = scope2.alloc <byte> (1024*1024 );

 

Cmemaidstackscope scope5;

Cmemaidstackscope scope6;

// Scope1, scope5, and scope6 do not apply for any memory,

// But they do not affect the maintenance of memory managed by other cmemaidstackscope objects

}

 

{

Pbyte pdata0 [memaid_len_max] = {0 };
Pbyte pdata1 [memaid_len_max] = {0 };
Pbyte pdata2 [memaid_len_max] = {0 };
Pbyte pdata3 [memaid_len_max] = {0 };
Pbyte pdata4 [memaid_len_max] = {0 };
Pbyte pdata5 [memaid_len_max] = {0 };
Pbyte pdata6 [memaid_len_max] = {0 };
Pbyte pdata7 [memaid_len_max] = {0 };

// Let memaid provide a fixed cache size, and the frequency of each increase is (each internal application unit is 4096*30)

// The above cache size memaid is aligned by the system page memory size by default

// The total size of fixed cache blocks that can cache up to 2 GB,

// You can still apply for a fixed cache larger than 2 GB, but it will not be cached if it is released.

// Others are similar

Ulong ulindex0 = gmemaidinst-> cacheinit (4096, 30 );
Ulong ulindex1 = gmemaidinst-> cacheinit (4096, 30 );
Ulong ulindex2 = gmemaidinst-> cacheinit (9000, 30 );
Ulong ulindex3 = gmemaidinst-> cacheinit (5000, 30 );
Ulong ulindex4 = gmemaidinst-> cacheinit (1, 30 );
Ulong ulindex5 = gmemaidinst-> cacheinit (15, 30 );
Ulong ulindex6 = gmemaidinst-> cacheinit (179, 30 );
Ulong ulindex7 = gmemaidinst-> cacheinit (300, 30 );

For (INT I = 0; I <memaid_len_max; ++ I)
{
Pdata0 [I] = gmemaidinst-> cachealloc (ulindex0, (RAND () % 4096); // If the cache request exceeds a fixed size, null is returned.
Pdata1 [I] = gmemaidinst-> cachealloc (ulindex1, (RAND () % 4096); // If the cache request exceeds a fixed size, null is returned.
Pdata2 [I] = gmemaidinst-> cachealloc (ulindex2, (RAND () % 10000); // If the cache request exceeds a fixed size, null is returned.
Pdata3 [I] = gmemaidinst-> cachealloc (ulindex3, (RAND () % 8000); // If the cache request exceeds a fixed size, null is returned.
Pdata4 [I] = gmemaidinst-> cachealloc (ulindex4, (RAND () % 200); // If the cache request exceeds a fixed size, null is returned.
Pdata5 [I] = gmemaidinst-> cachealloc (ulindex5, (RAND () % 200); // If the cache request exceeds a fixed size, null is returned.
Pdata6 [I] = gmemaidinst-> cachealloc (ulindex6, (RAND () % 400); // If the cache request exceeds a fixed size, null is returned.
Pdata7 [I] = gmemaidinst-> cachealloc (ulindex7, (RAND () % 600); // If the cache request exceeds a fixed size, null is returned.
}

For (INT I = 0; I <memaid_len_max; ++ I)
{
Int J = rand () % memaid_len_max;
If (pdata0 [J]! = NULL)
{
Gmemaidinst-> cachefree (ulindex0, pdata0 [J]);
}
If (pdata1 [J]! = NULL)
{
Gmemaidinst-> cachefree (ulindex1, pdata1 [J]);
}
If (pdata2 [J]! = NULL)
{
Gmemaidinst-> cachefree (ulindex2, pdata2 [J]);
}
If (pdata3 [J]! = NULL)
{
Gmemaidinst-> cachefree (ulindex3, pdata3 [J]);
}
If (pdata4 [J]! = NULL)
{
Gmemaidinst-> cachefree (ulindex4, pdata4 [J]);
}
If (pdata5 [J]! = NULL)
{
Gmemaidinst-> cachefree (ulindex5, pdata5 [J]);
}
If (pdata6 [J]! = NULL)
{
Gmemaidinst-> cachefree (ulindex6, pdata6 [J]);
}
If (pdata3 [J]! = NULL)
{
Gmemaidinst-> cachefree (ulindex7, pdata7 [J]);
}
}
Gmemaidinst-> cacheexit (ulindex0 );
Gmemaidinst-> cacheexit (ulindex1 );
Gmemaidinst-> cacheexit (ulindex2 );
Gmemaidinst-> cacheexit (ulindex3 );
Gmemaidinst-> cacheexit (ulindex4 );
Gmemaidinst-> cacheexit (ulindex5 );
Gmemaidinst-> cacheexit (ulindex6 );
Gmemaidinst-> cacheexit (ulindex7 );
For (INT I = 0; I <memaid_len_max; ++ I)
{
If (pdata0 [I]! = NULL)
{
Gmemaidinst-> cachefree (ulindex0, pdata0 [I]);
}
If (pdata1 [I]! = NULL)
{
Gmemaidinst-> cachefree (ulindex1, pdata1 [I]);
}
If (pdata2 [I]! = NULL)
{
Gmemaidinst-> cachefree (ulindex2, pdata2 [I]);
}
If (pdata3 [I]! = NULL)
{
Gmemaidinst-> cachefree (ulindex3, pdata3 [I]);
}
If (pdata4 [I]! = NULL)
{
Gmemaidinst-> cachefree (ulindex4, pdata4 [I]);
}
If (pdata5 [I]! = NULL)
{
Gmemaidinst-> cachefree (ulindex5, pdata5 [I]);
}
If (pdata6 [I]! = NULL)
{
Gmemaidinst-> cachefree (ulindex6, pdata6 [I]);
}
If (pdata7 [I]! = NULL)
{
Gmemaidinst-> cachefree (ulindex7, pdata7 [I]);
}
}
Gmemaidinst-> cacheexit (ulindex0 );
Gmemaidinst-> cacheexit (ulindex1 );
Gmemaidinst-> cacheexit (ulindex2 );
Gmemaidinst-> cacheexit (ulindex3 );
Gmemaidinst-> cacheexit (ulindex4 );
Gmemaidinst-> cacheexit (ulindex5 );
Gmemaidinst-> cacheexit (ulindex6 );
Gmemaidinst-> cacheexit (ulindex7 );

Ulong ulindex8 = gmemaidinst-> cacheinit (6000, 32 );
Dev_debug (Tf ("new cache index % d"), ulindex8 );
Ulong ulindex9 = gmemaidinst-> cacheinit (12000, 32 );
Dev_debug (Tf ("new cache index % d"), ulindex9 );
Ulong ulindex10 = gmemaidinst-> cacheinit (2000, 32 );
Dev_debug (Tf ("new cache index % d"), ulindex10 );
Ulong ulindex11 = gmemaidinst-> cacheinit (9000, 32 );
Dev_debug (Tf ("new cache index % d"), ulindex11 );
Ulong ulindex12 = gmemaidinst-> cacheinit (97, 32 );
Dev_debug (Tf ("new cache index % d"), ulindex12 );
Ulong ulindex13 = gmemaidinst-> cacheinit (200, 32 );
Dev_debug (Tf ("new cache index % d"), ulindex13 );
Ulong ulindex14 = gmemaidinst-> cacheinit (400, 32 );
Dev_debug (Tf ("new cache index % d"), ulindex14 );
Ulong ulindex15 = gmemaidinst-> cacheinit (800, 32 );
Dev_debug (Tf ("new cache index % d"), ulindex15 );

}

// Call exitcoreinstance of Vol: core to release non-core single-piece objects of Vol: core.

Exitcoreinstance ();

 

Return0;

}

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.