aboutsummaryrefslogtreecommitdiff
path: root/src/pj_malloc.c
blob: aab69e9917f38993bda4196630446425028e9e85 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/* allocate and deallocate memory */
/* These routines are used so that applications can readily replace
** projection system memory allocation/deallocation call with custom
** application procedures.  */
#include <projects.h>
#include <errno.h>

	void *
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
/ errno after success. pj_malloc tries to mimic this.
*/
        int old_errno = errno;
        void *res = malloc(size);
        if ( res && !old_errno )
                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;
}