PostgreSQL Source Code git master
Loading...
Searching...
No Matches
fe_memutils.c File Reference
#include "postgres_fe.h"
#include "common/int.h"
Include dependency graph for fe_memutils.c:

Go to the source code of this file.

Functions

static pg_noreturn pg_noinline void add_size_error (Size s1, Size s2)
 
static pg_noreturn pg_noinline void mul_size_error (Size s1, Size s2)
 
static voidpg_malloc_internal (size_t size, int flags)
 
voidpg_malloc (size_t size)
 
voidpg_malloc0 (size_t size)
 
voidpg_malloc_extended (size_t size, int flags)
 
voidpg_realloc (void *ptr, size_t size)
 
charpg_strdup (const char *in)
 
void pg_free (void *ptr)
 
voidpalloc (Size size)
 
voidpalloc0 (Size size)
 
voidpalloc_extended (Size size, int flags)
 
void pfree (void *pointer)
 
charpstrdup (const char *in)
 
charpnstrdup (const char *in, Size size)
 
voidrepalloc (void *pointer, Size size)
 
Size add_size (Size s1, Size s2)
 
Size mul_size (Size s1, Size s2)
 
voidpg_malloc_mul (Size s1, Size s2)
 
voidpg_malloc0_mul (Size s1, Size s2)
 
voidpg_malloc_mul_extended (Size s1, Size s2, int flags)
 
voidpg_realloc_mul (void *p, Size s1, Size s2)
 
voidpalloc_mul (Size s1, Size s2)
 
voidpalloc0_mul (Size s1, Size s2)
 
voidpalloc_mul_extended (Size s1, Size s2, int flags)
 
voidrepalloc_mul (void *p, Size s1, Size s2)
 

Function Documentation

◆ add_size()

Size add_size ( Size  s1,
Size  s2 
)

Definition at line 196 of file fe_memutils.c.

197{
198 Size result;
199
201 result > (SIZE_MAX / 2)))
203 return result;
204}
#define unlikely(x)
Definition c.h:438
size_t Size
Definition c.h:689
uint32 result
static pg_noreturn pg_noinline void add_size_error(Size s1, Size s2)
static bool pg_add_size_overflow(size_t a, size_t b, size_t *result)
Definition int.h:608
static int fb(int x)
char * s1
char * s2

References add_size_error(), fb(), pg_add_size_overflow(), result, s1, s2, and unlikely.

◆ add_size_error()

static pg_noreturn pg_noinline void add_size_error ( Size  s1,
Size  s2 
)
static

Definition at line 207 of file fe_memutils.c.

208{
209 fprintf(stderr, _("invalid memory allocation request size %zu + %zu\n"),
210 s1, s2);
212}
#define fprintf(file, fmt, msg)
Definition cubescan.l:21
#define _(x)
Definition elog.c:96
#define EXIT_FAILURE
Definition settings.h:197

References _, EXIT_FAILURE, fb(), fprintf, s1, and s2.

Referenced by add_size().

◆ mul_size()

Size mul_size ( Size  s1,
Size  s2 
)

Definition at line 215 of file fe_memutils.c.

216{
217 Size result;
218
220 result > (SIZE_MAX / 2)))
222 return result;
223}
static pg_noreturn pg_noinline void mul_size_error(Size s1, Size s2)
static bool pg_mul_size_overflow(size_t a, size_t b, size_t *result)
Definition int.h:642

References fb(), mul_size_error(), pg_mul_size_overflow(), result, s1, s2, and unlikely.

◆ mul_size_error()

static pg_noreturn pg_noinline void mul_size_error ( Size  s1,
Size  s2 
)
static

Definition at line 226 of file fe_memutils.c.

227{
228 fprintf(stderr, _("invalid memory allocation request size %zu * %zu\n"),
229 s1, s2);
231}

References _, EXIT_FAILURE, fb(), fprintf, s1, and s2.

Referenced by mul_size(), palloc0_mul(), palloc_mul(), palloc_mul_extended(), pg_malloc0_mul(), pg_malloc_mul(), pg_malloc_mul_extended(), pg_realloc_mul(), and repalloc_mul().

◆ palloc()

void * palloc ( Size  size)

Definition at line 121 of file fe_memutils.c.

122{
123 return pg_malloc_internal(size, 0);
124}
static void * pg_malloc_internal(size_t size, int flags)
Definition fe_memutils.c:29

References pg_malloc_internal().

Referenced by palloc_mul().

◆ palloc0()

void * palloc0 ( Size  size)

Definition at line 127 of file fe_memutils.c.

128{
130}
#define MCXT_ALLOC_ZERO
Definition fe_memutils.h:30

References MCXT_ALLOC_ZERO, and pg_malloc_internal().

Referenced by palloc0_mul().

◆ palloc0_mul()

void * palloc0_mul ( Size  s1,
Size  s2 
)

Definition at line 322 of file fe_memutils.c.

323{
324 /* inline mul_size() for efficiency */
325 Size req;
326
328 req > (SIZE_MAX / 2)))
330 return palloc0(req);
331}
void * palloc0(Size size)

References fb(), mul_size_error(), palloc0(), pg_mul_size_overflow(), s1, s2, and unlikely.

◆ palloc_extended()

void * palloc_extended ( Size  size,
int  flags 
)

Definition at line 133 of file fe_memutils.c.

134{
135 return pg_malloc_internal(size, flags);
136}

References pg_malloc_internal().

Referenced by palloc_mul_extended().

◆ palloc_mul()

void * palloc_mul ( Size  s1,
Size  s2 
)

Definition at line 304 of file fe_memutils.c.

305{
306 /* inline mul_size() for efficiency */
307 Size req;
308
310 req > (SIZE_MAX / 2)))
312 return palloc(req);
313}
void * palloc(Size size)

References fb(), mul_size_error(), palloc(), pg_mul_size_overflow(), s1, s2, and unlikely.

◆ palloc_mul_extended()

void * palloc_mul_extended ( Size  s1,
Size  s2,
int  flags 
)

Definition at line 338 of file fe_memutils.c.

339{
340 /* inline mul_size() for efficiency */
341 Size req;
342
344 req > (SIZE_MAX / 2)))
346 return palloc_extended(req, flags);
347}
void * palloc_extended(Size size, int flags)

References fb(), mul_size_error(), palloc_extended(), pg_mul_size_overflow(), s1, s2, and unlikely.

◆ pfree()

void pfree ( void pointer)

Definition at line 139 of file fe_memutils.c.

140{
141 pg_free(pointer);
142}
void pg_free(void *ptr)

References pg_free().

◆ pg_free()

void pg_free ( void ptr)

Definition at line 111 of file fe_memutils.c.

112{
113 free(ptr);
114}
#define free(a)

References free.

Referenced by _printTocEntry(), advanceConnectionState(), append_depends_on_extension(), appendQualifiedRelation(), avlFree(), bool_substitute_hook(), calculate_totals(), check_exec(), check_for_data_types_usage(), check_for_incompatible_polymorphics(), check_publisher(), checksum_file(), close_walfile(), copy_file_blocks(), copyFile(), create_script_for_old_cluster_deletion(), datapagemap_print(), dir_close(), dir_free(), dir_open_for_write(), do_connect(), do_watch(), dropDBs(), dumpFunc(), dumpRoleMembership(), dumpSequence(), EndCompressor(), EndCompressorNone(), FindStreamingStart(), free_archive_reader(), free_command(), free_db_and_rel_infos(), free_rel_infos(), free_retrieved_objects(), free_socket_set(), FreeSlruRead(), FreeSlruWrite(), get_control_data(), get_db_rel_and_slot_infos(), get_loadable_libraries(), get_publisher_databases(), get_restricted_token(), get_standby_sysid(), getTimelineHistory(), icu_language_tag(), init_libpq_conn(), IssueACLPerBlob(), IssueCommandPerBlob(), libpq_get_current_wal_insert_lsn(), listSchemas(), main(), make_rfile(), makeAlterConfigCommand(), modify_subscriber_sysid(), open_walfile(), parallel_exec_prog(), parallel_transfer_all_new_dbs(), parseAclItem(), parseCommandLine(), parseQuery(), perform_rewind(), pfree(), printCrosstab(), PrintResultInCrosstab(), process_queued_fetch_requests(), rankSort(), readCommandResponse(), ReadHead(), readMessageFromPipe(), ReceiveXlogStream(), reconstruct_from_incremental_file(), reindex_one_database(), restore_all_databases(), RestoreArchive(), retrieve_objects(), run_schedule(), SendQuery(), set_replication_progress(), setlocales(), setup_publisher(), SetVariable(), simple_oid_list_destroy(), simple_ptr_list_destroy(), simple_string_list_destroy(), SlruReadSwitchPageSlow(), SlruWriteSwitchPageSlow(), SortTocFromFile(), SyncVariables(), tar_close(), tar_free(), tar_open_for_write(), test_gb18030_page_multiple(), transfer_all_new_dbs(), upgrade_task_free(), upgrade_task_run(), vacuum_all_databases(), vacuum_one_database(), verify_tar_file(), WriteDataChunks(), and WriteToc().

◆ pg_malloc()

◆ pg_malloc0()

◆ pg_malloc0_mul()

void * pg_malloc0_mul ( Size  s1,
Size  s2 
)

Definition at line 256 of file fe_memutils.c.

257{
258 /* inline mul_size() for efficiency */
259 Size req;
260
262 req > (SIZE_MAX / 2)))
264 return pg_malloc0(req);
265}
void * pg_malloc0(size_t size)
Definition fe_memutils.c:59

References fb(), mul_size_error(), pg_malloc0(), pg_mul_size_overflow(), s1, s2, and unlikely.

◆ pg_malloc_extended()

void * pg_malloc_extended ( size_t  size,
int  flags 
)

Definition at line 65 of file fe_memutils.c.

66{
67 return pg_malloc_internal(size, flags);
68}

References pg_malloc_internal().

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

◆ pg_malloc_internal()

static void * pg_malloc_internal ( size_t  size,
int  flags 
)
inlinestatic

Definition at line 29 of file fe_memutils.c.

30{
31 void *tmp;
32
33 /* Avoid unportable behavior of malloc(0) */
34 if (size == 0)
35 size = 1;
36 tmp = malloc(size);
37 if (tmp == NULL)
38 {
39 if ((flags & MCXT_ALLOC_NO_OOM) == 0)
40 {
41 fprintf(stderr, _("out of memory\n"));
43 }
44 return NULL;
45 }
46
47 if ((flags & MCXT_ALLOC_ZERO) != 0)
48 MemSet(tmp, 0, size);
49 return tmp;
50}
#define MemSet(start, val, len)
Definition c.h:1107
#define MCXT_ALLOC_NO_OOM
Definition fe_memutils.h:29
#define malloc(a)

References _, EXIT_FAILURE, fb(), fprintf, malloc, MCXT_ALLOC_NO_OOM, MCXT_ALLOC_ZERO, and MemSet.

Referenced by palloc(), palloc0(), palloc_extended(), pg_malloc(), pg_malloc0(), and pg_malloc_extended().

◆ pg_malloc_mul()

void * pg_malloc_mul ( Size  s1,
Size  s2 
)

Definition at line 238 of file fe_memutils.c.

239{
240 /* inline mul_size() for efficiency */
241 Size req;
242
244 req > (SIZE_MAX / 2)))
246 return pg_malloc(req);
247}
void * pg_malloc(size_t size)
Definition fe_memutils.c:53

References fb(), mul_size_error(), pg_malloc(), pg_mul_size_overflow(), s1, s2, and unlikely.

◆ pg_malloc_mul_extended()

void * pg_malloc_mul_extended ( Size  s1,
Size  s2,
int  flags 
)

Definition at line 272 of file fe_memutils.c.

273{
274 /* inline mul_size() for efficiency */
275 Size req;
276
278 req > (SIZE_MAX / 2)))
280 return pg_malloc_extended(req, flags);
281}
void * pg_malloc_extended(size_t size, int flags)
Definition fe_memutils.c:65

References fb(), mul_size_error(), pg_malloc_extended(), pg_mul_size_overflow(), s1, s2, and unlikely.

◆ pg_realloc()

void * pg_realloc ( void ptr,
size_t  size 
)

Definition at line 71 of file fe_memutils.c.

72{
73 void *tmp;
74
75 /* Avoid unportable behavior of realloc(NULL, 0) */
76 if (ptr == NULL && size == 0)
77 size = 1;
78 tmp = realloc(ptr, size);
79 if (!tmp)
80 {
81 fprintf(stderr, _("out of memory\n"));
83 }
84 return tmp;
85}
#define realloc(a, b)

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

Referenced by addlitchar(), datapagemap_add(), exec_command_set(), icu_language_tag(), main(), pg_realloc_mul(), read_file_contents(), readMessageFromPipe(), repalloc(), and replaceVariable().

◆ pg_realloc_mul()

void * pg_realloc_mul ( void p,
Size  s1,
Size  s2 
)

Definition at line 288 of file fe_memutils.c.

289{
290 /* inline mul_size() for efficiency */
291 Size req;
292
294 req > (SIZE_MAX / 2)))
296 return pg_realloc(p, req);
297}
void * pg_realloc(void *ptr, size_t size)
Definition fe_memutils.c:71

References fb(), mul_size_error(), pg_mul_size_overflow(), pg_realloc(), s1, s2, and unlikely.

◆ pg_strdup()

char * pg_strdup ( const char in)

Definition at line 91 of file fe_memutils.c.

92{
93 char *tmp;
94
95 if (!in)
96 {
98 _("cannot duplicate null pointer (internal error)\n"));
100 }
101 tmp = strdup(in);
102 if (!tmp)
103 {
104 fprintf(stderr, _("out of memory\n"));
106 }
107 return tmp;
108}

References _, EXIT_FAILURE, fb(), and fprintf.

Referenced by _allocAH(), _ArchiveEntry(), _ArchiveEntry(), _becomeUser(), _check_database_version(), _getObjectDescription(), _selectOutputSchema(), _selectTableAccessMethod(), _selectTablespace(), _tarGetHeader(), add_one_elt(), add_stringlist_item(), add_stringlist_item(), adjust_data_dir(), adjust_data_dir(), append_depends_on_extension(), ArchiveEntry(), bool_substitute_hook(), check_locale_name(), check_publisher(), check_required_directory(), check_subscriber(), CloneArchive(), collectComments(), collectRoleNames(), collectSecLabels(), comp_keyword_case_substitute_hook(), concat_conninfo_dbname(), ConnectDatabaseAhx(), constructConnStr(), convertRegProcReference(), convertTSFunction(), create_logical_replication_slot(), create_script_for_old_cluster_deletion(), createBoundaryObjects(), CreateWalDirectoryMethod(), describeOneTableDetails(), describeRoles(), dir_open_for_write(), do_pset(), dumpAccessMethod(), dumpAttrDef(), dumpBaseType(), dumpCollation(), dumpCompositeType(), dumpConstraint(), 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(), dumpTableData(), dumpTableSchema(), dumpTablespaces(), dumpTrigger(), dumpTSConfig(), dumpTSDictionary(), dumpTSParser(), dumpTSTemplate(), dumpUndefinedType(), echo_substitute_hook(), encodingid_to_string(), exec_command_g(), exec_command_gset(), exec_command_password(), exec_command_set(), fetch_count_substitute_hook(), find_matching_ts_config(), FindDbnameInConnOpts(), fix_path_separator(), flagInhAttrs(), flagInhIndexes(), flagInhTables(), format_numeric_locale(), get_base_conninfo(), get_comma_elts(), get_conninfo_value(), get_control_data(), get_db_infos(), get_id(), get_language_name(), get_loadable_libraries(), get_opts(), get_restricted_token(), get_sock_dir(), get_sub_conninfo(), get_synchronized_snapshot(), get_tablespace_paths(), get_template0_info(), get_user_info(), getAccessMethods(), getAggregates(), getCollations(), getConstraints(), getConversions(), getDefaultACLs(), getDomainConstraints(), getEventTriggers(), getExtendedStatistics(), getExtensions(), getForeignDataWrappers(), getForeignServers(), getFormattedTypeName(), getFuncs(), getIndexes(), getLOs(), getNamespaces(), getOpclasses(), getOperators(), getOpfamilies(), getPolicies(), getProcLangs(), getPublications(), getPublicationTables(), getRelationStatistics(), getRules(), gets_fromFile(), getSubscriptionRelations(), getSubscriptions(), getTableAttrs(), getTables(), getTriggers(), getTSConfigurations(), getTSDictionaries(), getTSParsers(), getTSTemplates(), getTypes(), getVariable(), handle_args(), histcontrol_substitute_hook(), histsize_substitute_hook(), identify_target_directory(), ignoreeof_substitute_hook(), InitDiscoverCompressFileHandle(), initializeInput(), insert_filehash_entry(), IssueACLPerBlob(), IssueCommandPerBlob(), keepwal_add_entry(), listSchemas(), load_resultmap(), lookupCreateVariable(), main(), 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_old_cluster_logical_slot_infos(), process_rel_infos(), process_source_file(), process_target_file(), processEncodingEntry(), processExtensionTables(), processSearchPathEntry(), progress_update_filename(), psql_get_variable(), psqlscan_push_new_buffer(), pstrdup(), putVariable(), read_post_opts(), readfile(), ReconnectToServer(), regression_main(), RestoreArchive(), run_schedule(), run_simple_query(), RunIdentifySystem(), sanitize_line(), save_global_locale(), savePsetInfo(), search_directory(), sendCommand(), set_info_version(), setDecimalLocale(), setup(), setup_config(), setup_connection(), setup_pgdata(), setup_publisher(), SetVariable(), SetVariableHooks(), show_context_substitute_hook(), simple_action_list_append(), simple_prompt_extended(), split_path(), split_to_stringlist(), tar_open_for_write(), tarOpen(), verbosity_substitute_hook(), and watch_interval_substitute_hook().

◆ pnstrdup()

char * pnstrdup ( const char in,
Size  size 
)

Definition at line 151 of file fe_memutils.c.

152{
153 char *tmp;
154 int len;
155
156 if (!in)
157 {
159 _("cannot duplicate null pointer (internal error)\n"));
161 }
162
163 len = strnlen(in, size);
164 tmp = malloc(len + 1);
165 if (tmp == NULL)
166 {
167 fprintf(stderr, _("out of memory\n"));
169 }
170
171 memcpy(tmp, in, len);
172 tmp[len] = '\0';
173
174 return tmp;
175}
memcpy(sums, checksumBaseOffsets, sizeof(checksumBaseOffsets))
const void size_t len

References _, EXIT_FAILURE, fb(), fprintf, len, malloc, and memcpy().

◆ pstrdup()

char * pstrdup ( const char in)

Definition at line 145 of file fe_memutils.c.

146{
147 return pg_strdup(in);
148}
char * pg_strdup(const char *in)
Definition fe_memutils.c:91

References pg_strdup().

◆ repalloc()

void * repalloc ( void pointer,
Size  size 
)

Definition at line 178 of file fe_memutils.c.

179{
180 return pg_realloc(pointer, size);
181}

References pg_realloc().

Referenced by repalloc_mul().

◆ repalloc_mul()

void * repalloc_mul ( void p,
Size  s1,
Size  s2 
)

Definition at line 354 of file fe_memutils.c.

355{
356 /* inline mul_size() for efficiency */
357 Size req;
358
360 req > (SIZE_MAX / 2)))
362 return repalloc(p, req);
363}
void * repalloc(void *pointer, Size size)

References fb(), mul_size_error(), pg_mul_size_overflow(), repalloc(), s1, s2, and unlikely.