1 /* $NetBSD: vmbuf.c,v 1.5 2018/05/20 06:15:45 maxv Exp $ */
2
3 /* $KAME: vmbuf.c,v 1.11 2001/11/26 16:54:29 sakane Exp $ */
4
5 /*
6 * Copyright (C) 1995, 1996, 1997, and 1998 WIDE Project.
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the project nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE PROJECT AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE PROJECT OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #include "config.h"
35
36 #define NONEED_DRM
37
38 #include <sys/types.h>
39 #include <sys/param.h>
40
41 #include <stdlib.h>
42 #include <stdio.h>
43 #include <string.h>
44
45 #include "var.h"
46 #include "misc.h"
47 #include "vmbuf.h"
48 #include "debug.h"
49 #include "plog.h"
50 #include "gcmalloc.h"
51
52 vchar_t *
vmalloc(size_t size)53 vmalloc(size_t size)
54 {
55 vchar_t *var;
56
57 if ((var = (vchar_t *)racoon_malloc(sizeof(*var))) == NULL)
58 return NULL;
59
60 var->l = size;
61 if (size == 0) {
62 var->v = NULL;
63 } else {
64 var->v = (caddr_t)racoon_calloc(1, size);
65 if (var->v == NULL) {
66 (void)racoon_free(var);
67 return NULL;
68 }
69 }
70
71 return var;
72 }
73
74 vchar_t *
vrealloc(vchar_t * ptr,size_t size)75 vrealloc(vchar_t *ptr, size_t size)
76 {
77 caddr_t v;
78
79 if (ptr != NULL) {
80 if (ptr->l == 0) {
81 (void)vfree(ptr);
82 return vmalloc(size); /* zero-fill it? */
83 }
84
85 if ((v = (caddr_t)racoon_realloc(ptr->v, size)) == NULL) {
86 (void)vfree(ptr);
87 return NULL;
88 }
89
90 if ( size > ptr->l)
91 memset(v + ptr->l, 0, size - ptr->l);
92 ptr->v = v;
93 ptr->l = size;
94 } else {
95 if ((ptr = vmalloc(size)) == NULL)
96 return NULL;
97 }
98
99 return ptr;
100 }
101
102 void
vfree(vchar_t * var)103 vfree(vchar_t *var)
104 {
105 if (var == NULL)
106 return;
107
108 if (var->v)
109 (void)racoon_free(var->v);
110
111 (void)racoon_free(var);
112
113 return;
114 }
115
116 vchar_t *
vdup(vchar_t * src)117 vdup(vchar_t *src)
118 {
119 vchar_t *new;
120
121 if (src == NULL) {
122 plog(LLV_ERROR, LOCATION, NULL, "vdup(NULL) called\n");
123 return NULL;
124 }
125
126 if ((new = vmalloc(src->l)) == NULL)
127 return NULL;
128
129 memcpy(new->v, src->v, src->l);
130
131 return new;
132 }
133