PostgreSQL Source Code  git master
fe_memutils.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Macros

#define MCXT_ALLOC_HUGE
 
#define MCXT_ALLOC_NO_OOM   0x02 /* no failure if out-of-memory */
 
#define MCXT_ALLOC_ZERO   0x04 /* zero allocated memory */
 

Functions

char * pg_strdup (const char *in)
 
void * pg_malloc (size_t size)
 
void * pg_malloc0 (size_t size)
 
void * pg_malloc_extended (size_t size, int flags)
 
void * pg_realloc (void *pointer, size_t size)
 
void pg_free (void *pointer)
 
char * pstrdup (const char *in)
 
void * palloc (Size size)
 
void * palloc0 (Size size)
 
void * palloc_extended (Size size, int flags)
 
void * repalloc (void *pointer, Size size)
 
void pfree (void *pointer)
 
char * psprintf (const char *fmt,...) pg_attribute_printf(1
 
char size_t pvsnprintf (char *buf, size_t len, const char *fmt, va_list args) pg_attribute_printf(3
 

Macro Definition Documentation

◆ MCXT_ALLOC_HUGE

#define MCXT_ALLOC_HUGE
Value:
0x01 /* allow huge allocation (> 1 GB) not
* actually used for frontends */

Definition at line 16 of file fe_memutils.h.

Referenced by MemoryContextAllocExtended(), pagetable_allocate(), and palloc_extended().

◆ MCXT_ALLOC_NO_OOM

#define MCXT_ALLOC_NO_OOM   0x02 /* no failure if out-of-memory */

◆ MCXT_ALLOC_ZERO

#define MCXT_ALLOC_ZERO   0x04 /* zero allocated memory */

Function Documentation

◆ palloc()

void* palloc ( Size  size)

Definition at line 924 of file mcxt.c.

925 {
926  /* duplicates MemoryContextAlloc to avoid increased overhead */
927  void *ret;
929 
932 
933  if (!AllocSizeIsValid(size))
934  elog(ERROR, "invalid memory alloc request size %zu", size);
935 
936  context->isReset = false;
937 
938  ret = context->methods->alloc(context, size);
939  if (unlikely(ret == NULL))
940  {
942  ereport(ERROR,
943  (errcode(ERRCODE_OUT_OF_MEMORY),
944  errmsg("out of memory"),
945  errdetail("Failed on request of size %zu in memory context \"%s\".",
946  size, context->name)));
947  }
948 
949  VALGRIND_MEMPOOL_ALLOC(context, ret, size);
950 
951  return ret;
952 }
const MemoryContextMethods * methods
Definition: memnodes.h:82
int errcode(int sqlerrcode)
Definition: elog.c:575
#define AssertNotInCriticalSection(context)
Definition: mcxt.c:67
#define ERROR
Definition: elog.h:43
void MemoryContextStats(MemoryContext context)
Definition: mcxt.c:474
int errdetail(const char *fmt,...)
Definition: elog.c:873
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
void *(* alloc)(MemoryContext context, Size size)
Definition: memnodes.h:59
#define ereport(elevel, rest)
Definition: elog.h:122
#define AssertArg(condition)
Definition: c.h:701
MemoryContext TopMemoryContext
Definition: mcxt.c:44
#define AllocSizeIsValid(size)
Definition: memutils.h:42
#define MemoryContextIsValid(context)
Definition: memnodes.h:101
#define VALGRIND_MEMPOOL_ALLOC(context, addr, size)
Definition: memdebug.h:29
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define unlikely(x)
Definition: c.h:208
#define elog
Definition: elog.h:219
const char * name
Definition: memnodes.h:87

◆ palloc0()

void* palloc0 ( Size  size)

Definition at line 955 of file mcxt.c.

956 {
957  /* duplicates MemoryContextAllocZero to avoid increased overhead */
958  void *ret;
960 
963 
964  if (!AllocSizeIsValid(size))
965  elog(ERROR, "invalid memory alloc request size %zu", size);
966 
967  context->isReset = false;
968 
969  ret = context->methods->alloc(context, size);
970  if (unlikely(ret == NULL))
971  {
973  ereport(ERROR,
974  (errcode(ERRCODE_OUT_OF_MEMORY),
975  errmsg("out of memory"),
976  errdetail("Failed on request of size %zu in memory context \"%s\".",
977  size, context->name)));
978  }
979 
980  VALGRIND_MEMPOOL_ALLOC(context, ret, size);
981 
982  MemSetAligned(ret, 0, size);
983 
984  return ret;
985 }
#define MemSetAligned(start, val, len)
Definition: c.h:941
const MemoryContextMethods * methods
Definition: memnodes.h:82
int errcode(int sqlerrcode)
Definition: elog.c:575
#define AssertNotInCriticalSection(context)
Definition: mcxt.c:67
#define ERROR
Definition: elog.h:43
void MemoryContextStats(MemoryContext context)
Definition: mcxt.c:474
int errdetail(const char *fmt,...)
Definition: elog.c:873
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
void *(* alloc)(MemoryContext context, Size size)
Definition: memnodes.h:59
#define ereport(elevel, rest)
Definition: elog.h:122
#define AssertArg(condition)
Definition: c.h:701
MemoryContext TopMemoryContext
Definition: mcxt.c:44
#define AllocSizeIsValid(size)
Definition: memutils.h:42
#define MemoryContextIsValid(context)
Definition: memnodes.h:101
#define VALGRIND_MEMPOOL_ALLOC(context, addr, size)
Definition: memdebug.h:29
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define unlikely(x)
Definition: c.h:208
#define elog
Definition: elog.h:219
const char * name
Definition: memnodes.h:87

◆ palloc_extended()

void* palloc_extended ( Size  size,
int  flags 
)

Definition at line 988 of file mcxt.c.

989 {
990  /* duplicates MemoryContextAllocExtended to avoid increased overhead */
991  void *ret;
993 
996 
997  if (((flags & MCXT_ALLOC_HUGE) != 0 && !AllocHugeSizeIsValid(size)) ||
998  ((flags & MCXT_ALLOC_HUGE) == 0 && !AllocSizeIsValid(size)))
999  elog(ERROR, "invalid memory alloc request size %zu", size);
1000 
1001  context->isReset = false;
1002 
1003  ret = context->methods->alloc(context, size);
1004  if (unlikely(ret == NULL))
1005  {
1006  if ((flags & MCXT_ALLOC_NO_OOM) == 0)
1007  {
1009  ereport(ERROR,
1010  (errcode(ERRCODE_OUT_OF_MEMORY),
1011  errmsg("out of memory"),
1012  errdetail("Failed on request of size %zu in memory context \"%s\".",
1013  size, context->name)));
1014  }
1015  return NULL;
1016  }
1017 
1018  VALGRIND_MEMPOOL_ALLOC(context, ret, size);
1019 
1020  if ((flags & MCXT_ALLOC_ZERO) != 0)
1021  MemSetAligned(ret, 0, size);
1022 
1023  return ret;
1024 }
#define MemSetAligned(start, val, len)
Definition: c.h:941
#define MCXT_ALLOC_HUGE
Definition: fe_memutils.h:16
const MemoryContextMethods * methods
Definition: memnodes.h:82
#define MCXT_ALLOC_NO_OOM
Definition: fe_memutils.h:18
int errcode(int sqlerrcode)
Definition: elog.c:575
#define AssertNotInCriticalSection(context)
Definition: mcxt.c:67
#define ERROR
Definition: elog.h:43
void MemoryContextStats(MemoryContext context)
Definition: mcxt.c:474
int errdetail(const char *fmt,...)
Definition: elog.c:873
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
void *(* alloc)(MemoryContext context, Size size)
Definition: memnodes.h:59
#define ereport(elevel, rest)
Definition: elog.h:122
#define AssertArg(condition)
Definition: c.h:701
MemoryContext TopMemoryContext
Definition: mcxt.c:44
#define AllocSizeIsValid(size)
Definition: memutils.h:42
#define MemoryContextIsValid(context)
Definition: memnodes.h:101
#define VALGRIND_MEMPOOL_ALLOC(context, addr, size)
Definition: memdebug.h:29
#define MCXT_ALLOC_ZERO
Definition: fe_memutils.h:19
#define AllocHugeSizeIsValid(size)
Definition: memutils.h:46
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define unlikely(x)
Definition: c.h:208
#define elog
Definition: elog.h:219
const char * name
Definition: memnodes.h:87

◆ pfree()

void pfree ( void *  pointer)

Definition at line 1031 of file mcxt.c.

1032 {
1033  MemoryContext context = GetMemoryChunkContext(pointer);
1034 
1035  context->methods->free_p(context, pointer);
1036  VALGRIND_MEMPOOL_FREE(context, pointer);
1037 }
const MemoryContextMethods * methods
Definition: memnodes.h:82
void(* free_p)(MemoryContext context, void *pointer)
Definition: memnodes.h:61
#define VALGRIND_MEMPOOL_FREE(context, addr)
Definition: memdebug.h:30
static MemoryContext GetMemoryChunkContext(void *pointer)
Definition: memutils.h:112

◆ pg_free()

◆ pg_malloc()

void* pg_malloc ( size_t  size)

Definition at line 47 of file fe_memutils.c.

References pg_malloc_internal().

Referenced by _Clone(), _CustomReadFunc(), _PrintFileData(), _skipData(), add_one_elt(), add_stringlist_item(), addObjectDependency(), ahprintf(), ArchiveEntry(), archprintf(), AssignDumpId(), avlInsertNode(), BuildArchiveDependencies(), buildIndexArray(), cfopen(), CloneArchive(), collectComments(), collectSecLabels(), conditional_stack_create(), conditional_stack_push(), copyFile(), createBoundaryObjects(), CreateVariableSpace(), CreateWalTarMethod(), datapagemap_iterate(), do_connect(), do_watch(), EndCompressor(), escape_quotes_bki(), exec_command_prompt(), ExecuteSqlCommandBuf(), filemap_create(), filter_lines_with_token(), find_other_exec_or_die(), findDependencyLoops(), findParentsByOid(), fix_dependencies(), flagInhAttrs(), format_numeric_locale(), gen_db_file_maps(), get_comma_elts(), get_db_infos(), get_loadable_libraries(), get_rel_infos(), get_sock_dir(), get_tablespace_paths(), getAccessMethods(), getAggregates(), getBlobs(), getCasts(), getCollations(), getConstraints(), getConversions(), getDefaultACLs(), getDomainConstraints(), getDumpableObjects(), getEventTriggers(), getExtendedStatistics(), getExtensionMembership(), getExtensions(), getForeignDataWrappers(), getForeignServers(), getFuncs(), getIndexes(), getInherits(), getNamespaces(), getOpclasses(), getOperators(), getOpfamilies(), getPolicies(), getProcLangs(), getPublications(), getPublicationTables(), getRules(), getSubscriptions(), getTableAttrs(), getTimelineHistory(), getTransforms(), getTriggers(), getTSConfigurations(), getTSDictionaries(), getTSParsers(), getTSTemplates(), getTypes(), identify_locking_dependencies(), InitArchiveFmt_Custom(), InitArchiveFmt_Directory(), InitArchiveFmt_Null(), InitArchiveFmt_Tar(), libpqGetFile(), load_resultmap(), lookupCreateVariable(), main(), makeTableDataInfo(), NewDumpOptions(), parallel_exec_prog(), parallel_transfer_all_new_dbs(), ParallelBackupStart(), ParseScript(), parseScriptWeight(), parseVariable(), pg_attribute_unused(), pretty_wal_size(), print_aligned_text(), print_aligned_vertical(), printCrosstab(), PrintResultsInCrosstab(), process_source_file(), process_sql_command(), process_target_file(), pset_quoted_string(), quote_identifier(), quote_if_needed(), rankSort(), read_file_contents(), ReadControlFile(), ReadDataFromArchiveNone(), readfile(), readMessageFromPipe(), ReadStr(), ReadToc(), receiveFileChunks(), replace_token(), rewind_parseTimeLineHistory(), rewriteVisibilityMap(), run_all_permutations(), run_named_permutations(), run_permutation(), setalarm(), setup_bin_paths(), SetVariable(), SetVariableHooks(), SetWALSegSize(), simple_action_list_append(), simple_oid_list_append(), simple_string_list_append(), slurpFile(), sortDumpableObjects(), SortTocFromFile(), sql_exec(), sql_exec_searchtables(), strtokx(), tar_open_for_write(), tarPrintf(), TopoSort(), vacuum_one_database(), wait_for_tests(), WaitForTerminatingWorkers(), and WriteEmptyXLOG().

48 {
49  return pg_malloc_internal(size, 0);
50 }
static void * pg_malloc_internal(size_t size, int flags)
Definition: fe_memutils.c:23

◆ pg_malloc0()

◆ pg_malloc_extended()

void* pg_malloc_extended ( size_t  size,
int  flags 
)

Definition at line 59 of file fe_memutils.c.

References pg_malloc_internal().

60 {
61  return pg_malloc_internal(size, flags);
62 }
static void * pg_malloc_internal(size_t size, int flags)
Definition: fe_memutils.c:23

◆ pg_realloc()

void* pg_realloc ( void *  pointer,
size_t  size 
)

Definition at line 65 of file fe_memutils.c.

References _, EXIT_FAILURE, and realloc.

Referenced by add_one_elt(), addObjectDependency(), AssignDumpId(), BuildArchiveDependencies(), datapagemap_add(), exec_command_set(), filemap_list_to_array(), findDumpableDependencies(), identify_locking_dependencies(), lookupCreateVariable(), main(), ParseScript(), read_file_contents(), readMessageFromPipe(), ReadToc(), repalloc(), replaceVariable(), and rewind_parseTimeLineHistory().

66 {
67  void *tmp;
68 
69  /* Avoid unportable behavior of realloc(NULL, 0) */
70  if (ptr == NULL && size == 0)
71  size = 1;
72  tmp = realloc(ptr, size);
73  if (!tmp)
74  {
75  fprintf(stderr, _("out of memory\n"));
76  exit(EXIT_FAILURE);
77  }
78  return tmp;
79 }
#define realloc(a, b)
Definition: header.h:60
#define EXIT_FAILURE
Definition: settings.h:152
#define _(x)
Definition: elog.c:84

◆ pg_strdup()

char* pg_strdup ( const char *  in)

Definition at line 85 of file fe_memutils.c.

References _, and EXIT_FAILURE.

Referenced by _allocAH(), _ArchiveEntry(), _becomeUser(), _check_database_version(), _connectDB(), _getObjectDescription(), _printTocEntry(), _selectOutputSchema(), _selectTablespace(), _tarGetHeader(), add_one_elt(), add_stringlist_item(), adjust_data_dir(), ArchiveEntry(), bool_substitute_hook(), bootstrap_template1(), check_locale_name(), check_required_directory(), CloneArchive(), comp_keyword_case_substitute_hook(), ConnectDatabase(), constructConnStr(), convertRegProcReference(), convertTSFunction(), create_script_for_old_cluster_deletion(), createBoundaryObjects(), CreateWalDirectoryMethod(), describeOneTableDetails(), describeRoles(), dir_open_for_write(), do_connect(), do_pset(), dumpAccessMethod(), dumpAttrDef(), dumpBaseType(), dumpCollation(), dumpCompositeType(), dumpConversion(), dumpDatabase(), dumpDomain(), dumpEnumType(), dumpEventTrigger(), dumpExtension(), dumpForeignDataWrapper(), dumpForeignServer(), dumpGroups(), dumpIndex(), dumpNamespace(), dumpOpclass(), dumpOpfamily(), dumpProcLang(), dumpPublication(), dumpRangeType(), dumpRule(), dumpSearchPath(), dumpSequence(), dumpStatisticsExt(), dumpSubscription(), dumpTable(), dumpTableConstraintComment(), dumpTableSchema(), dumpTablespaces(), dumpTrigger(), dumpTSConfig(), dumpTSDictionary(), dumpTSParser(), dumpTSTemplate(), dumpUndefinedType(), echo_substitute_hook(), encodingid_to_string(), exec_command_g(), exec_command_gset(), exec_command_set(), fetch_count_substitute_hook(), find_matching_ts_config(), fix_path_separator(), flagInhAttrs(), flagInhIndexes(), format_numeric_locale(), get_canonical_locale_name(), get_comma_elts(), get_control_data(), get_db_infos(), get_id(), get_language_name(), get_loadable_libraries(), get_opts(), get_prompt(), get_rel_infos(), get_restricted_token(), get_sock_dir(), get_su_pwd(), get_synchronized_snapshot(), get_tablespace_paths(), get_user_info(), getAccessMethods(), getAggregates(), getBlobs(), getCollations(), getConstraints(), getConversions(), getDefaultACLs(), getDomainConstraints(), getEventTriggers(), getExtendedStatistics(), getExtensions(), getForeignDataWrappers(), getForeignServers(), getFormattedTypeName(), getFuncs(), getIndexes(), getNamespaces(), getOpclasses(), getOperators(), getOpfamilies(), getPolicies(), getProcLangs(), getPublications(), getPublicationTables(), getRules(), gets_fromFile(), getSubscriptions(), getTableAttrs(), getTables(), getTriggers(), getTSConfigurations(), getTSDictionaries(), getTSParsers(), getTSTemplates(), getTypes(), getVariable(), histcontrol_substitute_hook(), histsize_substitute_hook(), ignoreeof_substitute_hook(), initializeInput(), load_resultmap(), locale_date_order(), lookupCreateVariable(), main(), MainLoop(), makeAlterConfigCommand(), open_result_files(), parallel_exec_prog(), parallel_transfer_all_new_dbs(), parse_psql_options(), parse_slash_copy(), parseCommandLine(), parseQuery(), parseScriptWeight(), pg_send_history(), printTableAddFooter(), printTableSetFooter(), PrintTOCSummary(), process_backslash_command(), process_source_file(), process_sql_command(), process_target_file(), processEncodingEntry(), processExtensionTables(), processSearchPathEntry(), prompt_for_password(), psql_get_variable(), pstrdup(), putVariable(), read_post_opts(), readfile(), regression_main(), replace_line_endings(), replace_string(), RestoreArchive(), run_schedule(), run_simple_query(), RunIdentifySystem(), sendCommand(), set_info_version(), set_tablespace_directory_suffix(), setDecimalLocale(), setup(), setup_config(), setup_connection(), setup_pgdata(), SetVariable(), SetVariableHooks(), show_context_substitute_hook(), simple_action_list_append(), split_path(), split_table_columns_spec(), split_to_stringlist(), stop_postmaster(), tar_open_for_write(), tarOpen(), try_complete_step(), and verbosity_substitute_hook().

86 {
87  char *tmp;
88 
89  if (!in)
90  {
91  fprintf(stderr,
92  _("cannot duplicate null pointer (internal error)\n"));
93  exit(EXIT_FAILURE);
94  }
95  tmp = strdup(in);
96  if (!tmp)
97  {
98  fprintf(stderr, _("out of memory\n"));
99  exit(EXIT_FAILURE);
100  }
101  return tmp;
102 }
#define EXIT_FAILURE
Definition: settings.h:152
#define _(x)
Definition: elog.c:84

◆ psprintf()

char* psprintf ( const char *  fmt,
  ... 
)

◆ pstrdup()

char* pstrdup ( const char *  in)

Definition at line 1161 of file mcxt.c.

1162 {
1164 }
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
char * MemoryContextStrdup(MemoryContext context, const char *string)
Definition: mcxt.c:1148

◆ pvsnprintf()

char size_t pvsnprintf ( char *  buf,
size_t  len,
const char *  fmt,
va_list  args 
)

◆ repalloc()

void* repalloc ( void *  pointer,
Size  size 
)

Definition at line 1044 of file mcxt.c.

1045 {
1046  MemoryContext context = GetMemoryChunkContext(pointer);
1047  void *ret;
1048 
1049  if (!AllocSizeIsValid(size))
1050  elog(ERROR, "invalid memory alloc request size %zu", size);
1051 
1052  AssertNotInCriticalSection(context);
1053 
1054  /* isReset must be false already */
1055  Assert(!context->isReset);
1056 
1057  ret = context->methods->realloc(context, pointer, size);
1058  if (unlikely(ret == NULL))
1059  {
1061  ereport(ERROR,
1062  (errcode(ERRCODE_OUT_OF_MEMORY),
1063  errmsg("out of memory"),
1064  errdetail("Failed on request of size %zu in memory context \"%s\".",
1065  size, context->name)));
1066  }
1067 
1068  VALGRIND_MEMPOOL_CHANGE(context, pointer, ret, size);
1069 
1070  return ret;
1071 }
const MemoryContextMethods * methods
Definition: memnodes.h:82
int errcode(int sqlerrcode)
Definition: elog.c:575
#define AssertNotInCriticalSection(context)
Definition: mcxt.c:67
void *(* realloc)(MemoryContext context, void *pointer, Size size)
Definition: memnodes.h:62
#define ERROR
Definition: elog.h:43
#define VALGRIND_MEMPOOL_CHANGE(context, optr, nptr, size)
Definition: memdebug.h:31
void MemoryContextStats(MemoryContext context)
Definition: mcxt.c:474
int errdetail(const char *fmt,...)
Definition: elog.c:873
#define ereport(elevel, rest)
Definition: elog.h:122
MemoryContext TopMemoryContext
Definition: mcxt.c:44
#define AllocSizeIsValid(size)
Definition: memutils.h:42
#define Assert(condition)
Definition: c.h:699
int errmsg(const char *fmt,...)
Definition: elog.c:797
#define unlikely(x)
Definition: c.h:208
#define elog
Definition: elog.h:219
const char * name
Definition: memnodes.h:87
static MemoryContext GetMemoryChunkContext(void *pointer)
Definition: memutils.h:112