Changeset 1104 in MondoRescue


Ignore:
Timestamp:
Feb 7, 2007, 8:00:23 PM (17 years ago)
Author:
Bruno Cornec
Message:

Improvement of the low level library (Usage of LINE and FILE and simplified interfaces using macros)

Location:
branches/stable/mondo/src
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • branches/stable/mondo/src/include/mr_err.h

    r1064 r1104  
    99#define MR_ERR_H
    1010
     11#include "mr_msg.h"
     12
    1113/* functions (public methods) */
    1214
    1315extern inline void mr_exit(int errorcode, const char *message);
    14 extern inline void mr_log_exit(int errorcode, const char *message);
     16#define mr_log_exit(x,y) {mr_msg(0,y); mr_exit(x, y);}
    1517
    1618#endif                          /* MR_ERR_H */
  • branches/stable/mondo/src/include/mr_mem.h

    r1096 r1104  
    1717/* functions (public methods) */
    1818
    19 extern inline void mr_free(char *allocated);
    20 extern inline void mr_allocstr(char *alloc, const char *orig);
    21 extern inline void mr_asprintf(char **alloc, const char *fmt, ...);
    22 extern inline void mr_getline(char **lineptr, size_t *n, FILE *stream);
    23 extern inline void *mr_malloc(size_t size);
     19#define mr_free(x) mr_free_int((void **)&x,__LINE__,__FILE__)
     20#define mr_allocstr(x,y) mr_allocstr_int(x, y,__LINE__,__FILE__)
     21#define mr_asprintf(x,y,args...) mr_asprintf_int(x,__LINE__,__FILE__, y, ## args)
     22#define mr_getline(x,y,z) mr_getline_int(x, y,z,__LINE__,__FILE__)
     23#define mr_malloc(x) mr_malloc_int((size_t)x,__LINE__,__FILE__)
     24
     25/* Internal function bringing debuging info
     26 * called indirectly through macros */
     27extern inline void mr_free_int(void **allocated, int line, char *file);
     28extern inline void mr_allocstr_int(char *alloc, const char *orig, int line, char *file);
     29extern inline void mr_asprintf_int(char **alloc, int line, char *file, const char *fmt, ...);
     30extern inline void mr_getline_int(char **lineptr, size_t *n, FILE *stream, int line, char *file);
     31extern inline void *mr_malloc_int(size_t size, int line, char *file);
    2432
    2533#endif                          /* MR_MEM_H */
  • branches/stable/mondo/src/include/mr_msg.h

    r1064 r1104  
    1616/* functions (public methods) */
    1717
    18 extern inline void mr_msg(int debug, const char *fmt, ...);
     18#define mr_msg(x,y,args...) {mr_msg_int(x,__LINE__,__FILE__,y,## args);}
    1919extern void mr_msg_init(const char *configfile, int loglevel);
    2020extern void mr_msg_close(void);
    2121
     22/* Internal function bringing debuging info
     23 * called indirectly through macros */
     24extern inline void mr_msg_int(int debug,int line, const char *file, const char *fmt, ...);
     25
    2226#endif                          /* MR_MSG_H */
  • branches/stable/mondo/src/lib/mr_err.c

    r1064 r1104  
    2727        /* We have to properly end newt */
    2828        /* We have to remind people of log files */
     29        mr_msg_close();
    2930}
    3031
     
    3839    exit(errorcode);
    3940}
    40 
    41 void mr_log_exit(int errorcode, const char *message) {
    42     mr_msg(0, message);
    43     mr_exit(errorcode, message);
    44 }
  • branches/stable/mondo/src/lib/mr_mem.c

    r1064 r1104  
    2222/*
    2323 * 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
    2427 */
    25 void mr_free(void *allocated) {
     28void mr_free_int(void **allocated, int line, const char *file) {
    2629
    2730    /* free man pages says that if allocated is NULL
    2831     * nothing happens
    2932     */
    30     free(allocated);
    31     allocated = NULL;
     33    if (*allocated != NULL) {
     34        free(*allocated);
     35        *allocated = NULL;
     36    } else {
     37        mr_msg_int(0,line,file,"Attempt to reference NULL pointer\nExiting...");
     38        mr_exit(-1,"Attempt to reference NULL pointer");
     39    }
    3240}
    3341
    3442/* encapsulation function for malloc */
    35 void *mr_malloc(size_t size) {
     43void *mr_malloc_int(size_t size, int line, const char *file) {
    3644   
    3745    void *ret;
     
    3947    ret = malloc(size);
    4048    if (ret == NULL) {
    41         mr_log_exit(-1,"Unable to alloc memory in mr_malloc\nExiting...");
     49        mr_msg_int(0,line,file,"Unable to alloc memory in mr_malloc\nExiting...");
     50        mr_exit(-1,"Unable to alloc memory in mr_malloc");
    4251    }
    4352    return(ret);
     
    4554
    4655/* encapsulation function for getline */
    47 void mr_getline(char **lineptr, size_t *n, FILE *stream) {
     56void mr_getline_int(char **lineptr, size_t *n, FILE *stream, int line, const char *file) {
    4857   
    4958    ssize_t ret;
     
    5160    ret = getline(lineptr,n,stream);
    5261    if (ret == -1) {
    53         mr_log_exit(-1,"Unable to alloc memory in mr_getline\nExiting...");
     62        mr_msg_int(0,line,file,"Unable to alloc memory in mr_getline\nExiting...",line,file);
     63        mr_exit(-1,"Unable to alloc memory in mr_getline");
    5464    }
    5565}
    5666
    5767/* encapsulation function for asprintf */
    58 void mr_asprintf(char **strp, const char *fmt, ...) {
     68void mr_asprintf_int(char **strp, int line, const char *file, const char *fmt, ...) {
    5969
    6070    int res = 0;
     
    6474    res = vasprintf(strp, fmt, args);
    6575    if (res == -1) {
    66         mr_log_exit(-1,"Unable to alloc memory in mr_asprintf\nExiting...");
     76        mr_msg_int(0,line,file,"Unable to alloc memory in mr_asprintf\nExiting...",line,file);
     77        mr_exit(-1,"Unable to alloc memory in mr_asprintf");
    6778    }
    6879    va_end(args);
     
    7384 * freeing it before in any case
    7485 */
    75 void mr_allocstr(char *alloc, const char *orig) {
     86void mr_allocstr_int(char *alloc, const char *orig, int line, const char *file) {
    7687
    77     mr_free((void *)alloc);
    78     mr_asprintf(&alloc, orig);
     88    mr_free_int((void **)&alloc, line, file);
     89    mr_asprintf_int(&alloc, line, file, orig);
    7990}
  • branches/stable/mondo/src/lib/mr_msg.c

    r1065 r1104  
    5252 * Function that log a message. Not called directly
    5353 * but through other functions
     54 * fmt needs to be just before ...
    5455 */
    55 void mr_msg(int debug, const char *fmt, ...) {
     56void mr_msg_int(int debug, int line, const char *file, const char *fmt, ...) {
    5657
    5758    int i = 0;
     
    7475            for (i = 1; i < debug; i++)
    7576                fprintf(fout, "  ");
    76             fprintf(fout, "%s->%s#%d: ", __FILE__, __FUNCTION__, __LINE__);
     77            fprintf(fout, "%s #%d: ", file, line);
    7778        }
    7879        va_start(args,fmt);
  • branches/stable/mondo/src/test/mktest

    r1065 r1104  
    77
    88lib="../lib/mr_conf.c ../lib/mr_msg.c ../lib/mr_err.c ../lib/mr_mem.c"
    9 OPT="-Wall -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_REENTRANT -Wstrict-prototypes -Wshadow -funsigned-char -Wunused -Winit-self -Wcast-align -O2 -g -I../common -I../include"
     9OPT="-Wall -D_GNU_SOURCE -D_FILE_OFFSET_BITS=64 -D_LARGEFILE_SOURCE -D_LARGEFILE64_SOURCE -D_REENTRANT -Wstrict-prototypes -Wshadow -funsigned-char -Wunused -Winit-self -Wcast-align -fno-strict-aliasing -O2 -g -I../common -I../include"
    1010
    1111echo "Generating test-msg"
     
    1515echo "Generating test-conf"
    1616gcc $OPT test-conf.c $lib -o test-conf
     17echo "Generating test-mem"
     18gcc $OPT test-mem.c $lib -o test-mem
    1719
    1820echo "Testing against previous run"
    19 for f in test-conf test-string test-msg; do
     21for f in test-conf test-string test-msg test-mem; do
    2022    chmod 755 $f
    2123    ./$f > /tmp/$f.res
     
    2325    if [ $? -ne 0 ]; then
    2426        echo "$f test KO !!"
     27    else
     28        echo "$f test OK"
    2529    fi
    2630    valgrind -q --error-exitcode=1 --leak-check=yes ./$f 2>&1 > /tmp/valgrind-$f.res
Note: See TracChangeset for help on using the changeset viewer.