Postfix3.3.1
tok822_node.c
[詳解]
1 /*++
2 /* NAME
3 /* tok822_node 3
4 /* SUMMARY
5 /* token memory management
6 /* SYNOPSIS
7 /* #include <tok822.h>
8 /*
9 /* TOK822 *tok822_alloc(type, strval)
10 /* int type;
11 /* const char *strval;
12 /*
13 /* TOK822 *tok822_free(tp)
14 /* TOK822 *tp;
15 /* DESCRIPTION
16 /* This module implements memory management for token
17 /* structures. A distinction is made between single-character
18 /* tokens that have no string value, and string-valued tokens.
19 /*
20 /* tok822_alloc() allocates memory for a token structure of
21 /* the named type, and initializes it properly. In case of
22 /* a single-character token, no string memory is allocated.
23 /* Otherwise, \fIstrval\fR is a null pointer or provides
24 /* string data to initialize the token with.
25 /*
26 /* tok822_free() releases the memory used for the specified token
27 /* and conveniently returns a null pointer value.
28 /* LICENSE
29 /* .ad
30 /* .fi
31 /* The Secure Mailer license must be distributed with this software.
32 /* AUTHOR(S)
33 /* Wietse Venema
34 /* IBM T.J. Watson Research
35 /* P.O. Box 704
36 /* Yorktown Heights, NY 10598, USA
37 /*--*/
38 
39 /* System library. */
40 
41 #include <sys_defs.h>
42 #include <string.h>
43 
44 /* Utility library. */
45 
46 #include <mymalloc.h>
47 #include <vstring.h>
48 
49 /* Global library. */
50 
51 #include "tok822.h"
52 
53 /* tok822_alloc - allocate and initialize token */
54 
55 TOK822 *tok822_alloc(int type, const char *strval)
56 {
57  TOK822 *tp;
58 
59 #define CONTAINER_TOKEN(x) \
60  ((x) == TOK822_ADDR || (x) == TOK822_STARTGRP)
61 
62  tp = (TOK822 *) mymalloc(sizeof(*tp));
63  tp->type = type;
64  tp->next = tp->prev = tp->head = tp->tail = tp->owner = 0;
65  tp->vstr = (type < TOK822_MINTOK || CONTAINER_TOKEN(type) ? 0 :
66  strval == 0 ? vstring_alloc(10) :
67  vstring_strcpy(vstring_alloc(strlen(strval) + 1), strval));
68  return (tp);
69 }
70 
71 /* tok822_free - destroy token */
72 
74 {
75  if (tp->vstr)
76  vstring_free(tp->vstr);
77  myfree((void *) tp);
78  return (0);
79 }
void myfree(void *ptr)
Definition: mymalloc.c:207
#define TOK822_MINTOK
Definition: tok822.h:41
Definition: tok822.h:27
VSTRING * vstring_strcpy(VSTRING *vp, const char *src)
Definition: vstring.c:431
struct TOK822 * owner
Definition: tok822.h:34
VSTRING * vstr
Definition: tok822.h:29
struct TOK822 * head
Definition: tok822.h:32
int type
Definition: tok822.h:28
VSTRING * vstring_alloc(ssize_t len)
Definition: vstring.c:353
TOK822 * tok822_alloc(int type, const char *strval)
Definition: tok822_node.c:55
TOK822 * tok822_free(TOK822 *tp)
Definition: tok822_node.c:73
VSTRING * vstring_free(VSTRING *vp)
Definition: vstring.c:380
struct TOK822 * prev
Definition: tok822.h:30
struct TOK822 * next
Definition: tok822.h:31
#define CONTAINER_TOKEN(x)
void * mymalloc(ssize_t len)
Definition: mymalloc.c:150
struct TOK822 * tail
Definition: tok822.h:33