[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
|
---|
[1054] | 27 | */
|
---|
[1104] | 28 | void mr_free_int(void **allocated, int line, const char *file) {
|
---|
[1054] | 29 |
|
---|
| 30 | /* free man pages says that if allocated is NULL
|
---|
| 31 | * nothing happens
|
---|
| 32 | */
|
---|
[1104] | 33 | if (*allocated != NULL) {
|
---|
| 34 | free(*allocated);
|
---|
| 35 | *allocated = NULL;
|
---|
| 36 | } else {
|
---|
[1133] | 37 | mr_msg_int(1,line,file,"Attempt to reference NULL pointer\nExiting...");
|
---|
[1104] | 38 | mr_exit(-1,"Attempt to reference NULL pointer");
|
---|
| 39 | }
|
---|
[1054] | 40 | }
|
---|
| 41 |
|
---|
| 42 | /* encapsulation function for malloc */
|
---|
[1104] | 43 | void *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 */
|
---|
[1138] | 56 | void mr_getline_int(char **lineptr, size_t *n, FILE *fd, int line, const char *file) {
|
---|
[1054] | 57 |
|
---|
| 58 | ssize_t ret;
|
---|
| 59 |
|
---|
[1138] | 60 | ret = getline(lineptr,n,fd);
|
---|
| 61 | if ((ret == -1) && (! feof(fd))) {
|
---|
[1133] | 62 | mr_msg_int(1,line,file,"Unable to alloc memory in mr_getline\nExiting...");
|
---|
[1104] | 63 | mr_exit(-1,"Unable to alloc memory in mr_getline");
|
---|
[1054] | 64 | }
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | /* encapsulation function for asprintf */
|
---|
[1104] | 68 | void mr_asprintf_int(char **strp, int line, const char *file, const char *fmt, ...) {
|
---|
[1054] | 69 |
|
---|
| 70 | int res = 0;
|
---|
[1064] | 71 | va_list args;
|
---|
[1054] | 72 |
|
---|
[1064] | 73 | va_start(args,fmt);
|
---|
[1054] | 74 | res = vasprintf(strp, fmt, args);
|
---|
| 75 | if (res == -1) {
|
---|
[1133] | 76 | mr_msg_int(1,line,file,"Unable to alloc memory in mr_asprintf\nExiting...");
|
---|
[1104] | 77 | mr_exit(-1,"Unable to alloc memory in mr_asprintf");
|
---|
[1054] | 78 | }
|
---|
[1064] | 79 | va_end(args);
|
---|
[1054] | 80 | }
|
---|
| 81 |
|
---|
| 82 | /*
|
---|
| 83 | * Function that properly allocates a string from another one
|
---|
| 84 | * freeing it before in any case
|
---|
| 85 | */
|
---|
[1104] | 86 | void mr_allocstr_int(char *alloc, const char *orig, int line, const char *file) {
|
---|
[1054] | 87 |
|
---|
[1174] | 88 | if (alloc != NULL) {
|
---|
| 89 | mr_free_int((void **)&alloc, line, file);
|
---|
| 90 | }
|
---|
[1104] | 91 | mr_asprintf_int(&alloc, line, file, orig);
|
---|
[1054] | 92 | }
|
---|
[1152] | 93 |
|
---|
| 94 | /*
|
---|
| 95 | * Function that properly put a variable in the environment
|
---|
| 96 | */
|
---|
| 97 | void mr_setenv_int(const char *name, const char *value, int line, char *file) {
|
---|
| 98 |
|
---|
| 99 | if (name == NULL) {
|
---|
| 100 | mr_msg_int(1,line,file,"Unable to setenv a NULL variable\nExiting...");
|
---|
| 101 | mr_exit(-1, "Unable to setenv a NULL variable");
|
---|
| 102 | }
|
---|
| 103 | if (value == NULL) {
|
---|
| 104 | mr_msg_int(1,line,file,"Unable to affect NULL to %s\nExiting...", name);
|
---|
| 105 | mr_exit(-1, "Unable to affect a NULL variable");
|
---|
| 106 | }
|
---|
[1156] | 107 | if (setenv(name, value, 1) != 0) {
|
---|
[1152] | 108 | mr_msg_int(1,line,file,"Unable to put %s in environment", name);
|
---|
| 109 | mr_exit(-1,"Unable to put in environment");
|
---|
| 110 | }
|
---|
| 111 | }
|
---|
[1178] | 112 |
|
---|
| 113 | /*
|
---|
| 114 | * Equivalent function of strcat but safe
|
---|
| 115 | * from memory allocation point of view
|
---|
| 116 | */
|
---|
| 117 | void mr_strcat_int(char **in, const char *add, int line, char *file) {
|
---|
| 118 | char *p =NULL;
|
---|
| 119 |
|
---|
| 120 | if (add == NULL) {
|
---|
| 121 | return;
|
---|
| 122 | }
|
---|
| 123 | if (in == NULL) {
|
---|
| 124 | mr_msg_int(1,line,file,"Unable to add %s to NULL pointer\nExiting...", add);
|
---|
| 125 | mr_exit(-1, "Unable to add to a NULL pointer");
|
---|
| 126 | }
|
---|
| 127 | if (*in == NULL) {
|
---|
| 128 | mr_asprintf_int(&p,line,file,add);
|
---|
| 129 | } else {
|
---|
| 130 | mr_asprintf_int(&p,line,file,"%s%s",*in,add);
|
---|
| 131 | mr_free_int((void **)in,line,file);
|
---|
| 132 | }
|
---|
| 133 | *in = p;
|
---|
| 134 | }
|
---|