source: MondoRescue/branches/2.2.10/mondo/src/lib/mr_mem.c@ 2322

Last change on this file since 2322 was 2322, checked in by Bruno Cornec, 15 years ago

r3333@localhost: bruno | 2009-08-08 01:58:31 +0200

  • bkpinfo->prefix is now dynamically assigned
  • Fix newt mondoarchive
  • Property svn:eol-style set to native
File size: 3.8 KB
RevLine 
[1054]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
[1064]12#ifndef _GNU_SOURCE
13#define _GNU_SOURCE
14#endif
15
[1054]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
[1104]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
[2241]27 * In case of NULL pointer it just logs it
[1054]28 */
[1104]29void mr_free_int(void **allocated, int line, const char *file) {
[1054]30
31 /* free man pages says that if allocated is NULL
32 * nothing happens
33 */
[1104]34 if (*allocated != NULL) {
35 free(*allocated);
36 *allocated = NULL;
37 } else {
[2241]38 mr_msg_int(8,line,file,"Attempt to free NULL pointer");
[1104]39 }
[1054]40}
41
42/* encapsulation function for malloc */
[1104]43void *mr_malloc_int(size_t size, int line, const char *file) {
[1054]44
45 void *ret;
46
47 ret = malloc(size);
48 if (ret == NULL) {
[1133]49 mr_msg_int(1,line,file,"Unable to alloc memory in mr_malloc\nExiting...");
[1104]50 mr_exit(-1,"Unable to alloc memory in mr_malloc");
[1054]51 }
52 return(ret);
53}
54
55/* encapsulation function for getline */
[2316]56void mr_getline_int(char **lineptr, FILE *fd, int line, const char *file) {
[1054]57
58 ssize_t ret;
[2322]59 size_t n = 0;
[1054]60
[2320]61 ret = getline(lineptr,&n,fd);
[1138]62 if ((ret == -1) && (! feof(fd))) {
[1133]63 mr_msg_int(1,line,file,"Unable to alloc memory in mr_getline\nExiting...");
[1104]64 mr_exit(-1,"Unable to alloc memory in mr_getline");
[1054]65 }
66}
67
68/* encapsulation function for asprintf */
[1104]69void mr_asprintf_int(char **strp, int line, const char *file, const char *fmt, ...) {
[1054]70
71 int res = 0;
[1064]72 va_list args;
[1054]73
[1064]74 va_start(args,fmt);
[1054]75 res = vasprintf(strp, fmt, args);
76 if (res == -1) {
[1133]77 mr_msg_int(1,line,file,"Unable to alloc memory in mr_asprintf\nExiting...");
[1104]78 mr_exit(-1,"Unable to alloc memory in mr_asprintf");
[1054]79 }
[1064]80 va_end(args);
[1054]81}
82
83/*
84 * Function that properly allocates a string from another one
85 * freeing it before in any case
86 */
[1104]87void mr_allocstr_int(char *alloc, const char *orig, int line, const char *file) {
[1054]88
[1174]89 if (alloc != NULL) {
90 mr_free_int((void **)&alloc, line, file);
91 }
[1104]92 mr_asprintf_int(&alloc, line, file, orig);
[1054]93}
[1152]94
95/*
96 * Function that properly put a variable in the environment
97 */
98void mr_setenv_int(const char *name, const char *value, int line, char *file) {
99
100 if (name == NULL) {
101 mr_msg_int(1,line,file,"Unable to setenv a NULL variable\nExiting...");
102 mr_exit(-1, "Unable to setenv a NULL variable");
103 }
104 if (value == NULL) {
105 mr_msg_int(1,line,file,"Unable to affect NULL to %s\nExiting...", name);
106 mr_exit(-1, "Unable to affect a NULL variable");
107 }
[1156]108 if (setenv(name, value, 1) != 0) {
[1152]109 mr_msg_int(1,line,file,"Unable to put %s in environment", name);
110 mr_exit(-1,"Unable to put in environment");
111 }
112}
[1178]113
114/*
115 * Equivalent function of strcat but safe
116 * from memory allocation point of view
[1196]117 * and richer from an interface point of view
[1178]118 */
[1196]119void mr_strcat_int(char **in, int line, const char *file, const char *fmt, ...) {
120 char *p = NULL;
121 char *fmt2 = NULL;
122 va_list args;
123 int res = 0;
[1178]124
[1196]125 if (fmt == NULL) {
[1178]126 return;
127 }
128 if (in == NULL) {
[1196]129 mr_msg_int(1,line,file,"Unable to add to NULL pointer\nExiting...");
[1178]130 mr_exit(-1, "Unable to add to a NULL pointer");
131 }
[1196]132 va_start(args,fmt);
[1178]133 if (*in == NULL) {
[1196]134 res = vasprintf(in, fmt, args);
135 if (res == -1) {
136 mr_msg_int(1,line,file,"Unable to alloc memory in mr_strcat\nExiting...");
137 mr_exit(-1,"Unable to alloc memory in mr_strcat");
138 }
[1178]139 } else {
[1196]140 mr_asprintf_int(&fmt2, line, file, "%s%s", *in, fmt);
[2307]141 mr_free_int((void **)in,line,file);
142 res = vasprintf(in, fmt2, args);
143 if (res == -1) {
144 mr_msg_int(1,line,file,"Unable to alloc memory in mr_strcat\nExiting...");
145 mr_exit(-1,"Unable to alloc memory in mr_strcat");
146 }
[1196]147 mr_free_int((void **)&fmt2,line,file);
[1178]148 }
[1196]149 va_end(args);
[1178]150}
[1196]151
152
Note: See TracBrowser for help on using the repository browser.