PostGIS  2.3.8dev-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 
108 struct rt_context_t {
115 };
116 
117 /* Static variable, to be used for all rt_core functions */
118 static struct rt_context_t ctx_t = {
120  .realloc = default_rt_reallocator,
121  .dealloc = default_rt_deallocator,
125 };
126 
127 
134 void
136 {
137  ctx_t.alloc = default_rt_allocator;
143 }
144 
145 
150 void
152  rt_deallocator deallocator, rt_message_handler error_handler,
153  rt_message_handler info_handler, rt_message_handler warning_handler) {
154 
155  ctx_t.alloc = allocator;
156  ctx_t.realloc = reallocator;
157  ctx_t.dealloc = deallocator;
158 
159  ctx_t.err = error_handler;
160  ctx_t.info = info_handler;
161  ctx_t.warn = warning_handler;
162 }
163 
164 
170 void *
171 rtalloc(size_t size) {
172  void * mem = ctx_t.alloc(size);
173  RASTER_DEBUGF(5, "rtalloc called: %d@%p", size, mem);
174  return mem;
175 }
176 
177 
178 void *
179 rtrealloc(void * mem, size_t size) {
180  void * result = ctx_t.realloc(mem, size);
181  RASTER_DEBUGF(5, "rtrealloc called: %d@%p", size, result);
182  return result;
183 }
184 
185 void
186 rtdealloc(void * mem) {
187  ctx_t.dealloc(mem);
188  RASTER_DEBUG(5, "rtdealloc called");
189 }
190 
198 void
199 rterror(const char *fmt, ...) {
200  va_list ap;
201 
202  va_start(ap, fmt);
203 
204  /* Call the supplied function */
205  (*ctx_t.err)(fmt, ap);
206 
207  va_end(ap);
208 }
209 
210 void
211 rtinfo(const char *fmt, ...) {
212  va_list ap;
213 
214  va_start(ap, fmt);
215 
216  /* Call the supplied function */
217  (*ctx_t.info)(fmt, ap);
218 
219  va_end(ap);
220 }
221 
222 
223 void
224 rtwarn(const char *fmt, ...) {
225  va_list ap;
226 
227  va_start(ap, fmt);
228 
229  /* Call the supplied function */
230  (*ctx_t.warn)(fmt, ap);
231 
232  va_end(ap);
233 }
void rtinfo(const char *fmt,...)
Definition: rt_context.c:211
void *(* rt_allocator)(size_t size)
Global functions for memory/logging handlers.
Definition: librtcore.h:241
void * rtrealloc(void *mem, size_t size)
Definition: rt_context.c:179
void * rtalloc(size_t size)
Raster core memory management functions.
Definition: rt_context.c:171
void default_rt_info_handler(const char *fmt, va_list ap)
Definition: rt_context.c:93
void *(* rt_reallocator)(void *mem, size_t size)
Definition: librtcore.h:242
void * default_rt_reallocator(void *mem, size_t size)
Definition: rt_context.c:54
void * default_rt_allocator(size_t size)
The default memory/logging handlers installed by lwgeom_install_default_allocators() ...
Definition: rt_context.c:47
void(* rt_message_handler)(const char *string, va_list ap) __attribute__((format(printf
Definition: librtcore.h:244
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:151
def fmt
Definition: pixval.py:92
void default_rt_deallocator(void *mem)
Definition: rt_context.c:61
void default_rt_warning_handler(const char *fmt, va_list ap)
Definition: rt_context.c:80
static struct rt_context_t ctx_t
Definition: rt_context.c:118
void rtdealloc(void *mem)
Definition: rt_context.c:186
rt_message_handler info
Definition: rt_context.c:114
rt_message_handler warn
Definition: rt_context.c:113
rt_reallocator realloc
Definition: rt_context.c:110
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:135
Struct definition here.
Definition: rt_context.c:108
void rterror(const char *fmt,...)
Raster core error and info handlers.
Definition: rt_context.c:199
rt_deallocator dealloc
Definition: rt_context.c:111
#define RASTER_DEBUGF(level, msg,...)
Definition: librtcore.h:311
rt_message_handler err
Definition: rt_context.c:112
void(* rt_deallocator)(void *mem)
Definition: librtcore.h:243
void rtwarn(const char *fmt,...)
Definition: rt_context.c:224
#define RASTER_DEBUG(level, msg)
Definition: librtcore.h:307
This library is the generic raster handling section of PostGIS.
rt_allocator alloc
Definition: rt_context.c:109
void free(void *)
void * malloc(YYSIZE_T)
void default_rt_error_handler(const char *fmt, va_list ap)
Definition: rt_context.c:67