File: memory.c

package info (click to toggle)
mawk 1.3.4.20200120-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,916 kB
  • sloc: ansic: 16,165; sh: 3,515; yacc: 1,125; awk: 722; makefile: 251
file content (107 lines) | stat: -rw-r--r-- 1,939 bytes parent folder | download | duplicates (2)
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/********************************************
memory.c
copyright 2009,2010  Thomas E. Dickey
copyright 1991-1992,1993  Michael D. Brennan

This is a source file for mawk, an implementation of
the AWK programming language.

Mawk is distributed without warranty under the terms of
the GNU General Public License, version 2, 1991.
********************************************/

/*
 * $MawkId: memory.c,v 1.8 2010/12/10 17:00:00 tom Exp $
 * @Log: memory.c,v @
 * Revision 1.2  1993/07/17  13:23:08  mike
 * indent and general code cleanup
 *
 * Revision 1.1.1.1  1993/07/03	 18:58:17  mike
 * move source to cvs
 *
 * Revision 5.2	 1993/01/01  21:30:48  mike
 * split new_STRING() into new_STRING and new_STRING0
 *
 * Revision 5.1	 1991/12/05  07:56:21  brennan
 * 1.1 pre-release
 *
 */

/* memory.c */

#include "mawk.h"
#include "memory.h"

STRING null_str =
{0, 1, ""};

static STRING *
xnew_STRING(size_t len)
{
    STRING *sval = (STRING *) zmalloc(len + STRING_OH);

    sval->len = len;
    sval->ref_cnt = 1;
    return sval;
}

/* allocate space for a STRING */

STRING *
new_STRING0(size_t len)
{
    if (len == 0) {
	null_str.ref_cnt++;
	return &null_str;
    } else {
	STRING *sval = xnew_STRING(len);
	sval->str[len] = 0;
	return sval;
    }
}

/*
 * Create a new string which may contain embedded nulls.
 */
STRING *
new_STRING1(const char *s, size_t len)
{
    if (len == 0) {
	null_str.ref_cnt++;
	return &null_str;
    } else {
	STRING *sval = xnew_STRING(len);
	memcpy(sval->str, s, len);
	sval->str[len] = 0;
	return sval;
    }
}

/* convert char* to STRING* */

STRING *
new_STRING(const char *s)
{

    if (s[0] == 0) {
	null_str.ref_cnt++;
	return &null_str;
    } else {
	STRING *sval = xnew_STRING(strlen(s));
	strcpy(sval->str, s);
	return sval;
    }
}

#ifdef	 DEBUG

void
DB_free_STRING(STRING * sval)
{
    if (--sval->ref_cnt == 0 &&
	sval != &null_str) {
	zfree(sval, sval->len + STRING_OH);
    }
}

#endif