PostGIS  3.4.0dev-r@@SVN_REVISION@@
rt_context.c
Go to the documentation of this file.
1 /*
2  *
3  * WKTRaster - Raster Types for PostGIS
4  * http://trac.osgeo.org/postgis/wiki/WKTRaster
5  *
6  * Copyright (C) 2011-2013 Regents of the University of California
7  * <bkpark@ucdavis.edu>
8  * Copyright (C) 2010-2011 Jorge Arevalo <jorge.arevalo@deimos-space.com>
9  * Copyright (C) 2010-2011 David Zwarg <dzwarg@azavea.com>
10  * Copyright (C) 2009-2011 Pierre Racine <pierre.racine@sbf.ulaval.ca>
11  * Copyright (C) 2009-2011 Mateusz Loskot <mateusz@loskot.net>
12  * Copyright (C) 2008-2009 Sandro Santilli <strk@kbt.io>
13  *
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License
16  * as published by the Free Software Foundation; either version 2
17  * of the License, or (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software Foundation,
26  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
27  *
28  */
29 
30 #include <stdarg.h> /* for va_list, va_start etc */
31 
32 #include "librtcore.h"
33 #include "librtcore_internal.h"
34 
35 /******************************************************************************
36 * rt_context
37 ******************************************************************************/
38 
39 /*
40  * Default allocators
41  *
42  * We include some default allocators that use malloc/free/realloc
43  * along with stdout/stderr since this is the most common use case
44  *
45  */
46 void *
48 {
49  void *mem = malloc(size);
50  return mem;
51 }
52 
53 void *
54 default_rt_reallocator(void *mem, size_t size)
55 {
56  void *ret = realloc(mem, size);
57  return ret;
58 }
59 
60 void
62 {
63  free(mem);
64 }
65 
66 void
67 default_rt_error_handler(const char *fmt, va_list ap) {
68 
69  static const char *label = "ERROR: ";
70  char newfmt[1024] = {0};
71  snprintf(newfmt, 1024, "%s%s\n", label, fmt);
72  newfmt[1023] = '\0';
73 
74  vprintf(newfmt, ap);
75 
76  va_end(ap);
77 }
78 
79 void
80 default_rt_warning_handler(const char *fmt, va_list ap) {
81 
82  static const char *label = "WARNING: ";
83  char newfmt[1024] = {0};
84  snprintf(newfmt, 1024, "%s%s\n", label, fmt);
85  newfmt[1023] = '\0';
86 
87  vprintf(newfmt, ap);
88 
89  va_end(ap);
90 }
91 
92 void
93 default_rt_info_handler(const char *fmt, va_list ap) {
94 
95  static const char *label = "INFO: ";
96  char newfmt[1024] = {0};
97  snprintf(newfmt, 1024, "%s%s\n", label, fmt);
98  newfmt[1023] = '\0';
99 
100  vprintf(newfmt, ap);
101 
102  va_end(ap);
103 }
104 
105 char * default_rt_options(const char* varname) {
106  return NULL;
107 }
108 
109 
113 struct rt_context_t {
121 };
122 
123 /* Static variable, to be used for all rt_core functions */
124 static struct rt_context_t ctx_t = {
126  .realloc = default_rt_reallocator,
127  .dealloc = default_rt_deallocator,
130  .info = default_rt_info_handler,
131  .options = default_rt_options
132 };
133 
134 
141 void
143 {
151 }
152 
153 
158 void
160  rt_deallocator deallocator, rt_message_handler error_handler,
161  rt_message_handler info_handler, rt_message_handler warning_handler)
162 {
163  rt_set_handlers_options(allocator, reallocator, deallocator,
164  error_handler, info_handler, warning_handler,
166 }
167 
168 void
170  rt_deallocator deallocator, rt_message_handler error_handler,
171  rt_message_handler info_handler, rt_message_handler warning_handler,
172  rt_options options_handler)
173 {
174  ctx_t.alloc = allocator;
175  ctx_t.realloc = reallocator;
176  ctx_t.dealloc = deallocator;
177 
178  ctx_t.err = error_handler;
179  ctx_t.info = info_handler;
180  ctx_t.warn = warning_handler;
181 
182  ctx_t.options = options_handler;
183 }
184 
190 void *
191 rtalloc(size_t size) {
192  void * mem = ctx_t.alloc(size);
193  RASTER_DEBUGF(5, "rtalloc called: %d@%p", size, mem);
194  return mem;
195 }
196 
197 
198 void *
199 rtrealloc(void * mem, size_t size) {
200  void * result = ctx_t.realloc(mem, size);
201  RASTER_DEBUGF(5, "rtrealloc called: %d@%p", size, result);
202  return result;
203 }
204 
205 void
206 rtdealloc(void * mem) {
207  ctx_t.dealloc(mem);
208  RASTER_DEBUG(5, "rtdealloc called");
209 }
210 
218 void
219 rterror(const char *fmt, ...) {
220  va_list ap;
221 
222  va_start(ap, fmt);
223 
224  /* Call the supplied function */
225  (*ctx_t.err)(fmt, ap);
226 
227  va_end(ap);
228 }
229 
230 void
231 rtinfo(const char *fmt, ...) {
232  va_list ap;
233 
234  va_start(ap, fmt);
235 
236  /* Call the supplied function */
237  (*ctx_t.info)(fmt, ap);
238 
239  va_end(ap);
240 }
241 
242 
243 void
244 rtwarn(const char *fmt, ...) {
245  va_list ap;
246 
247  va_start(ap, fmt);
248 
249  /* Call the supplied function */
250  (*ctx_t.warn)(fmt, ap);
251 
252  va_end(ap);
253 }
254 
255 char *
256 rtoptions(const char *varname) {
257  if (!ctx_t.options)
258  return NULL;
259  return ctx_t.options(varname);
260 }
261 
262 char *
263 rtstrdup(const char *str) {
264  size_t sz;
265  char* dup;
266  if (!str) return NULL;
267  sz = strlen(str) + 1;
268  dup = rtalloc(sz);
269  memcpy(dup, str, sz);
270  return dup;
271 }
char result[OUT_DOUBLE_BUFFER_SIZE]
Definition: cu_print.c:262
void(* rt_message_handler)(const char *string, va_list ap) __attribute__((format(printf
Definition: librtcore.h:235
#define RASTER_DEBUG(level, msg)
Definition: librtcore.h:302
#define RASTER_DEBUGF(level, msg,...)
Definition: librtcore.h:306
void(* rt_deallocator)(void *mem)
Definition: librtcore.h:234
void *(* rt_allocator)(size_t size)
Definition: librtcore.h:232
char *(* rt_options)(const char *varname)
Global functions for memory/logging handlers.
Definition: librtcore.h:231
void *(* rt_reallocator)(void *mem, size_t size)
Definition: librtcore.h:233
This library is the generic raster handling section of PostGIS.
#define str(s)
void * malloc(YYSIZE_T)
void free(void *)
def fmt
Definition: pixval.py:93
void * default_rt_allocator(size_t size)
The default memory/logging handlers installed by lwgeom_install_default_allocators()
Definition: rt_context.c:47
void rterror(const char *fmt,...)
Raster core error and info handlers.
Definition: rt_context.c:219
void default_rt_error_handler(const char *fmt, va_list ap)
Definition: rt_context.c:67
void rtinfo(const char *fmt,...)
Definition: rt_context.c:231
char * rtoptions(const char *varname)
Wrappers used for options.
Definition: rt_context.c:256
void * rtalloc(size_t size)
Raster core memory management functions.
Definition: rt_context.c:191
void default_rt_deallocator(void *mem)
Definition: rt_context.c:61
void rtwarn(const char *fmt,...)
Definition: rt_context.c:244
void rt_install_default_allocators(void)
Useful in raster core testing and in the (future) loader, when we need to use raster core functions b...
Definition: rt_context.c:142
static struct rt_context_t ctx_t
Definition: rt_context.c:124
void * rtrealloc(void *mem, size_t size)
Definition: rt_context.c:199
char * default_rt_options(const char *varname)
Definition: rt_context.c:105
void rtdealloc(void *mem)
Definition: rt_context.c:206
void rt_set_handlers_options(rt_allocator allocator, rt_reallocator reallocator, rt_deallocator deallocator, rt_message_handler error_handler, rt_message_handler info_handler, rt_message_handler warning_handler, rt_options options_handler)
Definition: rt_context.c:169
void default_rt_warning_handler(const char *fmt, va_list ap)
Definition: rt_context.c:80
char * rtstrdup(const char *str)
Definition: rt_context.c:263
void default_rt_info_handler(const char *fmt, va_list ap)
Definition: rt_context.c:93
void * default_rt_reallocator(void *mem, size_t size)
Definition: rt_context.c:54
void rt_set_handlers(rt_allocator allocator, rt_reallocator reallocator, rt_deallocator deallocator, rt_message_handler error_handler, rt_message_handler info_handler, rt_message_handler warning_handler)
This function is called when the PostgreSQL backend is taking care of the memory and we want to use p...
Definition: rt_context.c:159
rt_message_handler warn
Definition: rt_context.c:118
rt_message_handler info
Definition: rt_context.c:119
rt_allocator alloc
Definition: rt_context.c:114
rt_message_handler err
Definition: rt_context.c:117
rt_reallocator realloc
Definition: rt_context.c:115
rt_options options
Definition: rt_context.c:120
rt_deallocator dealloc
Definition: rt_context.c:116
Struct definition here.
Definition: rt_context.c:113