source: MondoRescue/branches/stable/mondo/src/lib/mr_mem.c@ 1152

Last change on this file since 1152 was 1152, checked in by Bruno Cornec, 17 years ago

More memory updates around mondoarchive (addition of mr_setenv) (based on ideas sent as patches by Michel)

  • Property svn:eol-style set to native
File size: 2.7 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 */
28void mr_free_int(void **allocated, int line, const char *file) {
29
30 /* free man pages says that if allocated is NULL
31 * nothing happens
32 */
33 if (*allocated != NULL) {
34 free(*allocated);
35 *allocated = NULL;
36 } else {
37 mr_msg_int(1,line,file,"Attempt to reference NULL pointer\nExiting...");
38 mr_exit(-1,"Attempt to reference 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 getline */
56void mr_getline_int(char **lineptr, size_t *n, FILE *fd, int line, const char *file) {
57
58 ssize_t ret;
59
60 ret = getline(lineptr,n,fd);
61 if ((ret == -1) && (! feof(fd))) {
62 mr_msg_int(1,line,file,"Unable to alloc memory in mr_getline\nExiting...");
63 mr_exit(-1,"Unable to alloc memory in mr_getline");
64 }
65}
66
67/* encapsulation function for asprintf */
68void mr_asprintf_int(char **strp, int line, const char *file, const char *fmt, ...) {
69
70 int res = 0;
71 va_list args;
72
73 va_start(args,fmt);
74 res = vasprintf(strp, fmt, args);
75 if (res == -1) {
76 mr_msg_int(1,line,file,"Unable to alloc memory in mr_asprintf\nExiting...");
77 mr_exit(-1,"Unable to alloc memory in mr_asprintf");
78 }
79 va_end(args);
80}
81
82/*
83 * Function that properly allocates a string from another one
84 * freeing it before in any case
85 */
86void mr_allocstr_int(char *alloc, const char *orig, int line, const char *file) {
87
88 mr_free_int((void **)&alloc, line, file);
89 mr_asprintf_int(&alloc, line, file, orig);
90}
91
92/*
93 * Function that properly put a variable in the environment
94 */
95void mr_setenv_int(const char *name, const char *value, int line, char *file) {
96
97 if (name == NULL) {
98 mr_msg_int(1,line,file,"Unable to setenv a NULL variable\nExiting...");
99 mr_exit(-1, "Unable to setenv a NULL variable");
100 }
101 if (value == NULL) {
102 mr_msg_int(1,line,file,"Unable to affect NULL to %s\nExiting...", name);
103 mr_exit(-1, "Unable to affect a NULL variable");
104 }
105 if (mr_setenv(name, value, 1) != 0) {
106 mr_msg_int(1,line,file,"Unable to put %s in environment", name);
107 mr_exit(-1,"Unable to put in environment");
108 }
109}
Note: See TracBrowser for help on using the repository browser.