386BSD 0.1 development
[unix-history] / usr / src / usr.bin / awk / memory.c
CommitLineData
a7e60862
WJ
1
2/********************************************
3memory.c
4copyright 1991, Michael D. Brennan
5
6This is a source file for mawk, an implementation of
7the AWK programming language.
8
9Mawk is distributed without warranty under the terms of
10the GNU General Public License, version 2, 1991.
11********************************************/
12
13
14/* $Log: memory.c,v $
15 * Revision 5.1 91/12/05 07:56:21 brennan
16 * 1.1 pre-release
17 *
18*/
19
20
21/* memory.c */
22
23#include "mawk.h"
24
25#if HAVE_PROTOS
26#define SUPPRESS_NEW_STRING_PROTO /* get compiler off our back on
27 the definition of new_STRING() */
28#endif
29
30#include "memory.h"
31
32STRING null_str = {0, 1, "" } ;
33
34
35STRING *new_STRING(s, xlen)
36 char *s ; unsigned xlen ;
37 /* WARNING: if s != NULL, don't access xlen
38 because it won't be there */
39{ register STRING *sval ;
40 unsigned len ;
41
42 if ( s )
43 {
44 if ( *s == 0 ){ sval = &null_str ; null_str.ref_cnt++ ; }
45 else
46 {
47 len = strlen(s) ;
48 sval = (STRING *) zmalloc(len + STRING_OH) ;
49 sval->len = len ;
50 sval->ref_cnt = 1 ;
51 (void) strcpy(sval->str, s) ;
52 }
53 }
54 else
55 { sval = (STRING *) zmalloc( xlen + STRING_OH ) ;
56 sval->ref_cnt = 1 ; sval->len = xlen ;
57 /* zero out the end marker */
58 sval->str[xlen] = 0 ;
59 }
60
61 return sval ;
62}
63
64
65#ifdef DEBUG
66
67void DB_free_STRING(sval)
68 register STRING *sval ;
69{ if ( -- sval->ref_cnt == 0 ) zfree(sval, sval->len+STRING_OH) ; }
70
71#endif