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)
 
char * pnstrdup (const char *in, Size size)
 
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 950 of file mcxt.c.

951 {
952  /* duplicates MemoryContextAlloc to avoid increased overhead */
953  void *ret;
955 
958 
959  if (!AllocSizeIsValid(size))
960  elog(ERROR, "invalid memory alloc request size %zu", size);
961 
962  context->isReset = false;
963 
964  ret = context->methods->alloc(context, size);
965  if (unlikely(ret == NULL))
966  {
968  ereport(ERROR,
969  (errcode(ERRCODE_OUT_OF_MEMORY),
970  errmsg("out of memory"),
971  errdetail("Failed on request of size %zu in memory context \"%s\".",
972  size, context->name)));
973  }
974 
975  VALGRIND_MEMPOOL_ALLOC(context, ret, size);
976 
977  return ret;
978 }
const MemoryContextMethods * methods
Definition: memnodes.h:83
int errcode(int sqlerrcode)
Definition: elog.c:691
#define AssertNotInCriticalSection(context)
Definition: mcxt.c:67
#define ERROR
Definition: elog.h:43
void MemoryContextStats(MemoryContext context)
Definition: mcxt.c:499
int errdetail(const char *fmt,...)
Definition: elog.c:1035
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
void *(* alloc)(MemoryContext context, Size size)
Definition: memnodes.h:59
#define AssertArg(condition)
Definition: c.h:802
MemoryContext TopMemoryContext
Definition: mcxt.c:44
#define AllocSizeIsValid(size)
Definition: memutils.h:42
#define ereport(elevel,...)
Definition: elog.h:155
#define MemoryContextIsValid(context)
Definition: memnodes.h:102
#define VALGRIND_MEMPOOL_ALLOC(context, addr, size)
Definition: memdebug.h:29
int errmsg(const char *fmt,...)
Definition: elog.c:902
#define elog(elevel,...)
Definition: elog.h:228
#define unlikely(x)
Definition: c.h:261
const char * name
Definition: memnodes.h:88

◆ palloc0()

void* palloc0 ( Size  size)

Definition at line 981 of file mcxt.c.

982 {
983  /* duplicates MemoryContextAllocZero to avoid increased overhead */
984  void *ret;
986 
989 
990  if (!AllocSizeIsValid(size))
991  elog(ERROR, "invalid memory alloc request size %zu", size);
992 
993  context->isReset = false;
994 
995  ret = context->methods->alloc(context, size);
996  if (unlikely(ret == NULL))
997  {
999  ereport(ERROR,
1000  (errcode(ERRCODE_OUT_OF_MEMORY),
1001  errmsg("out of memory"),
1002  errdetail("Failed on request of size %zu in memory context \"%s\".",
1003  size, context->name)));
1004  }
1005 
1006  VALGRIND_MEMPOOL_ALLOC(context, ret, size);
1007 
1008  MemSetAligned(ret, 0, size);
1009 
1010  return ret;
1011 }
#define MemSetAligned(start, val, len)
Definition: c.h:1037
const MemoryContextMethods * methods
Definition: memnodes.h:83
int errcode(int sqlerrcode)
Definition: elog.c:691
#define AssertNotInCriticalSection(context)
Definition: mcxt.c:67
#define ERROR
Definition: elog.h:43
void MemoryContextStats(MemoryContext context)
Definition: mcxt.c:499
int errdetail(const char *fmt,...)
Definition: elog.c:1035
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
void *(* alloc)(MemoryContext context, Size size)
Definition: memnodes.h:59
#define AssertArg(condition)
Definition: c.h:802
MemoryContext TopMemoryContext
Definition: mcxt.c:44
#define AllocSizeIsValid(size)
Definition: memutils.h:42
#define ereport(elevel,...)
Definition: elog.h:155
#define MemoryContextIsValid(context)
Definition: memnodes.h:102
#define VALGRIND_MEMPOOL_ALLOC(context, addr, size)
Definition: memdebug.h:29
int errmsg(const char *fmt,...)
Definition: elog.c:902
#define elog(elevel,...)
Definition: elog.h:228
#define unlikely(x)
Definition: c.h:261
const char * name
Definition: memnodes.h:88

◆ palloc_extended()

void* palloc_extended ( Size  size,
int  flags 
)

Definition at line 1014 of file mcxt.c.

1015 {
1016  /* duplicates MemoryContextAllocExtended to avoid increased overhead */
1017  void *ret;
1019 
1020  AssertArg(MemoryContextIsValid(context));
1021  AssertNotInCriticalSection(context);
1022 
1023  if (((flags & MCXT_ALLOC_HUGE) != 0 && !AllocHugeSizeIsValid(size)) ||
1024  ((flags & MCXT_ALLOC_HUGE) == 0 && !AllocSizeIsValid(size)))
1025  elog(ERROR, "invalid memory alloc request size %zu", size);
1026 
1027  context->isReset = false;
1028 
1029  ret = context->methods->alloc(context, size);
1030  if (unlikely(ret == NULL))
1031  {
1032  if ((flags & MCXT_ALLOC_NO_OOM) == 0)
1033  {
1035  ereport(ERROR,
1036  (errcode(ERRCODE_OUT_OF_MEMORY),
1037  errmsg("out of memory"),
1038  errdetail("Failed on request of size %zu in memory context \"%s\".",
1039  size, context->name)));
1040  }
1041  return NULL;
1042  }
1043 
1044  VALGRIND_MEMPOOL_ALLOC(context, ret, size);
1045 
1046  if ((flags & MCXT_ALLOC_ZERO) != 0)
1047  MemSetAligned(ret, 0, size);
1048 
1049  return ret;
1050 }
#define MemSetAligned(start, val, len)
Definition: c.h:1037
#define MCXT_ALLOC_HUGE
Definition: fe_memutils.h:16
const MemoryContextMethods * methods
Definition: memnodes.h:83
#define MCXT_ALLOC_NO_OOM
Definition: fe_memutils.h:18
int errcode(int sqlerrcode)
Definition: elog.c:691
#define AssertNotInCriticalSection(context)
Definition: mcxt.c:67
#define ERROR
Definition: elog.h:43
void MemoryContextStats(MemoryContext context)
Definition: mcxt.c:499
int errdetail(const char *fmt,...)
Definition: elog.c:1035
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
void *(* alloc)(MemoryContext context, Size size)
Definition: memnodes.h:59
#define AssertArg(condition)
Definition: c.h:802
MemoryContext TopMemoryContext
Definition: mcxt.c:44
#define AllocSizeIsValid(size)
Definition: memutils.h:42
#define ereport(elevel,...)
Definition: elog.h:155
#define MemoryContextIsValid(context)
Definition: memnodes.h:102
#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:902
#define elog(elevel,...)
Definition: elog.h:228
#define unlikely(x)
Definition: c.h:261
const char * name
Definition: memnodes.h:88

◆ pfree()

void pfree ( void *  pointer)

Definition at line 1057 of file mcxt.c.

1058 {
1059  MemoryContext context = GetMemoryChunkContext(pointer);
1060 
1061  context->methods->free_p(context, pointer);
1062  VALGRIND_MEMPOOL_FREE(context, pointer);
1063 }
const MemoryContextMethods * methods
Definition: memnodes.h:83
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:113

◆ 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(), create_sql_command(), createBoundaryObjects(), CreateVariableSpace(), CreateWalTarMethod(), datapagemap_iterate(), decide_file_actions(), do_connect(), do_watch(), EndCompressor(), escape_quotes_bki(), ExecuteSqlCommandBuf(), 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_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(), initialize_environment(), libpq_fetch_file(), load_resultmap(), lookupCreateVariable(), main(), makeTableDataInfo(), NewDumpOptions(), parallel_exec_prog(), parallel_transfer_all_new_dbs(), ParallelBackupStart(), ParallelSlotsSetup(), parse_manifest_file(), ParseScript(), parseScriptWeight(), parseVariable(), pretty_wal_size(), print_aligned_text(), print_aligned_vertical(), printCrosstab(), PrintResultsInCrosstab(), process_queued_fetch_requests(), pset_quoted_string(), quote_identifier(), quote_if_needed(), rankSort(), read_controlfile(), read_file_contents(), ReadDataFromArchiveNone(), readfile(), readMessageFromPipe(), ReadStr(), ReadToc(), ready_list_init(), replace_token(), rewind_parseTimeLineHistory(), run_all_permutations(), run_named_permutations(), run_permutation(), savePsetInfo(), setup_bin_paths(), SetVariable(), SetVariableHooks(), simple_action_list_append(), simple_oid_list_append(), simple_ptr_list_append(), simple_string_list_append(), slurpFile(), socket_has_input(), sortDumpableObjects(), SplitGUCList(), sql_exec(), sql_exec_searchtables(), strtokx(), tar_open_for_write(), tarPrintf(), TopoSort(), wait_for_tests(), WaitForTerminatingWorkers(), and WriteDataChunks().

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().

Referenced by do_kill(), do_lo_import(), and pg_log_generic_v().

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, fprintf, and realloc.

Referenced by add_one_elt(), addObjectDependency(), AssignDumpId(), BuildArchiveDependencies(), datapagemap_add(), exec_command_set(), findDumpableDependencies(), identify_locking_dependencies(), lookupCreateVariable(), main(), ParseScript(), read_file_contents(), readfile(), 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 fprintf
Definition: port.h:219
#define realloc(a, b)
Definition: header.h:60
#define EXIT_FAILURE
Definition: settings.h:161
#define _(x)
Definition: elog.c:88

◆ pg_strdup()

char* pg_strdup ( const char *  in)

Definition at line 85 of file fe_memutils.c.

References _, EXIT_FAILURE, and fprintf.

Referenced by _allocAH(), _ArchiveEntry(), _becomeUser(), _check_database_version(), _getObjectDescription(), _selectOutputSchema(), _selectTableAccessMethod(), _selectTablespace(), _tarGetHeader(), add_one_elt(), add_stringlist_item(), adjust_data_dir(), append_depends_on_extension(), ArchiveEntry(), bool_substitute_hook(), 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_pset(), dumpAccessMethod(), dumpAttrDef(), dumpBaseType(), dumpCollation(), dumpCompositeType(), dumpConversion(), dumpDatabase(), dumpDomain(), dumpEnumType(), dumpEventTrigger(), dumpExtension(), dumpForeignDataWrapper(), dumpForeignServer(), dumpGroups(), dumpIndex(), dumpNamespace(), dumpOpclass(), dumpOpfamily(), dumpOptionsFromRestoreOptions(), dumpPolicy(), 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_rel_infos(), get_restricted_token(), get_sock_dir(), 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(), getRestoreCommand(), getRules(), gets_fromFile(), getSubscriptions(), getTableAttrs(), getTables(), getTriggers(), getTSConfigurations(), getTSDictionaries(), getTSParsers(), getTSTemplates(), getTypes(), getVariable(), handle_args(), histcontrol_substitute_hook(), histsize_substitute_hook(), identify_target_directory(), ignoreeof_substitute_hook(), initializeInput(), insert_filehash_entry(), 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(), parseAclItem(), parseCommandLine(), parseQuery(), ParseScript(), parseScriptWeight(), pg_send_history(), postprocess_sql_command(), printTableAddFooter(), printTableSetFooter(), process_backslash_command(), process_source_file(), process_target_file(), processEncodingEntry(), processExtensionTables(), processSearchPathEntry(), psql_get_variable(), pstrdup(), putVariable(), read_post_opts(), readfile(), ReconnectToServer(), regression_main(), replace_string(), RestoreArchive(), run_schedule(), run_simple_query(), RunIdentifySystem(), sanitize_line(), savePsetInfo(), 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(), simple_prompt(), split_path(), split_to_stringlist(), 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 fprintf
Definition: port.h:219
#define EXIT_FAILURE
Definition: settings.h:161
#define _(x)
Definition: elog.c:88

◆ pnstrdup()

char* pnstrdup ( const char *  in,
Size  size 
)

Definition at line 1198 of file mcxt.c.

1199 {
1200  char *out;
1201 
1202  len = strnlen(in, len);
1203 
1204  out = palloc(len + 1);
1205  memcpy(out, in, len);
1206  out[len] = '\0';
1207 
1208  return out;
1209 }
size_t strnlen(const char *str, size_t maxlen)
Definition: strnlen.c:26
void * palloc(Size size)
Definition: mcxt.c:950

◆ psprintf()

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

◆ pstrdup()

char* pstrdup ( const char *  in)

Definition at line 1187 of file mcxt.c.

1188 {
1190 }
MemoryContext CurrentMemoryContext
Definition: mcxt.c:38
char * MemoryContextStrdup(MemoryContext context, const char *string)
Definition: mcxt.c:1174

◆ 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 1070 of file mcxt.c.

1071 {
1072  MemoryContext context = GetMemoryChunkContext(pointer);
1073  void *ret;
1074 
1075  if (!AllocSizeIsValid(size))
1076  elog(ERROR, "invalid memory alloc request size %zu", size);
1077 
1078  AssertNotInCriticalSection(context);
1079 
1080  /* isReset must be false already */
1081  Assert(!context->isReset);
1082 
1083  ret = context->methods->realloc(context, pointer, size);
1084  if (unlikely(ret == NULL))
1085  {
1087  ereport(ERROR,
1088  (errcode(ERRCODE_OUT_OF_MEMORY),
1089  errmsg("out of memory"),
1090  errdetail("Failed on request of size %zu in memory context \"%s\".",
1091  size, context->name)));
1092  }
1093 
1094  VALGRIND_MEMPOOL_CHANGE(context, pointer, ret, size);
1095 
1096  return ret;
1097 }
const MemoryContextMethods * methods
Definition: memnodes.h:83
int errcode(int sqlerrcode)
Definition: elog.c:691
#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:499
int errdetail(const char *fmt,...)
Definition: elog.c:1035
MemoryContext TopMemoryContext
Definition: mcxt.c:44
#define AllocSizeIsValid(size)
Definition: memutils.h:42
#define ereport(elevel,...)
Definition: elog.h:155
#define Assert(condition)
Definition: c.h:800
int errmsg(const char *fmt,...)
Definition: elog.c:902
#define elog(elevel,...)
Definition: elog.h:228
#define unlikely(x)
Definition: c.h:261
const char * name
Definition: memnodes.h:88
static MemoryContext GetMemoryChunkContext(void *pointer)
Definition: memutils.h:113