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 */
 
#define pg_malloc_object(type)   ((type *) pg_malloc(sizeof(type)))
 
#define pg_malloc0_object(type)   ((type *) pg_malloc0(sizeof(type)))
 
#define pg_malloc_array(type, count)   ((type *) pg_malloc(sizeof(type) * (count)))
 
#define pg_malloc0_array(type, count)   ((type *) pg_malloc0(sizeof(type) * (count)))
 
#define pg_realloc_array(pointer, type, count)   ((type *) pg_realloc(pointer, sizeof(type) * (count)))
 
#define palloc_object(type)   ((type *) palloc(sizeof(type)))
 
#define palloc0_object(type)   ((type *) palloc0(sizeof(type)))
 
#define palloc_array(type, count)   ((type *) palloc(sizeof(type) * (count)))
 
#define palloc0_array(type, count)   ((type *) palloc0(sizeof(type) * (count)))
 
#define repalloc_array(pointer, type, count)   ((type *) repalloc(pointer, sizeof(type) * (count)))
 

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 *ptr, size_t size)
 
void pg_free (void *ptr)
 
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.

◆ MCXT_ALLOC_NO_OOM

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

Definition at line 17 of file fe_memutils.h.

◆ MCXT_ALLOC_ZERO

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

Definition at line 18 of file fe_memutils.h.

◆ palloc0_array

#define palloc0_array (   type,
  count 
)    ((type *) palloc0(sizeof(type) * (count)))

Definition at line 65 of file fe_memutils.h.

◆ palloc0_object

#define palloc0_object (   type)    ((type *) palloc0(sizeof(type)))

Definition at line 63 of file fe_memutils.h.

◆ palloc_array

#define palloc_array (   type,
  count 
)    ((type *) palloc(sizeof(type) * (count)))

Definition at line 64 of file fe_memutils.h.

◆ palloc_object

#define palloc_object (   type)    ((type *) palloc(sizeof(type)))

Definition at line 62 of file fe_memutils.h.

◆ pg_malloc0_array

#define pg_malloc0_array (   type,
  count 
)    ((type *) pg_malloc0(sizeof(type) * (count)))

Definition at line 45 of file fe_memutils.h.

◆ pg_malloc0_object

#define pg_malloc0_object (   type)    ((type *) pg_malloc0(sizeof(type)))

Definition at line 39 of file fe_memutils.h.

◆ pg_malloc_array

#define pg_malloc_array (   type,
  count 
)    ((type *) pg_malloc(sizeof(type) * (count)))

Definition at line 44 of file fe_memutils.h.

◆ pg_malloc_object

#define pg_malloc_object (   type)    ((type *) pg_malloc(sizeof(type)))

Definition at line 38 of file fe_memutils.h.

◆ pg_realloc_array

#define pg_realloc_array (   pointer,
  type,
  count 
)    ((type *) pg_realloc(pointer, sizeof(type) * (count)))

Definition at line 51 of file fe_memutils.h.

◆ repalloc_array

#define repalloc_array (   pointer,
  type,
  count 
)    ((type *) repalloc(pointer, sizeof(type) * (count)))

Definition at line 66 of file fe_memutils.h.

Function Documentation

◆ palloc()

void* palloc ( Size  size)

Definition at line 1199 of file mcxt.c.

1200 {
1201  /* duplicates MemoryContextAlloc to avoid increased overhead */
1202  void *ret;
1204 
1205  Assert(MemoryContextIsValid(context));
1206  AssertNotInCriticalSection(context);
1207 
1208  if (!AllocSizeIsValid(size))
1209  elog(ERROR, "invalid memory alloc request size %zu", size);
1210 
1211  context->isReset = false;
1212 
1213  ret = context->methods->alloc(context, size);
1214  if (unlikely(ret == NULL))
1215  {
1217  ereport(ERROR,
1218  (errcode(ERRCODE_OUT_OF_MEMORY),
1219  errmsg("out of memory"),
1220  errdetail("Failed on request of size %zu in memory context \"%s\".",
1221  size, context->name)));
1222  }
1223 
1224  VALGRIND_MEMPOOL_ALLOC(context, ret, size);
1225 
1226  return ret;
1227 }
#define unlikely(x)
Definition: c.h:295
int errdetail(const char *fmt,...)
Definition: elog.c:1039
int errcode(int sqlerrcode)
Definition: elog.c:695
int errmsg(const char *fmt,...)
Definition: elog.c:906
#define ERROR
Definition: elog.h:35
#define ereport(elevel,...)
Definition: elog.h:145
Assert(fmt[strlen(fmt) - 1] !='\n')
MemoryContext TopMemoryContext
Definition: mcxt.c:130
#define AssertNotInCriticalSection(context)
Definition: mcxt.c:155
MemoryContext CurrentMemoryContext
Definition: mcxt.c:124
void MemoryContextStats(MemoryContext context)
Definition: mcxt.c:672
#define VALGRIND_MEMPOOL_ALLOC(context, addr, size)
Definition: memdebug.h:29
#define MemoryContextIsValid(context)
Definition: memnodes.h:107
#define AllocSizeIsValid(size)
Definition: memutils.h:42
const MemoryContextMethods * methods
Definition: memnodes.h:88
const char * name
Definition: memnodes.h:93
void *(* alloc)(MemoryContext context, Size size)
Definition: memnodes.h:60

◆ palloc0()

void* palloc0 ( Size  size)

Definition at line 1230 of file mcxt.c.

1231 {
1232  /* duplicates MemoryContextAllocZero to avoid increased overhead */
1233  void *ret;
1235 
1236  Assert(MemoryContextIsValid(context));
1237  AssertNotInCriticalSection(context);
1238 
1239  if (!AllocSizeIsValid(size))
1240  elog(ERROR, "invalid memory alloc request size %zu", size);
1241 
1242  context->isReset = false;
1243 
1244  ret = context->methods->alloc(context, size);
1245  if (unlikely(ret == NULL))
1246  {
1248  ereport(ERROR,
1249  (errcode(ERRCODE_OUT_OF_MEMORY),
1250  errmsg("out of memory"),
1251  errdetail("Failed on request of size %zu in memory context \"%s\".",
1252  size, context->name)));
1253  }
1254 
1255  VALGRIND_MEMPOOL_ALLOC(context, ret, size);
1256 
1257  MemSetAligned(ret, 0, size);
1258 
1259  return ret;
1260 }
#define MemSetAligned(start, val, len)
Definition: c.h:983

◆ palloc_extended()

void* palloc_extended ( Size  size,
int  flags 
)

Definition at line 1263 of file mcxt.c.

1264 {
1265  /* duplicates MemoryContextAllocExtended to avoid increased overhead */
1266  void *ret;
1268 
1269  Assert(MemoryContextIsValid(context));
1270  AssertNotInCriticalSection(context);
1271 
1272  if (!((flags & MCXT_ALLOC_HUGE) != 0 ? AllocHugeSizeIsValid(size) :
1273  AllocSizeIsValid(size)))
1274  elog(ERROR, "invalid memory alloc request size %zu", size);
1275 
1276  context->isReset = false;
1277 
1278  ret = context->methods->alloc(context, size);
1279  if (unlikely(ret == NULL))
1280  {
1281  if ((flags & MCXT_ALLOC_NO_OOM) == 0)
1282  {
1284  ereport(ERROR,
1285  (errcode(ERRCODE_OUT_OF_MEMORY),
1286  errmsg("out of memory"),
1287  errdetail("Failed on request of size %zu in memory context \"%s\".",
1288  size, context->name)));
1289  }
1290  return NULL;
1291  }
1292 
1293  VALGRIND_MEMPOOL_ALLOC(context, ret, size);
1294 
1295  if ((flags & MCXT_ALLOC_ZERO) != 0)
1296  MemSetAligned(ret, 0, size);
1297 
1298  return ret;
1299 }
#define MCXT_ALLOC_ZERO
Definition: fe_memutils.h:18
#define MCXT_ALLOC_HUGE
Definition: fe_memutils.h:16
#define MCXT_ALLOC_NO_OOM
Definition: fe_memutils.h:17
#define AllocHugeSizeIsValid(size)
Definition: memutils.h:49

◆ pfree()

void pfree ( void *  pointer)

Definition at line 1306 of file mcxt.c.

1307 {
1308 #ifdef USE_VALGRIND
1309  MemoryContext context = GetMemoryChunkContext(pointer);
1310 #endif
1311 
1312  MCXT_METHOD(pointer, free_p) (pointer);
1313  VALGRIND_MEMPOOL_FREE(context, pointer);
1314 }
MemoryContext GetMemoryChunkContext(void *pointer)
Definition: mcxt.c:589
#define MCXT_METHOD(pointer, method)
Definition: mcxt.c:162
#define VALGRIND_MEMPOOL_FREE(context, addr)
Definition: memdebug.h:30

◆ pg_free()

◆ pg_malloc()

void* pg_malloc ( size_t  size)

Definition at line 47 of file fe_memutils.c.

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

References pg_malloc_internal().

Referenced by _Clone(), _CustomReadFunc(), _PrintFileData(), _skipData(), add_one_elt(), add_stringlist_item(), ahprintf(), ArchiveEntry(), archprintf(), avlInsertNode(), buildACLCommands(), BuildArchiveDependencies(), cfopen(), check_testspec(), CloneArchive(), collectComments(), collectRoleNames(), collectSecLabels(), conditional_stack_create(), conditional_stack_push(), copyFile(), create_sql_command(), createBoundaryObjects(), CreateVariableSpace(), CreateWalTarMethod(), datapagemap_iterate(), decide_file_actions(), do_connect(), do_watch(), dumpTableData_insert(), escape_quotes_bki(), ExecuteSqlCommandBuf(), find_other_exec_or_die(), findDependencyLoops(), fix_dependencies(), 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(), getEventTriggers(), getExtendedStatistics(), getExtensions(), getForeignDataWrappers(), getForeignServers(), getFuncs(), getIndexes(), getInherits(), getNamespaces(), getOpclasses(), getOperators(), getOpfamilies(), getPolicies(), getProcLangs(), getPublicationNamespaces(), getPublications(), getPublicationTables(), getRules(), getSubscriptions(), getTableAttrs(), getTimelineHistory(), getTransforms(), getTriggers(), getTSConfigurations(), getTSDictionaries(), getTSParsers(), getTSTemplates(), getTypes(), identify_locking_dependencies(), InitArchiveFmt_Custom(), InitArchiveFmt_Directory(), InitArchiveFmt_Null(), InitArchiveFmt_Tar(), libpq_fetch_file(), listSchemas(), load_resultmap(), main(), makeTableDataInfo(), NewDumpOptions(), parallel_exec_prog(), parallel_transfer_all_new_dbs(), ParallelBackupStart(), parse_manifest_file(), ParseScript(), parseScriptWeight(), parseVariable(), pretty_wal_size(), print_aligned_text(), print_aligned_vertical(), printCrosstab(), PrintResultInCrosstab(), 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_permutation(), savePsetInfo(), setup_bin_paths(), SetVariable(), SetVariableHooks(), simple_oid_list_append(), simple_ptr_list_append(), simple_string_list_append(), slurpFile(), sortDumpableObjects(), SplitGUCList(), sql_exec(), sql_exec_searchtables(), strtokx(), tar_open_for_write(), tarPrintf(), TopoSort(), wait_for_tests(), WaitForTerminatingWorkers(), and WriteDataChunks().

◆ pg_malloc0()

◆ pg_malloc_extended()

void* pg_malloc_extended ( size_t  size,
int  flags 
)

Definition at line 59 of file fe_memutils.c.

60 {
61  return pg_malloc_internal(size, flags);
62 }

References pg_malloc_internal().

Referenced by do_lo_import(), and pg_log_generic_v().

◆ pg_realloc()

void* pg_realloc ( void *  ptr,
size_t  size 
)

Definition at line 65 of file fe_memutils.c.

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"));
77  }
78  return tmp;
79 }
#define _(x)
Definition: elog.c:90
#define realloc(a, b)
Definition: header.h:60
exit(1)
#define fprintf
Definition: port.h:242
#define EXIT_FAILURE
Definition: settings.h:166

References _, exit(), EXIT_FAILURE, fprintf, and realloc.

Referenced by add_one_elt(), BuildArchiveDependencies(), datapagemap_add(), enlargeVariables(), exec_command_set(), extend_pattern_info_array(), findDumpableDependencies(), identify_locking_dependencies(), main(), ParseScript(), read_file_contents(), readfile(), readMessageFromPipe(), ReadToc(), repalloc(), replaceVariable(), and rewind_parseTimeLineHistory().

◆ pg_strdup()

char* pg_strdup ( const char *  in)

Definition at line 85 of file fe_memutils.c.

86 {
87  char *tmp;
88 
89  if (!in)
90  {
91  fprintf(stderr,
92  _("cannot duplicate null pointer (internal error)\n"));
94  }
95  tmp = strdup(in);
96  if (!tmp)
97  {
98  fprintf(stderr, _("out of memory\n"));
100  }
101  return tmp;
102 }

References _, exit(), 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(), collectComments(), collectRoleNames(), collectSecLabels(), 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(), dumpIndex(), dumpNamespace(), dumpOpclass(), dumpOpfamily(), dumpOptionsFromRestoreOptions(), dumpPolicy(), dumpProcLang(), dumpPublication(), dumpRangeType(), dumpRoleGUCPrivs(), dumpRule(), dumpSearchPath(), dumpSequence(), dumpStatisticsExt(), dumpSubscription(), dumpTable(), dumpTableConstraintComment(), dumpTableSchema(), dumpTablespaces(), dumpTrigger(), dumpTSConfig(), dumpTSDictionary(), dumpTSParser(), dumpTSTemplate(), dumpUndefinedType(), echo_substitute_hook(), encodingid_to_string(), exec_command_bind(), exec_command_g(), exec_command_gset(), exec_command_password(), exec_command_set(), fetch_count_substitute_hook(), find_matching_ts_config(), fix_path_separator(), flagInhAttrs(), flagInhIndexes(), flagInhTables(), 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(), listSchemas(), 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(), progress_update_filename(), psql_get_variable(), pstrdup(), putVariable(), read_post_opts(), readfile(), ReconnectToServer(), regression_main(), RestoreArchive(), run_schedule(), run_simple_query(), RunIdentifySystem(), sanitize_line(), savePsetInfo(), search_directory(), sendCommand(), set_info_version(), setDecimalLocale(), setup(), setup_config(), setup_connection(), setup_pgdata(), SetVariable(), SetVariableHooks(), show_context_substitute_hook(), simple_action_list_append(), simple_prompt_extended(), split_path(), split_to_stringlist(), tar_open_for_write(), tarOpen(), and verbosity_substitute_hook().

◆ pnstrdup()

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

Definition at line 1494 of file mcxt.c.

1495 {
1496  char *out;
1497 
1498  len = strnlen(in, len);
1499 
1500  out = palloc(len + 1);
1501  memcpy(out, in, len);
1502  out[len] = '\0';
1503 
1504  return out;
1505 }
void * palloc(Size size)
Definition: mcxt.c:1199
const void size_t len
size_t strnlen(const char *str, size_t maxlen)
Definition: strnlen.c:26

◆ psprintf()

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

◆ pstrdup()

char* pstrdup ( const char *  in)

Definition at line 1483 of file mcxt.c.

1484 {
1486 }
char * MemoryContextStrdup(MemoryContext context, const char *string)
Definition: mcxt.c:1470

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

1322 {
1323 #if defined(USE_ASSERT_CHECKING) || defined(USE_VALGRIND)
1324  MemoryContext context = GetMemoryChunkContext(pointer);
1325 #endif
1326  void *ret;
1327 
1328  if (!AllocSizeIsValid(size))
1329  elog(ERROR, "invalid memory alloc request size %zu", size);
1330 
1331  AssertNotInCriticalSection(context);
1332 
1333  /* isReset must be false already */
1334  Assert(!context->isReset);
1335 
1336  ret = MCXT_METHOD(pointer, realloc) (pointer, size);
1337  if (unlikely(ret == NULL))
1338  {
1339  MemoryContext cxt = GetMemoryChunkContext(pointer);
1340 
1342  ereport(ERROR,
1343  (errcode(ERRCODE_OUT_OF_MEMORY),
1344  errmsg("out of memory"),
1345  errdetail("Failed on request of size %zu in memory context \"%s\".",
1346  size, cxt->name)));
1347  }
1348 
1349  VALGRIND_MEMPOOL_CHANGE(context, pointer, ret, size);
1350 
1351  return ret;
1352 }
#define VALGRIND_MEMPOOL_CHANGE(context, optr, nptr, size)
Definition: memdebug.h:31