aboutsummaryrefslogtreecommitdiff
path: root/src/pj_malloc.c
diff options
context:
space:
mode:
authorThomas Knudsen <lastname DOT firstname AT gmail DOT com>2016-04-01 23:10:44 +0200
committerThomas Knudsen <lastname DOT firstname AT gmail DOT com>2016-04-01 23:10:44 +0200
commit186c6e3303ccef8e833026e4e9dbaa76be6cb93b (patch)
treeb806475ab8896a3a9841cad737da0b8ee0d30859 /src/pj_malloc.c
parenta648ae934034924f15e1468b04bd986e007fd381 (diff)
downloadPROJ-186c6e3303ccef8e833026e4e9dbaa76be6cb93b.tar.gz
PROJ-186c6e3303ccef8e833026e4e9dbaa76be6cb93b.zip
First steps toward simplified macros/internals
The brief version:: In an attempt to make proj.4 code slightly more secure and much easier to read and maintain, I'm trying to eliminate a few unfortunate design decisions from the early days of proj.4 The work will be *very* intrusive, especially in the PJ_xxx segment of the code tree, but great care has been taken to design a process that can be implemented stepwise and localized, one projection at a time, then finalized with a relatively small and concentrated work package. The (very) long version: See the comments in PJ_minimal.c
Diffstat (limited to 'src/pj_malloc.c')
-rw-r--r--src/pj_malloc.c57
1 files changed, 51 insertions, 6 deletions
diff --git a/src/pj_malloc.c b/src/pj_malloc.c
index 80443a2b..aab69e99 100644
--- a/src/pj_malloc.c
+++ b/src/pj_malloc.c
@@ -9,19 +9,64 @@
pj_malloc(size_t size) {
/*
/ Currently, pj_malloc is a hack to solve an errno problem.
-/ The problem is described in more details at
-/ https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=86420.
-/ It seems, that pj_init and similar functions incorrectly
-/ (under debian/glibs-2.3.2) assume that pj_malloc resets
+/ The problem is described in more details at
+/ https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=86420.
+/ It seems, that pj_init and similar functions incorrectly
+/ (under debian/glibs-2.3.2) assume that pj_malloc resets
/ errno after success. pj_malloc tries to mimic this.
*/
int old_errno = errno;
- void *res = malloc(size);
+ void *res = malloc(size);
if ( res && !old_errno )
- errno = 0;
+ errno = 0;
return res;
}
void
pj_dalloc(void *ptr) {
free(ptr);
}
+
+
+/**********************************************************************/
+void *pj_calloc (size_t n, size_t size) {
+/***********************************************************************
+
+pj_calloc is the pj-equivalent of calloc().
+
+It allocates space for an array of <n> elements of size <size>.
+The array is initialized to zeros.
+
+***********************************************************************/
+ void *res = pj_malloc (n*size);
+ if (0==res)
+ return 0;
+ memset (res, 0, n*size);
+ return res;
+}
+
+
+/**********************************************************************/
+void *pj_dealloc (void *ptr) {
+/***********************************************************************
+
+pj_dealloc supports the common use case of "clean up and return a null
+pointer" to signal an error in a multi level allocation:
+
+ struct foo { int bar; int *baz; };
+
+ struct foo *p = pj_calloc (1, sizeof (struct foo));
+ if (0==p)
+ return 0;
+
+ p->baz = pj_calloc (10, sizeof(int));
+ if (0==p->baz)
+ return pj_dealloc (p); // clean up + signal error by 0-return
+
+ return p; // success
+
+***********************************************************************/
+ if (0==ptr)
+ return 0;
+ pj_dalloc (ptr);
+ return 0;
+}