Git init
[external/mawk.git] / memory.c
1
2 /********************************************
3 memory.c
4 copyright 1991, 1992  Michael D. Brennan
5
6 This is a source file for mawk, an implementation of
7 the AWK programming language.
8
9 Mawk is distributed without warranty under the terms of
10 the GNU General Public License, version 2, 1991.
11 ********************************************/
12
13
14 /* $Log: memory.c,v $
15  * Revision 1.2  1993/07/17  13:23:08  mike
16  * indent and general code cleanup
17  *
18  * Revision 1.1.1.1  1993/07/03  18:58:17  mike
19  * move source to cvs
20  *
21  * Revision 5.2  1993/01/01  21:30:48  mike
22  * split new_STRING() into new_STRING and new_STRING0
23  *
24  * Revision 5.1  1991/12/05  07:56:21  brennan
25  * 1.1 pre-release
26  *
27 */
28
29
30 /* memory.c */
31
32 #include "mawk.h"
33 #include "memory.h"
34
35 static STRING *PROTO(xnew_STRING, (unsigned)) ;
36
37
38 STRING null_str =
39 {0, 1, ""} ;
40
41 static STRING *
42 xnew_STRING(len)
43    unsigned len ;
44 {
45    STRING *sval = (STRING *) zmalloc(len + STRING_OH) ;
46
47    sval->len = len ;
48    sval->ref_cnt = 1 ;
49    return sval ;
50 }
51
52 /* allocate space for a STRING */
53
54 STRING *
55 new_STRING0(len)
56    unsigned len ;
57 {
58    if (len == 0)
59    {
60       null_str.ref_cnt++ ;
61       return &null_str ;
62    }
63    else
64    {
65       STRING *sval = xnew_STRING(len) ;
66       sval->str[len] = 0 ;
67       return sval ;
68    }
69 }
70
71 /* convert char* to STRING* */
72
73 STRING *
74 new_STRING(s)
75    char *s ;
76 {
77
78    if (s[0] == 0)
79    {
80       null_str.ref_cnt++ ;
81       return &null_str ;
82    }
83    else
84    {
85       STRING *sval = xnew_STRING(strlen(s)) ;
86       strcpy(sval->str, s) ;
87       return sval ;
88    }
89 }
90
91
92 #ifdef   DEBUG
93
94 void
95 DB_free_STRING(sval)
96    register STRING *sval ;
97 {
98    if (--sval->ref_cnt == 0)  zfree(sval, sval->len + STRING_OH) ;
99 }
100
101 #endif