source: branches/3.2/mondo/src/lib/mr_mem.c @ 3292

Last change on this file since 3292 was 3292, checked in by Bruno Cornec, 6 years ago
  • Remove from mindi 2 functions replaced by perl code (ReadAllLink? by mr-read-all-link and ProcessLDD by mr-process-ldd)
  • Redirect mindi execution to mondo log on the fly to try to improve percentage display with newt (not finished yet)
  • Introduce a new global var MINDI_LOGFILE and use it everywhere
  • Fix some memory free mising (thanks valgrind)
  • Remove a call to rpm to log packages - not portable
  • Works with valgrind locally to create images in dir
  • Property svn:eol-style set to native
File size: 4.1 KB
Line 
1/*
2 * $Id$
3 *
4 * Code (c)2006 Bruno Cornec <bruno@mondorescue.org>
5 *
6 *     Main file of mr_mem : a very small and simple
7 *     library for memory management
8 *
9 * Provided under the GPLv2
10 */
11
12#ifndef _GNU_SOURCE
13#define _GNU_SOURCE
14#endif
15
16#include <stdio.h>
17#include <stdlib.h>
18
19#include "mr_err.h"
20#include "mr_msg.h"
21
22/*
23 * Function that frees memory if necessary
24 * A pointer to the memory pointed is passed to it.
25 * *allocated variable points then to the original content
26 * pointed to by the caller
27 * In case of NULL pointer it just logs it
28 */
29void mr_free_int(void **allocated, int line, const char *file) {
30
31    /* free man pages says that if allocated is NULL
32     * nothing happens
33     */
34    if (*allocated != NULL) {
35        free(*allocated);
36        *allocated = NULL;
37    } else {
38        mr_msg_int(8,line,file,"Attempt to free NULL pointer");
39    }
40}
41
42/* encapsulation function for malloc */
43void *mr_malloc_int(size_t size, int line, const char *file) {
44   
45    void *ret;
46
47    ret = malloc(size);
48    if (ret == NULL) {
49        mr_msg_int(1,line,file,"Unable to alloc memory in mr_malloc\nExiting...");
50        mr_exit(-1,"Unable to alloc memory in mr_malloc");
51    }
52    return(ret);
53}
54
55/* encapsulation function for asprintf */
56void mr_asprintf_int(char **strp, int line, const char *file, const char *fmt, ...) {
57
58    int res = 0;
59    va_list args;
60
61    va_start(args,fmt);
62    res = vasprintf(strp, fmt, args);
63    if (res == -1) {
64        mr_msg_int(1,line,file,"Unable to alloc memory in mr_asprintf\nExiting...");
65        mr_exit(-1,"Unable to alloc memory in mr_asprintf");
66    }
67    va_end(args);
68}
69
70/* encapsulation function for getline */
71void mr_getline_int(char **lineptr, FILE *fd, int line, const char *file) {
72   
73    ssize_t ret;
74    size_t n = 0;
75
76    if (*lineptr != NULL) {
77        /* free a variable which should be already free */
78        mr_msg_int(1,line,file,"mr_getline_ found a non freed variable here");
79        mr_free_int(lineptr,line,file);
80    }
81    ret = getline(lineptr,&n,fd);
82    if ((ret == -1) && (! feof(fd))) {
83        mr_msg_int(1,line,file,"Unable to alloc memory in mr_getline\nExiting...");
84        mr_exit(-1,"Unable to alloc memory in mr_getline");
85    }
86    /*  We reached end of file, allocating empty string */
87    if (ret == -1) {
88        mr_free_int(lineptr,line,file);
89        mr_asprintf_int(lineptr,line,file,"");
90    }
91}
92
93/*
94 * Function that properly allocates a string from another one
95 * freeing it before in any case
96 */
97void mr_allocstr_int(char *alloc, const char *orig, int line, const char *file) {
98
99    if (alloc != NULL) {
100        mr_free_int((void **)&alloc, line, file); 
101    }
102    mr_asprintf_int(&alloc, line, file, orig);
103}
104
105/*
106 * Function that properly put a variable in the environment
107 */
108void mr_setenv_int(const char *name, const char *value, int line, char *file) {
109   
110    if (name == NULL) {
111        mr_msg_int(1,line,file,"Unable to setenv a NULL variable\nExiting...");
112        mr_exit(-1, "Unable to setenv a NULL variable");
113    }
114    if (value == NULL) {
115        mr_msg_int(1,line,file,"Unable to affect NULL to %s\nExiting...", name);
116        mr_exit(-1, "Unable to affect a NULL variable");
117    }
118    if (setenv(name, value, 1) != 0) {
119        mr_msg_int(1,line,file,"Unable to put %s in environment", name);
120        mr_exit(-1,"Unable to put in environment");
121    }
122}
123
124/*
125 * Equivalent function of strcat but safe
126 * from memory allocation point of view
127 * and richer from an interface point of view
128 */
129void mr_strcat_int(char **in, int line, const char *file, const char *fmt, ...) {
130    char *fmt2 = NULL;
131    va_list args;
132    int res = 0;
133   
134    if (fmt == NULL) {
135        return;
136    }
137    if (in == NULL) {
138        mr_msg_int(1,line,file,"Unable to add to NULL pointer\nExiting...");
139        mr_exit(-1, "Unable to add to a NULL pointer");
140    }
141    va_start(args,fmt);
142    if (*in == NULL) {
143        res = vasprintf(in, fmt, args);
144        if (res == -1) {
145            mr_msg_int(1,line,file,"Unable to alloc memory in mr_strcat\nExiting...");
146            mr_exit(-1,"Unable to alloc memory in mr_strcat");
147        }
148    } else {
149        mr_asprintf_int(&fmt2, line, file, "%s%s", *in, fmt);
150        mr_free_int((void **)in,line,file);
151        res = vasprintf(in, fmt2, args);
152        if (res == -1) {
153            mr_msg_int(1,line,file,"Unable to alloc memory in mr_strcat\nExiting...");
154            mr_exit(-1,"Unable to alloc memory in mr_strcat");
155        }
156        mr_free_int((void **)&fmt2,line,file);
157    }
158    va_end(args);
159}
160
161
Note: See TracBrowser for help on using the repository browser.