File: mymalloc.c

package info (click to toggle)
mmorph 2.3.4.2-4
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 912 kB
  • ctags: 904
  • sloc: ansic: 4,992; yacc: 1,215; lex: 417; makefile: 296; sh: 48; sed: 33; csh: 26
file content (93 lines) | stat: -rw-r--r-- 1,776 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
    mmorph, MULTEXT morphology tool
    Version 2.3, October 1995
    Copyright (c) 1994,1995 ISSCO/SUISSETRA, Geneva, Switzerland
    Dominique Petitpierre, <petitp@divsun.unige.ch>
*/
/*
    mymalloc.c

    convenient wrappers around memory allocation functions
*/

#include <string.h>
#ifndef  MALLOC_FUNC_CHECK
#include <malloc.h>
#include <errno.h>
#include "mymalloc.h"
extern int  sys_nerr;
extern char *sys_errlist[];

/* extern char *strdup();*/

extern void fatal_error();

#define MEM_ERROR(f_name) \
	if (errno == ENOMEM) \
	    fatal_error("out of memory (%s)",f_name); \
	else \
	    fatal_error("program bug: memory allocation error (%s)",f_name)

void
my_malloc(var, size)
T_PTR      *var;
size_t      size;

{
    if ((*var = malloc(size)) == NULL)
	MEM_ERROR("malloc");
}

void
my_realloc(var, size)
T_PTR      *var;
size_t      size;

{
    if ((*var = realloc(*var, size)) == NULL)
	MEM_ERROR("realloc");
}


void
my_calloc(var, nelem, size)
T_PTR      *var;
size_t      nelem;
size_t      size;

{
    if ((*var = calloc(nelem, size)) == NULL)
	MEM_ERROR("calloc");
}

void
my_strdup(var, str)
char      **var;
char       *str;

{
    if ((*var = strdup(str)) == NULL)
	MEM_ERROR("strdup");
}

void
my_free(ptr)
T_PTR       ptr;

{
    if (ptr == NULL)
	fatal_error("program bug: trying to free a NULL pointer");
    errno = 0;	/* clear error */
#if defined(STDC_HEADERS) || defined(__GNUC__) || lint
    free(ptr);
    if (errno != 0)
	fatal_error("program bug: cannot free pointer (errno=%d=%s)",
		    errno, (errno < sys_nerr ? sys_errlist[errno] : "?"));
#else
    if (free(ptr) != 1)
	fatal_error("program bug: cannot free pointer (errno=%d)",
		    errno, (errno < sys_nerr ? sys_errlist[errno] : "?"));
#endif
}

#endif	/* MALLOC_FUNC_CHECK */