PostgreSQL Source Code git master
Loading...
Searching...
No Matches
util.c File Reference
#include "postgres_fe.h"
#include <unistd.h>
#include "preproc_extern.h"
Include dependency graph for util.c:

Go to the source code of this file.

Data Structures

struct  loc_chunk
 

Macros

#define LOC_CHUNK_OVERHEAD   MAXALIGN(offsetof(loc_chunk, data))
 
#define LOC_CHUNK_MIN_SIZE   8192
 

Typedefs

typedef struct loc_chunk loc_chunk
 

Functions

static void vmmerror (int error_code, enum errortype type, const char *error, va_list ap) pg_attribute_printf(3
 
void mmerror (int error_code, enum errortype type, const char *error,...)
 
void mmfatal (int error_code, const char *error,...)
 
voidmm_alloc (size_t size)
 
charmm_strdup (const char *string)
 
voidloc_alloc (size_t size)
 
charloc_strdup (const char *string)
 
void reclaim_local_storage (void)
 
charcat2_str (const char *str1, const char *str2)
 
charcat_str (int count,...)
 
charmake2_str (const char *str1, const char *str2)
 
charmake3_str (const char *str1, const char *str2, const char *str3)
 

Variables

static loc_chunkloc_chunks = NULL
 

Macro Definition Documentation

◆ LOC_CHUNK_MIN_SIZE

#define LOC_CHUNK_MIN_SIZE   8192

Definition at line 127 of file util.c.

◆ LOC_CHUNK_OVERHEAD

#define LOC_CHUNK_OVERHEAD   MAXALIGN(offsetof(loc_chunk, data))

Definition at line 126 of file util.c.

Typedef Documentation

◆ loc_chunk

Function Documentation

◆ cat2_str()

char * cat2_str ( const char str1,
const char str2 
)

Definition at line 205 of file util.c.

206{
207 char *res_str = (char *) loc_alloc(strlen(str1) + strlen(str2) + 2);
208
209 strcpy(res_str, str1);
210 if (strlen(str1) != 0 && strlen(str2) != 0)
211 strcat(res_str, " ");
212 strcat(res_str, str2);
213 return res_str;
214}
void * loc_alloc(size_t size)
Definition util.c:138
static int fb(int x)

References fb(), and loc_alloc().

Referenced by cat_str().

◆ cat_str()

char * cat_str ( int  count,
  ... 
)

Definition at line 220 of file util.c.

221{
223 int i;
224 char *res_str;
225
226 va_start(args, count);
227
228 res_str = va_arg(args, char *);
229
230 /* now add all other strings */
231 for (i = 1; i < count; i++)
232 res_str = cat2_str(res_str, va_arg(args, char *));
233
234 va_end(args);
235
236 return res_str;
237}
char * cat2_str(const char *str1, const char *str2)
Definition util.c:205
int i
Definition isn.c:77

References cat2_str(), fb(), and i.

◆ loc_alloc()

void * loc_alloc ( size_t  size)

Definition at line 138 of file util.c.

139{
140 void *result;
142
143 /* Ensure all allocations are adequately aligned */
144 size = MAXALIGN(size);
145
146 /* Need a new chunk? */
147 if (cur_chunk == NULL || size > cur_chunk->chunk_avail)
148 {
149 size_t chunk_size = Max(size, LOC_CHUNK_MIN_SIZE);
150
151 cur_chunk = mm_alloc(chunk_size + LOC_CHUNK_OVERHEAD);
152 /* Depending on alignment rules, we could waste a bit here */
154 cur_chunk->chunk_avail = chunk_size;
155 /* New chunk becomes the head of the list */
156 cur_chunk->next = loc_chunks;
158 }
159
160 result = cur_chunk->data + cur_chunk->chunk_used;
161 cur_chunk->chunk_used += size;
162 cur_chunk->chunk_avail -= size;
163 return result;
164}
#define MAXALIGN(LEN)
Definition c.h:826
#define Max(x, y)
Definition c.h:991
#define LOC_CHUNK_OVERHEAD
Definition util.c:126
static loc_chunk * loc_chunks
Definition util.c:130
void * mm_alloc(size_t size)
Definition util.c:85
#define LOC_CHUNK_MIN_SIZE
Definition util.c:127
const void * data
char data[FLEXIBLE_ARRAY_MEMBER]
Definition util.c:123

References loc_chunk::data, data, fb(), LOC_CHUNK_MIN_SIZE, LOC_CHUNK_OVERHEAD, loc_chunks, Max, MAXALIGN, and mm_alloc().

Referenced by cat2_str(), hashline_number(), loc_nstrdup(), loc_strdup(), make2_str(), make3_str(), and sqlda_variable().

◆ loc_strdup()

char * loc_strdup ( const char string)

Definition at line 170 of file util.c.

171{
172 char *result = loc_alloc(strlen(string) + 1);
173
174 strcpy(result, string);
175 return result;
176}

References fb(), and loc_alloc().

Referenced by base_yylex_location(), filtered_base_yylex(), process_integer_literal(), and sqlda_variable().

◆ make2_str()

char * make2_str ( const char str1,
const char str2 
)

Definition at line 243 of file util.c.

244{
245 char *res_str = (char *) loc_alloc(strlen(str1) + strlen(str2) + 1);
246
247 strcpy(res_str, str1);
248 strcat(res_str, str2);
249 return res_str;
250}

References fb(), and loc_alloc().

◆ make3_str()

char * make3_str ( const char str1,
const char str2,
const char str3 
)

Definition at line 256 of file util.c.

257{
258 char *res_str = (char *) loc_alloc(strlen(str1) + strlen(str2) + strlen(str3) + 1);
259
260 strcpy(res_str, str1);
261 strcat(res_str, str2);
263 return res_str;
264}

References fb(), and loc_alloc().

Referenced by filtered_base_yylex().

◆ mm_alloc()

void * mm_alloc ( size_t  size)

Definition at line 85 of file util.c.

86{
87 void *ptr = malloc(size);
88
89 if (ptr == NULL)
90 mmfatal(OUT_OF_MEMORY, "out of memory");
91
92 return ptr;
93}
void mmfatal(int error_code, const char *error,...)
Definition util.c:61
#define OUT_OF_MEMORY
#define malloc(a)

References fb(), malloc, mmfatal(), and OUT_OF_MEMORY.

Referenced by add_descriptor(), add_include_path(), add_preprocessor_define(), add_variable_to_head(), add_variable_to_tail(), ECPGdump_a_simple(), ECPGdump_a_struct(), ECPGmake_simple_type(), ECPGmake_struct_member(), isdefine(), isinformixdefine(), lex_init(), loc_alloc(), main(), new_variable(), parse_include(), and push_assignment().

◆ mm_strdup()

◆ mmerror()

void mmerror ( int  error_code,
enum errortype  type,
const char error,
  ... 
)

Definition at line 50 of file util.c.

51{
52 va_list ap;
53
56 va_end(ap);
57}
static void vmmerror(int error_code, enum errortype type, const char *error, va_list ap) pg_attribute_printf(3
Definition util.c:16
static void error(void)
const char * type

References error(), fb(), type, and vmmerror().

◆ mmfatal()

void mmfatal ( int  error_code,
const char error,
  ... 
)

Definition at line 61 of file util.c.

62{
63 va_list ap;
64
67 va_end(ap);
68
69 if (base_yyin)
71 if (base_yyout)
73
74 if (strcmp(output_filename, "-") != 0 && unlink(output_filename) != 0)
75 fprintf(stderr, _("could not remove output file \"%s\"\n"), output_filename);
77}
#define fprintf(file, fmt, msg)
Definition cubescan.l:21
char * output_filename
Definition ecpg.c:24
#define _(x)
Definition elog.c:91
FILE * base_yyin
FILE * base_yyout
@ ET_ERROR
Definition type.h:220

References _, base_yyin, base_yyout, error(), ET_ERROR, fb(), fprintf, output_filename, and vmmerror().

Referenced by mm_alloc(), and mm_strdup().

◆ reclaim_local_storage()

void reclaim_local_storage ( void  )

Definition at line 182 of file util.c.

183{
185 *next_chunk;
186
188 {
191 }
193}
#define free(a)
struct loc_chunk * next
Definition util.c:120

References fb(), free, loc_chunks, and loc_chunk::next.

◆ vmmerror()

static void static void vmmerror ( int  error_code,
enum errortype  type,
const char error,
va_list  ap 
)
static

Definition at line 16 of file util.c.

17{
18 /* localize the error message string */
19 error = _(error);
20
22
23 switch (type)
24 {
25 case ET_WARNING:
26 fprintf(stderr, _("WARNING: "));
27 break;
28 case ET_ERROR:
29 fprintf(stderr, _("ERROR: "));
30 break;
31 }
32
34
35 fprintf(stderr, "\n");
36
37 /* If appropriate, set error code to be inspected by ecpg.c */
38 switch (type)
39 {
40 case ET_WARNING:
41 break;
42 case ET_ERROR:
44 break;
45 }
46}
int ret_value
Definition ecpg.c:14
#define vfprintf
Definition port.h:263
int base_yylineno
char * input_filename
@ ET_WARNING
Definition type.h:220

References _, base_yylineno, error(), ET_ERROR, ET_WARNING, fb(), fprintf, input_filename, ret_value, type, and vfprintf.

Referenced by mmerror(), and mmfatal().

Variable Documentation

◆ loc_chunks

loc_chunk* loc_chunks = NULL
static

Definition at line 130 of file util.c.

Referenced by loc_alloc(), and reclaim_local_storage().