File: heap.c

package info (click to toggle)
html-xml-utils 7.7-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 2,488 kB
  • sloc: ansic: 11,213; sh: 7,996; lex: 243; makefile: 193; yacc: 125
file content (81 lines) | stat: -rw-r--r-- 2,127 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
/*
 * Some memory allocation routines, they call abort() in case of failure
 *
 * Part of HTML-XML-utils, see:
 * http://www.w3.org/Tools/HTML-XML-utils/
 *
 * Copyright © 1994-2003 World Wide Web Consortium
 * See http://www.w3.org/Consortium/Legal/copyright-software
 *
 * Author: Bert Bos <bert@w3.org>
 * Created: before 1995
 **/
#include "config.h"
#include <stdlib.h>
#include <stdio.h>
#if STDC_HEADERS
# include <string.h>
#else
# ifndef HAVE_STRCHR
#  define strchr index
#  define strrchr rindex
# endif
#endif
#include "export.h"

#ifdef __export
#undef __FILE__			/* Don't expand while making the .e file */
#undef __LINE__			/* Don't expand while making the .e file */
#endif

#define fatal(msg) fatal3(msg, __FILE__, __LINE__)
#define new(p) if (((p)=malloc(sizeof(*(p))))); else fatal("out of memory")
#define dispose(p) if (!(p)) ; else (free((void*)p), (p) = (void*)0) 
#define heapmax(p) 9999999 /* ? */
#define newstring(s) heap_newstring(s, __FILE__, __LINE__) 
#define newnstring(s,n) heap_newnstring(s, n, __FILE__, __LINE__) 
#define newarray(p,n) \
    if (((p)=malloc((n)*sizeof(*(p))))); else fatal("out of memory")
#define renewarray(p,n) \
    if (((p)=realloc(p,(n)*sizeof(*(p))))); else fatal("out of memory")

EXPORTDEF(fatal(msg))
EXPORTDEF(new(p))
EXPORTDEF(dispose(p))
EXPORTDEF(heapmax(p))
EXPORTDEF(newstring(s))
EXPORTDEF(newnstring(s,n))
EXPORTDEF(newarray(p,n))
EXPORTDEF(renewarray(p,n))


EXPORT void fatal3(const char *s, const char *file, const unsigned int line)
{
    fprintf(stderr, "%s (file %s, line %d)\n", s, file, line);
    abort();
}


EXPORT char * heap_newstring(const char *s, const char *file, const int line)
{
    char *t;

    if (!s) return NULL;
    t = malloc((strlen(s) + 1) * sizeof(*t));
    if (!t) fatal3("out of memory", file, line);
    strcpy(t, s);
    return t;
}

EXPORT char * heap_newnstring(const char *s, const size_t n,
			      const char *file, const int line)
{
    char *t;

    if (!s) return NULL;
    t = malloc((n + 1) * sizeof(*t));
    if (!t) fatal3("out of memory", file, line);
    strncpy(t, s, n);
    t[n] = '\0';
    return t;
}