2017-12-03 23:32:51 +01:00
< html > < head > < meta http-equiv = "X-UA-Compatible" content = "IE=edge" / > < link rel = "shortcut icon" href = "../icons/favicon.ico" / > < link rel = "stylesheet" type = "text/css" href = "../styles/branding.css" / > < link rel = "stylesheet" type = "text/css" href = "../styles/branding-en-US.css" / > < script type = "text/javascript" src = "../scripts/branding.js" > < / script > < meta http-equiv = "Content-Type" content = "text/html; charset=UTF-8" / > < title > gmp_lib.mp_set_memory_functions Method < / title > < meta name = "Language" content = "en-us" / > < meta name = "System.Keywords" content = "mp_set_memory_functions method" / > < meta name = "System.Keywords" content = "gmp_lib.mp_set_memory_functions method" / > < meta name = "Microsoft.Help.F1" content = "Math.Gmp.Native.gmp_lib.mp_set_memory_functions" / > < meta name = "Microsoft.Help.Id" content = "M:Math.Gmp.Native.gmp_lib.mp_set_memory_functions(Math.Gmp.Native.allocate_function,Math.Gmp.Native.reallocate_function,Math.Gmp.Native.free_function)" / > < meta name = "Description" content = "Replace the current allocation functions from the arguments." / > < meta name = "Microsoft.Help.ContentType" content = "Reference" / > < meta name = "BrandingAware" content = "true" / > < meta name = "container" content = "Math.Gmp.Native" / > < meta name = "file" content = "15ac48e3-750c-d154-7d8f-6dcaa137f5db" / > < meta name = "guid" content = "15ac48e3-750c-d154-7d8f-6dcaa137f5db" / > < link type = "text/css" rel = "stylesheet" href = "../styles/highlight.css" / > < script type = "text/javascript" src = "../scripts/highlight.js" > < / script > < link rel = "stylesheet" type = "text/css" href = "../styles/branding-Website.css" / > < script type = "text/javascript" src = "../scripts/jquery-1.11.0.min.js" > < / script > < script type = "text/javascript" src = "../scripts/branding-Website.js" > < / script > < script type = "text/javascript" src = "../scripts/clipboard.min.js" > < / script > < / head > < body onload = "OnLoad('cs')" > < input type = "hidden" id = "userDataCache" class = "userDataStyle" / > < div class = "pageHeader" id = "PageHeader" > GMP Native Interface for .NET< form id = "SearchForm" method = "get" action = "#" onsubmit = "javascript:TransferToSearchPage(); return false;" > < input id = "SearchTextBox" type = "text" maxlength = "200" / > < button id = "SearchButton" type = "submit" > < / button > < / form > < / div > < div class = "pageBody" > < div class = "leftNav" id = "leftNav" > < div id = "tocNav" > < div class = "toclevel0" data-toclevel = "0" > < a class = "tocCollapsed" onclick = "javascript: Toggle(this);" href = "#!" / > < a data-tochassubtree = "true" href = "846f5c8a-6cba-433e-9f18-cde2ff5695cd.htm" title = "GMP Native Interface for .NET" tocid = "roottoc" > GMP Native Interface for .NET< / a > < / div > < div class = "toclevel0" data-toclevel = "0" > < a class = "tocCollapsed" onclick = "javascript: Toggle(this);" href = "#!" / > < a data-tochassubtree = "true" href = "d9c51b9f-0c54-770b-28ea-6eebd8fef368.htm" title = "Math.Gmp.Native" tocid = "d9c51b9f-0c54-770b-28ea-6eebd8fef368" > Math.Gmp.Native< / a > < / div > < div class = "toclevel0" data-toclevel = "0" > < a class = "tocCollapsed" onclick = "javascript: Toggle(this);" href = "#!" / > < a data-tochassubtree = "true" href = "3af6b34c-3242-2b75-e7c7-ab79af8a4b0f.htm" title = "gmp_lib Class" tocid = "3af6b34c-3242-2b75-e7c7-ab79af8a4b0f" > gmp_lib Class< / a > < / div > < div class = "toclevel1" data-toclevel = "1" data-childrenloaded = "true" > < a class = "tocExpanded" onclick = "javascript: Toggle(this);" href = "#!" / > < a data-tochassubtree = "true" href = "0fa7cbf3-e8f4-6b14-d829-8aa663e77c74.htm" title = "gmp_lib Methods" tocid = "0fa7cbf3-e8f4-6b14-d829-8aa663e77c74" > gmp_lib Methods< / a > < / div > < div class = "toclevel2" data-toclevel = "2" > < a data-tochassubtree = "false" href = "c9cb6a67-59b8-5c03-f31d-f252441c53f6.htm" title = "_mpz_realloc Method " tocid = "c9cb6a67-59b8-5c03-f31d-f252441c53f6" > _mpz_realloc Method < / a > < / div > < div class = "toclevel2" data-toclevel = "2" > < a data-tochassubtree = "false" href = "2c68671c-cd5c-e2ec-52a2-65b0ca31c3d1.htm" title = "allocate Method " tocid = "2c68671c-cd5c-e2ec-52a2-65b0ca31c3d1" > allocate Method < / a > < / div > < div class = "toclevel2" data-toclevel = "2" > < a class = "tocCollapsed" onclick = "javascript: Toggle(this);" href = "#!" / > < a data-tochassubtree = "true" href = "92160da3-b1af-e005-cb05-e3fb4dd524d8.htm" title = "free Method " tocid = "92160da3-b1af-e005-cb05-e3fb4dd52
2017-11-06 13:38:19 +01:00
Replace the current allocation functions from the arguments.
< / div > < p > < / p >
< strong > Namespace:< / strong >
< a href = "d9c51b9f-0c54-770b-28ea-6eebd8fef368.htm" > Math.Gmp.Native< / a > < br / >
< strong > Assembly:< / strong >
Math.Gmp.Native (in Math.Gmp.Native.dll) Version: 1.0.0.0 (1.0.0.0)< div class = "collapsibleAreaRegion" > < span class = "collapsibleRegionTitle" onclick = "SectionExpandCollapse('ID1RB')" onkeypress = "SectionExpandCollapse_CheckKey('ID1RB', event)" tabindex = "0" > < img id = "ID1RBToggle" class = "collapseToggle" src = "../icons/SectionExpanded.png" / > Syntax< / span > < / div > < div id = "ID1RBSection" class = "collapsibleSection" > < div class = "codeSnippetContainer" > < div class = "codeSnippetContainerTabs" > < div id = "ID0EDCA_tab1" class = "codeSnippetContainerTab" > < a href = "#" onclick = "javascript:ChangeTab('ID0EDCA','cs','1','4');return false;" > C#< / a > < / div > < div id = "ID0EDCA_tab2" class = "codeSnippetContainerTab" > < a href = "#" onclick = "javascript:ChangeTab('ID0EDCA','vb','2','4');return false;" > VB< / a > < / div > < div id = "ID0EDCA_tab3" class = "codeSnippetContainerTab" > < a href = "#" onclick = "javascript:ChangeTab('ID0EDCA','cpp','3','4');return false;" > C++< / a > < / div > < div id = "ID0EDCA_tab4" class = "codeSnippetContainerTab" > < a href = "#" onclick = "javascript:ChangeTab('ID0EDCA','fs','4','4');return false;" > F#< / a > < / div > < / div > < div class = "codeSnippetContainerCodeContainer" > < div class = "codeSnippetToolBar" > < div class = "codeSnippetToolBarText" > < a id = "ID0EDCA_copyCode" href = "#" class = "copyCodeSnippet" onclick = "javascript:CopyToClipboard('ID0EDCA');return false;" title = "Copy" > Copy< / a > < / div > < / div > < div id = "ID0EDCA_code_Div1" class = "codeSnippetContainerCode" style = "display: block" > < pre xml:space = "preserve" > < span class = "keyword" > public< / span > < span class = "keyword" > static< / span > < span class = "keyword" > void< / span > < span class = "identifier" > mp_set_memory_functions< / span > (
< span class = "identifier" > allocate_function< / span > < span class = "parameter" > alloc_func_ptr< / span > ,
< span class = "identifier" > reallocate_function< / span > < span class = "parameter" > realloc_func_ptr< / span > ,
< span class = "identifier" > free_function< / span > < span class = "parameter" > free_func_ptr< / span >
)< / pre > < / div > < div id = "ID0EDCA_code_Div2" class = "codeSnippetContainerCode" style = "display: none" > < pre xml:space = "preserve" > < span class = "keyword" > Public< / span > < span class = "keyword" > Shared< / span > < span class = "keyword" > Sub< / span > < span class = "identifier" > mp_set_memory_functions< / span > (
< span class = "parameter" > alloc_func_ptr< / span > < span class = "keyword" > As< / span > < span class = "identifier" > allocate_function< / span > ,
< span class = "parameter" > realloc_func_ptr< / span > < span class = "keyword" > As< / span > < span class = "identifier" > reallocate_function< / span > ,
< span class = "parameter" > free_func_ptr< / span > < span class = "keyword" > As< / span > < span class = "identifier" > free_function< / span >
)< / pre > < / div > < div id = "ID0EDCA_code_Div3" class = "codeSnippetContainerCode" style = "display: none" > < pre xml:space = "preserve" > < span class = "keyword" > public< / span > :
< span class = "keyword" > static< / span > < span class = "keyword" > void< / span > < span class = "identifier" > mp_set_memory_functions< / span > (
< span class = "identifier" > allocate_function< / span > ^ < span class = "parameter" > alloc_func_ptr< / span > ,
< span class = "identifier" > reallocate_function< / span > ^ < span class = "parameter" > realloc_func_ptr< / span > ,
< span class = "identifier" > free_function< / span > ^ < span class = "parameter" > free_func_ptr< / span >
)< / pre > < / div > < div id = "ID0EDCA_code_Div4" class = "codeSnippetContainerCode" style = "display: none" > < pre xml:space = "preserve" > < span class = "keyword" > static< / span > < span class = "keyword" > member< / span > < span class = "identifier" > mp_set_memory_functions< / span > :
< span class = "parameter" > alloc_func_ptr< / span > : < span class = "identifier" > allocate_function< / span > *
< span class = "parameter" > realloc_func_ptr< / span > : < span class = "identifier" > reallocate_function< / span > *
< span class = "parameter" > free_func_ptr< / span > : < span class = "identifier" > free_function< / span > < span class = "keyword" > -> < / span > < span class = "keyword" > unit< / span >
< / pre > < / div > < / div > < / div > < script type = "text/javascript" > AddLanguageTabSet ( "ID0EDCA" ) ; < / script > < h4 class = "subHeading" > Parameters< / h4 > < dl > < dt > < span class = "parameter" > alloc_func_ptr< / span > < / dt > < dd > Type: < a href = "dfe3609d-cc50-e357-4254-e3a8e2847f07.htm" > Math.Gmp.Native< span id = "LST2CF63884_1" > < / span > < script type = "text/javascript" > AddLanguageSpecificTextSet ( "LST2CF63884_1?cs=.|vb=.|cpp=::|nu=.|fs=." ) ; < / script > allocate_function< / a > < br / > The new memory allocation function.< / dd > < dt > < span class = "parameter" > realloc_func_ptr< / span > < / dt > < dd > Type: < a href = "aa9d21b4-a929-d550-5a65-f9cbcfa7f638.htm" > Math.Gmp.Native< span id = "LST2CF63884_2" > < / span > < script type = "text/javascript" > AddLanguageSpecificTextSet ( "LST2CF63884_2?cs=.|vb=.|cpp=::|nu=.|fs=." ) ; < / script > reallocate_function< / a > < br / > The new memory reallocation function.< / dd > < dt > < span class = "parameter" > free_func_ptr< / span > < / dt > < dd > Type: < a href = "2c9876e2-a0f5-14c6-5069-1a9d50b89185.htm" > Math.Gmp.Native< span id = "LST2CF63884_3" > < / span > < script type = "text/javascript" > AddLanguageSpecificTextSet ( "LST2CF63884_3?cs=.|vb=.|cpp=::|nu=.|fs=." ) ; < / script > free_function< / a > < br / > The new memory de-allocation function.< / dd > < / dl > < / div > < div class = "collapsibleAreaRegion" > < span class = "collapsibleRegionTitle" onclick = "SectionExpandCollapse('ID2RB')" onkeypress = "SectionExpandCollapse_CheckKey('ID2RB', event)" tabindex = "0" > < img id = "ID2RBToggle" class = "collapseToggle" src = "../icons/SectionExpanded.png" / > Remarks< / span > < / div > < div id = "ID2RBSection" class = "collapsibleSection" > < p >
If an argument is < span class = "code" > null< / span > (< span class = "code" > Nothing< / span > in VB.NET), the corresponding
default function is used.
< / p > < / div > < div class = "collapsibleAreaRegion" > < span class = "collapsibleRegionTitle" onclick = "SectionExpandCollapse('ID3RB')" onkeypress = "SectionExpandCollapse_CheckKey('ID3RB', event)" tabindex = "0" > < img id = "ID3RBToggle" class = "collapseToggle" src = "../icons/SectionExpanded.png" / > Examples< / span > < / div > < div id = "ID3RBSection" class = "collapsibleSection" > < div class = "codeSnippetContainer" > < div class = "codeSnippetContainerTabs" > < div id = "ID0EDAABA_tab1" class = "codeSnippetContainerTab" > < a href = "#" onclick = "javascript:ChangeTab('ID0EDAABA','cs','1','4');return false;" > C#< / a > < / div > < div id = "ID0EDAABA_tab2" class = "codeSnippetContainerTab" > < a href = "#" onclick = "javascript:ChangeTab('ID0EDAABA','vb','2','4');return false;" > VB< / a > < / div > < div id = "ID0EDAABA_tab3" class = "codeSnippetContainerTabPhantom" > < a href = "#" onclick = "javascript:ChangeTab('ID0EDAABA','cpp','3','4');return false;" > C++< / a > < / div > < div id = "ID0EDAABA_tab4" class = "codeSnippetContainerTabPhantom" > < a href = "#" onclick = "javascript:ChangeTab('ID0EDAABA','fs','4','4');return false;" > F#< / a > < / div > < / div > < div class = "codeSnippetContainerCodeContainer" > < div class = "codeSnippetToolBar" > < div class = "codeSnippetToolBarText" > < a id = "ID0EDAABA_copyCode" href = "#" class = "copyCodeSnippet" onclick = "javascript:CopyToClipboard('ID0EDAABA');return false;" title = "Copy" > Copy< / a > < / div > < / div > < div id = "ID0EDAABA_code_Div1" class = "codeSnippetContainerCode" style = "display: block" > < pre xml:space = "preserve" > < span class = "highlight-comment" > // Retrieve GMP default memory allocation functions.< / span >
allocate_function default_allocate = < span class = "highlight-keyword" > null< / span > ;
reallocate_function default_reallocate = < span class = "highlight-keyword" > null< / span > ;
free_function default_free = < span class = "highlight-keyword" > null< / span > ;
gmp_lib.mp_get_memory_functions(< span class = "highlight-keyword" > ref< / span > default_allocate, < span class = "highlight-keyword" > ref< / span > default_reallocate, < span class = "highlight-keyword" > ref< / span > default_free);
< span class = "highlight-comment" > // Create and set new memory allocation functions that count the number of times they are called.< / span >
< span class = "highlight-keyword" > int< / span > counter = < span class = "highlight-number" > 0< / span > ;
allocate_function new_allocate = (size_t alloc_size) => { counter++; < span class = "highlight-keyword" > return< / span > default_allocate(alloc_size); };
reallocate_function new_reallocate = (void_ptr ptr, size_t old_size, size_t new_size) => { counter++; < span class = "highlight-keyword" > return< / span > default_reallocate(ptr, old_size, new_size); };
free_function new_free = (void_ptr ptr, size_t size) => { counter++; default_free(ptr, size); };
gmp_lib.mp_set_memory_functions(new_allocate, new_reallocate, new_free);
< span class = "highlight-comment" > // Retrieve GMP memory allocation functions.< / span >
allocate_function allocate = < span class = "highlight-keyword" > null< / span > ;
reallocate_function reallocate = < span class = "highlight-keyword" > null< / span > ;
free_function free = < span class = "highlight-keyword" > null< / span > ;
gmp_lib.mp_get_memory_functions(< span class = "highlight-keyword" > ref< / span > allocate, < span class = "highlight-keyword" > ref< / span > reallocate, < span class = "highlight-keyword" > ref< / span > free);
< span class = "highlight-comment" > // Call memory function and assert calls count.< / span >
void_ptr p = allocate(< span class = "highlight-number" > 10< / span > );
Assert.IsTrue(counter == < span class = "highlight-number" > 1< / span > );
reallocate(p, < span class = "highlight-number" > 10< / span > , < span class = "highlight-number" > 20< / span > );
Assert.IsTrue(counter == < span class = "highlight-number" > 2< / span > );
free(p, < span class = "highlight-number" > 20< / span > );
Assert.IsTrue(counter == < span class = "highlight-number" > 3< / span > );
< span class = "highlight-comment" > // Restore default memory allocation functions.< / span >
gmp_lib.mp_set_memory_functions(< span class = "highlight-keyword" > null< / span > , < span class = "highlight-keyword" > null< / span > , < span class = "highlight-keyword" > null< / span > );< / pre > < / div > < div id = "ID0EDAABA_code_Div2" class = "codeSnippetContainerCode" style = "display: none" > < pre xml:space = "preserve" > < span class = "highlight-comment" > ' Retrieve GMP default memory allocation functions.< / span >
< span class = "highlight-keyword" > Dim< / span > default_allocate < span class = "highlight-keyword" > As< / span > allocate_function = < span class = "highlight-keyword" > Nothing< / span >
< span class = "highlight-keyword" > Dim< / span > default_reallocate < span class = "highlight-keyword" > As< / span > reallocate_function = < span class = "highlight-keyword" > Nothing< / span >
< span class = "highlight-keyword" > Dim< / span > default_free < span class = "highlight-keyword" > As< / span > free_function = < span class = "highlight-keyword" > Nothing< / span >
gmp_lib.gmp_get_memory_functions(default_allocate, default_reallocate, default_free)
< span class = "highlight-comment" > ' Create and set new memory allocation functions that count the number of times they are called.< / span >
< span class = "highlight-keyword" > Dim< / span > counter < span class = "highlight-keyword" > As< / span > < span class = "highlight-keyword" > Integer< / span > = < span class = "highlight-number" > 0< / span >
< span class = "highlight-keyword" > Dim< / span > new_allocate < span class = "highlight-keyword" > As< / span > allocate_function =
< span class = "highlight-keyword" > Function< / span > (alloc_size < span class = "highlight-keyword" > As< / span > size_t)
counter += < span class = "highlight-number" > 1< / span >
< span class = "highlight-keyword" > Return< / span > default_allocate(alloc_size)
< span class = "highlight-keyword" > End< / span > < span class = "highlight-keyword" > Function< / span >
< span class = "highlight-keyword" > Dim< / span > new_reallocate < span class = "highlight-keyword" > As< / span > reallocate_function =
< span class = "highlight-keyword" > Function< / span > (ptr < span class = "highlight-keyword" > As< / span > void_ptr, old_size < span class = "highlight-keyword" > As< / span > size_t, new_size < span class = "highlight-keyword" > As< / span > size_t)
counter += < span class = "highlight-number" > 1< / span >
< span class = "highlight-keyword" > Return< / span > default_reallocate(ptr, old_size, new_size)
< span class = "highlight-keyword" > End< / span > < span class = "highlight-keyword" > Function< / span >
< span class = "highlight-keyword" > Dim< / span > new_free < span class = "highlight-keyword" > As< / span > free_function =
< span class = "highlight-keyword" > Function< / span > (ptr < span class = "highlight-keyword" > As< / span > void_ptr, size < span class = "highlight-keyword" > As< / span > size_t)
counter += < span class = "highlight-number" > 1< / span >
default_free(ptr, size)
< span class = "highlight-keyword" > End< / span > < span class = "highlight-keyword" > Function< / span >
gmp_lib.gmp_set_memory_functions(new_allocate, new_reallocate, new_free)
< span class = "highlight-comment" > ' Retrieve GMP memory allocation functions.< / span >
< span class = "highlight-keyword" > Dim< / span > allocate < span class = "highlight-keyword" > As< / span > allocate_function = < span class = "highlight-keyword" > Nothing< / span >
< span class = "highlight-keyword" > Dim< / span > reallocate < span class = "highlight-keyword" > As< / span > reallocate_function = < span class = "highlight-keyword" > Nothing< / span >
< span class = "highlight-keyword" > Dim< / span > free < span class = "highlight-keyword" > As< / span > free_function = < span class = "highlight-keyword" > Nothing< / span >
gmp_lib.gmp_get_memory_functions(allocate, reallocate, free)
< span class = "highlight-comment" > ' Call memory function and assert calls count.< / span >
< span class = "highlight-keyword" > Dim< / span > p < span class = "highlight-keyword" > As< / span > void_ptr = allocate(< span class = "highlight-number" > 10< / span > )
Assert.IsTrue(counter = < span class = "highlight-number" > 1< / span > )
reallocate(p, < span class = "highlight-number" > 10< / span > , < span class = "highlight-number" > 20< / span > )
Assert.IsTrue(counter = < span class = "highlight-number" > 2< / span > )
free(p, < span class = "highlight-number" > 20< / span > )
Assert.IsTrue(counter = < span class = "highlight-number" > 3< / span > )
< span class = "highlight-comment" > ' Restore default memory allocation functions.< / span >
2017-12-15 02:18:55 +01:00
gmp_lib.gmp_set_memory_functions(< span class = "highlight-keyword" > Nothing< / span > , < span class = "highlight-keyword" > Nothing< / span > , < span class = "highlight-keyword" > Nothing< / span > )< / pre > < / div > < div id = "ID0EDAABA_code_Div3" class = "codeSnippetContainerCode" style = "display: none" > < p > No code example is currently available or this language may not be supported.< / p > < / div > < div id = "ID0EDAABA_code_Div4" class = "codeSnippetContainerCode" style = "display: none" > < p > No code example is currently available or this language may not be supported.< / p > < / div > < / div > < / div > < script type = "text/javascript" > AddLanguageTabSet ( "ID0EDAABA" ) ; < / script > < / div > < div class = "collapsibleAreaRegion" id = "seeAlsoSection" > < span class = "collapsibleRegionTitle" onclick = "SectionExpandCollapse('ID4RB')" onkeypress = "SectionExpandCollapse_CheckKey('ID4RB', event)" tabindex = "0" > < img id = "ID4RBToggle" class = "collapseToggle" src = "../icons/SectionExpanded.png" / > See Also< / span > < / div > < div id = "ID4RBSection" class = "collapsibleSection" > < h4 class = "subHeading" > Reference< / h4 > < div class = "seeAlsoStyle" > < a href = "3af6b34c-3242-2b75-e7c7-ab79af8a4b0f.htm" > gmp_lib Class< / a > < / div > < div class = "seeAlsoStyle" > < a href = "d9c51b9f-0c54-770b-28ea-6eebd8fef368.htm" > Math.Gmp.Native Namespace< / a > < / div > < div class = "seeAlsoStyle" > < a href = "9a229bed-b8b6-e0ad-f153-689455252ec4.htm" > mp_get_memory_functions< / a > < / div > < div class = "seeAlsoStyle" > < a href = "3af6b34c-3242-2b75-e7c7-ab79af8a4b0f.htm" > < a href = "/html/3af6b34c-3242-2b75-e7c7-ab79af8a4b0f.htm#Custom_Allocation" > Custom Allocation< / a > < / a > < / div > < div class = "seeAlsoStyle" > < a href = "3af6b34c-3242-2b75-e7c7-ab79af8a4b0f.htm" > < a href = "https://gmplib.org/manual/Custom-Allocation.html#Custom-Allocation" > GNU MP - Custom Allocation< / a > < / a > < / div > < / div > < / div > < / div > < div id = "pageFooter" class = "pageFooter" > < / div > < / body > < / html >