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

Last change on this file since 1054 was 1054, checked in by bruno, 13 years ago
  • Backporting more trunk content into stable
  • Adding the test and lib directories
  • Preparing for l10n
  • Property svn:eol-style set to native
File size: 1.4 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#include <stdio.h>
13#include <stdlib.h>
14#include <stdarg.h>
15
16#include "mr_err.h"
17#include "mr_msg.h"
18
19/*
20 * Function that frees memory if necessary
21 */
22void mr_free(void *allocated) {
23
24    /* free man pages says that if allocated is NULL
25     * nothing happens
26     */
27    free(allocated);
28    allocated = NULL;
29}
30
31/* encapsulation function for malloc */
32void *mr_malloc(size_t size) {
33   
34    void *ret;
35
36    ret = malloc(size);
37    if (ret == NULL) {
38        mr_log_exit(-1,"Unable to alloc memory in mr_malloc\nExiting...");
39    }
40    return(ret);
41}
42
43/* encapsulation function for getline */
44void mr_getline(char **lineptr, size_t *n, FILE *stream) {
45   
46    ssize_t ret;
47
48    ret = getline(lineptr,n,stream);
49    if (ret == -1) {
50        mr_log_exit(-1,"Unable to alloc memory in mr_getline\nExiting...");
51    }
52}
53
54/* encapsulation function for asprintf */
55void mr_asprintf(char **strp, const char *fmt, ...) {
56
57    va_list args;
58    int res = 0;
59
60    va_start(args, fmt);
61    res = vasprintf(strp, fmt, args);
62    if (res == -1) {
63        mr_log_exit(-1,"Unable to alloc memory in mr_asprintf\nExiting...");
64    }
65    va_end(args);
66}
67
68/*
69 * Function that properly allocates a string from another one
70 * freeing it before in any case
71 */
72void mr_allocstr(char *alloc, const char *orig) {
73
74    mr_free((void *)alloc); 
75    mr_asprintf(&alloc, orig);
76}
Note: See TracBrowser for help on using the repository browser.