summaryrefslogtreecommitdiff
path: root/gnu/usr.bin/awk/memory.c
blob: 1793341cb3e15cf33e89450116aea1d08f94f94a (plain)
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

/********************************************
memory.c
copyright 1991, 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.
********************************************/


/* $Log: memory.c,v $
/* Revision 1.2  1993/07/02 23:57:41  jtc
/* Updated to mawk 1.1.4
/*
 * Revision 5.1  1991/12/05  07:56:21  brennan
 * 1.1 pre-release
 *
*/


/* memory.c */

#include "mawk.h"

#if     HAVE_PROTOS
#define SUPPRESS_NEW_STRING_PROTO  /* get compiler off our back on
         the definition of new_STRING() */
#endif

#include "memory.h"

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

  
STRING *new_STRING(s, xlen)   
  char *s ;  unsigned xlen ;
  /* WARNING: if s != NULL, don't access xlen
     because it won't be there   */
{ register STRING *sval ;
  unsigned len ;

  if ( s )
  {
    if ( *s == 0 ){ sval = &null_str ; null_str.ref_cnt++ ; }
    else
    {
      len = strlen(s) ;
      sval = (STRING *) zmalloc(len + STRING_OH) ;
      sval->len = len ;
      sval->ref_cnt = 1 ;
      (void) strcpy(sval->str, s) ;
    }
  }
  else  
  { sval = (STRING *) zmalloc( xlen + STRING_OH ) ;
    sval->ref_cnt = 1 ; sval->len = xlen ;
    /* zero out the end marker */
    sval->str[xlen] = 0 ; 
  }

  return sval ;
}


#ifdef   DEBUG

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

#endif