mirror of
https://github.com/pgbackrest/pgbackrest.git
synced 2024-12-14 10:13:05 +02:00
Add comments on String and Variant usage regarding memory contexts.
Contributed by Cynthia Shang.
This commit is contained in:
parent
11181e69b8
commit
a72759eb5b
@ -1,5 +1,19 @@
|
||||
/***********************************************************************************************************************************
|
||||
String Handler
|
||||
|
||||
Strings are lightweight objects in that they do not have their own memory context, instead they exist in the current context in
|
||||
which they are instantiated. If a string is needed outside the current memory context, the memory context must be switched to the
|
||||
old context and then back. Below is a simplified example:
|
||||
|
||||
String *result = NULL; <--- is created in the current memory context (referred to as "old context" below)
|
||||
MEM_CONTEXT_TEMP_BEGIN() <--- begins a new temporary context
|
||||
{
|
||||
String *resultStr = strNewN("myNewStr"); <--- creates a string in the temporary memory context
|
||||
memContextSwitch(MEM_CONTEXT_OLD()); <--- switch to the old context so the duplication of the string is in that context
|
||||
result = strDup(resultStr); <--- recreates a copy of the string in the old context where "result" was created.
|
||||
memContextSwitch(MEM_CONTEXT_TEMP()); <--- switch back to the temporary context
|
||||
}
|
||||
MEM_CONTEXT_TEMP_END(); <-- frees everything created inside this temporary memory context - i.e resultStr
|
||||
***********************************************************************************************************************************/
|
||||
#ifndef COMMON_TYPE_STRING_H
|
||||
#define COMMON_TYPE_STRING_H
|
||||
|
@ -1,5 +1,19 @@
|
||||
/***********************************************************************************************************************************
|
||||
Variant Data Type
|
||||
|
||||
Variants are lightweight objects in that they do not have their own memory context, instead they exist in the current context in
|
||||
which they are instantiated. If a variant is needed outside the current memory context, the memory context must be switched to the
|
||||
old context and then back. Below is a simplified example:
|
||||
|
||||
Variant *result = NULL; <--- is created in the current memory context (referred to as "old context" below)
|
||||
MEM_CONTEXT_TEMP_BEGIN() <--- begins a new temporary context
|
||||
{
|
||||
String *resultStr = strNewN("myNewStr"); <--- creates a string in the temporary memory context
|
||||
memContextSwitch(MEM_CONTEXT_OLD()); <--- switch to old context so creation of the variant from the string is in old context
|
||||
result = varNewUInt64(cvtZToUInt64(strPtr(resultStr))); <--- recreates variant from the string in the old context.
|
||||
memContextSwitch(MEM_CONTEXT_TEMP()); <--- switch back to the temporary context
|
||||
}
|
||||
MEM_CONTEXT_TEMP_END(); <-- frees everything created inside this temporary memory context - i.e resultStr
|
||||
***********************************************************************************************************************************/
|
||||
#ifndef COMMON_TYPE_VARIANT_H
|
||||
#define COMMON_TYPE_VARIANT_H
|
||||
|
Loading…
Reference in New Issue
Block a user